Skip to content

Commit

Permalink
feat: building unit test for oracle validator
Browse files Browse the repository at this point in the history
  • Loading branch information
HinsonSIDAN committed Nov 26, 2023
1 parent 81110be commit 780eae4
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 16 deletions.
8 changes: 5 additions & 3 deletions lib/aiken-content-ownership/diagnostic.ak
Original file line number Diff line number Diff line change
@@ -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)
}
90 changes: 84 additions & 6 deletions lib/aiken-content-ownership/placeholder.ak
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
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 {
#"1c1b7afe8affbee1505cf3ec5a58bd2734d4ffdfcc9b9f059625bd76"
}

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 {
Expand All @@ -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,
}
}
3 changes: 2 additions & 1 deletion lib/aiken-content-ownership/utils.ak
Original file line number Diff line number Diff line change
Expand Up @@ -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(")")
}
4 changes: 2 additions & 2 deletions plutus.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
"$ref": "#/definitions/oracle_validator~1Redeemer"
}
},
"compiledCode": "5906ea0100003232323232323232322223232323232533300b323232323253330103370e900100089919191919191919191919191919191919191919191919191919191919191919191919191919299981b19b87480000204cccc004080050c00803d2002153330363370e9001004099998008100089801006a40042a66606c66e1d20040081323232323232533303f3042002132533303d3370e9002181e00089919191929998209919198008008011129998238008a51132325333046323300100102c22533304b00114a026464a66609466e3c00801852889980200200098278011bae304d00113300400400114a060960046eb8c124004cc114dd480b198229ba90144bd7008008a503375e0046464646464646466e9520003304b304c0073304b304c0063304b304c0053304b304c0043304b304c0033304b304c0023304b304c0013304b304c304d0013304b3752020660966ea40392f5c060980026096002609400260920026090002608e002607c07a606e002608600260760022c60826084608460740022c6080002646600200205044a66607e002297ae013232533303e533303e3375e6086607800403e266e1cccc888c8cc88c94ccc114cdc3a4004002200426eb4c128c10c00cc10c008c94ccc10ccdc3a4004002298103d87a8000132323300100100222533304900114c103d87a8000132323232533304a3371e014004266e9520003304e375000297ae0133006006003375a60960066eb8c124008c134008c12c004dd5982418208011820800a4000646600200200844a66608c0022980103d87a800013232323253330473371e010004266e9520003304b374c00297ae0133006006003375660900066eb8c118008c128008c120004dd598219822181e001010a45004800852809982100119802002000899802002000982180118208009bae303e001303e002375c6078002606806429448888c8c94ccc0f8c1040084c8c8c8c8c94ccc1014ccc100cdc7802805099b8f00300914a0266e1c0040205281bad30440013044002375c608400260840046eb8c10000458dd6181f80099191980080080111299981f8008a5eb804c8ccc888c8cc00400400c894ccc114004400c4c8ccc888cc128dd3998251ba90093304a3752006660946ea00092f5c00026eb8c110004dd6982280099801801982480118238009bae303e0013756607e0026600600660860046082002646600200200a44a66607c002297adef6c60132323232533303f3371e9101000021003133043337606ea4008dd3000998030030019bab3040003375c607c00460840046080002466e2922010b526567697374727920282900330020013300500148008c0040048894ccc0d4cdc3800a400426600891010033700004903009980219980180199b8600200133706002900a19b803370600400290301119b8b0010023001001222533303233710004900a0800899980180199b8300248050cdc1000a4028605a0546eb8c0c8004c0c8008dd7181800098180011bad302e001302e002375a605800260580046eb8c0a8004c0a8004c0a4008dd7181380098138009813001181200098120011bae3022001301a019375860400026040002603e002603c002603a0046eacc06c004c06c004c068008dd6180c000980c000980b80098070020a50300e001301300130130023011001300900614984d958c94ccc02ccdc3a40000022a66601c601200e2930b0a99980599b874800800454ccc038c02401c526161533300b3370e90020008991919192999809180a8010a4c2c6eb8c04c004c04c008dd7180880098048038a99980599b874801800454ccc038c02401c526161630090063001006232533300a3370e90000008991919191919191919191919191919191919191929998109812001099191924c603201a6030020602e0222c6eb8c088004c088008dd7181000098100011bad301e001301e002375a603800260380046eb8c068004c068008c060004c060008dd7180b000980b001180a000980a001180900098090011bae3010001300800216300800123253330093370e9000000899191919299980818098010991924c64a66601e66e1d20000011323253330143017002132498c94ccc048cdc3a400000226464a66602e60340042649318070008b180c00098080010a99980919b87480080044c8c8c8c8c8c94ccc06cc07800852616375a603800260380046eb4c068004c068008dd6980c00098080010b18080008b180a80098068018a99980799b874800800454ccc048c03400c5261616300d00230070031630110013011002300f001300700216300700123253330083370e900000089919299980698080010a4c2c6eb8c038004c01800854ccc020cdc3a400400226464a66601a60200042930b1bae300e0013006002163006001230053754002460066ea80055cd2ab9d5573caae7d5d02ba15745",
"hash": "2f3c08311a0dae07d6e0d0152b9813fb3591305cd7685b7423e3f434"
"compiledCode": "5907070100003232323232323232322223232323232533300b323232323253330103370e900100089919191919191919191919191919191919191919191919191919191919191919191919191919299981b19b87480000204cccc004080050c00803d2002153330363370e9001004099998008100089801006a4004264a66606e66e1d2004009132323232323253330403043002132533303e3370e9002181e80089919191929998211919198008008011129998240008a511323253330473301102c00213300400400114a060980046eb8c128004cc118dd480b998231ba90154bd7008008a503375e0046464646464646466e9520003304c304d0073304c304d0063304c304d0053304c304d0043304c304d0033304c304d0023304c304d0013304c304d304e0013304c3752020660986ea40392f5c0609a00260980026096002609400260920026090002607e07c6070002608800260780022c60846086608660760022c6082002646600200205244a666080002297ae013232533303f533303f3375e6088607a004040266e1cccc888c8cc88c94ccc118cdc3a4004002200426eb4c12cc11000cc110008c94ccc110cdc3a4004002298103d87a8000132323300100100222533304a00114c103d87a8000132323232533304b3371e014004266e9520003304f375000297ae0133006006003375a60980066eb8c128008c138008c130004dd5982498210011821000a4000646600200200844a66608e0022980103d87a800013232323253330483371e010004266e9520003304c374c00297ae0133006006003375660920066eb8c11c008c12c008c124004dd598221822981e801011245004800852809982180119802002000899802002000982200118210009bae303f001303f002375c607a002606a0662a66606e6600203801426666004042034911004800452811191980080080191299981e8008a5013232533303c3371e00400a29444cc010010004c104008dd7181f80091111919299981f1820801099191919192999820299982019b8f00500a13371e00601229404cdc38008040a50375a608800260880046eb8c108004c108008dd718200008b1bac303f00132323300100100222533303f00114bd7009919991119198008008019129998228008801899199911198251ba73304a3752012660946ea400ccc128dd400125eb80004dd718220009bad30450013300300330490023047001375c607c0026eacc0fc004cc00c00cc10c008c104004c8cc004004014894ccc0f800452f5bded8c0264646464a66607e66e3d2201000021003133043337606ea4008dd3000998030030019bab3040003375c607c00460840046080002466e2922010b526567697374727920282900330020013300500148008c0040048894ccc0d4cdc3800a400426600891010033700004903009980219980180199b8600200133706002900a19b803370600400290301119b8b0010023001001222533303233710004900a0800899980180199b8300248050cdc1000a4028605a0546eb8c0c8004c0c8008dd7181800098180011bad302e001302e002375a605800260580046eb8c0a8004c0a8004c0a4008dd7181380098138009813001181200098120011bae3022001301a019375860400026040002603e002603c002603a0046eacc06c004c06c004c068008dd6180c000980c000980b80098070020a50300e001301300130130023011001300900614984d958c94ccc02ccdc3a40000022a66601c601200e2930b0a99980599b874800800454ccc038c02401c526161533300b3370e90020008991919192999809180a8010a4c2c6eb8c04c004c04c008dd7180880098048038a99980599b874801800454ccc038c02401c526161630090063001006232533300a3370e90000008991919191919191919191919191919191919191929998109812001099191924c603201a6030020602e0222c6eb8c088004c088008dd7181000098100011bad301e001301e002375a603800260380046eb8c068004c068008c060004c060008dd7180b000980b001180a000980a001180900098090011bae3010001300800216300800123253330093370e9000000899191919299980818098010991924c64a66601e66e1d20000011323253330143017002132498c94ccc048cdc3a400000226464a66602e60340042649318070008b180c00098080010a99980919b87480080044c8c8c8c8c8c94ccc06cc07800852616375a603800260380046eb4c068004c068008dd6980c00098080010b18080008b180a80098068018a99980799b874800800454ccc048c03400c5261616300d00230070031630110013011002300f001300700216300700123253330083370e900000089919299980698080010a4c2c6eb8c038004c01800854ccc020cdc3a400400226464a66601a60200042930b1bae300e0013006002163006001230053754002460066ea80055cd2ab9d5573caae7d5d02ba15745",
"hash": "63bfdd4bc9cff236160ec45f6fa657861bd0f4e200fc82ff1404073a"
}
],
"definitions": {
Expand Down
1 change: 1 addition & 0 deletions validators/content_registry.ak
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions validators/content_registry_ref_token.ak
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

115 changes: 111 additions & 4 deletions validators/oracle_validator.ak
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions validators/ownership_registry.ak
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions validators/ownership_registry_ref_token.ak
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

0 comments on commit 780eae4

Please sign in to comment.