Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Note structure refactoring #464

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions miden-lib/src/tests/test_note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fn test_get_sender() {
let transaction = prepare_transaction(tx_inputs, None, code, None);
let process = run_tx(&transaction).unwrap();

let sender = transaction.input_notes().get_note(0).note().metadata().sender().into();
let sender = transaction.input_notes().get_note(0).metadata().sender().into();
assert_eq!(process.stack.get(0), sender);
}

Expand Down Expand Up @@ -107,10 +107,10 @@ fn test_get_vault_data() {
push.{note_1_num_assets} assert_eq
end
",
note_0_asset_hash = prepare_word(&notes.get_note(0).note().assets().commitment()),
note_0_num_assets = notes.get_note(0).note().assets().num_assets(),
note_1_asset_hash = prepare_word(&notes.get_note(1).note().assets().commitment()),
note_1_num_assets = notes.get_note(1).note().assets().num_assets(),
note_0_asset_hash = prepare_word(&notes.get_note(0).assets().commitment()),
note_0_num_assets = notes.get_note(0).assets().num_assets(),
note_1_asset_hash = prepare_word(&notes.get_note(1).assets().commitment()),
note_1_num_assets = notes.get_note(1).assets().num_assets(),
);

let transaction = prepare_transaction(tx_inputs, None, &code, None);
Expand Down Expand Up @@ -213,10 +213,10 @@ fn test_get_assets() {
call.process_note_1
end
",
note_0_num_assets = notes.get_note(0).note().assets().num_assets(),
note_1_num_assets = notes.get_note(1).note().assets().num_assets(),
NOTE_0_ASSET_ASSERTIONS = construct_asset_assertions(notes.get_note(0).note()),
NOTE_1_ASSET_ASSERTIONS = construct_asset_assertions(notes.get_note(1).note()),
note_0_num_assets = notes.get_note(0).assets().num_assets(),
note_1_num_assets = notes.get_note(1).assets().num_assets(),
NOTE_0_ASSET_ASSERTIONS = construct_asset_assertions(notes.get_note(0).inner()),
NOTE_1_ASSET_ASSERTIONS = construct_asset_assertions(notes.get_note(1).inner()),
);

let transaction = prepare_transaction(tx_inputs, None, &code, None);
Expand Down Expand Up @@ -248,7 +248,7 @@ fn test_get_inputs() {
code
}

let note1 = notes.get_note(0).note();
let note1 = notes.get_note(0).inner();

// calling get_assets should return assets at the specified address
let code = format!(
Expand Down Expand Up @@ -326,7 +326,7 @@ fn note_setup_stack_assertions(process: &Process<MockHost>, inputs: &PreparedTra
let mut expected_stack = [ZERO; 16];

// replace the top four elements with the tx script root
let mut note_script_root = *inputs.input_notes().get_note(0).note().script().hash();
let mut note_script_root = *inputs.input_notes().get_note(0).script().hash();
note_script_root.reverse();
expected_stack[..4].copy_from_slice(&note_script_root);

Expand Down
2 changes: 1 addition & 1 deletion miden-lib/src/tests/test_prologue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ fn consumed_notes_memory_assertions(process: &Process<MockHost>, inputs: &Prepar
);

for (note, note_idx) in inputs.input_notes().iter().zip(0_u32..) {
let note = note.note();
let note = note.inner();

// The note nullifier should be computer and stored at the correct offset
assert_eq!(
Expand Down
4 changes: 2 additions & 2 deletions miden-lib/src/tests/test_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ fn test_get_output_notes_hash() {
mock_inputs(MockAccountType::StandardExisting, AssetPreservationStatus::Preserved);

// extract input note data
let input_note_1 = tx_inputs.input_notes().get_note(0).note();
let input_note_1 = tx_inputs.input_notes().get_note(0).inner();
let input_asset_1 = **input_note_1.assets().iter().take(1).collect::<Vec<_>>().first().unwrap();
let input_note_2 = tx_inputs.input_notes().get_note(1).note();
let input_note_2 = tx_inputs.input_notes().get_note(1).inner();
let input_asset_2 = **input_note_2.assets().iter().take(1).collect::<Vec<_>>().first().unwrap();

// create output note 1
Expand Down
41 changes: 25 additions & 16 deletions miden-lib/src/transaction/inputs.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use miden_objects::{
accounts::Account,
transaction::{
ChainMmr, ExecutedTransaction, InputNotes, PreparedTransaction, TransactionInputs,
TransactionScript, TransactionWitness,
ChainMmr, ExecutedTransaction, PreparedTransaction, TransactionInputs, TransactionScript,
TransactionWitness,
},
utils::{collections::Vec, vec, IntoBytes},
vm::{AdviceInputs, StackInputs},
Expand Down Expand Up @@ -91,7 +91,7 @@ fn extend_advice_inputs(
// build the advice map and Merkle store for relevant components
add_chain_mmr_to_advice_inputs(tx_inputs.block_chain(), advice_inputs);
add_account_to_advice_inputs(tx_inputs.account(), tx_inputs.account_seed(), advice_inputs);
add_input_notes_to_advice_inputs(tx_inputs.input_notes(), advice_inputs);
add_input_notes_to_advice_inputs(tx_inputs, advice_inputs);
add_tx_script_inputs_to_advice_map(tx_script, advice_inputs);
}

Expand Down Expand Up @@ -276,27 +276,25 @@ fn add_account_to_advice_inputs(
/// - inputs_hash |-> inputs
/// - asset_hash |-> assets
/// - notes_hash |-> combined note data
fn add_input_notes_to_advice_inputs(notes: &InputNotes, inputs: &mut AdviceInputs) {
fn add_input_notes_to_advice_inputs(tx_inputs: &TransactionInputs, inputs: &mut AdviceInputs) {
let notes = tx_inputs.input_notes();

// if there are no input notes, nothing is added to the advice inputs
if notes.is_empty() {
return;
}

let mut note_data = Vec::new();
for input_note in notes.iter() {
let note = input_note.note();
let proof = input_note.proof();

for note in notes.iter() {
// insert note inputs and assets into the advice map
inputs.extend_map([(note.inputs().commitment().into(), note.inputs().to_padded_values())]);
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(
proof
.note_path()
.inner_nodes(proof.origin().node_index.value(), note.authentication_hash())
.unwrap(),
note.auth_path()
.inner_nodes(note.location().note_index() as u64, note.authentication_hash())
.expect("failed to compute inner nodes for Merkle path"),
);

// add the note elements to the combined vector of note data
Expand All @@ -311,10 +309,21 @@ fn add_input_notes_to_advice_inputs(notes: &InputNotes, inputs: &mut AdviceInput
note_data.push((note.assets().num_assets() as u32).into());
note_data.extend(note.assets().to_padded_assets());

note_data.push(proof.origin().block_num.into());
note_data.extend(*proof.sub_hash());
note_data.extend(*proof.note_root());
note_data.push(proof.origin().node_index.value().into());
// determine which block header is associated with the note
let block_num = note.location().block_num();
let block_header = if block_num == tx_inputs.block_header().block_num() {
tx_inputs.block_header()
} else {
tx_inputs
.block_chain()
.get_block(block_num)
.expect("missing block header for note")
};

note_data.push(block_num.into());
note_data.extend(*block_header.sub_hash());
note_data.extend(*block_header.note_root());
note_data.push(note.location().note_index().into());
}

// insert the combined note data into the advice map
Expand Down
2 changes: 1 addition & 1 deletion miden-tx/src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ impl TransactionCompiler {
for recorded_note in notes.iter() {
let note_program = self
.assembler
.compile_in_context(recorded_note.note().script().code(), assembly_context)
.compile_in_context(recorded_note.script().code(), assembly_context)
.map_err(TransactionCompilerError::CompileNoteScriptFailed)?;
verify_program_account_compatibility(
&note_program,
Expand Down
12 changes: 2 additions & 10 deletions miden-tx/src/compiler/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use miden_objects::{
accounts::ACCOUNT_ID_REGULAR_ACCOUNT_IMMUTABLE_CODE_ON_CHAIN,
assets::{Asset, FungibleAsset},
notes::{Note, NoteInclusionProof},
notes::Note,
transaction::{InputNote, InputNotes},
Felt, FieldElement, Word,
};
Expand Down Expand Up @@ -187,17 +187,9 @@ fn test_transaction_compilation_succeeds() {
let _account_code = tx_compiler.load_account(account_id, account_code_ast).unwrap();

let notes = mock_consumed_notes(&mut tx_compiler, account_id);
let mock_inclusion_proof = NoteInclusionProof::new(
Default::default(),
Default::default(),
Default::default(),
0,
Default::default(),
)
.unwrap();
let notes = notes
.into_iter()
.map(|note| InputNote::new(note, mock_inclusion_proof.clone()))
.map(|note| InputNote::new(note, Default::default(), Default::default()))
.collect::<Vec<_>>();

let notes = InputNotes::new(notes).unwrap();
Expand Down
11 changes: 2 additions & 9 deletions miden-tx/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,8 @@ fn executed_transaction_account_delta() {
// vault delta
// --------------------------------------------------------------------------------------------
// assert that added assets are tracked
let added_assets = data_store
.notes
.last()
.unwrap()
.note()
.assets()
.iter()
.cloned()
.collect::<Vec<_>>();
let added_assets =
data_store.notes.last().unwrap().assets().iter().cloned().collect::<Vec<_>>();
assert!(executed_transaction
.account_delta()
.vault()
Expand Down
9 changes: 1 addition & 8 deletions mock/src/builders/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use miden_objects::{
accounts::AccountId,
assembly::ProgramAst,
assets::Asset,
notes::{Note, NoteInclusionProof, NoteInputs, NoteScript},
notes::{Note, NoteInputs, NoteScript},
utils::{
collections::Vec,
string::{String, ToString},
Expand All @@ -27,7 +27,6 @@ pub struct NoteBuilder {
serial_num: Word,
tag: Felt,
code: String,
proof: Option<NoteInclusionProof>,
}

impl NoteBuilder {
Expand All @@ -46,7 +45,6 @@ impl NoteBuilder {
serial_num,
tag: Felt::default(),
code: DEFAULT_NOTE_CODE.to_string(),
proof: None,
}
}

Expand All @@ -71,11 +69,6 @@ impl NoteBuilder {
self
}

pub fn proof(mut self, proof: NoteInclusionProof) -> Self {
self.proof = Some(proof);
self
}

pub fn build(self) -> Result<Note, NoteError> {
let assembler = TransactionKernel::assembler();
let note_ast = ProgramAst::parse(&self.code).unwrap();
Expand Down
28 changes: 5 additions & 23 deletions mock/src/mock/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use miden_objects::{
accounts::{Account, AccountId, AccountType, SlotItem},
assets::Asset,
crypto::merkle::{LeafIndex, Mmr, PartialMmr, SimpleSmt, Smt},
notes::{Note, NoteInclusionProof},
notes::{Note, NoteLocation},
transaction::{ChainMmr, InputNote},
utils::collections::Vec,
BlockHeader, Digest, Felt, FieldElement, Word, ACCOUNT_TREE_DEPTH, NOTE_TREE_DEPTH,
Expand Down Expand Up @@ -104,17 +104,8 @@ impl<R: Rng> Objects<R> {
.enumerate()
.map(|(index, note)| {
let auth_index = LeafIndex::new(index as u64).expect("index bigger than 2**20");
InputNote::new(
note.clone(),
NoteInclusionProof::new(
header.block_num(),
header.sub_hash(),
header.note_root(),
index as u64,
notes.open(&auth_index).path,
)
.expect("Invalid data provided to proof constructor"),
)
let location = NoteLocation::new(header.block_num(), auth_index.value() as u32);
InputNote::new(note.clone(), location, notes.open(&auth_index).path)
})
.collect::<Vec<_>>()
}
Expand Down Expand Up @@ -633,17 +624,8 @@ pub fn mock_chain_data(consumed_notes: Vec<Note>) -> (ChainMmr, Vec<InputNote>)
.map(|(index, note)| {
let block_header = &block_chain[index];
let auth_index = LeafIndex::new(index as u64).unwrap();
InputNote::new(
note,
NoteInclusionProof::new(
block_header.block_num(),
block_header.sub_hash(),
block_header.note_root(),
index as u64,
note_trees[index].open(&auth_index).path,
)
.unwrap(),
)
let location = NoteLocation::new(block_header.block_num(), auth_index.value() as u32);
InputNote::new(note, location, note_trees[index].open(&auth_index).path)
})
.collect::<Vec<_>>();

Expand Down
47 changes: 47 additions & 0 deletions objects/src/notes/location.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use super::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable};

// NOTE LOCATION
// ================================================================================================

/// Location at which the note is recorded in the chain.
///
/// The location consists of two elements:
/// - The number of the block at which the note was recorded in the chain.
/// - The index of the note in the block's note tree.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct NoteLocation {
block_num: u32,
note_index: u32, // TODO: change to u16
}

impl NoteLocation {
pub fn new(block_num: u32, note_index: u32) -> Self {
Self { block_num, note_index }
}

/// Returns the number of the block at which the note was recorded in the chain.
pub fn block_num(&self) -> u32 {
self.block_num
}

/// Return the index of thn note in the block's note tree.
pub fn note_index(&self) -> u32 {
self.note_index
}
}

impl Serializable for NoteLocation {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
target.write_u32(self.block_num);
self.note_index.write_into(target);
}
}

impl Deserializable for NoteLocation {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let block_num = source.read_u32()?;
let note_index = source.read_u32()?;
Ok(Self { block_num, note_index })
}
}
6 changes: 3 additions & 3 deletions objects/src/notes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::{
utils::{
collections::Vec,
serde::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable},
string::{String, ToString},
string::String,
},
vm::CodeBlock,
Digest, Felt, Hasher, NoteError, Word, NOTE_TREE_DEPTH, WORD_SIZE, ZERO,
Expand All @@ -28,8 +28,8 @@ pub use note_id::NoteId;
mod nullifier;
pub use nullifier::Nullifier;

mod origin;
pub use origin::{NoteInclusionProof, NoteOrigin};
mod location;
pub use location::NoteLocation;

mod script;
pub use script::NoteScript;
Expand Down
Loading
Loading