Skip to content

Commit

Permalink
feat: add benchmark for default transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
Fumuran committed Apr 9, 2024
1 parent cb8cc05 commit 0667acd
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 1 deletion.
15 changes: 15 additions & 0 deletions miden-lib/asm/kernels/transaction/main.masm
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,19 @@ proc.main.1
# Prologue
# ---------------------------------------------------------------------------------------------

push.0 drop
emit.131077

# execute the transaction prologue
exec.prologue::prepare_transaction
# => []

# Note Processing
# ---------------------------------------------------------------------------------------------

push.0 drop
emit.131077

# get the total number of consumed notes
exec.memory::get_total_num_consumed_notes
# => [num_consumed_notes]
Expand Down Expand Up @@ -112,6 +118,9 @@ proc.main.1
# Transaction Script Processing
# ---------------------------------------------------------------------------------------------

push.0 drop
emit.131077

# execute the transaction script
exec.memory::get_tx_script_root
# => [TX_SCRIPT_ROOT]
Expand All @@ -136,9 +145,15 @@ proc.main.1
# Epilogue
# ---------------------------------------------------------------------------------------------

push.0 drop
emit.131077

# execute the transaction epilogue
exec.epilogue::finalize_transaction
# => [TX_SCRIPT_ROOT, CREATED_NOTES_COMMITMENT, FINAL_ACCOUNT_HASH]

push.0 drop
emit.131077
end

begin
Expand Down
2 changes: 2 additions & 0 deletions miden-lib/src/transaction/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub enum TransactionEvent {
AccountStorageSetItem = 0x2_0002, // 131074
AccountIncrementNonce = 0x2_0003, // 131075
AccountPushProcedureIndex = 0x2_0004, // 131076
AccountBenchData = 0x2_0005, // 131077
}

impl TransactionEvent {
Expand Down Expand Up @@ -47,6 +48,7 @@ impl TryFrom<u32> for TransactionEvent {
0x2_0002 => Ok(TransactionEvent::AccountStorageSetItem),
0x2_0003 => Ok(TransactionEvent::AccountIncrementNonce),
0x2_0004 => Ok(TransactionEvent::AccountPushProcedureIndex),
0x2_0005 => Ok(TransactionEvent::AccountBenchData),
_ => Err(TransactionEventParsingError::InvalidTransactionEvent(value)),
}
}
Expand Down
13 changes: 13 additions & 0 deletions miden-tx/src/host/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use alloc::{collections::BTreeMap, string::ToString};

#[cfg(feature = "std")]
use std::println;

use miden_lib::transaction::{TransactionEvent, TransactionKernelError};
use miden_objects::{
accounts::{AccountDelta, AccountStub},
Expand Down Expand Up @@ -61,6 +64,15 @@ impl<A: AdviceProvider> TransactionHost<A> {
.expect("failed to push value onto advice stack");
Ok(())
}

fn on_account_print_cycle_data<S: ProcessState>(
&self,
process: &S,
) -> Result<(), TransactionKernelError> {
#[cfg(feature = "std")]
println!("Event emitted at step {} in context {}", process.clk(), process.ctx(),);
Ok(())
}
}

impl<A: AdviceProvider> Host for TransactionHost<A> {
Expand Down Expand Up @@ -101,6 +113,7 @@ impl<A: AdviceProvider> Host for TransactionHost<A> {
AccountStorageSetItem => self.on_account_storage_set_item(process),
AccountIncrementNonce => self.on_account_increment_nonce(process),
AccountPushProcedureIndex => self.on_account_push_procedure_index(process),
AccountBenchData => self.on_account_print_cycle_data(process),
}
.map_err(|err| ExecutionError::EventError(err.to_string()))?;

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 @@ -355,7 +355,7 @@ fn test_tx_script() {
// ================================================================================================

#[derive(Clone)]
struct MockDataStore {
pub struct MockDataStore {
pub account: Account,
pub block_header: BlockHeader,
pub block_chain: ChainMmr,
Expand Down
1 change: 1 addition & 0 deletions miden-tx/tests/integration/bench/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod tx_benchmark;
16 changes: 16 additions & 0 deletions miden-tx/tests/integration/bench/tx_benchmark.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::MockDataStore;
use miden_tx::TransactionExecutor;

#[test]
fn benchmark_default_tx() {
let data_store = MockDataStore::default();
let mut executor = TransactionExecutor::new(data_store.clone());

let account_id = data_store.account.id();
executor.load_account(account_id).unwrap();

let block_ref = data_store.block_header.block_num();
let note_ids = data_store.notes.iter().map(|note| note.id()).collect::<Vec<_>>();

executor.execute_transaction(account_id, block_ref, &note_ids, None).unwrap();
}
1 change: 1 addition & 0 deletions miden-tx/tests/integration/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod bench;
mod scripts;
mod wallet;

Expand Down

0 comments on commit 0667acd

Please sign in to comment.