From 780eae4ecd647eca453d6a6a365359e527e50865 Mon Sep 17 00:00:00 2001 From: SIDANWhatever Date: Sun, 26 Nov 2023 17:05:54 +0800 Subject: [PATCH] feat: building unit test for oracle validator --- lib/aiken-content-ownership/diagnostic.ak | 8 +- lib/aiken-content-ownership/placeholder.ak | 90 ++++++++++++++-- lib/aiken-content-ownership/utils.ak | 3 +- plutus.json | 4 +- validators/content_registry.ak | 1 + validators/content_registry_ref_token.ak | 1 + validators/oracle_validator.ak | 115 ++++++++++++++++++++- validators/ownership_registry.ak | 1 + validators/ownership_registry_ref_token.ak | 1 + 9 files changed, 208 insertions(+), 16 deletions(-) diff --git a/lib/aiken-content-ownership/diagnostic.ak b/lib/aiken-content-ownership/diagnostic.ak index 80c40c6..2d966fd 100644 --- a/lib/aiken-content-ownership/diagnostic.ak +++ b/lib/aiken-content-ownership/diagnostic.ak @@ -1,8 +1,10 @@ -use aiken/cbor.{serialise} -use aiken_content_ownership/placeholder.{mock_utxo_ref} +// use aiken/cbor.{serialise} +// use aiken_content_ownership/placeholder.{mock_utxo_ref} +use aiken_content_ownership/utils.{get_registry_token_name} test diag() { - serialise(mock_utxo_ref(0)) == #"d8799fd8799f58205a077cbcdffb88b104f292aacb9687ce93e2191e103a30a0cc5505c18b719f98ff00ff" + // serialise(mock_utxo_ref(0)) == #"d8799fd8799f58205a077cbcdffb88b104f292aacb9687ce93e2191e103a30a0cc5505c18b719f98ff00ff" // serialise(mock_utxo_ref(1)) == #"d8799fd8799f58205a077cbcdffb88b104f292aacb9687ce93e2191e103a30a0cc5505c18b719f98ff01ff" // serialise(mock_verfication_key_credential()) == #"d8799f5820e822178d3ceaeef383a84cbd41a702b3837360bf889aaac714e6f380731d20d4ff" + "Registry (0)" == get_registry_token_name(0) } diff --git a/lib/aiken-content-ownership/placeholder.ak b/lib/aiken-content-ownership/placeholder.ak index 564d3d1..6bb25e8 100644 --- a/lib/aiken-content-ownership/placeholder.ak +++ b/lib/aiken-content-ownership/placeholder.ak @@ -1,6 +1,7 @@ use aiken/transaction.{NoDatum, Output, OutputReference, TransactionId} -use aiken/transaction/credential.{Address, - Credential, VerificationKeyCredential} +use aiken/transaction/credential.{ + Address, Credential, ScriptCredential, VerificationKeyCredential, +} use aiken/transaction/value.{PolicyId, from_lovelace} pub fn mock_policy_id() -> PolicyId { @@ -8,7 +9,11 @@ pub fn mock_policy_id() -> PolicyId { } pub fn mock_policy_id_2() -> PolicyId { - #"1c1b7afe8affbee1505cf3ec5a58bd2734d4ffdfcc9b9f059625bd77" + #"52e322a5dd87fe977c5ee5b91e3f42eedeccd6795a7ef4f158bd8892" +} + +pub fn mock_policy_id_3() -> PolicyId { + #"a15cd6051ea2fc92c034b0bb1d33f2b7ba7ac92277e6ee5c30ab19a4" } pub fn mock_utxo_ref(output_index: Int) -> OutputReference { @@ -32,8 +37,81 @@ pub fn mock_output() -> Output { } } +// Pub Key Addresses + +pub fn mock_vkey_hex() -> ByteArray { + #"a2c20c77887ace1cd986193e4e75babd8993cfd56995cd5cfce609c2" +} + pub fn mock_verfication_key_credential() -> Credential { - VerificationKeyCredential( - #"e822178d3ceaeef383a84cbd41a702b3837360bf889aaac714e6f380731d20d4", - ) + VerificationKeyCredential(mock_vkey_hex()) +} + +pub fn mock_pub_key_address(stake_credential) -> Address { + Address { + payment_credential: mock_verfication_key_credential(), + stake_credential, + } +} + +pub fn mock_vkey_hex_2() -> ByteArray { + #"e2cfca46ecdf7feb8a3a7f957ead86b88c156f3fdd9607bcb44eb8f5" +} + +pub fn mock_verfication_key_credential_2() -> Credential { + VerificationKeyCredential(mock_vkey_hex_2()) +} + +pub fn mock_pub_key_address_2(stake_credential) -> Address { + Address { + payment_credential: mock_verfication_key_credential_2(), + stake_credential, + } +} + +// Script Addresses + +pub fn mock_script_hex() -> ByteArray { + #"70ce90786897b6f7b6430a57ae005ecaf7c8cab817dd7510598403a9" +} + +pub fn mock_script_credential() -> Credential { + ScriptCredential(mock_script_hex()) +} + +pub fn mock_script_address(stake_credential) -> Address { + Address { + payment_credential: ScriptCredential(mock_script_hex()), + stake_credential, + } +} + +pub fn mock_script_hex_2() -> ByteArray { + #"efa5288b2b13cff1db555eed5debc54f3b25a62b7321db37f8af9213" +} + +pub fn mock_script_credential_2() -> Credential { + ScriptCredential(mock_script_hex_2()) +} + +pub fn mock_script_address_2(stake_credential) -> Address { + Address { + payment_credential: ScriptCredential(mock_script_hex_2()), + stake_credential, + } +} + +pub fn mock_script_hex_3() -> ByteArray { + #"a8ba4d8ab8d7f1f864a27a63dab1185cc47929dc3c3a34956d946286" +} + +pub fn mock_script_credential_3() -> Credential { + ScriptCredential(mock_script_hex_3()) +} + +pub fn mock_script_address_3(stake_credential) -> Address { + Address { + payment_credential: ScriptCredential(mock_script_hex_3()), + stake_credential, + } } diff --git a/lib/aiken-content-ownership/utils.ak b/lib/aiken-content-ownership/utils.ak index 5f858d7..96d61c2 100644 --- a/lib/aiken-content-ownership/utils.ak +++ b/lib/aiken-content-ownership/utils.ak @@ -3,5 +3,6 @@ use aiken/transaction/value.{AssetName} use aiken_content_ownership/common.{convert_int_to_bytes} pub fn get_registry_token_name(count: Int) -> AssetName { - bytearray.concat("Registry ()", convert_int_to_bytes(count)) + bytearray.concat("Registry (", convert_int_to_bytes(count)) + |> bytearray.concat(")") } diff --git a/plutus.json b/plutus.json index 4f6013c..dc39a64 100644 --- a/plutus.json +++ b/plutus.json @@ -44,8 +44,8 @@ "$ref": "#/definitions/oracle_validator~1Redeemer" } }, - "compiledCode": "5906ea0100003232323232323232322223232323232533300b323232323253330103370e900100089919191919191919191919191919191919191919191919191919191919191919191919191919299981b19b87480000204cccc004080050c00803d2002153330363370e9001004099998008100089801006a40042a66606c66e1d20040081323232323232533303f3042002132533303d3370e9002181e00089919191929998209919198008008011129998238008a51132325333046323300100102c22533304b00114a026464a66609466e3c00801852889980200200098278011bae304d00113300400400114a060960046eb8c124004cc114dd480b198229ba90144bd7008008a503375e0046464646464646466e9520003304b304c0073304b304c0063304b304c0053304b304c0043304b304c0033304b304c0023304b304c0013304b304c304d0013304b3752020660966ea40392f5c060980026096002609400260920026090002608e002607c07a606e002608600260760022c60826084608460740022c6080002646600200205044a66607e002297ae013232533303e533303e3375e6086607800403e266e1cccc888c8cc88c94ccc114cdc3a4004002200426eb4c128c10c00cc10c008c94ccc10ccdc3a4004002298103d87a8000132323300100100222533304900114c103d87a8000132323232533304a3371e014004266e9520003304e375000297ae0133006006003375a60960066eb8c124008c134008c12c004dd5982418208011820800a4000646600200200844a66608c0022980103d87a800013232323253330473371e010004266e9520003304b374c00297ae0133006006003375660900066eb8c118008c128008c120004dd598219822181e001010a45004800852809982100119802002000899802002000982180118208009bae303e001303e002375c6078002606806429448888c8c94ccc0f8c1040084c8c8c8c8c94ccc1014ccc100cdc7802805099b8f00300914a0266e1c0040205281bad30440013044002375c608400260840046eb8c10000458dd6181f80099191980080080111299981f8008a5eb804c8ccc888c8cc00400400c894ccc114004400c4c8ccc888cc128dd3998251ba90093304a3752006660946ea00092f5c00026eb8c110004dd6982280099801801982480118238009bae303e0013756607e0026600600660860046082002646600200200a44a66607c002297adef6c60132323232533303f3371e9101000021003133043337606ea4008dd3000998030030019bab3040003375c607c00460840046080002466e2922010b526567697374727920282900330020013300500148008c0040048894ccc0d4cdc3800a400426600891010033700004903009980219980180199b8600200133706002900a19b803370600400290301119b8b0010023001001222533303233710004900a0800899980180199b8300248050cdc1000a4028605a0546eb8c0c8004c0c8008dd7181800098180011bad302e001302e002375a605800260580046eb8c0a8004c0a8004c0a4008dd7181380098138009813001181200098120011bae3022001301a019375860400026040002603e002603c002603a0046eacc06c004c06c004c068008dd6180c000980c000980b80098070020a50300e001301300130130023011001300900614984d958c94ccc02ccdc3a40000022a66601c601200e2930b0a99980599b874800800454ccc038c02401c526161533300b3370e90020008991919192999809180a8010a4c2c6eb8c04c004c04c008dd7180880098048038a99980599b874801800454ccc038c02401c526161630090063001006232533300a3370e90000008991919191919191919191919191919191919191929998109812001099191924c603201a6030020602e0222c6eb8c088004c088008dd7181000098100011bad301e001301e002375a603800260380046eb8c068004c068008c060004c060008dd7180b000980b001180a000980a001180900098090011bae3010001300800216300800123253330093370e9000000899191919299980818098010991924c64a66601e66e1d20000011323253330143017002132498c94ccc048cdc3a400000226464a66602e60340042649318070008b180c00098080010a99980919b87480080044c8c8c8c8c8c94ccc06cc07800852616375a603800260380046eb4c068004c068008dd6980c00098080010b18080008b180a80098068018a99980799b874800800454ccc048c03400c5261616300d00230070031630110013011002300f001300700216300700123253330083370e900000089919299980698080010a4c2c6eb8c038004c01800854ccc020cdc3a400400226464a66601a60200042930b1bae300e0013006002163006001230053754002460066ea80055cd2ab9d5573caae7d5d02ba15745", - "hash": "2f3c08311a0dae07d6e0d0152b9813fb3591305cd7685b7423e3f434" + "compiledCode": "5907070100003232323232323232322223232323232533300b323232323253330103370e900100089919191919191919191919191919191919191919191919191919191919191919191919191919299981b19b87480000204cccc004080050c00803d2002153330363370e9001004099998008100089801006a4004264a66606e66e1d2004009132323232323253330403043002132533303e3370e9002181e80089919191929998211919198008008011129998240008a511323253330473301102c00213300400400114a060980046eb8c128004cc118dd480b998231ba90154bd7008008a503375e0046464646464646466e9520003304c304d0073304c304d0063304c304d0053304c304d0043304c304d0033304c304d0023304c304d0013304c304d304e0013304c3752020660986ea40392f5c0609a00260980026096002609400260920026090002607e07c6070002608800260780022c60846086608660760022c6082002646600200205244a666080002297ae013232533303f533303f3375e6088607a004040266e1cccc888c8cc88c94ccc118cdc3a4004002200426eb4c12cc11000cc110008c94ccc110cdc3a4004002298103d87a8000132323300100100222533304a00114c103d87a8000132323232533304b3371e014004266e9520003304f375000297ae0133006006003375a60980066eb8c128008c138008c130004dd5982498210011821000a4000646600200200844a66608e0022980103d87a800013232323253330483371e010004266e9520003304c374c00297ae0133006006003375660920066eb8c11c008c12c008c124004dd598221822981e801011245004800852809982180119802002000899802002000982200118210009bae303f001303f002375c607a002606a0662a66606e6600203801426666004042034911004800452811191980080080191299981e8008a5013232533303c3371e00400a29444cc010010004c104008dd7181f80091111919299981f1820801099191919192999820299982019b8f00500a13371e00601229404cdc38008040a50375a608800260880046eb8c108004c108008dd718200008b1bac303f00132323300100100222533303f00114bd7009919991119198008008019129998228008801899199911198251ba73304a3752012660946ea400ccc128dd400125eb80004dd718220009bad30450013300300330490023047001375c607c0026eacc0fc004cc00c00cc10c008c104004c8cc004004014894ccc0f800452f5bded8c0264646464a66607e66e3d2201000021003133043337606ea4008dd3000998030030019bab3040003375c607c00460840046080002466e2922010b526567697374727920282900330020013300500148008c0040048894ccc0d4cdc3800a400426600891010033700004903009980219980180199b8600200133706002900a19b803370600400290301119b8b0010023001001222533303233710004900a0800899980180199b8300248050cdc1000a4028605a0546eb8c0c8004c0c8008dd7181800098180011bad302e001302e002375a605800260580046eb8c0a8004c0a8004c0a4008dd7181380098138009813001181200098120011bae3022001301a019375860400026040002603e002603c002603a0046eacc06c004c06c004c068008dd6180c000980c000980b80098070020a50300e001301300130130023011001300900614984d958c94ccc02ccdc3a40000022a66601c601200e2930b0a99980599b874800800454ccc038c02401c526161533300b3370e90020008991919192999809180a8010a4c2c6eb8c04c004c04c008dd7180880098048038a99980599b874801800454ccc038c02401c526161630090063001006232533300a3370e90000008991919191919191919191919191919191919191929998109812001099191924c603201a6030020602e0222c6eb8c088004c088008dd7181000098100011bad301e001301e002375a603800260380046eb8c068004c068008c060004c060008dd7180b000980b001180a000980a001180900098090011bae3010001300800216300800123253330093370e9000000899191919299980818098010991924c64a66601e66e1d20000011323253330143017002132498c94ccc048cdc3a400000226464a66602e60340042649318070008b180c00098080010a99980919b87480080044c8c8c8c8c8c94ccc06cc07800852616375a603800260380046eb4c068004c068008dd6980c00098080010b18080008b180a80098068018a99980799b874800800454ccc048c03400c5261616300d00230070031630110013011002300f001300700216300700123253330083370e900000089919299980698080010a4c2c6eb8c038004c01800854ccc020cdc3a400400226464a66601a60200042930b1bae300e0013006002163006001230053754002460066ea80055cd2ab9d5573caae7d5d02ba15745", + "hash": "63bfdd4bc9cff236160ec45f6fa657861bd0f4e200fc82ff1404073a" } ], "definitions": { diff --git a/validators/content_registry.ak b/validators/content_registry.ak index e69de29..8b13789 100644 --- a/validators/content_registry.ak +++ b/validators/content_registry.ak @@ -0,0 +1 @@ + diff --git a/validators/content_registry_ref_token.ak b/validators/content_registry_ref_token.ak index e69de29..8b13789 100644 --- a/validators/content_registry_ref_token.ak +++ b/validators/content_registry_ref_token.ak @@ -0,0 +1 @@ + diff --git a/validators/oracle_validator.ak b/validators/oracle_validator.ak index 4ac7b5e..677472b 100644 --- a/validators/oracle_validator.ak +++ b/validators/oracle_validator.ak @@ -1,18 +1,25 @@ -use aiken/transaction.{InlineDatum, ScriptContext, Spend, Transaction} +use aiken/transaction.{ + InlineDatum, Input, Output, ScriptContext, Spend, Transaction, placeholder, +} use aiken/transaction/credential.{Address} -use aiken/transaction/value.{PolicyId} +use aiken/transaction/value.{PolicyId, from_asset, to_minted_value} use aiken_content_ownership/common.{ all_key_signed, key_signed, onlyMintedToken, utxos_at_with, } +use aiken_content_ownership/placeholder.{ + mock_policy_id, mock_policy_id_2, mock_policy_id_3, mock_script_address, + mock_script_address_2, mock_script_address_3, mock_utxo_ref, mock_vkey_hex, + mock_vkey_hex_2, +} use aiken_content_ownership/utils.{get_registry_token_name} type Datum { oracle_nft: PolicyId, oracle_address: Address, - content_registry_address: Address, content_registry_ref_token: PolicyId, - ownership_registry_address: Address, + content_registry_address: Address, ownership_registry_ref_token: PolicyId, + ownership_registry_address: Address, content_registry_count: Int, ownership_registry_count: Int, operation_key: ByteArray, @@ -93,3 +100,103 @@ validator { } } } + +fn mock_datum() { + Datum { + oracle_nft: mock_policy_id(), + oracle_address: mock_script_address(None), + content_registry_ref_token: mock_policy_id_2(), + content_registry_address: mock_script_address_2(None), + ownership_registry_ref_token: mock_policy_id_3(), + ownership_registry_address: mock_script_address_3(None), + content_registry_count: 0, + ownership_registry_count: 0, + operation_key: mock_vkey_hex(), + stop_key: mock_vkey_hex_2(), + } +} + +test success_create_content_registry() { + let redeemer = CreateContentRegistry + let policy_id: PolicyId = mock_policy_id_2() + + let tx = + Transaction { + ..placeholder(), + mint: to_minted_value(from_asset(policy_id, "Registry (0)", 1)), + } + let ctx = ScriptContext { purpose: Spend(mock_utxo_ref(0)), transaction: tx } + + oracle_validator(mock_datum(), redeemer, ctx) +} + +test success_create_ownership_registry() { + let redeemer = CreateOwnershipRegistry + let policy_id: PolicyId = mock_policy_id_3() + + let tx = + Transaction { + ..placeholder(), + mint: to_minted_value(from_asset(policy_id, "Registry (0)", 1)), + } + let ctx = ScriptContext { purpose: Spend(mock_utxo_ref(0)), transaction: tx } + + oracle_validator(mock_datum(), redeemer, ctx) +} + +test success_rotate_key() { + let redeemer = + RotateKey { + new_operation_key: mock_vkey_hex_2(), + new_stop_key: mock_vkey_hex(), + } + + let input_utxo = mock_utxo_ref(0) + let input = + Input { + output_reference: input_utxo, + output: Output { + address: mock_script_address(None), + value: from_asset(mock_policy_id(), "", 1), + datum: InlineDatum(mock_datum()), + reference_script: None, + }, + } + let output = + Output { + address: mock_script_address(None), + value: from_asset(mock_policy_id(), "", 1), + datum: InlineDatum( + Datum { + ..mock_datum(), + operation_key: mock_vkey_hex_2(), + stop_key: mock_vkey_hex(), + }, + ), + reference_script: None, + } + + let tx = + Transaction { + ..placeholder(), + inputs: [input], + outputs: [output], + extra_signatories: [mock_vkey_hex(), mock_vkey_hex_2()], + } + let ctx = ScriptContext { purpose: Spend(input_utxo), transaction: tx } + + oracle_validator(mock_datum(), redeemer, ctx) +} + +test success_stop_app() { + let redeemer = StopApp + let tx = + Transaction { + ..placeholder(), + mint: to_minted_value(from_asset(mock_policy_id(), "", -1)), + extra_signatories: [mock_vkey_hex_2()], + } + let ctx = ScriptContext { purpose: Spend(mock_utxo_ref(0)), transaction: tx } + + oracle_validator(mock_datum(), redeemer, ctx) +} diff --git a/validators/ownership_registry.ak b/validators/ownership_registry.ak index e69de29..8b13789 100644 --- a/validators/ownership_registry.ak +++ b/validators/ownership_registry.ak @@ -0,0 +1 @@ + diff --git a/validators/ownership_registry_ref_token.ak b/validators/ownership_registry_ref_token.ak index e69de29..8b13789 100644 --- a/validators/ownership_registry_ref_token.ak +++ b/validators/ownership_registry_ref_token.ak @@ -0,0 +1 @@ +