Skip to content

Commit

Permalink
Only allow DeInit with empty head
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetruska committed May 11, 2024
1 parent ac30815 commit d23b7e5
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 4 deletions.
11 changes: 10 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
# Mac OS

.DS_Store

# Aiken

build
.VSCodeCounter
plutus.json

# VSCode

.VSCodeCounter
.vscode
22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

.PHONY: help
help:
@echo "Usage: make <target>"
@echo
@echo "Targets:"
@echo " help -- show this help"
@echo " shell -- start a nix development shell"
@echo " build -- build the project"
@echo " test -- test the project"

.PHONY: shell
shell:
nix shell github:aiken-lang/aiken#aiken

.PHONY: build
build:
aiken build

.PHONY: test
test:
aiken check
7 changes: 4 additions & 3 deletions lib/linkedlist/linked_list.ak
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use aiken/list
use aiken/transaction.{Output}
use aiken/transaction/value.{lovelace_of}
use linkedlist/constants
use linkedlist/types.{Common, Config, POSIXTime, PubKeyHash, SetNode}
use linkedlist/types.{Common, Config, Empty, POSIXTime, PubKeyHash, SetNode}
use linkedlist/utils

pub fn init(common: Common) -> Bool {
Expand All @@ -21,7 +21,8 @@ pub fn init(common: Common) -> Bool {
}

pub fn deinit(common: Common) -> Bool {
let must_spend_exactly_one_node_input = list.length(common.node_inputs) == 1
expect [head_node] = common.node_inputs
expect Empty = head_node.node.next
let must_not_produce_node_output = list.length(common.node_outputs) == 0
let must_burn_correctly =
utils.validate_mint(
Expand All @@ -30,7 +31,7 @@ pub fn deinit(common: Common) -> Bool {
constants.origin_node_token_name,
-1,
)
must_spend_exactly_one_node_input? && must_not_produce_node_output? && must_burn_correctly?
must_not_produce_node_output? && must_burn_correctly?
}

pub fn insert(common: Common, insert_key: PubKeyHash, node: SetNode) -> Bool {
Expand Down
70 changes: 70 additions & 0 deletions validators/sample.ak
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,76 @@ test mint_validator_deinit() {
mint_validator(config, redeemer, context)
}

test mint_validator_deinit_fails_on_non_empty() fail {
let own_cs = #"746fa3ba2daded6ab9ccc1e39d3835aa1dfcb9b5a54acc2ebe6b79a4"
let init_output_ref =
OutputReference {
transaction_id: TransactionId {
hash: #"2c6dbc95c1e96349c4131a9d19b029362542b31ffd2340ea85dd8f28e271ff6d",
},
output_index: 1,
}
let config =
Config {
init_utxo: init_output_ref,
deadline: 86_400_000,
penalty_address: Address(credential.ScriptCredential("P"), None),
}
let user2_pkh =
bytearray.from_string(
@"e18d73505be6420225ed2a42c8e975e4c6f9148ab38e951ea2572e54",
)
let user2_tn = "FSNe18d73505be6420225ed2a42c8e975e4c6f9148ab38e951ea2572e54"
let remove_output_ref =
OutputReference {
transaction_id: TransactionId { hash: #"" },
output_index: 1,
}
let remove_node =
SetNode { key: Key { key: user2_pkh }, next: Key { key: user2_pkh } }
let user2_value = value.add(value.zero(), own_cs, user2_tn, 1)
let remove_node_output =
Output {
address: Address(credential.ScriptCredential("I"), None),
value: value.add(
user2_value,
value.ada_policy_id,
value.ada_asset_name,
9_000_000,
),
datum: InlineDatum(remove_node),
reference_script: None,
}
let burn_value = value.add(value.zero(), own_cs, origin_node_token_name, -1)
let redeemer = Deinit
let context =
ScriptContext {
purpose: Mint(own_cs),
transaction: Transaction {
inputs: [
Input {
output_reference: remove_output_ref,
output: remove_node_output,
},
],
reference_inputs: [],
outputs: [],
fee: value.zero(),
mint: value.to_minted_value(burn_value),
certificates: [],
withdrawals: dict.new(),
validity_range: interval.everything(),
extra_signatories: [],
redeemers: dict.new(),
datums: dict.new(),
id: TransactionId {
hash: #"2c6dbc95c1e96349c4131a9d19b029362542b31ffd2340ea85dd8f28e271ff6d",
},
},
}
mint_validator(config, redeemer, context)
}

test mint_validator_insert() {
let own_cs = #"746fa3ba2daded6ab9ccc1e39d3835aa1dfcb9b5a54acc2ebe6b79a4"
let init_output_ref =
Expand Down

0 comments on commit d23b7e5

Please sign in to comment.