feat: separate event handling into data extraction and handling#2071
feat: separate event handling into data extraction and handling#2071
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR refactors the transaction event handling system by renaming TransactionEvent to TransactionEventId and introducing a new internal TransactionEvent enum that encapsulates both the event type and its associated data. This separation improves code organization by splitting event data extraction from event handling logic.
Key changes:
- Renamed
TransactionEventenum toTransactionEventIdto represent event identifiers - Created new
TransactionEventenum intx_event.rsthat combines event IDs with their data - Refactored event handlers to accept extracted data as parameters instead of reading from
ProcessState - Simplified return types in
LinkMapevent handlers by removing unnecessaryResultwrapper
Reviewed Changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
crates/miden-tx/src/host/tx_event.rs |
New file containing TransactionEvent enum with data extraction logic from process state |
crates/miden-tx/src/prover/prover_host.rs |
Updated to use new event handling pattern with extracted data |
crates/miden-tx/src/host/mod.rs |
Refactored event handlers to accept pre-extracted data as parameters |
crates/miden-tx/src/executor/exec_host.rs |
Updated async event handling to use new pattern with data extraction |
crates/miden-tx/src/host/link_map.rs |
Simplified return types by removing unnecessary Result wrappers |
crates/miden-tx/src/host/kernel_process.rs |
Added new helper methods for account code commitment and merkle path checking |
crates/miden-tx/src/host/account_procedures.rs |
Simplified API to accept pre-extracted data instead of ProcessState |
crates/miden-lib/src/transaction/tx_event_id.rs |
Renamed from events.rs, contains TransactionEventId enum |
crates/miden-lib/src/transaction/mod.rs |
Updated exports to reflect TransactionEventId rename |
crates/miden-testing/src/mock_host.rs |
Updated to use TransactionEventId instead of TransactionEvent |
crates/miden-tx/src/prover/mod.rs |
Updated destructuring of into_parts() return value |
crates/miden-tx/src/errors/mod.rs |
Changed MissingNote error type from u64 to usize |
CHANGELOG.md |
Added entry documenting the breaking change |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
bobbinth
left a comment
There was a problem hiding this comment.
Looks great! Thank you! I left some comments inline.
| // TODO: We can technically avoid putting the &ProcessState reference here into the future, | ||
| // but it seems to work and maybe we should try building the web client against this branch | ||
| // to test if we can do this, as it does result in more readable code. | ||
| async move { |
There was a problem hiding this comment.
I would try to move it both the handling of stdlib events and TransactionEvent construction out of the async context. It will be a bit more code - but I think it is conceptually cleaner this way:
- First we handle stdlib events.
- Then we read w/e data we need from the process/host.
- And only then we enter async context to make any required async requests.
There was a problem hiding this comment.
I moved it outside so you can see what it looks like, but because we cannot return anything outside the async block, this is not as pretty overall.
There was a problem hiding this comment.
Yes, makes sense - but I'd still probably go with this approach (the additional code complexity is pretty small IMO).
bobbinth
left a comment
There was a problem hiding this comment.
Looks good! Thank you! I left a few more small comments inline.
bobbinth
left a comment
There was a problem hiding this comment.
All looks good! Thank you!
Until now, we basically had the following event handling setup:
TransactionEventHandling::Unhandledwith the data for the executor or prover host to further handle it.The change in this PR is to:
TransactionEvent.DataStore.Overall, this second approach appears cleaner because data extraction and handling logic are separated. It comes at the cost of a bit more code duplication in the sense that both exec and prover host match on every single
TransactionEvent, but this also provides the increase in clarity since events are essentially handled in a single place.Changes
TransactionEventbut rather renamed it toTransactionEventId.EventNameforTransactionEvent#2043.MockHostfor instance (seecrates/miden-testing/src/mock_host.rs).TransactionEventinmiden-txto contain the data extract from the stack or advice provider for handling it in the executor or prover host.miden-txbecause it relies onTransactionKernelProcess,TransactionKernelErrorandLinkMap, all of which are inmiden-tx. It may be possible to move all these tomiden-libbut not sure there's a benefit.Implementation Notes
TransactionEventIds are mapped to the sameTransactionEvent, e.g.AccountVaultBeforeGetBalance,AccountVaultBeforeHasNonFungibleAsset,AccountVaultBeforeAddAsset,AccountVaultBeforeRemoveAssetare all mapped toTransactionEvent::AccountVaultBeforeAssetAccess.ProcessState, such as checking whether a merkle path is already present in the advice provider. At the same time,TransactionEvent::extract_from_processdoes not have access to the base host and so it cannot run the same logic as the base host did before, which is why some odd fields are necessary such asis_witness_presentinTransactionEvent::AccountVaultBeforeAssetAccess.NoteAfterCreatedAuthRequestandUnauthorizedAccountPushProcedureIndex&ProcessStateinto the returnedFuturefromon_eventin the executor host. This is not technically necessary, but I wonder if we should try to see if this works with building the web client. If it doesn't, it would be good to have a check/test in miden-base that alerts us if we do this anyway.TransactionProgressis only tracked in the executor host as the prover host doesn't do anything useful with it anyway and so we can save a few lines of event handling code. Totally possible to do this anyway, though.closes #1709