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

Refactor tx kernel events errors #819

Open
wants to merge 1 commit into
base: next
Choose a base branch
from

Conversation

Dominik1999
Copy link
Collaborator

@Dominik1999 Dominik1999 commented Aug 6, 2024

Closes #768

This PR moves (almost) all events into api.masm. All slot-check are now in the subsequent files from api.masm

Two open questions about events that are not in api.masm yet:

  • The event ACCOUNT_PUSH_PROCEDURE_INDEX_EVENT seems to be needed in account.masm or we need to rewrite this test

    fn test_authenticate_procedure() {
    let tx_context = TransactionContextBuilder::with_standard_account(
    ACCOUNT_ID_REGULAR_ACCOUNT_UPDATABLE_CODE_OFF_CHAIN,
    ONE,
    )
    Rewriting the test is not hard, just not sure, if this is what we want.

  • Some events are still emitted in tx.masm regarding note creation. I can move those to api.masm, too.

# EVENTS
# =================================================================================================

# Event emitted before a new note is created.
const.NOTE_BEFORE_CREATED_EVENT=131083
# Event emitted after a new note is created.
const.NOTE_AFTER_CREATED_EVENT=131084

# Event emitted before an ASSET is added to a note
const.NOTE_BEFORE_ADD_ASSET_EVENT=131085
# Event emitted after an ASSET is added to a note
const.NOTE_AFTER_ADD_ASSET_EVENT=131086

Let me know what you think @bobbinth, then I can finalize the PR and we can merge this week.

@Dominik1999 Dominik1999 changed the base branch from main to next August 6, 2024 15:48
@Dominik1999 Dominik1999 force-pushed the dominik_refactor_tx_kernel_events_errors branch from b77fad9 to a200964 Compare August 6, 2024 17:22
@Dominik1999 Dominik1999 force-pushed the dominik_refactor_tx_kernel_events_errors branch from a200964 to b9937fe Compare August 6, 2024 17:25
@Dominik1999 Dominik1999 self-assigned this Aug 6, 2024
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thank you! I left a few comments line. I think the biggest issue is that moving storage events into api.masm would break how faucets work. I don't have a great solution to this yet - but also, we should probably modify a test or two to make sure these errors get caught (or confirm that there are no errors with the current changes).

Comment on lines +52 to +61
#exec.memory::get_input_vault_root_ptr
#push.{ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN}
#exec.asset_vault::get_balance
#push.{FUNGIBLE_ASSET_AMOUNT} assert_eq

# assert the faucet storage has been updated
push.{FAUCET_STORAGE_DATA_SLOT}
exec.account::get_item
push.{expected_final_storage_amount}
assert_eq
#push.{FAUCET_STORAGE_DATA_SLOT}
#exec.account::get_item
#push.{expected_final_storage_amount}
#assert_eq
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be un-commented?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, I forgot to work on this. The test also passes when un-commented.

Comment on lines +66 to +67
# note: storage slot is known here, so set_item_raw can be used
add exec.account::get_faucet_storage_data_slot exec.account::set_item_raw dropw
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure this is going to work. If I'm understanding the flow correctly:

  • account::set_item_raw will not fire STORAGE_ON_AFTER_SET_ITEM event.
  • This means that Host::on_account_storage_after_set_item() won't get executed and the AccountDeltaTracker won't be updated.
  • This means that changes to the account storage for the faucet reserved slot won't be included in account delta.

We should probably modify one of the tests to test for this (i.e., when we mint a new asset, account storage delta should include the update incrementing the total number of assets minted).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One potential way to handle this is to introduce new events which get fired in mint_asset and burn_asset procedures in api.masm. Then handlers of these events would modify the account delta tracker accordingly.

Though, this may be introducing too much complexity.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about the same. If this change ("firing all events from api.masm") is purely aesthetics, we should not introduce this extra complexity. If the change adds readability to the transaction kernel, we should make the change.

One problem is that in api.masm, we don't have the information that we want to update yet. To update the account_storage_delta in the correct way, we need the TOTAL_ISSUANCE, which we compute at a later stage. So, to fire all events in api.masm, we need to rewrite the way how the faucet works.

Comment on lines +104 to +105
# note: storage slot is known here, so set_item_raw can be used
sub exec.account::get_faucet_storage_data_slot exec.account::set_item_raw dropw
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comment to the one above and this is also relevant to the other changes in this file.

Comment on lines +198 to +200
# emit event to signal that an account storage item is being updated
padw loc_loadw.1 loc_load.0 emit.ACCOUNT_STORAGE_AFTER_SET_ITEM_EVENT drop dropw
# => [OLD_VALUE]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I would probably move drop dropw to line 203 below and update the comments accordingly.

# => [index, KEY, NEW_VALUE, OLD_MAP_ROOT, OLD_VALUE, ...]

# emit event to signal that an account storage map item is being updated
emit.ACCOUNT_STORAGE_AFTER_SET_MAP_ITEM_EVENT drop dropw dropw
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar nit as above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor transaction kernel events and errors
2 participants