Skip to content

Commit

Permalink
test: add tests for Account::apply_delta() (#423)
Browse files Browse the repository at this point in the history
  • Loading branch information
phklive authored Jan 26, 2024
1 parent 6f856fa commit b160b2c
Showing 1 changed file with 147 additions and 0 deletions.
147 changes: 147 additions & 0 deletions objects/src/accounts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ pub const ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_OFF_CHAIN: u64 = 0b000110111
#[cfg(any(feature = "testing", test))]
pub const ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN: u64 = 0b1010011100 << 54;

#[cfg(any(feature = "testing", test))]
pub const ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_2: u64 = 0b1010011101 << 54;

#[cfg(any(feature = "testing", test))]
pub const ACCOUNT_ID_NON_FUNGIBLE_FAUCET_OFF_CHAIN: u64 = 0b1101100110 << 54;

Expand Down Expand Up @@ -272,3 +275,147 @@ pub fn hash_account(
elements[12..].copy_from_slice(&*code_root);
Hasher::hash_elements(&elements)
}

// TESTS
// ================================================================================================

#[cfg(test)]
mod tests {
use super::{
Account, AccountCode, AccountDelta, AccountId, AccountStorage, AccountStorageDelta,
AccountVaultDelta, Assembler, Felt, ModuleAst, SlotItem, StorageSlotType, Word,
ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN, ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_2,
ACCOUNT_ID_REGULAR_ACCOUNT_IMMUTABLE_CODE_ON_CHAIN,
};
use crate::assets::{Asset, AssetVault, FungibleAsset};

fn build_account(assets: Vec<Asset>, nonce: Felt, storage_items: Vec<Word>) -> Account {
// build account code
let source = "
export.foo
push.1 push.2 mul
end
export.bar
push.1 push.2 add
end
";
let module = ModuleAst::parse(source).unwrap();
let assembler = Assembler::default();
let code = AccountCode::new(module, &assembler).unwrap();

// build account data
let vault = AssetVault::new(&assets).unwrap();

let slot_type = StorageSlotType::Value { value_arity: 0 };
let slot_items: Vec<SlotItem> = storage_items
.into_iter()
.enumerate()
.map(|(i, item)| (i as u8, (slot_type, item)))
.collect();
let storage = AccountStorage::new(slot_items).unwrap();

// create account
let id = AccountId::try_from(ACCOUNT_ID_REGULAR_ACCOUNT_IMMUTABLE_CODE_ON_CHAIN).unwrap();
Account::new(id, vault, storage, code, nonce)
}

fn build_account_delta(
added_assets: Vec<Asset>,
removed_assets: Vec<Asset>,
nonce: Felt,
) -> AccountDelta {
let word = [Felt::new(1), Felt::new(2), Felt::new(3), Felt::new(4)];
let storage_delta = AccountStorageDelta {
cleared_items: vec![0],
updated_items: vec![(1, word)],
};

let vault_delta = AccountVaultDelta { added_assets, removed_assets };

AccountDelta::new(storage_delta, vault_delta, Some(nonce)).unwrap()
}

fn build_assets() -> (Asset, Asset) {
// build asset 0
let faucet_id_0 = AccountId::try_from(ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN).unwrap();
let asset_0: Asset = FungibleAsset::new(faucet_id_0, 123).unwrap().into();

// build asset 1
let faucet_id_1 = AccountId::try_from(ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_2).unwrap();
let asset_1: Asset = FungibleAsset::new(faucet_id_1, 345).unwrap().into();

(asset_0, asset_1)
}

#[test]
fn valid_account_delta_is_correctly_applied() {
// build account
let init_nonce = Felt::new(1);
let (asset_0, asset_1) = build_assets();
let word = [Felt::new(1), Felt::new(2), Felt::new(3), Felt::new(4)];
let mut account = build_account(vec![asset_0], init_nonce, vec![word]);

// build account delta
let final_nonce = Felt::new(2);
let account_delta = build_account_delta(vec![asset_1], vec![asset_0], final_nonce);

// apply delta and create final_account
account.apply_delta(&account_delta).unwrap();
let final_account = build_account(vec![asset_1], final_nonce, vec![Word::default(), word]);

// assert account is what it should be
assert_eq!(account, final_account);
}

#[test]
#[should_panic]
fn valid_account_delta_with_unchanged_nonce() {
// build account
let init_nonce = Felt::new(1);
let (asset, _) = build_assets();
let mut account = build_account(vec![asset], init_nonce, vec![Word::default()]);

// build account delta
let account_delta = build_account_delta(vec![], vec![asset], init_nonce);

// apply delta
account.apply_delta(&account_delta).unwrap()
}

#[test]
#[should_panic]
fn valid_account_delta_with_decremented_nonce() {
// build account
let init_nonce = Felt::new(2);
let (asset, _) = build_assets();
let mut account = build_account(vec![asset], init_nonce, vec![Word::default()]);

// build account delta
let final_nonce = Felt::new(1);
let account_delta = build_account_delta(vec![], vec![asset], final_nonce);

// apply delta
account.apply_delta(&account_delta).unwrap()
}

#[test]
#[should_panic]
fn empty_account_delta_with_incremented_nonce() {
// build account
let init_nonce = Felt::new(1);
let word = [Felt::new(1), Felt::new(2), Felt::new(3), Felt::new(4)];
let mut account = build_account(vec![], init_nonce, vec![word]);

// build account delta
let final_nonce = Felt::new(2);
let account_delta = AccountDelta::new(
AccountStorageDelta::default(),
AccountVaultDelta::default(),
Some(final_nonce),
)
.unwrap();

// apply delta
account.apply_delta(&account_delta).unwrap()
}
}

0 comments on commit b160b2c

Please sign in to comment.