Skip to content
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- [BREAKING] Added cycle counts to notes returned by `NoteConsumptionInfo` and removed public fields from related types ([#2772](https://github.com/0xMiden/miden-base/issues/2772)).
- [BREAKING] Removed unused `payback_attachment` from `SwapNoteStorage` and `attachment` from `MintNoteStorage` ([#2789](https://github.com/0xMiden/protocol/pull/2789)).
- Automatically enable `concurrent` feature in `miden-tx` for `std` context ([#2791](https://github.com/0xMiden/protocol/pull/2791)).
- Added `tx::get_tx_script_root` kernel procedure returning the root of the executed transaction script (zero word if no script was executed) ([#2816](https://github.com/0xMiden/protocol/pull/2816)).
Comment thread
partylikeits1983 marked this conversation as resolved.
Outdated
- Added `TransactionScript::from_package()` method to create `TransactionScript` from `miden-mast-package::Package` ([#2779](https://github.com/0xMiden/protocol/pull/2779)).

### Fixes
Expand Down
20 changes: 20 additions & 0 deletions crates/miden-protocol/asm/kernels/transaction/api.masm
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,26 @@ pub proc tx_get_num_input_notes
# => [num_input_notes, pad(15)]
end

#! Returns the transaction script root.
#!
#! Inputs: [pad(16)]
#! Outputs: [TX_SCRIPT_ROOT, pad(12)]
#!
#! Where:
#! - TX_SCRIPT_ROOT is the transaction script root, or the zero word if no transaction script was
#! executed.
#!
#! Invocation: dynexec
pub proc tx_get_tx_script_root
# get the tx script root
exec.tx::get_tx_script_root
# => [TX_SCRIPT_ROOT, pad(16)]

# truncate the stack
swapw dropw
# => [TX_SCRIPT_ROOT, pad(12)]
end

#! Returns the current number of output notes created in this transaction.
#!
#! Inputs: [pad(16)]
Expand Down
12 changes: 12 additions & 0 deletions crates/miden-protocol/asm/kernels/transaction/lib/memory.masm
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,18 @@ pub proc set_tx_script_root
mem_storew_le.TX_SCRIPT_ROOT_PTR
end

#! Returns the transaction script root.
#!
#! Inputs: []
#! Outputs: [TX_SCRIPT_ROOT]
#!
#! Where:
#! - TX_SCRIPT_ROOT is the transaction script root, or the zero word if no transaction script was
#! executed.
pub proc get_tx_script_root
padw mem_loadw_le.TX_SCRIPT_ROOT_PTR
end

#! Returns the transaction script arguments.
#!
#! Inputs: []
Expand Down
10 changes: 10 additions & 0 deletions crates/miden-protocol/asm/kernels/transaction/lib/tx.masm
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ pub use memory::get_num_input_notes
#! - num_output_notes is the number of output notes created in this transaction so far.
pub use memory::get_num_output_notes

#! Returns the transaction script root.
#!
#! Inputs: []
#! Outputs: [TX_SCRIPT_ROOT]
#!
#! Where:
#! - TX_SCRIPT_ROOT is the transaction script root, or the zero word if no transaction script was
#! executed.
pub use memory::get_tx_script_root

#! Updates the transaction expiration block delta.
#!
#! The input block_height_delta is added to the block reference number in order to output an upper
Expand Down
3 changes: 3 additions & 0 deletions crates/miden-protocol/asm/protocol/kernel_proc_offsets.masm
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,6 @@ pub const TX_EXEC_FOREIGN_PROC_OFFSET = 48
# expiration data
pub const TX_GET_EXPIRATION_DELTA_OFFSET=49 # accessor
pub const TX_UPDATE_EXPIRATION_BLOCK_DELTA_OFFSET=50 # mutator

# tx script
pub const TX_GET_TX_SCRIPT_ROOT_OFFSET=51
27 changes: 27 additions & 0 deletions crates/miden-protocol/asm/protocol/tx.masm
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use miden::protocol::kernel_proc_offsets::TX_PREPARE_FPI_OFFSET
use miden::protocol::kernel_proc_offsets::TX_EXEC_FOREIGN_PROC_OFFSET
use miden::protocol::kernel_proc_offsets::TX_UPDATE_EXPIRATION_BLOCK_DELTA_OFFSET
use miden::protocol::kernel_proc_offsets::TX_GET_EXPIRATION_DELTA_OFFSET
use miden::protocol::kernel_proc_offsets::TX_GET_TX_SCRIPT_ROOT_OFFSET

#! Returns the block number of the transaction reference block.
#!
Expand Down Expand Up @@ -319,3 +320,29 @@ pub proc get_expiration_block_delta
swapdw dropw dropw swapw dropw movdn.3 drop drop drop
# => [expiration_delta]
end

#! Returns the transaction script root, or the zero word if no transaction script was executed.
#!
#! Inputs: []
#! Outputs: [TX_SCRIPT_ROOT]
#!
#! Where:
#! - TX_SCRIPT_ROOT is the root of the transaction script executed in this transaction, or the zero
#! word if no transaction script was executed.
Comment thread
partylikeits1983 marked this conversation as resolved.
Outdated
#!
#! Invocation: exec
pub proc get_tx_script_root
# pad the stack
padw padw padw push.0.0.0
# => [pad(15)]

push.TX_GET_TX_SCRIPT_ROOT_OFFSET
# => [offset, pad(15)]

syscall.exec_kernel_proc
# => [TX_SCRIPT_ROOT, pad(12)]

# clean the stack
swapdw dropw dropw swapw dropw
# => [TX_SCRIPT_ROOT]
end
56 changes: 56 additions & 0 deletions crates/miden-testing/src/kernel_tests/tx/test_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,62 @@ async fn test_tx_script_args() -> anyhow::Result<()> {
Ok(())
}

/// Tests that `tx::get_tx_script_root` returns the root of the executed transaction script.
#[tokio::test]
async fn test_get_script_root_with_script() -> anyhow::Result<()> {
let tx_script = CodeBuilder::default().compile_tx_script("begin nop end")?;
let expected_root = tx_script.root();

let code = format!(
r#"
use miden::protocol::tx
use $kernel::prologue

begin
exec.prologue::prepare_transaction

exec.tx::get_tx_script_root
# => [TX_SCRIPT_ROOT]

push.{expected_root} assert_eqw.err="tx script root mismatch"
end
"#
);

let tx_context = TransactionContextBuilder::with_existing_mock_account()
.tx_script(tx_script)
.build()?;

tx_context.execute_code(&code).await?;

Ok(())
}

/// Tests that `tx::get_tx_script_root` returns the zero word when no transaction script is
Comment thread
partylikeits1983 marked this conversation as resolved.
Outdated
/// executed.
#[tokio::test]
async fn test_get_script_root_without_script() -> anyhow::Result<()> {
let code = r#"
use miden::protocol::tx
use $kernel::prologue

begin
exec.prologue::prepare_transaction

exec.tx::get_tx_script_root
# => [TX_SCRIPT_ROOT]

padw assert_eqw.err="tx script root must be zero when no script is executed"
end
"#;

let tx_context = TransactionContextBuilder::with_existing_mock_account().build()?;

tx_context.execute_code(code).await?;

Ok(())
}

// Tests that advice map from the account code and transaction script gets correctly passed as
// part of the transaction advice inputs
#[tokio::test]
Expand Down
Loading