Skip to content

Commit

Permalink
Oct 26
Browse files Browse the repository at this point in the history
  • Loading branch information
ariady-putra committed Oct 26, 2024
1 parent ed499a8 commit 8f5cffb
Show file tree
Hide file tree
Showing 8 changed files with 307 additions and 0 deletions.
11 changes: 11 additions & 0 deletions aiken.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,21 @@ name = "aiken-lang/stdlib"
version = "v2.1.0"
source = "github"

[[requirements]]
name = "aiken-lang/fuzz"
version = "v2.1.0"
source = "github"

[[packages]]
name = "aiken-lang/stdlib"
version = "v2.1.0"
requirements = []
source = "github"

[[packages]]
name = "aiken-lang/fuzz"
version = "v2.1.0"
requirements = []
source = "github"

[etags]
5 changes: 5 additions & 0 deletions aiken.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ name = "aiken-lang/stdlib"
version = "v2.1.0"
source = "github"

[[dependencies]]
name = "aiken-lang/fuzz"
version = "v2.1.0"
source = "github"

[config]
55 changes: 55 additions & 0 deletions lib/modules/functions.ak
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// use aiken/fuzz
use aiken/option

// trace
// aiken build -t verbose
// trace @"LABEL" | LABEL
// trace variable | variable_value
// trace @"LABEL": variable, vars | LABEL: variable_value, vars_value

// aiken build -t compact
// trace @"LABEL" | LABEL
// trace @"LABEL": variable, vars | LABEL

// ? operator
// silent
// compact
// verbose: (n == x) ? False

pub fn increment(n: Int, step: Option<Int>) {
let s = option.or_else(step, 1)
n + s
}
// /// Unit testing
// ///
// /// It can return:
// /// - True
// /// - False
// /// - fail
// test increment_test() {
// // ? operator
// and {
// (increment(1, Some(2)) == 3)?,
// (increment(2, Some(3)) == 5)?,
// (increment(3, Some(4)) == 7)?,
// (increment(4, None) == 5)?,
// }
// }

// /// Testing failure
// test test_fail() fail {
// expect 5 = increment(3, None)
// }

// /// Property testing
// ///
// /// Testing failure:
// /// - fail (if it runs 100x, then ALL 100x it must fail) -> PASS
// /// - fail once (not ALL fail) -> PASS
// test increment_property_test(
// ls: List<Int> via fuzz.list_between(fuzz.int(), 2, 2),
// ) fail once {
// expect [n, step] = ls

// increment(n, Some(step)) > n
// }
30 changes: 30 additions & 0 deletions lib/tests/functions.ak
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use aiken/fuzz
use modules/functions.{increment}

test increment_test() {
// ? operator
and {
(increment(1, Some(2)) == 3)?,
(increment(2, Some(3)) == 5)?,
(increment(3, Some(4)) == 7)?,
(increment(4, None) == 5)?,
}
}

/// Testing failure
test test_fail() fail {
expect 5 = increment(3, None)
}

/// Property testing
///
/// Testing failure:
/// - fail (if it runs 100x, then ALL 100x it must fail) -> PASS
/// - fail once (not ALL fail) -> PASS
test increment_property_test(
ls: List<Int> via fuzz.list_between(fuzz.int(), 2, 2),
) fail once {
expect [n, step] = ls

increment(n, Some(step)) > n
}
88 changes: 88 additions & 0 deletions plutus.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{
"preamble": {
"title": "apea-aiken-batch-1/testing_debugging",
"description": "Aiken contracts for project 'apea-aiken-batch-1/testing_debugging'",
"version": "0.0.0",
"plutusVersion": "v3",
"compiler": {
"name": "Aiken",
"version": "v1.1.5+unknown"
},
"license": "Apache-2.0"
},
"validators": [
{
"title": "spend_validator.spd.spend",
"datum": {
"title": "datum",
"schema": {
"$ref": "#/definitions/Data"
}
},
"redeemer": {
"title": "redeemer",
"schema": {
"$ref": "#/definitions/Int"
}
},
"parameters": [
{
"title": "param_1",
"schema": {
"$ref": "#/definitions/VerificationKeyHash"
}
}
],
"compiledCode": "58f4010100323232323232323232232253330053232323232533300a3370e900118061baa0011323232533333301400500c00c00c132323300100137586028602a602a602a602a602a602a602a602a60246ea8024894ccc050004528099299980899b8f375c602c00401c29444cc00c00c004c058004dd680280618089809001180800098069baa00116300e300f002300d001300d002300b001300837540022930a998032491856616c696461746f722072657475726e65642066616c73650013656375c0022a660049211772656465656d6572205f72656465656d65723a20496e7400165734ae7155ceaab9e5573eae855d12ba41",
"hash": "f7241035f40cb7f13518c51934dddad28657ef21dec4c308ea26588e"
},
{
"title": "spend_validator.spd.else",
"redeemer": {
"schema": {}
},
"parameters": [
{
"title": "param_1",
"schema": {
"$ref": "#/definitions/VerificationKeyHash"
}
}
],
"compiledCode": "58f4010100323232323232323232232253330053232323232533300a3370e900118061baa0011323232533333301400500c00c00c132323300100137586028602a602a602a602a602a602a602a602a60246ea8024894ccc050004528099299980899b8f375c602c00401c29444cc00c00c004c058004dd680280618089809001180800098069baa00116300e300f002300d001300d002300b001300837540022930a998032491856616c696461746f722072657475726e65642066616c73650013656375c0022a660049211772656465656d6572205f72656465656d65723a20496e7400165734ae7155ceaab9e5573eae855d12ba41",
"hash": "f7241035f40cb7f13518c51934dddad28657ef21dec4c308ea26588e"
},
{
"title": "test_debug.td.mint",
"redeemer": {
"title": "redeemer",
"schema": {
"$ref": "#/definitions/Int"
}
},
"compiledCode": "5903c101010032323232323232323225333003323232323253330083370e900018051baa001132533333301000300800800813253300b3372c92010a52656465656d65723a200037326664646464646002002444a6664666603200626464646464646601800400266e2922010128000025333016337100069007099b80483c80400c54ccc058cdc4001a410004266e00cdc0241002800690068b299980c800899b8a4881035b5d2900005133714911035b5f2000333300800133714911025d2900005223330090090023006001223330090090020013758602e0046eb4c054004c8cdd81ba83015001374e602c0026ea800c4c94ccc05c0044cdc52441027b7d00003133714911037b5f200032323300100100322533301a00110031533301a301c0011323330090093019001337149101023a2000333009009301a001004301b0011323330090093019001337149101023a2000333009009301a001300633003003301d002301b0013371491102207d000033756006264a66602e002266e29221025b5d00003133714911035b5f2000333300600133714911015d000032233300700700230040012233300700700200137580066e292201022c2000133005375a0040022646466e2922010268270000132333001001337006e3400920013371491101270000322253330153371000490000800899191919980300319b8000548004cdc599b80002533301833710004900a0a40c02903719b8b33700002a66603066e2000520141481805206e0043370c004901019b8300148080cdc70020011bae002222232330010010052253330170011005153330173019001133003301800100513300430180013300200230190012232330010010032253330103370e0029000099b8a488101300000315333010337100029000099b8a489012d003300200233702900000089980299b8400148050cdc599b803370a002900a240c00066002002444a66601a66e2400920001001133300300333708004900a19b8b3370066e14009201448180004dd4000a45001533300a3370e002902a0a51153300b4911672656465656d6572203d3d203432203f2046616c73650014a06eb400c020dd7180698059baa00113253330093370e90020008a51153330093370e90030008a51153300a4910b556e737570706f727465640016300a3754601a601c601c60166ea8018c030c034008c02c004c02c008c024004c018dd50008a4c2a6600892011856616c696461746f722072657475726e65642066616c73650013656153300249010d72656465656d65723a20496e7400165734ae7155ceaab9e5573eae855d12ba41",
"hash": "efdfce93f448490d6807386297b9b5992dc1c19e0e66dead81e6338c"
},
{
"title": "test_debug.td.else",
"redeemer": {
"schema": {}
},
"compiledCode": "5903c101010032323232323232323225333003323232323253330083370e900018051baa001132533333301000300800800813253300b3372c92010a52656465656d65723a200037326664646464646002002444a6664666603200626464646464646601800400266e2922010128000025333016337100069007099b80483c80400c54ccc058cdc4001a410004266e00cdc0241002800690068b299980c800899b8a4881035b5d2900005133714911035b5f2000333300800133714911025d2900005223330090090023006001223330090090020013758602e0046eb4c054004c8cdd81ba83015001374e602c0026ea800c4c94ccc05c0044cdc52441027b7d00003133714911037b5f200032323300100100322533301a00110031533301a301c0011323330090093019001337149101023a2000333009009301a001004301b0011323330090093019001337149101023a2000333009009301a001300633003003301d002301b0013371491102207d000033756006264a66602e002266e29221025b5d00003133714911035b5f2000333300600133714911015d000032233300700700230040012233300700700200137580066e292201022c2000133005375a0040022646466e2922010268270000132333001001337006e3400920013371491101270000322253330153371000490000800899191919980300319b8000548004cdc599b80002533301833710004900a0a40c02903719b8b33700002a66603066e2000520141481805206e0043370c004901019b8300148080cdc70020011bae002222232330010010052253330170011005153330173019001133003301800100513300430180013300200230190012232330010010032253330103370e0029000099b8a488101300000315333010337100029000099b8a489012d003300200233702900000089980299b8400148050cdc599b803370a002900a240c00066002002444a66601a66e2400920001001133300300333708004900a19b8b3370066e14009201448180004dd4000a45001533300a3370e002902a0a51153300b4911672656465656d6572203d3d203432203f2046616c73650014a06eb400c020dd7180698059baa00113253330093370e90020008a51153330093370e90030008a51153300a4910b556e737570706f727465640016300a3754601a601c601c60166ea8018c030c034008c02c004c02c008c024004c018dd50008a4c2a6600892011856616c696461746f722072657475726e65642066616c73650013656153300249010d72656465656d65723a20496e7400165734ae7155ceaab9e5573eae855d12ba41",
"hash": "efdfce93f448490d6807386297b9b5992dc1c19e0e66dead81e6338c"
}
],
"definitions": {
"Data": {
"title": "Data",
"description": "Any Plutus data."
},
"Int": {
"dataType": "integer"
},
"VerificationKeyHash": {
"title": "VerificationKeyHash",
"dataType": "bytes"
}
}
}
28 changes: 28 additions & 0 deletions validators/spend_validator.ak
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use aiken/collection/list
use aiken/crypto.{VerificationKeyHash}
use cardano/transaction.{OutputReference, Transaction}

validator spd(param_1 pkh: VerificationKeyHash) {
spend(
datum _datum: Option<Data>,
redeemer _redeemer: Int,
oref _oref: OutputReference,
tx: Transaction,
) {
list.has(tx.extra_signatories, pkh)
}

else(_) {
fail
}
}

test spd_test_1() {
spd.spend(
param_1: "PKH",
datum: None,
redeemer: 0,
oref: OutputReference { transaction_id: "", output_index: 0 },
tx: Transaction { ..transaction.placeholder, extra_signatories: ["PKH"] },
)
}
36 changes: 36 additions & 0 deletions validators/test_debug.ak
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use cardano/assets.{PolicyId}
use cardano/script_context.{Publishing, ScriptContext, Withdrawing}
use cardano/transaction.{Transaction}

validator td {
mint(redeemer: Int, _policy_id: PolicyId, _tx: Transaction) {
// IF the redeemer is NOT 42
// THEN it will print:
// ```
// Redeemer: redeemer_value
// redeemer == 42 ? False
// ```
trace @"Redeemer": redeemer
(redeemer == 42)?
}

/// ScriptContext {
/// transaction: Transaction,
/// redeemer: Redeemer,
/// info: ScriptInfo
/// }
else(ctx: ScriptContext) {
// kinda like switch-case
when ctx.info is {
Withdrawing { .. } | Publishing { .. } -> True
_ -> fail @"Unsupported"
}
}

// PlutusV3
// allValidator(ScriptContext)

// PlutusV1/V2
// spending(datum, redeemer, ScriptContext)
// anyOther(redeemer, ScriptContext)
}
54 changes: 54 additions & 0 deletions validators/testing_validator.ak
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use aiken/fuzz
use cardano/address.{VerificationKey}
use cardano/governance.{StakePool}
use cardano/script_context.{ScriptContext, Voting, Withdrawing}
use cardano/transaction
use test_debug

test test_validator_td_1() {
test_debug.td.mint(42, "", transaction.placeholder)
}

test test_validator_td_2() {
!test_debug.td.mint(41, "", transaction.placeholder)
}

test test_validator_td_3() {
test_debug.td.else(
ScriptContext {
transaction: transaction.placeholder,
redeemer: 42,
info: Withdrawing(VerificationKey("")),
},
)
}

test test_validator_td_4() fail {
test_debug.td.else(
ScriptContext {
transaction: transaction.placeholder,
redeemer: 42,
info: Voting(StakePool("")),
},
)
}

test test_validator_td_5(n: Int via fuzz.int()) {
test_debug.td.else(
ScriptContext {
transaction: transaction.placeholder,
redeemer: n,
info: Withdrawing(VerificationKey("")),
},
)
}

test test_validator_td_6(n: Int via fuzz.int()) fail {
test_debug.td.else(
ScriptContext {
transaction: transaction.placeholder,
redeemer: n,
info: Voting(StakePool("")),
},
)
}

0 comments on commit 8f5cffb

Please sign in to comment.