diff --git a/lib/aiken-content-ownership/validators/content_registry.ak b/lib/aiken-content-ownership/validators/content_registry.ak index 0a785fe..64207ff 100644 --- a/lib/aiken-content-ownership/validators/content_registry.ak +++ b/lib/aiken-content-ownership/validators/content_registry.ak @@ -14,19 +14,37 @@ use aiken_content_ownership/types.{ OwnershipRegistryDatum, StopContentRegistry, UpdateContent, } +pub fn update_content_registry_at( + registry: List, + index: Int, + new_value: ByteArray, +) -> List { + let spanned_registry = list.span(registry, index) + expect Some(dropped_head_tail) = list.tail(spanned_registry.2nd) + let updated_tail = list.push(dropped_head_tail, new_value) + list.concat(spanned_registry.1st, updated_tail) +} + pub fn check_is_content_registry_updated( old_datum: Datum, new_datum: Datum, + content_number: Int, new_content_hash: ByteArray, ) -> Bool { expect InlineDatum(raw_input_datum) = old_datum expect input_datum: ContentRegistryDatum = raw_input_datum expect InlineDatum(raw_output_datum) = new_datum expect output_datum: ContentRegistryDatum = raw_output_datum + let updated_registry = + update_content_registry_at( + input_datum.registry, + content_number, + new_content_hash, + ) let is_content_registry_updated = output_datum == ContentRegistryDatum { count: input_datum.count, - registry: list.concat(input_datum.registry, [new_content_hash]), + registry: updated_registry, } is_content_registry_updated } @@ -167,6 +185,7 @@ pub fn content_registry_logic( check_is_content_registry_updated( content_input.output.datum, content_output.datum, + content_number, new_content_hash, ) expect InlineDatum(raw_ownership_ref_datum) = diff --git a/lib/aiken-content-ownership/validators/ownership_registry.ak b/lib/aiken-content-ownership/validators/ownership_registry.ak index 215cda1..1138477 100644 --- a/lib/aiken-content-ownership/validators/ownership_registry.ak +++ b/lib/aiken-content-ownership/validators/ownership_registry.ak @@ -4,7 +4,7 @@ use aiken/transaction.{ InlineDatum, ScriptContext, ScriptPurpose, Spend, Transaction, } use aiken/transaction/value.{ - PolicyId, flatten, from_minted_value, negate, without_lovelace, + AssetName, PolicyId, flatten, from_minted_value, negate, without_lovelace, } use aiken_content_ownership/common.{ inputs_at, inputs_at_with_policy, inputs_token_quantity, inputs_with, @@ -16,6 +16,17 @@ use aiken_content_ownership/types.{ TransferOwnership, } +pub fn update_ownership_registry_at( + registry: List<(PolicyId, AssetName)>, + index: Int, + new_value: (PolicyId, AssetName), +) -> List<(PolicyId, AssetName)> { + let spanned_registry = list.span(registry, index) + expect Some(dropped_head_tail) = list.tail(spanned_registry.2nd) + let updated_tail = list.push(dropped_head_tail, new_value) + list.concat(spanned_registry.1st, updated_tail) +} + pub fn ownership_registry_logic( oracle_nft: PolicyId, _datum: OwnershipRegistryDatum, @@ -85,7 +96,11 @@ pub fn ownership_registry_logic( let is_registry_updated = output_datum == OwnershipRegistryDatum { count: input_datum.count, - registry: list.concat(input_datum.registry, [new_owner_token]), + registry: update_ownership_registry_at( + input_datum.registry, + content_number, + new_owner_token, + ), } let is_registry_value_clean = list.length(flatten(ownership_output.value)) == 2 diff --git a/validators/tests/integration-tests/update_content.ak b/validators/tests/integration-tests/update_content.ak index e69de29..8b13789 100644 --- a/validators/tests/integration-tests/update_content.ak +++ b/validators/tests/integration-tests/update_content.ak @@ -0,0 +1 @@ + diff --git a/validators/tests/unit-tests/content_registry.ak b/validators/tests/unit-tests/content_registry.ak index 879844d..53703be 100644 --- a/validators/tests/unit-tests/content_registry.ak +++ b/validators/tests/unit-tests/content_registry.ak @@ -16,7 +16,9 @@ use aiken_content_ownership/types.{ StopContentRegistry, UpdateContent, } use aiken_content_ownership/utils.{get_registry_token_name} -use aiken_content_ownership/validators/content_registry.{content_registry_logic} +use aiken_content_ownership/validators/content_registry.{ + content_registry_logic, update_content_registry_at, +} type TestCase { is_content_count_updated: Bool, @@ -242,6 +244,7 @@ type UpdateTestCase { } fn make_mock_tx_body_for_update( + record_count: Int, content_registry: List, ownership_registry: List<(PolicyId, AssetName)>, owner: (PolicyId, AssetName), @@ -291,7 +294,11 @@ fn make_mock_tx_body_for_update( ContentRegistryDatum { count: 6, registry: if test_case.is_registry_updated { - list.concat(content_registry, [new_content_hash]) + update_content_registry_at( + content_registry, + record_count, + new_content_hash, + ) } else { content_registry }, @@ -349,6 +356,7 @@ fn update_base_case(test_case: UpdateTestCase) { let dat = ContentRegistryDatum { count: 0, registry: current_registry } let tx = make_mock_tx_body_for_update( + 4, current_registry, ownership_registry, owner, diff --git a/validators/tests/unit-tests/ownership_registry.ak b/validators/tests/unit-tests/ownership_registry.ak index b277e38..cb96ba4 100644 --- a/validators/tests/unit-tests/ownership_registry.ak +++ b/validators/tests/unit-tests/ownership_registry.ak @@ -19,7 +19,7 @@ use aiken_content_ownership/types.{ } use aiken_content_ownership/utils.{get_registry_token_name} use aiken_content_ownership/validators/ownership_registry.{ - ownership_registry_logic, + ownership_registry_logic, update_ownership_registry_at, } type CreateTestCase { @@ -252,7 +252,11 @@ fn make_mock_tx_body_for_update( OwnershipRegistryDatum { count: record_count, registry: if test_case.is_registry_updated { - list.concat(ownership_registry, [new_owner]) + update_ownership_registry_at( + ownership_registry, + record_count, + new_owner, + ) } else { ownership_registry },