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

[verifier] allow &TxContext in entry functions #7043

Merged
merged 1 commit into from
Jan 3, 2023

Conversation

sblackshear
Copy link
Collaborator

@sblackshear sblackshear commented Dec 28, 2022

Previously, we required entry functions that need a TxContext to take a &mut TxContext input. But an entry function that only reads the epoch or transaction sender does not need &mut. This change makes it possible to see whether an entry function may create new objects just by looking at the type signature. The new policy is thus:

  • entry functions need not include a TxContext input
  • If an entry function wants a TxContext input, it can declare either &TxContext or &mut TxContext according to its needs.

Tweaked the verifier to alow this, and updated some framework and example code accordingly.

@vercel
Copy link

vercel bot commented Dec 28, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated
explorer 🔄 Building (Inspect) Jan 3, 2023 at 5:24PM (UTC)
2 Ignored Deployments
Name Status Preview Comments Updated
explorer-storybook ⬜️ Ignored (Inspect) Jan 3, 2023 at 5:24PM (UTC)
wallet-adapter ⬜️ Ignored (Inspect) Jan 3, 2023 at 5:24PM (UTC)

@sblackshear sblackshear force-pushed the tx_context_relax branch 2 times, most recently from 53b30c9 to 8a3afb9 Compare December 28, 2022 19:57
@sblackshear
Copy link
Collaborator Author

@tnowacki, interestingly, this change causes a compiler crash via sui move test:

-    public entry fun accept_transfer(r: &Registry, coin: &mut RCoin<Abc>, transfer: Transfer) {
+    public entry fun accept_transfer(r: &Registry, coin: &mut RCoin<Abc>, transfer: Transfer, _: &mut TxContext) { 

I think this is because there was test code that still passed the now-redundant context:

abc::accept_transfer(reg_ref, &mut coin, transfer, ctx(test));

Stack trace was

thread 'main' panicked at 'assertion failed: all_refs.is_empty()', /Users/shb/.cargo/git/checkouts/move-0639dd674f581c30/265e879/language/move-compiler/src/cfgir/borrows/state.rs:863:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
2022-12-29T00:04:24.158609Z ERROR telemetry_subscribers: panicked at 'assertion failed: all_refs.is_empty()', /Users/shb/.cargo/git/checkouts/move-0639dd674f581c30/265e879/language/move-compiler/src/cfgir/borrows/state.rs:863:9 panic.file="/Users/shb/.cargo/git/checkouts/move-0639dd674f581c30/265e879/language/move-compiler/src/cfgir/borrows/state.rs" panic.line=863 panic.column=9

Will try to create a smaller repro and submit an issue in the Move repo, but flagging here just in case.

@sblackshear sblackshear force-pushed the tx_context_relax branch 2 times, most recently from a368977 to 6e5ff66 Compare December 29, 2022 00:40
/// - optional one-time witness type (see one_time_witness verifier pass) passed by value in the first
/// position
///
/// For transaction entry points
/// - The function must have `is_entry` true
/// - The function must have at least one parameter: &mut TxContext (see `is_tx_context`)
/// - The function may have an optional &mut TxContext or &TxContext (see `is_tx_context`) parameter
Copy link
Collaborator

@kchalkias kchalkias Dec 30, 2022

Choose a reason for hiding this comment

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

From Issue description and this comment, it's not clear if we assume the input to an entry function can be any of &mut TxContext or &TxContext or we can even omit TxContext completely. (probably the word "optional" here causes that confusion)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Updated the PR description and will fix the comment as well.

Copy link
Contributor

@amnn amnn left a comment

Choose a reason for hiding this comment

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

LGTM! Love the legibility improvement for entry functions.

Comment on lines 142 to 150
Err(format!(
"Expected last (and at most second) parameter for {}::{} to be &mut {}::{}::{}, but found {}",
"Expected last (and at most second) parameter for {}::{} to be &mut {}::{}::{} or &{}::{}::{}, but found {}",
module.self_id(),
INIT_FN_NAME,
SUI_FRAMEWORK_ADDRESS,
TX_CONTEXT_MODULE_NAME,
TX_CONTEXT_STRUCT_NAME,
SUI_FRAMEWORK_ADDRESS,
TX_CONTEXT_MODULE_NAME,
TX_CONTEXT_STRUCT_NAME,
format_signature_token(view, &parameters[0]),
))
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider re-using the earlier parameters?

Suggested change
Err(format!(
"Expected last (and at most second) parameter for {}::{} to be &mut {}::{}::{}, but found {}",
"Expected last (and at most second) parameter for {}::{} to be &mut {}::{}::{} or &{}::{}::{}, but found {}",
module.self_id(),
INIT_FN_NAME,
SUI_FRAMEWORK_ADDRESS,
TX_CONTEXT_MODULE_NAME,
TX_CONTEXT_STRUCT_NAME,
SUI_FRAMEWORK_ADDRESS,
TX_CONTEXT_MODULE_NAME,
TX_CONTEXT_STRUCT_NAME,
format_signature_token(view, &parameters[0]),
))
Err(format!(
"Expected last (and at most second) parameter for {0}::{1} to be &mut {2}::{3}::{4} or &{2}::{3}::{4}, but found {5}",
module.self_id(),
INIT_FN_NAME,
SUI_FRAMEWORK_ADDRESS,
TX_CONTEXT_MODULE_NAME,
TX_CONTEXT_STRUCT_NAME,
format_signature_token(view, &parameters[0]),
))

This makes it possible to see whether an entry function may create new objects just by looking at the type signature.

Tweaked the verifier to alow this, and updated some framework and example code accordingly.
@sblackshear sblackshear enabled auto-merge (rebase) January 3, 2023 17:24
@sblackshear sblackshear merged commit 8b49992 into MystenLabs:main Jan 3, 2023
Copy link
Contributor

@tnowacki tnowacki left a comment

Choose a reason for hiding this comment

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

I think we need to update the comments in crates/sui-verifier/src/one_time_witness_verifier.rs too. which refers to &mut TxContext in some of it's error messages.

Similarly, should we allow &TxContext in init?
At one point, I removed the necessity of the transaction context at all in init but then decided that was silly since then your init can't do anything meaningful. Thoughts?

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.

None yet

4 participants