Skip to content

Commit

Permalink
feat: refactoring dict to list
Browse files Browse the repository at this point in the history
  • Loading branch information
HinsonSIDAN committed Dec 5, 2023
1 parent 3e92321 commit 027847d
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 136 deletions.
5 changes: 2 additions & 3 deletions lib/aiken-content-ownership/placeholder.ak
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use aiken/dict.{Dict}
use aiken/transaction.{
InlineDatum, NoDatum, Output, OutputReference, TransactionId,
}
Expand Down Expand Up @@ -158,7 +157,7 @@ pub fn mock_oracle_output(oracle_datum: OracleDatum) -> Output {

pub fn mock_content_registry_datum(
count: Int,
registry: Dict<Int, ByteArray>,
registry: List<ByteArray>,
) -> ContentRegistryDatum {
ContentRegistryDatum { count, registry }
}
Expand All @@ -182,7 +181,7 @@ pub fn mock_content_registry_output(

pub fn mock_ownership_registry_datum(
count: Int,
registry: Dict<Int, (ByteArray, ByteArray)>,
registry: List<(ByteArray, ByteArray)>,
) -> OwnershipRegistryDatum {
OwnershipRegistryDatum { count, registry }
}
Expand Down
5 changes: 2 additions & 3 deletions lib/aiken-content-ownership/types.ak
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use aiken/dict.{Dict}
use aiken/transaction/credential.{Address}
use aiken/transaction/value.{AssetName, PolicyId}

Expand Down Expand Up @@ -29,7 +28,7 @@ pub type OracleRedeemer {

pub type ContentRegistryDatum {
count: Int,
registry: Dict<Int, ByteArray>,
registry: List<ByteArray>,
}

pub type ContentRegistryRedeemer {
Expand All @@ -40,7 +39,7 @@ pub type ContentRegistryRedeemer {

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

pub type OwnershipRegistryRedeemer {
Expand Down
28 changes: 6 additions & 22 deletions lib/aiken-content-ownership/validators/content_registry.ak
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use aiken/dict
use aiken/int
use aiken/list
use aiken/transaction.{
Datum, InlineDatum, ScriptContext, ScriptPurpose, Spend, Transaction,
Expand All @@ -19,7 +17,6 @@ use aiken_content_ownership/types.{
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
Expand All @@ -29,12 +26,7 @@ pub fn check_is_content_registry_updated(
let is_content_registry_updated =
output_datum == ContentRegistryDatum {
count: input_datum.count,
registry: dict.insert(
input_datum.registry,
content_number,
new_content_hash,
int.compare,
),
registry: list.concat(input_datum.registry, [new_content_hash]),
}
is_content_registry_updated
}
Expand Down Expand Up @@ -131,28 +123,21 @@ pub fn content_registry_logic(
let content_new_datum_correct =
content_output_datum == ContentRegistryDatum {
count: content_input_datum.count + 1,
registry: dict.insert(
registry: list.concat(
content_input_datum.registry,
content_input_datum.count,
content_hash,
int.compare,
[content_hash],
),
}
let ownership_new_datum_correct =
ownership_output_datum == OwnershipRegistryDatum {
count: ownership_input_datum.count + 1,
registry: dict.insert(
ownership_input_datum.registry,
ownership_input_datum.count,
owner,
int.compare,
),
registry: list.concat(ownership_input_datum.registry, [owner]),
}
let content_registry_value_clean =
list.length(flatten(content_output.value)) == 2
let ownership_registry_value_clean =
list.length(flatten(ownership_output.value)) == 2
ref_tokens_equal && current_count_equal && content_new_datum_correct && ownership_new_datum_correct && content_registry_value_clean && ownership_registry_value_clean
ref_tokens_equal && current_count_equal && content_registry_value_clean && ownership_registry_value_clean && content_new_datum_correct && ownership_new_datum_correct
}
_ -> False
}
Expand Down Expand Up @@ -182,15 +167,14 @@ 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) =
ownership_ref_input.output.datum
expect ownership_ref_datum: OwnershipRegistryDatum =
raw_ownership_ref_datum
expect Some(original_owner) =
dict.get(ownership_ref_datum.registry, content_number)
list.at(ownership_ref_datum.registry, content_number)
let is_update_authorized =
inputs_token_quantity(inputs, original_owner) > 0
let is_registry_value_clean =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use aiken/dict
// use aiken/list
use aiken/transaction.{
InlineDatum, Input, Mint, Output, ScriptContext, Transaction,
}
Expand Down Expand Up @@ -54,8 +52,7 @@ pub fn content_registry_ref_token_logic(
..input_datum,
content_registry_count: content_registry_count + 1,
}
let registry_initial_datum_correct =
count == 0 && registry == dict.new()
let registry_initial_datum_correct = count == 0 && registry == []
let oracle_output_value_clean =
value_length(oracle_output.value) == 2
let registry_output_value_clean =
Expand Down
10 changes: 2 additions & 8 deletions lib/aiken-content-ownership/validators/ownership_registry.ak
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use aiken/dict
use aiken/int
use aiken/list
use aiken/transaction.{
InlineDatum, ScriptContext, ScriptPurpose, Spend, Transaction,
Expand Down Expand Up @@ -80,18 +79,13 @@ pub fn ownership_registry_logic(
expect InlineDatum(raw_output_datum) = ownership_output.datum
expect output_datum: OwnershipRegistryDatum = raw_output_datum
expect Some(original_owner) =
dict.get(input_datum.registry, content_number)
list.at(input_datum.registry, content_number)
let is_original_owner_authorized =
inputs_token_quantity(inputs, original_owner) > 0
let is_registry_updated =
output_datum == OwnershipRegistryDatum {
count: input_datum.count,
registry: dict.insert(
input_datum.registry,
content_number,
new_owner_token,
int.compare,
),
registry: list.concat(input_datum.registry, [new_owner_token]),
}
let is_registry_value_clean =
list.length(flatten(ownership_output.value)) == 2
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use aiken/dict
// use aiken/list
use aiken/transaction.{
InlineDatum, Input, Mint, Output, ScriptContext, Transaction,
}
Expand Down Expand Up @@ -54,8 +52,7 @@ pub fn ownership_registry_ref_token_logic(
..input_datum,
ownership_registry_count: ownership_registry_count + 1,
}
let registry_initial_datum_correct =
count == 0 && registry == dict.new()
let registry_initial_datum_correct = count == 0 && registry == []
let oracle_output_value_clean =
value_length(oracle_output.value) == 2
let registry_output_value_clean =
Expand Down
91 changes: 81 additions & 10 deletions validators/tests/integration-tests/create_content.ak
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use aiken/dict.{Dict}
use aiken/int
use aiken/dict
use aiken/list
use aiken/transaction.{
Input, Output, ScriptContext, Spend, Transaction, placeholder,
}
Expand Down Expand Up @@ -49,8 +49,8 @@ fn default_test_case() -> TestCase {

fn make_mock_tx_body(
record_count: Int,
content_registry: Dict<Int, ByteArray>,
ownership_registry: Dict<Int, (PolicyId, AssetName)>,
content_registry: List<ByteArray>,
ownership_registry: List<(PolicyId, AssetName)>,
content_hash: ByteArray,
owner: (PolicyId, AssetName),
test_case: TestCase,
Expand Down Expand Up @@ -80,13 +80,13 @@ fn make_mock_tx_body(
}
let new_content_registry =
if is_content_registry_updated {
dict.insert(content_registry, record_count, content_hash, int.compare)
list.concat(content_registry, [content_hash])
} else {
content_registry
}
let new_ownership_registry =
if is_ownership_registry_updated {
dict.insert(ownership_registry, record_count, owner, int.compare)
list.concat(ownership_registry, [owner])
} else {
ownership_registry
}
Expand Down Expand Up @@ -199,10 +199,9 @@ fn make_mock_tx_body(
fn run_create_content_test(test_case: TestCase) -> Bool {
let content_hash = "QmWBaeu6y1zEcKbsEqCuhuDHPL3W8pZouCPdafMCRCSUWk"
let owner = (mock_policy_id_4(), "my_token_name")
let transaction =
make_mock_tx_body(0, dict.new(), dict.new(), content_hash, owner, test_case)
let content_dat = mock_content_registry_datum(0, dict.new())
let ownership_dat = mock_ownership_registry_datum(0, dict.new())
let transaction = make_mock_tx_body(0, [], [], content_hash, owner, test_case)
let content_dat = mock_content_registry_datum(0, [])
let ownership_dat = mock_ownership_registry_datum(0, [])
let ctx = ScriptContext { purpose: Spend(mock_utxo_ref(1)), transaction }
ownership_registry_logic(
mock_policy_id(),
Expand Down Expand Up @@ -277,3 +276,75 @@ test fail_create_content_record_with_incorrect_redeemer() {
}
!run_create_content_test(test_case)
}

fn create_preloaded_content_registry(
registry: List<ByteArray>,
curr: Int,
size: Int,
) -> List<ByteArray> {
let content_hash = "QmWBaeu6y1zEcKbsEqCuhuDHPL3W8pZouCPdafMCRCSUWk"
let new_registry = list.concat(registry, [content_hash])
if curr == size {
new_registry
} else {
create_preloaded_content_registry(new_registry, curr + 1, size)
}
}

fn mock_content_registry_datum_at_scale(size: Int) -> ContentRegistryDatum {
let registry = create_preloaded_content_registry([], 0, size)
ContentRegistryDatum { count: size, registry }
}

fn create_preloaded_ownership_registry(
registry: List<(PolicyId, AssetName)>,
curr: Int,
size: Int,
) -> List<(PolicyId, AssetName)> {
let owner = (mock_policy_id_4(), "my_token_name")
let new_registry = list.concat(registry, [owner])
if curr == size {
new_registry
} else {
create_preloaded_ownership_registry(new_registry, curr + 1, size)
}
}

fn mock_ownership_registry_datum_at_scale(size: Int) -> OwnershipRegistryDatum {
let registry = create_preloaded_ownership_registry([], 0, size)
OwnershipRegistryDatum { count: size, registry }
}

fn run_create_content_at_scale_test(test_case: TestCase) -> Bool {
let content_hash = "QmWBaeu6y1zEcKbsEqCuhuDHPL3W8pZouCPdafMCRCSUWk"
let owner = (mock_policy_id_4(), "my_token_name")
let content_registry = create_preloaded_content_registry([], 0, 50)
let ownership_registry = create_preloaded_ownership_registry([], 0, 50)
let transaction =
make_mock_tx_body(
0,
content_registry,
ownership_registry,
content_hash,
owner,
test_case,
)
let content_dat = mock_content_registry_datum(0, [])
let ownership_dat = mock_ownership_registry_datum(0, [])
let ctx = ScriptContext { purpose: Spend(mock_utxo_ref(1)), transaction }
ownership_registry_logic(
mock_policy_id(),
ownership_dat,
CreateOwnershipRecord,
ctx,
) && content_registry_logic(
mock_policy_id(),
content_dat,
CreateContent { content_hash, owner },
ctx,
)
}

test success_create_content_at_scale() {
run_create_content_at_scale_test(default_test_case())
}
Empty file.
Loading

0 comments on commit 027847d

Please sign in to comment.