mirror of
https://github.com/logos-blockchain/logos-execution-zone.git
synced 2026-07-11 16:29:45 +00:00
* refactor(lee): split large modules into directories and extract tests Split state.rs, program.rs, circuit.rs, validated_state_diff.rs, merkle_tree, and core/program.rs into module directories with separate test files. State tests are further split into themed files (genesis, authenticated_transfer, circuit, claiming, etc.). Extract authenticate_public_transaction_signers helper in validated_state_diff to remove duplicated authentication logic. * chore: rebuild artifacts
129 lines
3.9 KiB
Rust
129 lines
3.9 KiB
Rust
use super::*;
|
|
|
|
#[test]
|
|
fn new_works() {
|
|
let key1 = PrivateKey::try_new([1; 32]).unwrap();
|
|
let key2 = PrivateKey::try_new([2; 32]).unwrap();
|
|
let addr1 = AccountId::from(&PublicKey::new_from_private_key(&key1));
|
|
let addr2 = AccountId::from(&PublicKey::new_from_private_key(&key2));
|
|
let expected_public_state = {
|
|
let mut this = HashMap::new();
|
|
this.insert(
|
|
addr1,
|
|
Account {
|
|
balance: 100,
|
|
..Account::default()
|
|
},
|
|
);
|
|
this.insert(
|
|
addr2,
|
|
Account {
|
|
balance: 151,
|
|
..Account::default()
|
|
},
|
|
);
|
|
this
|
|
};
|
|
let expected_builtin_programs = HashMap::new();
|
|
|
|
let state =
|
|
V03State::new().with_public_account_balances([(addr1, 100_u128), (addr2, 151_u128)]);
|
|
|
|
assert_eq!(state.public_state, expected_public_state);
|
|
assert_eq!(state.programs, expected_builtin_programs);
|
|
}
|
|
|
|
#[test]
|
|
fn new_includes_nullifiers_for_private_accounts() {
|
|
let keys1 = test_private_account_keys_1();
|
|
let keys2 = test_private_account_keys_2();
|
|
|
|
let account = Account {
|
|
balance: 100,
|
|
..Account::default()
|
|
};
|
|
|
|
let account_id1 = AccountId::for_regular_private_account(&keys1.npk(), 0);
|
|
let account_id2 = AccountId::for_regular_private_account(&keys2.npk(), 0);
|
|
|
|
let init_commitment1 = Commitment::new(&account_id1, &account);
|
|
let init_commitment2 = Commitment::new(&account_id2, &account);
|
|
let init_nullifier1 = Nullifier::for_account_initialization(&account_id1);
|
|
let init_nullifier2 = Nullifier::for_account_initialization(&account_id2);
|
|
|
|
let initial_private_accounts = vec![
|
|
(init_commitment1, init_nullifier1),
|
|
(init_commitment2, init_nullifier2),
|
|
];
|
|
|
|
let state = V03State::new().with_private_accounts(initial_private_accounts);
|
|
|
|
assert!(state.private_state.1.contains(&init_nullifier1));
|
|
assert!(state.private_state.1.contains(&init_nullifier2));
|
|
}
|
|
|
|
#[test]
|
|
fn insert_program() {
|
|
let mut state = V03State::new();
|
|
let program_to_insert = crate::test_methods::simple_balance_transfer();
|
|
let program_id = program_to_insert.id();
|
|
assert!(!state.programs.contains_key(&program_id));
|
|
|
|
state.insert_program(program_to_insert);
|
|
|
|
assert!(state.programs.contains_key(&program_id));
|
|
}
|
|
|
|
#[test]
|
|
fn get_account_by_account_id_non_default_account() {
|
|
let key = PrivateKey::try_new([1; 32]).unwrap();
|
|
let account_id = AccountId::from(&PublicKey::new_from_private_key(&key));
|
|
let initial_data = [(
|
|
account_id,
|
|
Account {
|
|
program_owner: crate::test_methods::simple_balance_transfer().id(),
|
|
balance: 100,
|
|
..Account::default()
|
|
},
|
|
)];
|
|
let state = V03State::new().with_public_accounts(initial_data);
|
|
let expected_account = &state.public_state[&account_id];
|
|
|
|
let account = state.get_account_by_id(account_id);
|
|
|
|
assert_eq!(&account, expected_account);
|
|
}
|
|
|
|
#[test]
|
|
fn get_account_by_account_id_default_account() {
|
|
let addr2 = AccountId::new([0; 32]);
|
|
let state = V03State::new();
|
|
let expected_account = Account::default();
|
|
|
|
let account = state.get_account_by_id(addr2);
|
|
|
|
assert_eq!(account, expected_account);
|
|
}
|
|
|
|
#[test]
|
|
fn builtin_programs_getter() {
|
|
let state = V03State::new();
|
|
|
|
let builtin_programs = state.programs();
|
|
|
|
assert_eq!(builtin_programs, &state.programs);
|
|
}
|
|
|
|
#[test]
|
|
fn state_serialization_roundtrip() {
|
|
let account_id_1 = AccountId::new([1; 32]);
|
|
let account_id_2 = AccountId::new([2; 32]);
|
|
let initial_data = [(account_id_1, 100_u128), (account_id_2, 151_u128)];
|
|
let state = V03State::new()
|
|
.with_public_accounts(public_state_from_balances(&initial_data))
|
|
.with_test_programs();
|
|
let bytes = borsh::to_vec(&state).unwrap();
|
|
let state_from_bytes: V03State = borsh::from_slice(&bytes).unwrap();
|
|
assert_eq!(state, state_from_bytes);
|
|
}
|