Skip to content

Commit

Permalink
Merge pull request #393 from 0xPolygonMiden/bobbin-note-assets
Browse files Browse the repository at this point in the history
Renamed `AccountVault` into `AssetVault`
  • Loading branch information
bobbinth authored Jan 3, 2024
2 parents 9813531 + 75a4646 commit b698c74
Show file tree
Hide file tree
Showing 35 changed files with 264 additions and 259 deletions.
8 changes: 3 additions & 5 deletions miden-lib/src/accounts/faucets/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use assembly::LibraryPath;
use miden_objects::{
accounts::{
Account, AccountCode, AccountId, AccountStorage, AccountType, AccountVault, StorageSlotType,
},
assets::TokenSymbol,
accounts::{Account, AccountCode, AccountId, AccountStorage, AccountType, StorageSlotType},
assets::{AssetVault, TokenSymbol},
utils::{string::ToString, vec},
AccountError, Felt, StarkField, Word, ZERO,
};
Expand Down Expand Up @@ -72,7 +70,7 @@ pub fn create_basic_fungible_faucet(
(0, (StorageSlotType::Value { value_arity: 0 }, auth_data)),
(1, (StorageSlotType::Value { value_arity: 0 }, metadata)),
])?;
let account_vault = AccountVault::new(&[])?;
let account_vault = AssetVault::new(&[]).map_err(AccountError::AssetVaultError)?;

let account_seed = AccountId::get_account_seed(
init_seed,
Expand Down
7 changes: 3 additions & 4 deletions miden-lib/src/accounts/wallets/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use miden_objects::{
accounts::{
Account, AccountCode, AccountId, AccountStorage, AccountType, AccountVault, StorageSlotType,
},
accounts::{Account, AccountCode, AccountId, AccountStorage, AccountType, StorageSlotType},
assembly::ModuleAst,
assets::AssetVault,
utils::{
format,
string::{String, ToString},
Expand Down Expand Up @@ -64,7 +63,7 @@ pub fn create_basic_wallet(
0,
(StorageSlotType::Value { value_arity: 0 }, storage_slot_0_data),
)])?;
let account_vault = AccountVault::new(&[])?;
let account_vault = AssetVault::new(&[]).map_err(AccountError::AssetVaultError)?;

let account_seed = AccountId::get_account_seed(
init_seed,
Expand Down
10 changes: 5 additions & 5 deletions miden-lib/src/tests/test_asset_vault.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use miden_objects::{
accounts::{AccountId, AccountVault},
accounts::AccountId,
assets::{Asset, FungibleAsset, NonFungibleAsset, NonFungibleAssetDetails},
StarkField,
};
Expand Down Expand Up @@ -105,7 +105,7 @@ fn test_has_non_fungible_asset() {
fn test_add_fungible_asset_success() {
let (account, block_header, chain, notes) =
mock_inputs(MockAccountType::StandardExisting, AssetPreservationStatus::Preserved);
let mut account_vault: AccountVault = account.vault().clone();
let mut account_vault = account.vault().clone();

let faucet_id: AccountId = ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN.try_into().unwrap();
let amount = FungibleAsset::MAX_AMOUNT - FUNGIBLE_ASSET_AMOUNT;
Expand Down Expand Up @@ -146,7 +146,7 @@ fn test_add_fungible_asset_success() {
fn test_add_non_fungible_asset_fail_overflow() {
let (account, block_header, chain, notes) =
mock_inputs(MockAccountType::StandardExisting, AssetPreservationStatus::Preserved);
let mut account_vault: AccountVault = account.vault().clone();
let mut account_vault = account.vault().clone();

let faucet_id: AccountId = ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN.try_into().unwrap();
let amount = FungibleAsset::MAX_AMOUNT - FUNGIBLE_ASSET_AMOUNT + 1;
Expand Down Expand Up @@ -259,7 +259,7 @@ fn test_add_non_fungible_asset_fail_duplicate() {
fn test_remove_fungible_asset_success_no_balance_remaining() {
let (account, block_header, chain, notes) =
mock_inputs(MockAccountType::StandardExisting, AssetPreservationStatus::Preserved);
let mut account_vault: AccountVault = account.vault().clone();
let mut account_vault = account.vault().clone();

let faucet_id: AccountId = ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN.try_into().unwrap();
let amount = FUNGIBLE_ASSET_AMOUNT;
Expand Down Expand Up @@ -332,7 +332,7 @@ fn test_remove_fungible_asset_fail_remove_too_much() {
fn test_remove_fungible_asset_success_balance_remaining() {
let (account, block_header, chain, notes) =
mock_inputs(MockAccountType::StandardExisting, AssetPreservationStatus::Preserved);
let mut account_vault: AccountVault = account.vault().clone();
let mut account_vault = account.vault().clone();

let faucet_id: AccountId = ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN.try_into().unwrap();
let amount = FUNGIBLE_ASSET_AMOUNT - 1;
Expand Down
16 changes: 8 additions & 8 deletions miden-lib/src/tests/test_epilogue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::{
build_module_path, ContextId, MemAdviceProvider, ProcessState, Word, TX_KERNEL_DIR, ZERO,
};
use crate::transaction::{
memory::{CREATED_NOTE_SECTION_OFFSET, CREATED_NOTE_VAULT_HASH_OFFSET, NOTE_MEM_SIZE},
memory::{CREATED_NOTE_ASSET_HASH_OFFSET, CREATED_NOTE_SECTION_OFFSET, NOTE_MEM_SIZE},
ToTransactionKernelInputs, FINAL_ACCOUNT_HASH_WORD_IDX, OUTPUT_NOTES_COMMITMENT_WORD_IDX,
TX_SCRIPT_ROOT_WORD_IDX,
};
Expand Down Expand Up @@ -107,13 +107,13 @@ fn test_compute_created_note_id() {
)
.unwrap();

// assert the vault hash is correct
let expected_vault_hash = note.vault().hash();
let vault_hash_memory_address =
CREATED_NOTE_SECTION_OFFSET + i * NOTE_MEM_SIZE + CREATED_NOTE_VAULT_HASH_OFFSET;
let actual_vault_hash =
process.get_mem_value(ContextId::root(), vault_hash_memory_address).unwrap();
assert_eq!(expected_vault_hash.as_elements(), actual_vault_hash);
// assert the note asset hash is correct
let expected_asset_hash = note.assets().commitment();
let asset_hash_memory_address =
CREATED_NOTE_SECTION_OFFSET + i * NOTE_MEM_SIZE + CREATED_NOTE_ASSET_HASH_OFFSET;
let actual_asset_hash =
process.get_mem_value(ContextId::root(), asset_hash_memory_address).unwrap();
assert_eq!(expected_asset_hash.as_elements(), actual_asset_hash);

// assert the note ID is correct
let expected_id = note.id();
Expand Down
18 changes: 9 additions & 9 deletions miden-lib/src/tests/test_note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ fn test_get_vault_data() {
exec.note::get_vault_info
# assert the vault data is correct
push.{note_0_vault_root} assert_eqw
push.{note_0_asset_hash} assert_eqw
push.{note_0_num_assets} assert_eq
# increment current consumed note pointer
Expand All @@ -104,14 +104,14 @@ fn test_get_vault_data() {
exec.note::get_vault_info
# assert the vault data is correct
push.{note_1_vault_root} assert_eqw
push.{note_1_asset_hash} assert_eqw
push.{note_1_num_assets} assert_eq
end
",
note_0_vault_root = prepare_word(&notes[0].note().vault().hash()),
note_0_num_assets = notes[0].note().vault().num_assets(),
note_1_vault_root = prepare_word(&notes[1].note().vault().hash()),
note_1_num_assets = notes[1].note().vault().num_assets(),
note_0_asset_hash = prepare_word(&notes[0].note().assets().commitment()),
note_0_num_assets = notes[0].note().assets().num_assets(),
note_1_asset_hash = prepare_word(&notes[1].note().assets().commitment()),
note_1_num_assets = notes[1].note().assets().num_assets(),
);

let transaction =
Expand All @@ -130,7 +130,7 @@ fn test_get_assets() {

fn construct_asset_assertions(note: &Note) -> String {
let mut code = String::new();
for asset in note.vault().iter() {
for asset in note.assets().iter() {
code += &format!(
"
# assert the asset is correct
Expand Down Expand Up @@ -215,8 +215,8 @@ fn test_get_assets() {
call.process_note_1
end
",
note_0_num_assets = notes[0].note().vault().num_assets(),
note_1_num_assets = notes[1].note().vault().num_assets(),
note_0_num_assets = notes[0].note().assets().num_assets(),
note_1_num_assets = notes[1].note().assets().num_assets(),
NOTE_0_ASSET_ASSERTIONS = construct_asset_assertions(notes[0].note()),
NOTE_1_ASSET_ASSERTIONS = construct_asset_assertions(notes[1].note()),
);
Expand Down
6 changes: 3 additions & 3 deletions miden-lib/src/tests/test_prologue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,12 @@ fn consumed_notes_memory_assertions<A: AdviceProvider>(
note.note().inputs().hash().as_elements()
);

// The note vault hash should be stored at (CONSUMED_NOTES_OFFSET + (note_index + 1) * 1024 + 4)
// The note asset hash should be stored at (CONSUMED_NOTES_OFFSET + (note_index + 1) * 1024 + 4)
assert_eq!(
process
.get_mem_value(ContextId::root(), consumed_note_data_ptr(note_idx) + 4)
.unwrap(),
note.note().vault().hash().as_elements()
note.note().assets().commitment().as_elements()
);

// The number of assets should be stored at (CONSUMED_NOTES_OFFSET + (note_index + 1) * 1024 + 5)
Expand All @@ -317,7 +317,7 @@ fn consumed_notes_memory_assertions<A: AdviceProvider>(
);

// The assets should be stored at (CONSUMED_NOTES_OFFSET + (note_index + 1) * 1024 + 6..)
for (asset, asset_idx) in note.note().vault().iter().cloned().zip(0u32..) {
for (asset, asset_idx) in note.note().assets().iter().cloned().zip(0u32..) {
let word: Word = asset.into();
assert_eq!(
process
Expand Down
8 changes: 4 additions & 4 deletions miden-lib/src/tests/test_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ fn test_get_output_notes_hash() {

// extract input note data
let input_note_1 = notes.first().unwrap().note();
let input_asset_1 = **input_note_1.vault().iter().take(1).collect::<Vec<_>>().first().unwrap();
let input_asset_1 = **input_note_1.assets().iter().take(1).collect::<Vec<_>>().first().unwrap();
let input_note_2 = notes.last().unwrap().note();
let input_asset_2 = **input_note_2.vault().iter().take(1).collect::<Vec<_>>().first().unwrap();
let input_asset_2 = **input_note_2.assets().iter().take(1).collect::<Vec<_>>().first().unwrap();

// create output note 1
let output_serial_no_1 = [Felt::new(8); 4];
Expand Down Expand Up @@ -206,12 +206,12 @@ fn test_get_output_notes_hash() {
recipient_1 = prepare_word(&output_note_1.recipient()),
tag_1 = output_note_1.metadata().tag(),
asset_1 = prepare_word(&Word::from(
**output_note_1.vault().iter().take(1).collect::<Vec<_>>().first().unwrap()
**output_note_1.assets().iter().take(1).collect::<Vec<_>>().first().unwrap()
)),
recipient_2 = prepare_word(&output_note_2.recipient()),
tag_2 = output_note_2.metadata().tag(),
asset_2 = prepare_word(&Word::from(
**output_note_2.vault().iter().take(1).collect::<Vec<_>>().first().unwrap()
**output_note_2.assets().iter().take(1).collect::<Vec<_>>().first().unwrap()
)),
expected = prepare_word(&expected_output_notes_hash)
);
Expand Down
12 changes: 6 additions & 6 deletions miden-lib/src/transaction/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,15 @@ fn add_account_to_advice_inputs(
/// Populates the advice inputs for all input notes.
///
/// For each note the authentication path is populated into the Merkle store, the note inputs
/// and vault assets are populated in the advice map.
/// and assets are populated in the advice map.
///
/// A combined note data vector is also constructed that holds core data for all notes. This
/// combined vector is added to the advice map against the input notes commitment. For each note
/// the following data items are added to the vector:
/// out[0..4] = serial num
/// out[4..8] = script root
/// out[8..12] = input root
/// out[12..16] = vault_hash
/// out[12..16] = asset_hash
/// out[16..20] = metadata
/// out[20..24] = asset_1
/// out[24..28] = asset_2
Expand All @@ -276,7 +276,7 @@ fn add_account_to_advice_inputs(
///
/// Inserts the following entries into the advice map:
/// - inputs_hash |-> inputs
/// - vault_hash |-> assets
/// - asset_hash |-> assets
/// - notes_hash |-> combined note data
fn add_input_notes_to_advice_inputs(notes: &InputNotes, inputs: &mut AdviceInputs) {
let mut note_data: Vec<Felt> = Vec::new();
Expand All @@ -289,7 +289,7 @@ fn add_input_notes_to_advice_inputs(notes: &InputNotes, inputs: &mut AdviceInput

// insert note inputs and assets into the advice map
inputs.extend_map([(note.inputs().hash().into(), note.inputs().inputs().to_vec())]);
inputs.extend_map([(note.vault().hash().into(), note.vault().to_padded_assets())]);
inputs.extend_map([(note.assets().commitment().into(), note.assets().to_padded_assets())]);

// insert note authentication path nodes into the Merkle store
inputs.extend_merkle_store(
Expand All @@ -303,10 +303,10 @@ fn add_input_notes_to_advice_inputs(notes: &InputNotes, inputs: &mut AdviceInput
note_data.extend(note.serial_num());
note_data.extend(*note.script().hash());
note_data.extend(*note.inputs().hash());
note_data.extend(*note.vault().hash());
note_data.extend(*note.assets().commitment());
note_data.extend(Word::from(note.metadata()));

note_data.extend(note.vault().to_padded_assets());
note_data.extend(note.assets().to_padded_assets());

note_data.push(proof.origin().block_num.into());
note_data.extend(*proof.sub_hash());
Expand Down
4 changes: 2 additions & 2 deletions miden-lib/src/transaction/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ pub const CONSUMED_NOTE_ID_OFFSET: MemoryOffset = 0;
pub const CONSUMED_NOTE_SERIAL_NUM_OFFSET: MemoryOffset = 1;
pub const CONSUMED_NOTE_SCRIPT_ROOT_OFFSET: MemoryOffset = 2;
pub const CONSUMED_NOTE_INPUTS_HASH_OFFSET: MemoryOffset = 3;
pub const CONSUMED_NOTE_VAULT_ROOT_OFFSET: MemoryOffset = 4;
pub const CONSUMED_NOTE_ASSET_HASH_OFFSET: MemoryOffset = 4;
pub const CONSUMED_NOTE_METADATA_OFFSET: MemoryOffset = 5;
pub const CONSUMED_NOTE_ASSETS_OFFSET: MemoryOffset = 6;

Expand All @@ -215,7 +215,7 @@ pub const CREATED_NOTE_SECTION_OFFSET: MemoryOffset = 10000;
pub const CREATED_NOTE_ID_OFFSET: MemoryOffset = 0;
pub const CREATED_NOTE_METADATA_OFFSET: MemoryOffset = 1;
pub const CREATED_NOTE_RECIPIENT_OFFSET: MemoryOffset = 2;
pub const CREATED_NOTE_VAULT_HASH_OFFSET: MemoryOffset = 3;
pub const CREATED_NOTE_ASSET_HASH_OFFSET: MemoryOffset = 3;
pub const CREATED_NOTE_ASSETS_OFFSET: MemoryOffset = 4;

/// The maximum number of created notes that can be produced in a single transaction.
Expand Down
25 changes: 16 additions & 9 deletions miden-lib/src/transaction/outputs.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use miden_objects::{
accounts::{AccountId, AccountStub},
notes::{NoteId, NoteMetadata, NoteVault},
assets::Asset,
notes::{NoteAssets, NoteId, NoteMetadata},
transaction::OutputNote,
utils::collections::Vec,
AccountError, Digest, NoteError, StarkField, Word, WORD_SIZE,
};

use super::memory::{
ACCT_CODE_ROOT_OFFSET, ACCT_DATA_MEM_SIZE, ACCT_ID_AND_NONCE_OFFSET, ACCT_ID_IDX,
ACCT_NONCE_IDX, ACCT_STORAGE_ROOT_OFFSET, ACCT_VAULT_ROOT_OFFSET, CREATED_NOTE_ASSETS_OFFSET,
CREATED_NOTE_CORE_DATA_SIZE, CREATED_NOTE_ID_OFFSET, CREATED_NOTE_METADATA_OFFSET,
CREATED_NOTE_RECIPIENT_OFFSET, CREATED_NOTE_VAULT_HASH_OFFSET,
CREATED_NOTE_ASSET_HASH_OFFSET, CREATED_NOTE_CORE_DATA_SIZE, CREATED_NOTE_ID_OFFSET,
CREATED_NOTE_METADATA_OFFSET, CREATED_NOTE_RECIPIENT_OFFSET,
};

// STACK OUTPUTS
Expand Down Expand Up @@ -54,7 +56,7 @@ pub fn notes_try_from_elements(elements: &[Word]) -> Result<OutputNote, NoteErro
let note_id: NoteId = elements[CREATED_NOTE_ID_OFFSET as usize].into();
let metadata: NoteMetadata = elements[CREATED_NOTE_METADATA_OFFSET as usize].try_into()?;
let recipient = elements[CREATED_NOTE_RECIPIENT_OFFSET as usize].into();
let vault_hash: Digest = elements[CREATED_NOTE_VAULT_HASH_OFFSET as usize].into();
let asset_hash: Digest = elements[CREATED_NOTE_ASSET_HASH_OFFSET as usize].into();

if elements.len()
< (CREATED_NOTE_ASSETS_OFFSET as usize + metadata.num_assets().as_int() as usize)
Expand All @@ -63,14 +65,19 @@ pub fn notes_try_from_elements(elements: &[Word]) -> Result<OutputNote, NoteErro
return Err(NoteError::InvalidStubDataLen(elements.len()));
}

let vault: NoteVault = elements[CREATED_NOTE_ASSETS_OFFSET as usize
let assets = elements[CREATED_NOTE_ASSETS_OFFSET as usize
..(CREATED_NOTE_ASSETS_OFFSET as usize + metadata.num_assets().as_int() as usize)]
.try_into()?;
if vault.hash() != vault_hash {
return Err(NoteError::InconsistentStubVaultHash(vault_hash, vault.hash()));
.iter()
.map(|word| (*word).try_into())
.collect::<Result<Vec<Asset>, _>>()
.map_err(NoteError::InvalidAssetData)?;

let assets = NoteAssets::new(&assets)?;
if assets.commitment() != asset_hash {
return Err(NoteError::InconsistentStubAssetHash(asset_hash, assets.commitment()));
}

let stub = OutputNote::new(recipient, vault, metadata);
let stub = OutputNote::new(recipient, assets, metadata);
if stub.id() != note_id {
return Err(NoteError::InconsistentStubId(stub.id(), note_id));
}
Expand Down
2 changes: 1 addition & 1 deletion miden-tx/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ fn test_transaction_result_account_delta() {
.last()
.unwrap()
.note()
.vault()
.assets()
.iter()
.cloned()
.collect::<Vec<_>>();
Expand Down
8 changes: 4 additions & 4 deletions miden-tx/tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use miden_lib::transaction::TransactionKernel;
use miden_objects::{
accounts::{Account, AccountCode, AccountId, AccountStorage, AccountVault, StorageSlotType},
accounts::{Account, AccountCode, AccountId, AccountStorage, StorageSlotType},
assembly::{ModuleAst, ProgramAst},
assets::{Asset, FungibleAsset},
assets::{Asset, AssetVault, FungibleAsset},
crypto::{dsa::rpo_falcon512::KeyPair, utils::Serializable},
notes::{Note, NoteId, NoteScript},
transaction::{ChainMmr, InputNote, InputNotes, TransactionInputs},
Expand Down Expand Up @@ -135,8 +135,8 @@ pub fn get_account_with_default_account_code(
.unwrap();

let account_vault = match assets {
Some(asset) => AccountVault::new(&[asset]).unwrap(),
None => AccountVault::new(&[]).unwrap(),
Some(asset) => AssetVault::new(&[asset]).unwrap(),
None => AssetVault::new(&[]).unwrap(),
};

Account::new(account_id, account_vault, account_storage, account_code, Felt::new(1))
Expand Down
Loading

0 comments on commit b698c74

Please sign in to comment.