Skip to content

Commit

Permalink
UPdated test
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexeyVavilin committed Feb 5, 2025
1 parent 4e1a27f commit 6a959e8
Showing 1 changed file with 176 additions and 73 deletions.
249 changes: 176 additions & 73 deletions tvm_block/src/tests/test_merkle_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -913,34 +913,34 @@ fn test_update_shard_state_with_external_cell() {
.unwrap();
}
shard_state_full.write_accounts(&shard_accounts_full).unwrap();
let account_id5 = UInt256::from([50].as_slice());
let account_id6 = UInt256::from([60].as_slice());
let account_id5 = UInt256::from([60].as_slice());
let account_id6 = UInt256::from([50].as_slice());

let create_apply_and_check = |old_state: &ShardStateUnsplit,
apply_to: &ShardStateUnsplit,
fast: bool,
modifier: &dyn Fn(
&mut ShardStateUnsplit,
&mut ShardStateUnsplit,
) -> Option<ShardAccount>|
-> Result<(MerkleUpdate, MerkleUpdate)> {
-> Result<MerkleUpdate> {
let mut old_state = old_state.clone();
let old_state_root = old_state.serialize().unwrap();
let usage_tree = UsageTree::with_root(old_state_root);
let usage_tree = UsageTree::with_params(old_state_root, true);
let old_state_root = usage_tree.root_cell();
let mut new_state = ShardStateUnsplit::construct_from_cell(old_state_root.clone()).unwrap();

let acc = modifier(&mut old_state, &mut new_state);

let create_fast_result = MerkleUpdate::create_fast(
&old_state.serialize().unwrap(),
&new_state.serialize().unwrap(),
|hash| usage_tree.contains(hash),
);
let create_result =
MerkleUpdate::create(&old_state.serialize().unwrap(), &new_state.serialize().unwrap());
assert_eq!(create_fast_result.is_ok(), create_result.is_ok());
let update = create_result?;
let update_fast = create_fast_result?;
let update = if fast {
MerkleUpdate::create_fast(
&old_state.serialize().unwrap(),
&new_state.serialize().unwrap(),
|hash| usage_tree.contains(hash),
)?
} else {
MerkleUpdate::create(&old_state.serialize().unwrap(), &new_state.serialize().unwrap())?
};

let new_state_acc = apply_to
.read_accounts()
Expand All @@ -951,25 +951,16 @@ fn test_update_shard_state_with_external_cell() {
.read_account()
.unwrap();

let check = |update: &MerkleUpdate| -> Result<()> {
let new_state_constructed = ShardStateUnsplit::construct_from_cell(
update.apply_for(&apply_to.serialize().unwrap())?,
)
.unwrap();
let shard_accounts = new_state_constructed.read_accounts().unwrap();
assert_eq!(acc, shard_accounts.account(&account_id5.clone().into()).unwrap());
let shard_acc = shard_accounts.account(&account_id6.clone().into()).unwrap().unwrap();
assert_eq!(shard_acc.read_account().unwrap(), new_state_acc);
Ok(())
};

let result = check(&update);
let result_fast = check(&update_fast);
assert_eq!(result.is_ok(), result_fast.is_ok());
result?;
result_fast?;
let new_state_constructed = ShardStateUnsplit::construct_from_cell(
update.apply_for(&apply_to.serialize().unwrap())?,
)
.unwrap();
let shard_accounts = new_state_constructed.read_accounts().unwrap();
assert_eq!(acc, shard_accounts.account(&account_id5.clone().into()).unwrap());
let shard_acc = shard_accounts.account(&account_id6.clone().into()).unwrap().unwrap();
assert_eq!(shard_acc.read_account().unwrap(), new_state_acc);

Ok((update, update_fast))
Ok(update)
};

let add_balance = |_old_state: &mut ShardStateUnsplit,
Expand All @@ -986,9 +977,6 @@ fn test_update_shard_state_with_external_cell() {
Some(shard_acc)
};

let update_full_full =
create_apply_and_check(&shard_state_full, &shard_state_full, &add_balance).unwrap();

let mut shard_accounts_ext5 = shard_accounts_full.clone();
let acc5_root = shard_accounts_ext5.replace_with_external(&account_id5).unwrap();
let mut shard_state_ext5 = shard_state_full.clone();
Expand All @@ -1009,14 +997,6 @@ fn test_update_shard_state_with_external_cell() {
Some(shard_acc)
};

// assert!(create_apply_and_check(&shard_state_full, &shard_state_ext5, &add_balance).is_err());
assert!(
create_apply_and_check(&shard_state_ext5, &shard_state_full, &add_balance_ext).is_err()
);
assert!(
create_apply_and_check(&shard_state_ext5, &shard_state_ext5, &add_balance_ext).is_err()
);

let add_balance_ext_update = |old_state: &mut ShardStateUnsplit,
new_state: &mut ShardStateUnsplit|
-> Option<ShardAccount> {
Expand All @@ -1031,37 +1011,116 @@ fn test_update_shard_state_with_external_cell() {
result
};

let update_ext_full =
create_apply_and_check(&shard_state_ext5, &shard_state_full, &add_balance_ext_update)
.unwrap();
assert_eq!(update_full_full, update_ext_full);
// assert!(create_apply_and_check(&shard_state_ext5, &shard_state_ext5, &add_balance_ext_update).is_err());

let mut shard_acc = shard_accounts_ext5.account(&account_id5.clone().into()).unwrap().unwrap();
shard_acc.set_account_cell(acc5_root.clone());
shard_accounts_ext5.insert(&account_id5, &shard_acc).unwrap();
let mut shard_state_loaded = shard_state_ext5.clone();
shard_state_loaded.write_accounts(&shard_accounts_ext5).unwrap();

let mut shard_accounts_ext6 = shard_accounts_full.clone();
shard_accounts_ext6.replace_with_external(&account_id6).unwrap();
let mut shard_state_ext6 = shard_state_full.clone();
shard_state_ext6.write_accounts(&shard_accounts_ext6).unwrap();

let update_full_full =
create_apply_and_check(&shard_state_full, &shard_state_full, false, &add_balance).unwrap();

assert!(
create_apply_and_check(&shard_state_full, &shard_state_ext5, false, &add_balance).is_err()
);
assert!(
create_apply_and_check(&shard_state_ext5, &shard_state_full, false, &add_balance_ext)
.is_err()
);
assert!(
create_apply_and_check(&shard_state_ext5, &shard_state_ext5, false, &add_balance_ext)
.is_err()
);

let update_ext_full = create_apply_and_check(
&shard_state_ext5,
&shard_state_full,
false,
&add_balance_ext_update,
)
.unwrap();
assert_eq!(update_full_full, update_ext_full);
assert!(
create_apply_and_check(
&shard_state_ext5,
&shard_state_ext5,
false,
&add_balance_ext_update
)
.is_err()
);

let update_full_ext =
create_apply_and_check(&shard_state_full, &shard_state_loaded, &add_balance).unwrap();
let update_ext_ext =
create_apply_and_check(&shard_state_ext5, &shard_state_loaded, &add_balance_ext_update)
create_apply_and_check(&shard_state_full, &shard_state_loaded, false, &add_balance)
.unwrap();
let update_ext_ext = create_apply_and_check(
&shard_state_ext5,
&shard_state_loaded,
false,
&add_balance_ext_update,
)
.unwrap();
assert_eq!(update_full_full, update_full_ext);
assert_eq!(update_full_full, update_ext_ext);

let mut shard_accounts_ext6 = shard_accounts_full.clone();
shard_accounts_ext6.replace_with_external(&account_id6).unwrap();
let mut shard_state_ext6 = shard_state_full.clone();
shard_state_ext6.write_accounts(&shard_accounts_ext6).unwrap();
let update_ext_full =
create_apply_and_check(&shard_state_ext6, &shard_state_full, false, &add_balance).unwrap();
let update_full_ext =
create_apply_and_check(&shard_state_full, &shard_state_ext6, false, &add_balance).unwrap();
let update_ext_ext =
create_apply_and_check(&shard_state_ext6, &shard_state_ext6, false, &add_balance).unwrap();

assert_eq!(update_full_full, update_ext_full);
assert_eq!(update_full_full, update_full_ext);
assert_eq!(update_full_full, update_ext_ext);

let update_full_full =
create_apply_and_check(&shard_state_full, &shard_state_full, true, &add_balance).unwrap();

assert!(
create_apply_and_check(&shard_state_ext5, &shard_state_full, true, &add_balance_ext)
.is_err()
);
assert!(
create_apply_and_check(&shard_state_full, &shard_state_ext5, true, &add_balance).is_err()
);
assert!(
create_apply_and_check(&shard_state_ext5, &shard_state_ext5, true, &add_balance_ext)
.is_err()
);

let update_ext_full =
create_apply_and_check(&shard_state_ext6, &shard_state_full, &add_balance).unwrap();
create_apply_and_check(&shard_state_ext5, &shard_state_full, true, &add_balance_ext_update)
.unwrap();
assert_ne!(update_full_full, update_ext_full);
let update_ext_ext =
create_apply_and_check(&shard_state_ext5, &shard_state_ext5, true, &add_balance_ext_update)
.unwrap();
assert_ne!(update_full_full, update_ext_ext);

let update_full_ext =
create_apply_and_check(&shard_state_full, &shard_state_ext6, &add_balance).unwrap();
create_apply_and_check(&shard_state_full, &shard_state_loaded, true, &add_balance).unwrap();
let update_ext_ext = create_apply_and_check(
&shard_state_ext5,
&shard_state_loaded,
true,
&add_balance_ext_update,
)
.unwrap();
assert_eq!(update_full_full, update_full_ext);
assert_ne!(update_full_full, update_ext_ext);

let update_ext_full =
create_apply_and_check(&shard_state_ext6, &shard_state_full, true, &add_balance).unwrap();
let update_full_ext =
create_apply_and_check(&shard_state_full, &shard_state_ext6, true, &add_balance).unwrap();
let update_ext_ext =
create_apply_and_check(&shard_state_ext6, &shard_state_ext6, &add_balance).unwrap();
create_apply_and_check(&shard_state_ext6, &shard_state_ext6, true, &add_balance).unwrap();

assert_eq!(update_full_full, update_ext_full);
assert_eq!(update_full_full, update_full_ext);
Expand All @@ -1079,13 +1138,6 @@ fn test_update_shard_state_with_external_cell() {
None
};

let update_full_full =
create_apply_and_check(&shard_state_full, &shard_state_full, &remove).unwrap();

// assert!(create_apply_and_check(&shard_state_full, &shard_state_ext5, &remove).is_err());
assert!(create_apply_and_check(&shard_state_ext5, &shard_state_full, &remove).is_err());
assert!(create_apply_and_check(&shard_state_ext5, &shard_state_ext5, &remove).is_err());

let remove_update = |old_state: &mut ShardStateUnsplit,
new_state: &mut ShardStateUnsplit|
-> Option<ShardAccount> {
Expand All @@ -1100,24 +1152,75 @@ fn test_update_shard_state_with_external_cell() {
None
};

let update_full_full =
create_apply_and_check(&shard_state_full, &shard_state_full, false, &remove).unwrap();
let update_full_ext =
create_apply_and_check(&shard_state_full, &shard_state_ext5, false, &remove).unwrap();
assert_eq!(update_full_full, update_full_ext);

assert!(create_apply_and_check(&shard_state_ext5, &shard_state_full, false, &remove).is_err());
assert!(create_apply_and_check(&shard_state_ext5, &shard_state_ext5, false, &remove).is_err());

let update_ext_full =
create_apply_and_check(&shard_state_ext5, &shard_state_full, false, &remove_update)
.unwrap();
assert_eq!(update_full_full, update_ext_full);
let update_ext_ext =
create_apply_and_check(&shard_state_ext5, &shard_state_ext5, false, &remove_update)
.unwrap();
assert_eq!(update_full_full, update_ext_ext);

let update_full_ext =
create_apply_and_check(&shard_state_full, &shard_state_loaded, false, &remove_update)
.unwrap();
let update_ext_ext =
create_apply_and_check(&shard_state_ext5, &shard_state_loaded, false, &remove_update)
.unwrap();
assert_eq!(update_full_full, update_full_ext);
assert_eq!(update_full_full, update_ext_ext);

let update_ext_full =
create_apply_and_check(&shard_state_ext5, &shard_state_full, &remove_update).unwrap();
create_apply_and_check(&shard_state_ext6, &shard_state_full, false, &remove).unwrap();
let update_full_ext =
create_apply_and_check(&shard_state_full, &shard_state_ext6, false, &remove).unwrap();
let update_ext_ext =
create_apply_and_check(&shard_state_ext6, &shard_state_ext6, false, &remove).unwrap();

assert_eq!(update_full_full, update_ext_full);
// assert!(create_apply_and_check(&shard_state_ext5, &shard_state_ext5, &remove_update).is_err());
assert_eq!(update_full_full, update_full_ext);
assert_eq!(update_full_full, update_ext_ext);

let update_full_full =
create_apply_and_check(&shard_state_full, &shard_state_full, true, &remove).unwrap();
let update_full_ext =
create_apply_and_check(&shard_state_full, &shard_state_loaded, &remove_update).unwrap();
create_apply_and_check(&shard_state_full, &shard_state_ext5, true, &remove).unwrap();
assert_eq!(update_full_full, update_full_ext);

assert!(create_apply_and_check(&shard_state_ext5, &shard_state_full, true, &remove).is_err());
assert!(create_apply_and_check(&shard_state_ext5, &shard_state_ext5, true, &remove).is_err());

let update_ext_full =
create_apply_and_check(&shard_state_ext5, &shard_state_full, true, &remove_update).unwrap();
assert_eq!(update_full_full, update_ext_full);
let update_ext_ext =
create_apply_and_check(&shard_state_ext5, &shard_state_loaded, &remove_update).unwrap();
create_apply_and_check(&shard_state_ext5, &shard_state_ext5, true, &remove_update).unwrap();
assert_eq!(update_full_full, update_ext_ext);

let update_full_ext =
create_apply_and_check(&shard_state_full, &shard_state_loaded, true, &remove_update)
.unwrap();
let update_ext_ext =
create_apply_and_check(&shard_state_ext5, &shard_state_loaded, true, &remove_update)
.unwrap();
assert_eq!(update_full_full, update_full_ext);
assert_eq!(update_full_full, update_ext_ext);

let update_ext_full =
create_apply_and_check(&shard_state_ext6, &shard_state_full, &remove).unwrap();
create_apply_and_check(&shard_state_ext6, &shard_state_full, true, &remove).unwrap();
let update_full_ext =
create_apply_and_check(&shard_state_full, &shard_state_ext6, &remove).unwrap();
create_apply_and_check(&shard_state_full, &shard_state_ext6, true, &remove).unwrap();
let update_ext_ext =
create_apply_and_check(&shard_state_ext6, &shard_state_ext6, &remove).unwrap();
create_apply_and_check(&shard_state_ext6, &shard_state_ext6, true, &remove).unwrap();

assert_eq!(update_full_full, update_ext_full);
assert_eq!(update_full_full, update_full_ext);
Expand Down

0 comments on commit 6a959e8

Please sign in to comment.