Skip to content

Commit

Permalink
feat: refactoring code for integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HinsonSIDAN committed Nov 26, 2023
1 parent 0e285c9 commit c493ff9
Show file tree
Hide file tree
Showing 14 changed files with 944 additions and 461 deletions.
14 changes: 14 additions & 0 deletions lib/aiken-content-ownership/types.ak
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,21 @@ pub type ContentRegistryDatum {
registry: Dict<Int, ByteArray>,
}

pub type ContentRegistryRedeemer {
CreateContent { content_hash: ByteArray }
StopContentRegistry
}

pub type OwnershipRegistryDatum {
count: Int,
registry: Dict<Int, (PolicyId, AssetName)>,
}

pub type OwnershipRegistryRedeemer {
CreateOwnershipRecord { owner_token: (PolicyId, AssetName) }
TransferOwnership {
new_owner_toen: (PolicyId, AssetName),
content_number: Int,
}
StopOwnershipRegistry
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use aiken/dict
// use aiken/list
use aiken/transaction.{
InlineDatum, Input, Mint, Output, ScriptContext, Transaction,
}
use aiken/transaction/value.{PolicyId}
use aiken_content_ownership/common.{
check_policy_only_burn, inputs_with, outputs_at_with, value_length,
}
use aiken_content_ownership/types.{
ContentRegistryDatum, MintPolarity, OracleDatum, RBurn, RMint,
}
use aiken_content_ownership/utils.{get_registry_token_name}

pub fn content_registry_ref_token_logic(
oracle_nft: PolicyId,
redeemer: MintPolarity,
context: ScriptContext,
) -> Bool {
let ScriptContext { purpose, transaction } = context
let Transaction { inputs, outputs, mint, .. } = transaction
when purpose is {
Mint(current_policy) ->
when (redeemer, inputs_with(inputs, oracle_nft, "")) is {
(RMint, [oracle_input]) -> {
expect InlineDatum(inline_datum) = oracle_input.output.datum
expect input_datum: OracleDatum = inline_datum
let OracleDatum {
oracle_address,
content_registry_ref_token,
content_registry_address,
content_registry_count,
..
} = input_datum
when
(
outputs_at_with(outputs, oracle_address, oracle_nft, ""),
outputs_at_with(
outputs,
content_registry_address,
content_registry_ref_token,
get_registry_token_name(content_registry_count),
),
)
is {
([oracle_output], [registry_output]) -> {
expect InlineDatum(raw_oracle_datum) = oracle_output.datum
expect InlineDatum(raw_registry_datum) = registry_output.datum
expect oracle_datum: OracleDatum = raw_oracle_datum
expect ContentRegistryDatum { count, registry }: ContentRegistryDatum =
raw_registry_datum
let oracle_datum_updated =
oracle_datum == OracleDatum {
..input_datum,
content_registry_count: content_registry_count + 1,
}
let registry_initial_datum_correct =
count == 0 && registry == dict.new()
let oracle_output_value_clean =
value_length(oracle_output.value) == 2
let registry_output_value_clean =
value_length(registry_output.value) == 2
oracle_datum_updated && registry_initial_datum_correct && oracle_output_value_clean && registry_output_value_clean
}
_ -> False
}
}
(RBurn, _) -> check_policy_only_burn(mint, current_policy)
_ -> False
}
_ -> False
}
}
32 changes: 32 additions & 0 deletions lib/aiken-content-ownership/validators/one_time_minting_policy.ak
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use aiken/list
use aiken/transaction.{Input, Mint, OutputReference, ScriptContext, Transaction}
use aiken_content_ownership/common.{check_policy_only_burn}
use aiken_content_ownership/types.{MintPolarity, RBurn, RMint}

pub fn one_time_minting_policy_logic(
utxo_ref: OutputReference,
redeemer: MintPolarity,
context: ScriptContext,
) -> Bool {
let ScriptContext { purpose, transaction } = context
when purpose is {
Mint(current_policy) ->
when redeemer is {
RMint -> {
let Transaction { inputs, .. } = transaction
let hash_equal =
fn(input: Input) {
let hash = input.output_reference
utxo_ref == hash
}
let target_input_exist = list.find(inputs, hash_equal)
when target_input_exist is {
Some(_) -> True
None -> False
}
}
RBurn -> check_policy_only_burn(transaction.mint, current_policy)
}
_ -> False
}
}
75 changes: 75 additions & 0 deletions lib/aiken-content-ownership/validators/oracle_validator.ak
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use aiken/transaction.{InlineDatum, Output, ScriptContext, Spend, Transaction}
use aiken_content_ownership/common.{
all_key_signed, key_signed, only_minted_token, outputs_at_with,
}
use aiken_content_ownership/types.{
CreateContentRegistry, CreateOwnershipRegistry, OracleDatum, RotateKey,
StopApp,
}
use aiken_content_ownership/utils.{get_registry_token_name}

pub fn oracle_validator_logic(datum, redeemer, context) {
let ScriptContext { purpose, transaction } = context
when purpose is {
Spend(_) -> {
let Transaction { outputs, mint, extra_signatories, .. } = transaction
let OracleDatum {
oracle_nft,
oracle_address,
content_registry_ref_token,
content_registry_count,
ownership_registry_ref_token,
ownership_registry_count,
operation_key,
stop_key,
..
} = datum

when redeemer is {
CreateContentRegistry ->
// Ref token is minted
only_minted_token(
mint,
content_registry_ref_token,
get_registry_token_name(content_registry_count),
1,
)

CreateOwnershipRegistry ->
// Ref token is minted
only_minted_token(
mint,
ownership_registry_ref_token,
get_registry_token_name(ownership_registry_count),
1,
)
RotateKey { new_operation_key, new_stop_key } -> {
// Sign by both ops and stop key
expect [own_output] =
outputs_at_with(outputs, oracle_address, oracle_nft, "")
expect InlineDatum(dat) = own_output.datum
expect own_datum: OracleDatum = dat
let datum_correct =
own_datum == OracleDatum {
..datum,
operation_key: new_operation_key,
stop_key: new_stop_key,
}
all_key_signed(extra_signatories, [operation_key, stop_key]) && datum_correct
}

// datum update correctly (key rotated)
StopApp ->
// signed by stop key
// oracle nft is burned
key_signed(extra_signatories, stop_key) && only_minted_token(
mint,
oracle_nft,
"",
-1,
)
}
}
_ -> False
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use aiken/dict
// use aiken/list
use aiken/transaction.{
InlineDatum, Input, Mint, Output, ScriptContext, Transaction,
}
use aiken/transaction/value.{PolicyId}
use aiken_content_ownership/common.{
check_policy_only_burn, inputs_with, outputs_at_with, value_length,
}
use aiken_content_ownership/types.{
MintPolarity, OracleDatum, OwnershipRegistryDatum, RBurn, RMint,
}
use aiken_content_ownership/utils.{get_registry_token_name}

pub fn ownership_registry_ref_token_logic(
oracle_nft: PolicyId,
redeemer: MintPolarity,
context: ScriptContext,
) -> Bool {
let ScriptContext { purpose, transaction } = context
let Transaction { inputs, outputs, mint, .. } = transaction
when purpose is {
Mint(current_policy) ->
when (redeemer, inputs_with(inputs, oracle_nft, "")) is {
(RMint, [oracle_input]) -> {
expect InlineDatum(inline_datum) = oracle_input.output.datum
expect input_datum: OracleDatum = inline_datum
let OracleDatum {
oracle_address,
ownership_registry_ref_token,
ownership_registry_address,
ownership_registry_count,
..
} = input_datum
when
(
outputs_at_with(outputs, oracle_address, oracle_nft, ""),
outputs_at_with(
outputs,
ownership_registry_address,
ownership_registry_ref_token,
get_registry_token_name(ownership_registry_count),
),
)
is {
([oracle_output], [registry_output]) -> {
expect InlineDatum(raw_oracle_datum) = oracle_output.datum
expect InlineDatum(raw_registry_datum) = registry_output.datum
expect oracle_datum: OracleDatum = raw_oracle_datum
expect OwnershipRegistryDatum { count, registry }: OwnershipRegistryDatum =
raw_registry_datum
let oracle_datum_updated =
oracle_datum == OracleDatum {
..input_datum,
ownership_registry_count: ownership_registry_count + 1,
}
let registry_initial_datum_correct =
count == 0 && registry == dict.new()
let oracle_output_value_clean =
value_length(oracle_output.value) == 2
let registry_output_value_clean =
value_length(registry_output.value) == 2
oracle_datum_updated && registry_initial_datum_correct && oracle_output_value_clean && registry_output_value_clean
}
_ -> False
}
}
(RBurn, _) -> check_policy_only_burn(mint, current_policy)
_ -> False
}
_ -> False
}
}
Loading

0 comments on commit c493ff9

Please sign in to comment.