forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Move] Implemented one-type witness checking (MystenLabs#3771)
* [Move] Implemented one-type witness checking * Added missing files * Adjusted number of object created in init functions * Renamed char(acteristic) type to one-time witness * Changed location of one-time witness checking function * All coins now use one-time witness * Added a new line * Updated test output * Adjusted tests * Updated snapshot test file * Removed check to make the test more robust * Updated snapshot test file * one-timeness check moved to Coin (MystenLabs#3894) * Removed a function that could compromise one-time witness safety and adjusted tests Co-authored-by: Damir Shamanaev <[email protected]>
- Loading branch information
Showing
39 changed files
with
193 additions
and
134 deletions.
There are no files selected for viewing
3 changes: 2 additions & 1 deletion
3
crates/sui-config/tests/snapshots/snapshot_tests__empty_genesis_snapshot_matches.snap
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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,11 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
/// Sui types helpers and utilities | ||
module sui::types { | ||
// === one-time witness === | ||
|
||
/// Tests if the argument type is a one-time witness, that is a type with only one instantiation | ||
/// across the entire code base. | ||
public native fun is_one_time_witness<T: drop>(_: &T): bool; | ||
} |
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
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,40 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use move_binary_format::errors::PartialVMResult; | ||
use move_core_types::language_storage::TypeTag; | ||
use move_vm_runtime::native_functions::NativeContext; | ||
use move_vm_types::{ | ||
gas_schedule::NativeCostIndex, | ||
loaded_data::runtime_types::Type, | ||
natives::function::{native_gas, NativeResult}, | ||
values::Value, | ||
}; | ||
use smallvec::smallvec; | ||
use std::collections::VecDeque; | ||
|
||
pub fn is_one_time_witness( | ||
context: &mut NativeContext, | ||
mut ty_args: Vec<Type>, | ||
args: VecDeque<Value>, | ||
) -> PartialVMResult<NativeResult> { | ||
debug_assert!(ty_args.len() == 1); | ||
debug_assert!(args.len() == 1); | ||
|
||
// unwrap safe because the interface of native function guarantees it. | ||
let type_tag = context.type_to_type_tag(&ty_args.pop().unwrap())?; | ||
|
||
// TODO: what should the cost of this be? | ||
let cost = native_gas(context.cost_table(), NativeCostIndex::LENGTH, 1); | ||
|
||
// If a struct type has the same name as the module that defines it but capitalized, it means | ||
// that it's a characteristic type (which is one way of implementing a one-time witness | ||
// type). This is checked in the char_type validator pass (a type with this type of name that | ||
// does not have all properties required of a characteristic type will cause a validator error). | ||
Ok(NativeResult::ok( | ||
cost, | ||
smallvec![Value::bool( | ||
matches!(type_tag, TypeTag::Struct(struct_tag) if struct_tag.name.to_string() == struct_tag.module.to_string().to_ascii_uppercase()) | ||
)], | ||
)) | ||
} |
15 changes: 0 additions & 15 deletions
15
crates/sui-verifier-transactional-tests/tests/char_type/wrong_name.move
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
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
File renamed without changes.
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
2 changes: 1 addition & 1 deletion
2
...tional-tests/tests/char_type/no_drop.move → ...tests/tests/one_time_witness/no_drop.move
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
File renamed without changes.
File renamed without changes.
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
2 changes: 1 addition & 1 deletion
2
...al-tests/tests/char_type/no_init_arg.move → ...s/tests/one_time_witness/no_init_arg.move
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
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...-tests/tests/char_type/other_mod_def.move → ...tests/one_time_witness/other_mod_def.move
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
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
15 changes: 15 additions & 0 deletions
15
crates/sui-verifier-transactional-tests/tests/one_time_witness/wrong_name.move
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,15 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// invalid, wrong one-time witness type name | ||
|
||
//# init --addresses test=0x0 | ||
|
||
//# publish | ||
module test::m { | ||
|
||
struct OneTimeWitness has drop { } | ||
|
||
fun init(_: OneTimeWitness, _ctx: &mut sui::tx_context::TxContext) { | ||
} | ||
} |
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...ts/tests/char_type/wrong_name_format.move → ...s/one_time_witness/wrong_name_format.move
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
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
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.