-
Notifications
You must be signed in to change notification settings - Fork 11.3k
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
[adapter][verifier] allow option and ID arguments in entry points #2149
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,10 @@ use sui_types::{ | |
object::{self, Data, MoveObject, Object, Owner}, | ||
storage::{DeleteKind, Storage}, | ||
}; | ||
use sui_verifier::{entry_points_verifier::INIT_FN_NAME, verifier}; | ||
use sui_verifier::{ | ||
entry_points_verifier::{INIT_FN_NAME, RESOLVED_STD_OPTION, RESOLVED_SUI_ID}, | ||
verifier, | ||
}; | ||
|
||
use move_core_types::{ | ||
account_address::AccountAddress, | ||
|
@@ -692,15 +695,15 @@ pub fn resolve_and_type_check( | |
// Track the mapping from each input object to its Move type. | ||
// This will be needed latter in `check_child_object_of_shared_object`. | ||
let mut object_type_map = BTreeMap::new(); | ||
|
||
let view = &BinaryIndexedView::Module(module); | ||
let bcs_args = args | ||
.into_iter() | ||
.enumerate() | ||
.map(|(idx, arg)| { | ||
let param_type = ¶meters[idx]; | ||
let object_kind = match arg { | ||
CallArg::Pure(arg) => { | ||
if !is_primitive(module, type_args, param_type) { | ||
if !is_primitive(view, type_args, param_type) { | ||
return Err(SuiError::TypeError { | ||
error: format!( | ||
"Non-primitive argument at index {}. If it is an object, it must \ | ||
|
@@ -799,7 +802,7 @@ pub fn resolve_and_type_check( | |
}) | ||
} | ||
}; | ||
type_check_struct(module, type_args, &move_object.type_, inner_param_type)?; | ||
type_check_struct(view, type_args, &move_object.type_, inner_param_type)?; | ||
object_type_map.insert(id, move_object.type_.module_id()); | ||
Ok(object_arg) | ||
}) | ||
|
@@ -891,7 +894,7 @@ fn check_child_object_of_shared_object( | |
} | ||
|
||
fn is_primitive( | ||
_module: &CompiledModule, | ||
view: &BinaryIndexedView, | ||
function_type_arguments: &[TypeTag], | ||
t: &SignatureToken, | ||
) -> bool { | ||
|
@@ -902,16 +905,27 @@ fn is_primitive( | |
| SignatureToken::U128 | ||
| SignatureToken::Address => true, | ||
|
||
SignatureToken::Vector(inner) => is_primitive(_module, function_type_arguments, inner), | ||
SignatureToken::Struct(idx) => { | ||
let resolved_struct = sui_verifier::resolve_struct(view, *idx); | ||
// is ID | ||
resolved_struct == RESOLVED_SUI_ID | ||
} | ||
|
||
SignatureToken::StructInstantiation(idx, targs) => { | ||
let resolved_struct = sui_verifier::resolve_struct(view, *idx); | ||
// is option of a primitive | ||
resolved_struct == RESOLVED_STD_OPTION | ||
&& targs.len() == 1 | ||
&& is_primitive(view, function_type_arguments, &targs[0]) | ||
} | ||
SignatureToken::Vector(inner) => is_primitive(view, function_type_arguments, inner), | ||
|
||
SignatureToken::TypeParameter(idx) => function_type_arguments | ||
.get(*idx as usize) | ||
.map(is_primitive_type_tag) | ||
.unwrap_or(false), | ||
|
||
SignatureToken::Signer | ||
| SignatureToken::Struct(_) | ||
| SignatureToken::StructInstantiation(_, _) | ||
| SignatureToken::Reference(_) | ||
| SignatureToken::MutableReference(_) => false, | ||
} | ||
|
@@ -921,24 +935,37 @@ fn is_primitive_type_tag(t: &TypeTag) -> bool { | |
match t { | ||
TypeTag::Bool | TypeTag::U8 | TypeTag::U64 | TypeTag::U128 | TypeTag::Address => true, | ||
TypeTag::Vector(inner) => is_primitive_type_tag(inner), | ||
TypeTag::Signer | TypeTag::Struct(_) => false, | ||
TypeTag::Struct(StructTag { | ||
address, | ||
module, | ||
name, | ||
type_params: type_args, | ||
}) => { | ||
let resolved_struct = (address, module.as_ident_str(), name.as_ident_str()); | ||
// is id or.. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is this comment about? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is id or... is option of a primitive (down below) |
||
if resolved_struct == RESOLVED_SUI_ID { | ||
return true; | ||
} | ||
// is option of a primitive | ||
resolved_struct == RESOLVED_STD_OPTION | ||
&& type_args.len() == 1 | ||
&& is_primitive_type_tag(&type_args[0]) | ||
} | ||
TypeTag::Signer => false, | ||
} | ||
} | ||
|
||
fn type_check_struct( | ||
module: &CompiledModule, | ||
view: &BinaryIndexedView, | ||
function_type_arguments: &[TypeTag], | ||
arg_type: &StructTag, | ||
param_type: &SignatureToken, | ||
) -> Result<(), SuiError> { | ||
if !struct_tag_equals_sig_token(module, function_type_arguments, arg_type, param_type) { | ||
if !struct_tag_equals_sig_token(view, function_type_arguments, arg_type, param_type) { | ||
Err(SuiError::TypeError { | ||
error: format!( | ||
"Expected argument of type {}, but found type {}", | ||
sui_verifier::format_signature_token( | ||
&BinaryIndexedView::Module(module), | ||
param_type | ||
), | ||
sui_verifier::format_signature_token(view, param_type), | ||
arg_type | ||
), | ||
}) | ||
|
@@ -948,7 +975,7 @@ fn type_check_struct( | |
} | ||
|
||
fn type_tag_equals_sig_token( | ||
module: &CompiledModule, | ||
view: &BinaryIndexedView, | ||
function_type_arguments: &[TypeTag], | ||
arg_type: &TypeTag, | ||
param_type: &SignatureToken, | ||
|
@@ -963,7 +990,7 @@ fn type_tag_equals_sig_token( | |
|
||
(TypeTag::Vector(inner_arg_type), SignatureToken::Vector(inner_param_type)) => { | ||
type_tag_equals_sig_token( | ||
module, | ||
view, | ||
function_type_arguments, | ||
inner_arg_type, | ||
inner_param_type, | ||
|
@@ -972,7 +999,7 @@ fn type_tag_equals_sig_token( | |
|
||
(TypeTag::Struct(arg_struct), SignatureToken::Struct(_)) | ||
| (TypeTag::Struct(arg_struct), SignatureToken::StructInstantiation(_, _)) => { | ||
struct_tag_equals_sig_token(module, function_type_arguments, arg_struct, param_type) | ||
struct_tag_equals_sig_token(view, function_type_arguments, arg_struct, param_type) | ||
} | ||
|
||
(_, SignatureToken::TypeParameter(idx)) => { | ||
|
@@ -983,17 +1010,17 @@ fn type_tag_equals_sig_token( | |
} | ||
|
||
fn struct_tag_equals_sig_token( | ||
module: &CompiledModule, | ||
view: &BinaryIndexedView, | ||
function_type_arguments: &[TypeTag], | ||
arg_type: &StructTag, | ||
param_type: &SignatureToken, | ||
) -> bool { | ||
match param_type { | ||
SignatureToken::Struct(idx) => { | ||
struct_tag_equals_struct_inst(module, function_type_arguments, arg_type, *idx, &[]) | ||
struct_tag_equals_struct_inst(view, function_type_arguments, arg_type, *idx, &[]) | ||
} | ||
SignatureToken::StructInstantiation(idx, args) => { | ||
struct_tag_equals_struct_inst(module, function_type_arguments, arg_type, *idx, args) | ||
struct_tag_equals_struct_inst(view, function_type_arguments, arg_type, *idx, args) | ||
} | ||
SignatureToken::TypeParameter(idx) => match &function_type_arguments[*idx as usize] { | ||
TypeTag::Struct(s) => arg_type == s, | ||
|
@@ -1004,14 +1031,13 @@ fn struct_tag_equals_sig_token( | |
} | ||
|
||
fn struct_tag_equals_struct_inst( | ||
module: &CompiledModule, | ||
view: &BinaryIndexedView, | ||
function_type_arguments: &[TypeTag], | ||
arg_type: &StructTag, | ||
param_type: StructHandleIndex, | ||
param_type_arguments: &[SignatureToken], | ||
) -> bool { | ||
let view = BinaryIndexedView::Module(module); | ||
let (address, module_name, struct_name) = sui_verifier::resolve_struct(&view, param_type); | ||
let (address, module_name, struct_name) = sui_verifier::resolve_struct(view, param_type); | ||
|
||
// same address, module, name, and type parameters | ||
&arg_type.address == address | ||
|
@@ -1021,7 +1047,7 @@ fn struct_tag_equals_struct_inst( | |
&& arg_type.type_params.iter().zip(param_type_arguments).all( | ||
|(arg_type_arg, param_type_arg)| { | ||
type_tag_equals_sig_token( | ||
module, | ||
view, | ||
function_type_arguments, | ||
arg_type_arg, | ||
param_type_arg, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
crates/sui-verifier-transactional-tests/tests/entry_points/generic_with_key_invalid.exp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
processed 2 tasks | ||
|
||
task 0 'publish'. lines 6-16: | ||
Error: Failed to verify the Move module, reason: "Invalid entry point parameter type. Expected primitive or object type. Got: Std::Option::Option<T0>". | ||
|
||
task 1 'publish'. lines 18-28: | ||
Error: Failed to verify the Move module, reason: "Invalid entry point parameter type. Expected primitive or object type. Got: vector<Std::Option::Option<T0>>". |
28 changes: 28 additions & 0 deletions
28
crates/sui-verifier-transactional-tests/tests/entry_points/generic_with_key_invalid.mvir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// invalid, type parameters with key are not valid when nested as no primitive has key | ||
|
||
//# publish | ||
module 0x0.M { | ||
import 0x2.TxContext; | ||
import 0x1.Option; | ||
|
||
public(script) no<T:key>(l0: Option.Option<T>, ctx: &mut TxContext.TxContext) { | ||
label l0: | ||
abort 0; | ||
} | ||
|
||
} | ||
|
||
//# publish | ||
module 0x0.M { | ||
import 0x2.TxContext; | ||
import 0x1.Option; | ||
|
||
public(script) no<T:key>(l0: vector<Option.Option<T>>, ctx: &mut TxContext.TxContext) { | ||
label l0: | ||
abort 0; | ||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
crates/sui-verifier-transactional-tests/tests/entry_points/generic_with_key_valid.exp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
processed 1 task | ||
|
||
task 0 'publish'. lines 6-16: | ||
created: object(103) | ||
written: object(102) |
16 changes: 16 additions & 0 deletions
16
crates/sui-verifier-transactional-tests/tests/entry_points/generic_with_key_valid.mvir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// valid, type parameters with key are valid as long as they are not nested | ||
|
||
//# publish | ||
module 0x0.M { | ||
import 0x2.TxContext; | ||
import 0x1.Option; | ||
|
||
public(script) yes<T:key>(l0: T, l1: &T, l2: &mut T, ctx: &mut TxContext.TxContext) { | ||
label l0: | ||
abort 0; | ||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
crates/sui-verifier-transactional-tests/tests/entry_points/id.exp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
processed 1 task | ||
|
||
task 0 'publish'. lines 6-21: | ||
created: object(103) | ||
written: object(102) |
21 changes: 21 additions & 0 deletions
21
crates/sui-verifier-transactional-tests/tests/entry_points/id.mvir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// valid, ID is allowed | ||
|
||
//# publish | ||
module 0x0.M { | ||
import 0x2.TxContext; | ||
import 0x2.ID; | ||
|
||
public(script) yes<T>( | ||
l0: ID.ID, | ||
l1: vector<ID.ID>, | ||
l2: vector<vector<ID.ID>>, | ||
ctx: &mut TxContext.TxContext, | ||
) { | ||
label l0: | ||
abort 0; | ||
} | ||
|
||
} |
5 changes: 3 additions & 2 deletions
5
crates/sui-verifier-transactional-tests/tests/entry_points/option.exp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
processed 1 task | ||
|
||
task 0 'publish'. lines 6-16: | ||
Error: Failed to verify the Move module, reason: "Invalid entry point parameter type. Expected primitive or object type. Got: Std::Option::Option<u64>". | ||
task 0 'publish'. lines 6-23: | ||
created: object(103) | ||
written: object(102) |
11 changes: 9 additions & 2 deletions
11
crates/sui-verifier-transactional-tests/tests/entry_points/option.mvir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will an
Option<ID>
work as well?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes