Skip to content

Commit

Permalink
feat: updating record update logic after refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
HinsonSIDAN committed Dec 5, 2023
1 parent 027847d commit 054fe70
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 7 deletions.
21 changes: 20 additions & 1 deletion lib/aiken-content-ownership/validators/content_registry.ak
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,37 @@ use aiken_content_ownership/types.{
OwnershipRegistryDatum, StopContentRegistry, UpdateContent,
}

pub fn update_content_registry_at(
registry: List<ByteArray>,
index: Int,
new_value: ByteArray,
) -> List<ByteArray> {
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
}
Expand Down Expand Up @@ -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) =
Expand Down
19 changes: 17 additions & 2 deletions lib/aiken-content-ownership/validators/ownership_registry.ak
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions validators/tests/integration-tests/update_content.ak
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

12 changes: 10 additions & 2 deletions validators/tests/unit-tests/content_registry.ak
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -242,6 +244,7 @@ type UpdateTestCase {
}

fn make_mock_tx_body_for_update(
record_count: Int,
content_registry: List<ByteArray>,
ownership_registry: List<(PolicyId, AssetName)>,
owner: (PolicyId, AssetName),
Expand Down Expand Up @@ -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
},
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 6 additions & 2 deletions validators/tests/unit-tests/ownership_registry.ak
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
},
Expand Down

0 comments on commit 054fe70

Please sign in to comment.