From 489397b999f0ee7809418230e6328a1bb9fd7d3c Mon Sep 17 00:00:00 2001 From: Timothy Zakian Date: Fri, 31 Jan 2025 14:08:18 -0800 Subject: [PATCH] [6/n][vm-rewrite][sui-execution] Update object runtime to use type tags instead of runtime VM types. Since the VM instance no longer will live across commands/the entire lifetime of the object runtime, this replaces all VM runtime `Type`s with `TypeTag`s or `MoveObjectType`s where appropriate. This also updates the natives and runtime to handle the new `NativeContextExtensions` model. NB: The code in the PR may not be working as future PRs will build on top of this. --- .../latest/sui-move-natives/Cargo.toml | 2 - .../latest/sui-move-natives/src/address.rs | 8 +- .../latest/sui-move-natives/src/config.rs | 29 ++-- .../sui-move-natives/src/crypto/bls12381.rs | 12 +- .../sui-move-natives/src/crypto/ecdsa_k1.rs | 12 +- .../sui-move-natives/src/crypto/ecdsa_r1.rs | 12 +- .../sui-move-natives/src/crypto/ecvrf.rs | 12 +- .../sui-move-natives/src/crypto/ed25519.rs | 12 +- .../sui-move-natives/src/crypto/groth16.rs | 12 +- .../sui-move-natives/src/crypto/group_ops.rs | 12 +- .../sui-move-natives/src/crypto/hash.rs | 12 +- .../sui-move-natives/src/crypto/hmac.rs | 12 +- .../sui-move-natives/src/crypto/poseidon.rs | 13 +- .../latest/sui-move-natives/src/crypto/vdf.rs | 13 +- .../sui-move-natives/src/crypto/zklogin.rs | 13 +- .../sui-move-natives/src/dynamic_field.rs | 71 +++++---- .../latest/sui-move-natives/src/event.rs | 28 ++-- .../latest/sui-move-natives/src/lib.rs | 28 ++-- .../latest/sui-move-natives/src/object.rs | 24 ++- .../src/object_runtime/mod.rs | 42 ++--- .../src/object_runtime/object_store.rs | 62 +++----- .../latest/sui-move-natives/src/random.rs | 6 +- .../sui-move-natives/src/test_scenario.rs | 148 ++++++++++++------ .../latest/sui-move-natives/src/test_utils.rs | 11 +- .../latest/sui-move-natives/src/transfer.rs | 41 +++-- .../latest/sui-move-natives/src/tx_context.rs | 15 +- .../latest/sui-move-natives/src/types.rs | 6 +- .../latest/sui-move-natives/src/validator.rs | 6 +- 28 files changed, 388 insertions(+), 286 deletions(-) diff --git a/sui-execution/latest/sui-move-natives/Cargo.toml b/sui-execution/latest/sui-move-natives/Cargo.toml index 9295aeeccd89c4..4c59e822ab5d9e 100644 --- a/sui-execution/latest/sui-move-natives/Cargo.toml +++ b/sui-execution/latest/sui-move-natives/Cargo.toml @@ -19,9 +19,7 @@ fastcrypto-vdf.workspace = true fastcrypto.workspace = true move-binary-format.workspace = true move-core-types.workspace = true -move-vm-types.workspace = true -move-stdlib-natives = { path = "../../../external-crates/move/crates/move-stdlib-natives" } move-vm-runtime = { path = "../../../external-crates/move/crates/move-vm-runtime" } sui-protocol-config.workspace = true diff --git a/sui-execution/latest/sui-move-natives/src/address.rs b/sui-execution/latest/sui-move-natives/src/address.rs index da1b69b1bc9968..ac13a8d1626492 100644 --- a/sui-execution/latest/sui-move-natives/src/address.rs +++ b/sui-execution/latest/sui-move-natives/src/address.rs @@ -4,10 +4,12 @@ use crate::NativesCostTable; use move_binary_format::errors::PartialVMResult; use move_core_types::{account_address::AccountAddress, gas_algebra::InternalGas, u256::U256}; -use move_vm_runtime::{native_charge_gas_early_exit, native_functions::NativeContext}; -use move_vm_types::{ - loaded_data::runtime_types::Type, natives::function::NativeResult, pop_arg, values::Value, +use move_vm_runtime::{ + execution::{values::Value, Type}, + natives::functions::NativeResult, + pop_arg, }; +use move_vm_runtime::{native_charge_gas_early_exit, natives::functions::NativeContext}; use smallvec::smallvec; use std::collections::VecDeque; diff --git a/sui-execution/latest/sui-move-natives/src/config.rs b/sui-execution/latest/sui-move-natives/src/config.rs index e576f9d99ed9a3..3a59ce6924c1ae 100644 --- a/sui-execution/latest/sui-move-natives/src/config.rs +++ b/sui-execution/latest/sui-move-natives/src/config.rs @@ -8,14 +8,18 @@ use move_core_types::{ runtime_value as R, vm_status::StatusCode, }; use move_vm_runtime::native_charge_gas_early_exit; -use move_vm_runtime::native_functions::NativeContext; -use move_vm_types::{ - loaded_data::runtime_types::Type, - natives::function::NativeResult, +use move_vm_runtime::natives::extensions::NativeContextMut; +use move_vm_runtime::natives::functions::NativeContext; +use move_vm_runtime::{ + execution::{ + values::{Struct, Value, Vector}, + Type, + }, + natives::functions::NativeResult, pop_arg, - values::{Struct, Value, Vector}, }; use smallvec::smallvec; +use std::cell::RefMut; use std::collections::VecDeque; use sui_types::{base_types::MoveObjectType, TypeTag}; use tracing::{error, instrument}; @@ -83,11 +87,14 @@ pub fn read_setting_impl( E_BCS_SERIALIZATION_FAILURE, )); }; - let object_runtime: &mut ObjectRuntime = context.extensions_mut().get_mut(); + // let object_runtime: &NativeContextMut + let object_runtime: std::cell::RefMut<'_, ObjectRuntime<'_>> = context + .extensions_mut() + .get::>() + .get_mut(); let read_value_opt = consistent_value_before_current_epoch( object_runtime, - &field_setting_ty, field_setting_tag, &field_setting_layout, &setting_value_ty, @@ -110,8 +117,7 @@ pub fn read_setting_impl( } fn consistent_value_before_current_epoch( - object_runtime: &mut ObjectRuntime, - field_setting_ty: &Type, + mut object_runtime: RefMut<'_, ObjectRuntime<'_>>, field_setting_tag: StructTag, field_setting_layout: &R::MoveTypeLayout, _setting_value_ty: &Type, @@ -125,7 +131,6 @@ fn consistent_value_before_current_epoch( let Some(field) = object_runtime.config_setting_unsequenced_read( config_addr.into(), name_df_addr.into(), - field_setting_ty, field_setting_layout, &field_setting_obj_ty, ) else { @@ -150,8 +155,8 @@ fn consistent_value_before_current_epoch( let [newer_value_epoch, newer_value, older_value_opt]: [Value; 3] = unpack_struct(data)?; let newer_value_epoch: u64 = newer_value_epoch.value_as()?; debug_assert!( - unpack_option(newer_value.copy_value()?, value_ty)?.is_some() - || unpack_option(older_value_opt.copy_value()?, value_ty)?.is_some() + unpack_option(newer_value.copy_value(), value_ty)?.is_some() + || unpack_option(older_value_opt.copy_value(), value_ty)?.is_some() ); Ok(if current_epoch > newer_value_epoch { newer_value diff --git a/sui-execution/latest/sui-move-natives/src/crypto/bls12381.rs b/sui-execution/latest/sui-move-natives/src/crypto/bls12381.rs index 28c1ef5b218645..e3ac9e5b19a8a3 100644 --- a/sui-execution/latest/sui-move-natives/src/crypto/bls12381.rs +++ b/sui-execution/latest/sui-move-natives/src/crypto/bls12381.rs @@ -6,13 +6,15 @@ use fastcrypto::{ }; use move_binary_format::errors::PartialVMResult; use move_core_types::gas_algebra::InternalGas; -use move_vm_runtime::{native_charge_gas_early_exit, native_functions::NativeContext}; -use move_vm_types::{ - loaded_data::runtime_types::Type, - natives::function::NativeResult, +use move_vm_runtime::{ + execution::{ + values::{Value, VectorRef}, + Type, + }, + natives::functions::NativeResult, pop_arg, - values::{Value, VectorRef}, }; +use move_vm_runtime::{native_charge_gas_early_exit, natives::functions::NativeContext}; use smallvec::smallvec; use std::collections::VecDeque; diff --git a/sui-execution/latest/sui-move-natives/src/crypto/ecdsa_k1.rs b/sui-execution/latest/sui-move-natives/src/crypto/ecdsa_k1.rs index 9d728b10b5c020..f32d3f2f66e53b 100644 --- a/sui-execution/latest/sui-move-natives/src/crypto/ecdsa_k1.rs +++ b/sui-execution/latest/sui-move-natives/src/crypto/ecdsa_k1.rs @@ -14,13 +14,15 @@ use fastcrypto::{ }; use move_binary_format::errors::PartialVMResult; use move_core_types::gas_algebra::InternalGas; -use move_vm_runtime::{native_charge_gas_early_exit, native_functions::NativeContext}; -use move_vm_types::{ - loaded_data::runtime_types::Type, - natives::function::NativeResult, +use move_vm_runtime::{ + execution::{ + values::{self, Value, VectorRef}, + Type, + }, + natives::functions::NativeResult, pop_arg, - values::{self, Value, VectorRef}, }; +use move_vm_runtime::{native_charge_gas_early_exit, natives::functions::NativeContext}; use rand::rngs::StdRng; use rand::SeedableRng; use smallvec::smallvec; diff --git a/sui-execution/latest/sui-move-natives/src/crypto/ecdsa_r1.rs b/sui-execution/latest/sui-move-natives/src/crypto/ecdsa_r1.rs index 9d68fe744dedc6..a016a8c2948b19 100644 --- a/sui-execution/latest/sui-move-natives/src/crypto/ecdsa_r1.rs +++ b/sui-execution/latest/sui-move-natives/src/crypto/ecdsa_r1.rs @@ -13,12 +13,14 @@ use fastcrypto::{ use move_binary_format::errors::PartialVMResult; use move_core_types::gas_algebra::InternalGas; use move_vm_runtime::native_charge_gas_early_exit; -use move_vm_runtime::native_functions::NativeContext; -use move_vm_types::{ - loaded_data::runtime_types::Type, - natives::function::NativeResult, +use move_vm_runtime::natives::functions::NativeContext; +use move_vm_runtime::{ + execution::{ + values::{Value, VectorRef}, + Type, + }, + natives::functions::NativeResult, pop_arg, - values::{Value, VectorRef}, }; use smallvec::smallvec; use std::collections::VecDeque; diff --git a/sui-execution/latest/sui-move-natives/src/crypto/ecvrf.rs b/sui-execution/latest/sui-move-natives/src/crypto/ecvrf.rs index 7605d7235391a3..7b4fcd88b065d8 100644 --- a/sui-execution/latest/sui-move-natives/src/crypto/ecvrf.rs +++ b/sui-execution/latest/sui-move-natives/src/crypto/ecvrf.rs @@ -5,13 +5,15 @@ use fastcrypto::vrf::ecvrf::{ECVRFProof, ECVRFPublicKey}; use fastcrypto::vrf::VRFProof; use move_binary_format::errors::PartialVMResult; use move_core_types::gas_algebra::InternalGas; -use move_vm_runtime::{native_charge_gas_early_exit, native_functions::NativeContext}; -use move_vm_types::{ - loaded_data::runtime_types::Type, - natives::function::NativeResult, +use move_vm_runtime::{ + execution::{ + values::{Value, VectorRef}, + Type, + }, + natives::functions::NativeResult, pop_arg, - values::{Value, VectorRef}, }; +use move_vm_runtime::{native_charge_gas_early_exit, natives::functions::NativeContext}; use smallvec::smallvec; use std::collections::VecDeque; diff --git a/sui-execution/latest/sui-move-natives/src/crypto/ed25519.rs b/sui-execution/latest/sui-move-natives/src/crypto/ed25519.rs index b082deca9adae7..86a3d5bad37c40 100644 --- a/sui-execution/latest/sui-move-natives/src/crypto/ed25519.rs +++ b/sui-execution/latest/sui-move-natives/src/crypto/ed25519.rs @@ -7,13 +7,15 @@ use fastcrypto::{ }; use move_binary_format::errors::PartialVMResult; use move_core_types::gas_algebra::InternalGas; -use move_vm_runtime::{native_charge_gas_early_exit, native_functions::NativeContext}; -use move_vm_types::{ - loaded_data::runtime_types::Type, - natives::function::NativeResult, +use move_vm_runtime::{ + execution::{ + values::{Value, VectorRef}, + Type, + }, + natives::functions::NativeResult, pop_arg, - values::{Value, VectorRef}, }; +use move_vm_runtime::{native_charge_gas_early_exit, natives::functions::NativeContext}; use smallvec::smallvec; use std::collections::VecDeque; diff --git a/sui-execution/latest/sui-move-natives/src/crypto/groth16.rs b/sui-execution/latest/sui-move-natives/src/crypto/groth16.rs index 38d4dffc66eda3..0cec1ab418e1f5 100644 --- a/sui-execution/latest/sui-move-natives/src/crypto/groth16.rs +++ b/sui-execution/latest/sui-move-natives/src/crypto/groth16.rs @@ -3,13 +3,15 @@ use crate::{object_runtime::ObjectRuntime, NativesCostTable}; use move_binary_format::errors::PartialVMResult; use move_core_types::gas_algebra::InternalGas; -use move_vm_runtime::{native_charge_gas_early_exit, native_functions::NativeContext}; -use move_vm_types::{ - loaded_data::runtime_types::Type, - natives::function::NativeResult, +use move_vm_runtime::{ + execution::{ + values::{self, Value, VectorRef}, + Type, + }, + natives::functions::NativeResult, pop_arg, - values::{self, Value, VectorRef}, }; +use move_vm_runtime::{native_charge_gas_early_exit, natives::functions::NativeContext}; use smallvec::smallvec; use std::collections::VecDeque; diff --git a/sui-execution/latest/sui-move-natives/src/crypto/group_ops.rs b/sui-execution/latest/sui-move-natives/src/crypto/group_ops.rs index b0aff4080bae14..cb9aa18dd2d0bf 100644 --- a/sui-execution/latest/sui-move-natives/src/crypto/group_ops.rs +++ b/sui-execution/latest/sui-move-natives/src/crypto/group_ops.rs @@ -12,12 +12,14 @@ use move_binary_format::errors::{PartialVMError, PartialVMResult}; use move_core_types::gas_algebra::InternalGas; use move_core_types::vm_status::StatusCode; use move_vm_runtime::native_charge_gas_early_exit; -use move_vm_runtime::native_functions::NativeContext; -use move_vm_types::{ - loaded_data::runtime_types::Type, - natives::function::NativeResult, +use move_vm_runtime::natives::functions::NativeContext; +use move_vm_runtime::{ + execution::{ + values::{Value, VectorRef}, + Type, + }, + natives::functions::NativeResult, pop_arg, - values::{Value, VectorRef}, }; use smallvec::smallvec; use std::collections::VecDeque; diff --git a/sui-execution/latest/sui-move-natives/src/crypto/hash.rs b/sui-execution/latest/sui-move-natives/src/crypto/hash.rs index ac5d5b749165d3..9aa04f11cce10a 100644 --- a/sui-execution/latest/sui-move-natives/src/crypto/hash.rs +++ b/sui-execution/latest/sui-move-natives/src/crypto/hash.rs @@ -4,13 +4,15 @@ use crate::NativesCostTable; use fastcrypto::hash::{Blake2b256, HashFunction, Keccak256}; use move_binary_format::errors::PartialVMResult; use move_core_types::gas_algebra::InternalGas; -use move_vm_runtime::{native_charge_gas_early_exit, native_functions::NativeContext}; -use move_vm_types::{ - loaded_data::runtime_types::Type, - natives::function::NativeResult, +use move_vm_runtime::{ + execution::{ + values::{Value, VectorRef}, + Type, + }, + natives::functions::NativeResult, pop_arg, - values::{Value, VectorRef}, }; +use move_vm_runtime::{native_charge_gas_early_exit, natives::functions::NativeContext}; use smallvec::smallvec; use std::{collections::VecDeque, ops::Mul}; diff --git a/sui-execution/latest/sui-move-natives/src/crypto/hmac.rs b/sui-execution/latest/sui-move-natives/src/crypto/hmac.rs index 16404a9f8a6165..a5948b068034f2 100644 --- a/sui-execution/latest/sui-move-natives/src/crypto/hmac.rs +++ b/sui-execution/latest/sui-move-natives/src/crypto/hmac.rs @@ -4,13 +4,15 @@ use crate::NativesCostTable; use fastcrypto::{hmac, traits::ToFromBytes}; use move_binary_format::errors::PartialVMResult; use move_core_types::gas_algebra::InternalGas; -use move_vm_runtime::{native_charge_gas_early_exit, native_functions::NativeContext}; -use move_vm_types::{ - loaded_data::runtime_types::Type, - natives::function::NativeResult, +use move_vm_runtime::{ + execution::{ + values::{Value, VectorRef}, + Type, + }, + natives::functions::NativeResult, pop_arg, - values::{Value, VectorRef}, }; +use move_vm_runtime::{native_charge_gas_early_exit, natives::functions::NativeContext}; use smallvec::smallvec; use std::collections::VecDeque; diff --git a/sui-execution/latest/sui-move-natives/src/crypto/poseidon.rs b/sui-execution/latest/sui-move-natives/src/crypto/poseidon.rs index abe6bfd7f76e32..da7a1f0b0c5930 100644 --- a/sui-execution/latest/sui-move-natives/src/crypto/poseidon.rs +++ b/sui-execution/latest/sui-move-natives/src/crypto/poseidon.rs @@ -6,14 +6,15 @@ use fastcrypto_zkp::bn254::poseidon::poseidon_bytes; use move_binary_format::errors::PartialVMResult; use move_core_types::gas_algebra::InternalGas; use move_core_types::vm_status::StatusCode; -use move_vm_runtime::{native_charge_gas_early_exit, native_functions::NativeContext}; -use move_vm_types::natives::function::PartialVMError; -use move_vm_types::{ - loaded_data::runtime_types::Type, - natives::function::NativeResult, +use move_vm_runtime::{ + execution::{ + values::{Value, VectorRef}, + Type, + }, + natives::functions::{NativeResult, PartialVMError}, pop_arg, - values::{Value, VectorRef}, }; +use move_vm_runtime::{native_charge_gas_early_exit, natives::functions::NativeContext}; use smallvec::smallvec; use std::collections::VecDeque; use std::ops::Mul; diff --git a/sui-execution/latest/sui-move-natives/src/crypto/vdf.rs b/sui-execution/latest/sui-move-natives/src/crypto/vdf.rs index 88616e037cd56b..8e781d265a2598 100644 --- a/sui-execution/latest/sui-move-natives/src/crypto/vdf.rs +++ b/sui-execution/latest/sui-move-natives/src/crypto/vdf.rs @@ -9,14 +9,15 @@ use fastcrypto_vdf::vdf::VDF; use move_binary_format::errors::PartialVMResult; use move_core_types::gas_algebra::InternalGas; use move_core_types::vm_status::StatusCode; -use move_vm_runtime::{native_charge_gas_early_exit, native_functions::NativeContext}; -use move_vm_types::natives::function::PartialVMError; -use move_vm_types::{ - loaded_data::runtime_types::Type, - natives::function::NativeResult, +use move_vm_runtime::{ + execution::{ + values::{Value, VectorRef}, + Type, + }, + natives::functions::{NativeResult, PartialVMError}, pop_arg, - values::{Value, VectorRef}, }; +use move_vm_runtime::{native_charge_gas_early_exit, natives::functions::NativeContext}; use smallvec::smallvec; use std::collections::VecDeque; diff --git a/sui-execution/latest/sui-move-natives/src/crypto/zklogin.rs b/sui-execution/latest/sui-move-natives/src/crypto/zklogin.rs index e0288f507da678..fbcb344fb96eab 100644 --- a/sui-execution/latest/sui-move-natives/src/crypto/zklogin.rs +++ b/sui-execution/latest/sui-move-natives/src/crypto/zklogin.rs @@ -7,12 +7,15 @@ use move_core_types::account_address::AccountAddress; use move_core_types::gas_algebra::InternalGas; use move_core_types::u256::U256; use move_core_types::vm_status::StatusCode; -use move_vm_runtime::{native_charge_gas_early_exit, native_functions::NativeContext}; -use move_vm_types::natives::function::PartialVMError; -use move_vm_types::values::VectorRef; -use move_vm_types::{ - loaded_data::runtime_types::Type, natives::function::NativeResult, pop_arg, values::Value, +use move_vm_runtime::{ + execution::{ + values::{Value, VectorRef}, + Type, + }, + natives::functions::{NativeResult, PartialVMError}, + pop_arg, }; +use move_vm_runtime::{native_charge_gas_early_exit, natives::functions::NativeContext}; use smallvec::smallvec; use std::collections::VecDeque; diff --git a/sui-execution/latest/sui-move-natives/src/dynamic_field.rs b/sui-execution/latest/sui-move-natives/src/dynamic_field.rs index ba37beef3f0145..15e59decc16975 100644 --- a/sui-execution/latest/sui-move-natives/src/dynamic_field.rs +++ b/sui-execution/latest/sui-move-natives/src/dynamic_field.rs @@ -13,16 +13,18 @@ use move_core_types::{ language_storage::{StructTag, TypeTag}, vm_status::StatusCode, }; -use move_vm_runtime::native_charge_gas_early_exit; -use move_vm_runtime::native_functions::NativeContext; -use move_vm_types::{ - loaded_data::runtime_types::Type, - natives::function::NativeResult, +use move_vm_runtime::natives::functions::NativeContext; +use move_vm_runtime::{ + execution::{ + values::{StructRef, Value}, + Type, + }, + natives::functions::NativeResult, pop_arg, - values::{StructRef, Value}, }; +use move_vm_runtime::{native_charge_gas_early_exit, natives::extensions::NativeContextMut}; use smallvec::smallvec; -use std::collections::VecDeque; +use std::{cell::RefMut, collections::VecDeque}; use sui_types::{base_types::MoveObjectType, dynamic_field::derive_dynamic_field_id}; use tracing::instrument; @@ -31,7 +33,9 @@ const E_FIELD_TYPE_MISMATCH: u64 = 2; const E_BCS_SERIALIZATION_FAILURE: u64 = 3; macro_rules! get_or_fetch_object { - ($context:ident, $ty_args:ident, $parent:ident, $child_id:ident, $ty_cost_per_byte:expr) => {{ + ($context:ident, + $object_runtime:ident, + $ty_args:ident, $parent:ident, $child_id:ident, $ty_cost_per_byte:expr) => {{ let child_ty = $ty_args.pop().unwrap(); native_charge_gas_early_exit!( $context, @@ -50,11 +54,9 @@ macro_rules! get_or_fetch_object { } }; - let object_runtime: &mut ObjectRuntime = $context.extensions_mut().get_mut(); - object_runtime.get_or_fetch_child_object( + $object_runtime.get_or_fetch_child_object( $parent, $child_id, - &child_ty, &layout, &annotated_layout, MoveObjectType::from(tag), @@ -194,7 +196,7 @@ pub fn add_child_object( ); // TODO remove this copy_value, which will require VM changes - let child_id = get_object_id(child.copy_value().unwrap()) + let child_id = get_object_id(child.copy_value()) .unwrap() .value_as::() .unwrap() @@ -228,14 +230,13 @@ pub fn add_child_object( * struct_tag_size.into() ); - let object_runtime: &mut ObjectRuntime = context.extensions_mut().get_mut(); - object_runtime.add_child_object( - parent, - child_id, - &child_ty, - MoveObjectType::from(tag), - child, - )?; + { + let mut object_runtime: RefMut<'_, ObjectRuntime<'_>> = context + .extensions_mut() + .get::>() + .get_mut(); + object_runtime.add_child_object(parent, child_id, MoveObjectType::from(tag), child)?; + }; Ok(NativeResult::ok(context.gas_used(), smallvec![])) } @@ -285,8 +286,14 @@ pub fn borrow_child_object( .into(); assert!(args.is_empty()); + // NB: We need to borrow the runtime and grab the mutable reference and then pass this into + // `get_or_fetch_object` so that the lifetime of the returned object is tied to the lifetime of + // the runtime reference we are creating here and the borrow checker will be happy with us. + let object_runtime: &NativeContextMut<'_, ObjectRuntime<'_>> = context.extensions().get(); + let mut object_runtime_mut: RefMut<'_, ObjectRuntime<'_>> = object_runtime.get_mut(); let global_value_result = get_or_fetch_object!( context, + object_runtime_mut, ty_args, parent, child_id, @@ -353,8 +360,14 @@ pub fn remove_child_object( let child_id = pop_arg!(args, AccountAddress).into(); let parent = pop_arg!(args, AccountAddress).into(); assert!(args.is_empty()); + // NB: We need to borrow the runtime and grab the mutable reference and then pass this into + // `get_or_fetch_object` so that the lifetime of the returned object is tied to the lifetime of + // the runtime reference we are creating here and the borrow checker will be happy with us. + let object_runtime: &NativeContextMut<'_, ObjectRuntime<'_>> = context.extensions().get(); + let mut object_runtime_mut: RefMut<'_, ObjectRuntime<'_>> = object_runtime.get_mut(); let global_value_result = get_or_fetch_object!( context, + object_runtime_mut, ty_args, parent, child_id, @@ -415,8 +428,11 @@ pub fn has_child_object( let child_id = pop_arg!(args, AccountAddress).into(); let parent = pop_arg!(args, AccountAddress).into(); - let object_runtime: &mut ObjectRuntime = context.extensions_mut().get_mut(); - let has_child = object_runtime.child_object_exists(parent, child_id)?; + let has_child = { + let mut object_runtime: RefMut<'_, ObjectRuntime<'_>> = context.extensions().get::>().get_mut(); + object_runtime.child_object_exists(parent, child_id)? + }; + Ok(NativeResult::ok( context.gas_used(), smallvec![Value::bool(has_child)], @@ -485,12 +501,11 @@ pub fn has_child_object_with_ty( * u64::from(tag.abstract_size_for_gas_metering()).into() ); - let object_runtime: &mut ObjectRuntime = context.extensions_mut().get_mut(); - let has_child = object_runtime.child_object_exists_and_has_type( - parent, - child_id, - &MoveObjectType::from(tag), - )?; + let has_child = { + let mut object_runtime: RefMut<'_, ObjectRuntime<'_>> = context.extensions().get::>().get_mut(); + object_runtime.child_object_exists_and_has_type(parent, child_id, &MoveObjectType::from(tag))? + }; + Ok(NativeResult::ok( context.gas_used(), smallvec![Value::bool(has_child)], diff --git a/sui-execution/latest/sui-move-natives/src/event.rs b/sui-execution/latest/sui-move-natives/src/event.rs index b74a465ede13e9..10bcc55829a8fc 100644 --- a/sui-execution/latest/sui-move-natives/src/event.rs +++ b/sui-execution/latest/sui-move-natives/src/event.rs @@ -4,12 +4,13 @@ use crate::{legacy_test_cost, object_runtime::ObjectRuntime, NativesCostTable}; use move_binary_format::errors::{PartialVMError, PartialVMResult}; use move_core_types::{gas_algebra::InternalGas, language_storage::TypeTag, vm_status::StatusCode}; -use move_vm_runtime::{native_charge_gas_early_exit, native_functions::NativeContext}; -use move_vm_types::{ - loaded_data::runtime_types::Type, natives::function::NativeResult, values::Value, +use move_vm_runtime::{ + execution::{values::Value, Type}, + natives::{extensions::NativeContextMut, functions::NativeResult}, }; +use move_vm_runtime::{native_charge_gas_early_exit, natives::functions::NativeContext}; use smallvec::smallvec; -use std::collections::VecDeque; +use std::{cell::RefMut, collections::VecDeque}; use sui_types::error::VMMemoryLimitExceededSubStatusCode; #[derive(Clone, Debug)] @@ -74,7 +75,10 @@ pub fn emit( * u64::from(tag_size).into() ); - let obj_runtime: &mut ObjectRuntime = context.extensions_mut().get_mut(); + let mut obj_runtime: RefMut<'_, ObjectRuntime<'_>> = context + .extensions() + .get::>() + .get_mut(); let max_event_emit_size = obj_runtime.protocol_config.max_event_emit_size(); let ev_size = u64::from(tag_size + event_value_size); // Check if the event size is within the limit @@ -112,9 +116,7 @@ pub fn emit( event_emit_cost_params.event_emit_output_cost_per_byte * ev_size.into() ); - let obj_runtime: &mut ObjectRuntime = context.extensions_mut().get_mut(); - - obj_runtime.emit_event(ty, *tag, event_value)?; + obj_runtime.emit_event(*tag, event_value)?; Ok(NativeResult::ok(context.gas_used(), smallvec![])) } @@ -144,13 +146,17 @@ pub fn get_events_by_type( let specified_ty = ty_args.pop().unwrap(); assert!(args.is_empty()); let object_runtime_ref: &ObjectRuntime = context.extensions().get(); + let specified_type_tag = match context.type_to_type_tag(&specified_ty)? { + TypeTag::Struct(s) => *s, + _ => return Ok(NativeResult::ok(legacy_test_cost(), smallvec![])), + }; let matched_events = object_runtime_ref .state .events() .iter() - .filter_map(|(ty, _, event)| { - if specified_ty == *ty { - Some(event.copy_value().unwrap()) + .filter_map(|(ty, event)| { + if specified_type_tag == *ty { + Some(event.copy_value()) } else { None } diff --git a/sui-execution/latest/sui-move-natives/src/lib.rs b/sui-execution/latest/sui-move-natives/src/lib.rs index 193d2f1d51935b..546c4eb4cd9f68 100644 --- a/sui-execution/latest/sui-move-natives/src/lib.rs +++ b/sui-execution/latest/sui-move-natives/src/lib.rs @@ -51,12 +51,16 @@ use move_core_types::{ runtime_value as R, vm_status::StatusCode, }; -use move_stdlib_natives::{self as MSN, GasParameters}; -use move_vm_runtime::native_functions::{NativeContext, NativeFunction, NativeFunctionTable}; -use move_vm_types::{ - loaded_data::runtime_types::Type, - natives::function::NativeResult, - values::{Struct, Value}, +use move_vm_runtime::natives::{ + functions::{NativeContext, NativeFunction, NativeFunctionTable}, + move_stdlib::{self as MSN, GasParameters}, +}; +use move_vm_runtime::{ + execution::{ + values::{Struct, Value}, + Type, + }, + natives::functions::NativeResult, }; use std::sync::Arc; use sui_protocol_config::ProtocolConfig; @@ -1093,11 +1097,13 @@ pub fn all_natives(silent: bool, protocol_config: &ProtocolConfig) -> NativeFunc ) }) .chain(sui_framework_natives_iter) - .chain(move_stdlib_natives::all_natives( - MOVE_STDLIB_ADDRESS, - make_stdlib_gas_params_for_protocol_config(protocol_config), - silent, - )) + .chain( + move_vm_runtime::natives::move_stdlib::stdlib_native_function_table( + MOVE_STDLIB_ADDRESS, + make_stdlib_gas_params_for_protocol_config(protocol_config), + silent, + ), + ) .collect() } diff --git a/sui-execution/latest/sui-move-natives/src/object.rs b/sui-execution/latest/sui-move-natives/src/object.rs index dc7a692faef760..2fcdbda4ad4c31 100644 --- a/sui-execution/latest/sui-move-natives/src/object.rs +++ b/sui-execution/latest/sui-move-natives/src/object.rs @@ -4,15 +4,17 @@ use crate::{object_runtime::ObjectRuntime, NativesCostTable}; use move_binary_format::errors::PartialVMResult; use move_core_types::{account_address::AccountAddress, gas_algebra::InternalGas}; -use move_vm_runtime::{native_charge_gas_early_exit, native_functions::NativeContext}; -use move_vm_types::{ - loaded_data::runtime_types::Type, - natives::function::NativeResult, +use move_vm_runtime::{ + execution::{ + values::{StructRef, Value}, + Type, + }, + natives::{extensions::NativeContextMut, functions::NativeResult}, pop_arg, - values::{StructRef, Value}, }; +use move_vm_runtime::{native_charge_gas_early_exit, natives::functions::NativeContext}; use smallvec::smallvec; -use std::collections::VecDeque; +use std::{cell::RefMut, collections::VecDeque}; #[derive(Clone)] pub struct BorrowUidCostParams { @@ -78,7 +80,10 @@ pub fn delete_impl( // unwrap safe because the interface of native function guarantees it. let uid_bytes = pop_arg!(args, AccountAddress); - let obj_runtime: &mut ObjectRuntime = context.extensions_mut().get_mut(); + let mut obj_runtime: RefMut = context + .extensions() + .get::>() + .get_mut(); obj_runtime.delete_id(uid_bytes.into())?; Ok(NativeResult::ok(context.gas_used(), smallvec![])) } @@ -115,7 +120,10 @@ pub fn record_new_uid( // unwrap safe because the interface of native function guarantees it. let uid_bytes = pop_arg!(args, AccountAddress); - let obj_runtime: &mut ObjectRuntime = context.extensions_mut().get_mut(); + let mut obj_runtime: RefMut = context + .extensions() + .get::>() + .get_mut(); obj_runtime.new_id(uid_bytes.into())?; Ok(NativeResult::ok(context.gas_used(), smallvec![])) } diff --git a/sui-execution/latest/sui-move-natives/src/object_runtime/mod.rs b/sui-execution/latest/sui-move-natives/src/object_runtime/mod.rs index 943404771aef06..ae0b4535aafe2e 100644 --- a/sui-execution/latest/sui-move-natives/src/object_runtime/mod.rs +++ b/sui-execution/latest/sui-move-natives/src/object_runtime/mod.rs @@ -18,8 +18,7 @@ use move_core_types::{ runtime_value as R, vm_status::StatusCode, }; -use move_vm_types::{ - loaded_data::runtime_types::Type, +use move_vm_runtime::execution::{ values::{GlobalValue, Value}, }; use object_store::{ActiveChildObject, ChildObjectStore}; @@ -37,7 +36,7 @@ use sui_types::{ metrics::LimitsMetrics, object::{MoveObject, Owner}, storage::ChildObjectResolver, - SUI_AUTHENTICATOR_STATE_OBJECT_ID, SUI_BRIDGE_OBJECT_ID, SUI_CLOCK_OBJECT_ID, + TypeTag, SUI_AUTHENTICATOR_STATE_OBJECT_ID, SUI_BRIDGE_OBJECT_ID, SUI_CLOCK_OBJECT_ID, SUI_DENY_LIST_OBJECT_ID, SUI_RANDOMNESS_STATE_OBJECT_ID, SUI_SYSTEM_STATE_OBJECT_ID, }; use tracing::error; @@ -55,11 +54,11 @@ type Set = IndexSet; pub(crate) struct TestInventories { pub(crate) objects: BTreeMap, // address inventories. Most recent objects are at the back of the set - pub(crate) address_inventories: BTreeMap>>, + pub(crate) address_inventories: BTreeMap>>, // global inventories.Most recent objects are at the back of the set - pub(crate) shared_inventory: BTreeMap>, - pub(crate) immutable_inventory: BTreeMap>, - pub(crate) taken_immutable_values: BTreeMap>, + pub(crate) shared_inventory: BTreeMap>, + pub(crate) immutable_inventory: BTreeMap>, + pub(crate) taken_immutable_values: BTreeMap>, // object has been taken from the inventory pub(crate) taken: BTreeMap, // allocated receiving tickets @@ -72,8 +71,8 @@ pub struct LoadedRuntimeObject { } pub struct RuntimeResults { - pub writes: IndexMap, - pub user_events: Vec<(Type, StructTag, Value)>, + pub writes: IndexMap, + pub user_events: Vec<(StructTag, Value)>, // Loaded child objects, their loaded version/digest and whether they were modified. pub loaded_child_objects: BTreeMap, pub created_object_ids: Set, @@ -89,8 +88,8 @@ pub(crate) struct ObjectRuntimeState { deleted_ids: Set, // transfers to a new owner (shared, immutable, object, or account address) // TODO these struct tags can be removed if type_to_type_tag was exposed in the session - transfers: IndexMap, - events: Vec<(Type, StructTag, Value)>, + transfers: IndexMap, + events: Vec<(StructTag, Value)>, // total size of events emitted so far total_events_size: u64, received: IndexMap, @@ -240,10 +239,10 @@ impl<'a> ObjectRuntime<'a> { pub fn transfer( &mut self, owner: Owner, - ty: Type, + ty: MoveObjectType, obj: Value, ) -> PartialVMResult { - let id: ObjectID = get_object_id(obj.copy_value()?)? + let id: ObjectID = get_object_id(obj.copy_value())? .value_as::()? .into(); // - An object is new if it is contained in the new ids or if it is one of the objects @@ -300,15 +299,15 @@ impl<'a> ObjectRuntime<'a> { Ok(transfer_result) } - pub fn emit_event(&mut self, ty: Type, tag: StructTag, event: Value) -> PartialVMResult<()> { + pub fn emit_event(&mut self, tag: StructTag, event: Value) -> PartialVMResult<()> { if self.state.events.len() >= (self.protocol_config.max_num_event_emit() as usize) { return Err(max_event_error(self.protocol_config.max_num_event_emit())); } - self.state.events.push((ty, tag, event)); + self.state.events.push((tag, event)); Ok(()) } - pub fn take_user_events(&mut self) -> Vec<(Type, StructTag, Value)> { + pub fn take_user_events(&mut self) -> Vec<(StructTag, Value)> { std::mem::take(&mut self.state.events) } @@ -335,7 +334,6 @@ impl<'a> ObjectRuntime<'a> { parent: ObjectID, child: ObjectID, child_version: SequenceNumber, - child_ty: &Type, child_layout: &R::MoveTypeLayout, child_fully_annotated_layout: &MoveTypeLayout, child_move_type: MoveObjectType, @@ -344,7 +342,6 @@ impl<'a> ObjectRuntime<'a> { parent, child, child_version, - child_ty, child_layout, child_fully_annotated_layout, child_move_type, @@ -371,7 +368,6 @@ impl<'a> ObjectRuntime<'a> { &mut self, parent: ObjectID, child: ObjectID, - child_ty: &Type, child_layout: &R::MoveTypeLayout, child_fully_annotated_layout: &MoveTypeLayout, child_move_type: MoveObjectType, @@ -379,7 +375,6 @@ impl<'a> ObjectRuntime<'a> { let res = self.child_object_store.get_or_fetch_object( parent, child, - child_ty, child_layout, child_fully_annotated_layout, child_move_type, @@ -394,26 +389,23 @@ impl<'a> ObjectRuntime<'a> { &mut self, parent: ObjectID, child: ObjectID, - child_ty: &Type, child_move_type: MoveObjectType, child_value: Value, ) -> PartialVMResult<()> { self.child_object_store - .add_object(parent, child, child_ty, child_move_type, child_value) + .add_object(parent, child, child_move_type, child_value) } pub(crate) fn config_setting_unsequenced_read( &mut self, config_id: ObjectID, name_df_id: ObjectID, - field_setting_ty: &Type, field_setting_layout: &R::MoveTypeLayout, field_setting_object_type: &MoveObjectType, ) -> Option { match self.child_object_store.config_setting_unsequenced_read( config_id, name_df_id, - field_setting_ty, field_setting_layout, field_setting_object_type, ) { @@ -641,7 +633,7 @@ impl ObjectRuntimeState { }) } - pub fn events(&self) -> &[(Type, StructTag, Value)] { + pub fn events(&self) -> &[(StructTag, Value)] { &self.events } diff --git a/sui-execution/latest/sui-move-natives/src/object_runtime/object_store.rs b/sui-execution/latest/sui-move-natives/src/object_runtime/object_store.rs index 323905b909dc5d..5a087a106af77d 100644 --- a/sui-execution/latest/sui-move-natives/src/object_runtime/object_store.rs +++ b/sui-execution/latest/sui-move-natives/src/object_runtime/object_store.rs @@ -6,10 +6,7 @@ use move_binary_format::errors::{PartialVMError, PartialVMResult}; use move_core_types::{ annotated_value as A, effects::Op, runtime_value as R, vm_status::StatusCode, }; -use move_vm_types::{ - loaded_data::runtime_types::Type, - values::{GlobalValue, StructRef, Value}, -}; +use move_vm_runtime::execution::values::{GlobalValue, StructRef, Value}; use std::{ collections::{btree_map, BTreeMap}, sync::Arc, @@ -27,7 +24,6 @@ use sui_types::{ pub(super) struct ChildObject { pub(super) owner: ObjectID, - pub(super) ty: Type, pub(super) move_type: MoveObjectType, pub(super) value: GlobalValue, } @@ -35,7 +31,6 @@ pub(super) struct ChildObject { pub(crate) struct ActiveChildObject<'a> { pub(crate) id: &'a ObjectID, pub(crate) owner: &'a ObjectID, - pub(crate) ty: &'a Type, pub(crate) move_type: &'a MoveObjectType, pub(crate) copied_value: Option, } @@ -50,7 +45,7 @@ struct ConfigSetting { #[derive(Debug)] pub(crate) struct ChildObjectEffect { pub(super) owner: ObjectID, - pub(super) ty: Type, + pub(super) ty: MoveObjectType, pub(super) effect: Op, } @@ -287,22 +282,21 @@ impl<'a> Inner<'a> { &mut self, parent: ObjectID, child: ObjectID, - child_ty: &Type, child_ty_layout: &R::MoveTypeLayout, child_ty_fully_annotated_layout: &A::MoveTypeLayout, - child_move_type: &MoveObjectType, - ) -> PartialVMResult> { + child_move_type: MoveObjectType, + ) -> PartialVMResult> { let obj = match self.get_or_fetch_object_from_store(parent, child)? { None => { return Ok(ObjectResult::Loaded(( - child_ty.clone(), + child_move_type.clone(), GlobalValue::none(), ))) } Some(obj) => obj, }; // object exists, but the type does not match - if obj.type_() != child_move_type { + if obj.type_() != &child_move_type { return Ok(ObjectResult::MismatchedType); } // generate a GlobalValue @@ -341,16 +335,15 @@ impl<'a> Inner<'a> { } } } - Ok(ObjectResult::Loaded((child_ty.clone(), global_value))) + Ok(ObjectResult::Loaded((child_move_type, global_value))) } } fn deserialize_move_object( obj: &MoveObject, - child_ty: &Type, child_ty_layout: &R::MoveTypeLayout, child_move_type: MoveObjectType, -) -> PartialVMResult> { +) -> PartialVMResult> { let child_id = obj.id(); // object exists, but the type does not match if obj.type_() != &child_move_type { @@ -366,11 +359,7 @@ fn deserialize_move_object( ) } }; - Ok(ObjectResult::Loaded(( - child_ty.clone(), - child_move_type, - value, - ))) + Ok(ObjectResult::Loaded((child_move_type, value))) } impl<'a> ChildObjectStore<'a> { @@ -405,7 +394,6 @@ impl<'a> ChildObjectStore<'a> { parent: ObjectID, child: ObjectID, child_version: SequenceNumber, - child_ty: &Type, child_layout: &R::MoveTypeLayout, child_fully_annotated_layout: &A::MoveTypeLayout, child_move_type: MoveObjectType, @@ -418,9 +406,9 @@ impl<'a> ChildObjectStore<'a> { }; Ok(Some( - match deserialize_move_object(&obj, child_ty, child_layout, child_move_type)? { + match deserialize_move_object(&obj, child_layout, child_move_type)? { ObjectResult::MismatchedType => (ObjectResult::MismatchedType, obj_meta), - ObjectResult::Loaded((_, _, v)) => { + ObjectResult::Loaded((_, v)) => { // Find all UIDs inside of the value and update the object parent maps with the contained // UIDs in the received value. They should all have an upper bound version as the receiving object. // Only do this if we successfully load the object though. @@ -479,7 +467,6 @@ impl<'a> ChildObjectStore<'a> { &mut self, parent: ObjectID, child: ObjectID, - child_ty: &Type, child_layout: &R::MoveTypeLayout, child_fully_annotated_layout: &A::MoveTypeLayout, child_move_type: MoveObjectType, @@ -490,10 +477,9 @@ impl<'a> ChildObjectStore<'a> { let (ty, value) = match self.inner.fetch_object_impl( parent, child, - child_ty, child_layout, child_fully_annotated_layout, - &child_move_type, + child_move_type, )? { ObjectResult::MismatchedType => return Ok(ObjectResult::MismatchedType), ObjectResult::Loaded(res) => res, @@ -523,8 +509,7 @@ impl<'a> ChildObjectStore<'a> { e.insert(ChildObject { owner: parent, - ty, - move_type: child_move_type, + move_type: ty, value, }) } @@ -543,7 +528,6 @@ impl<'a> ChildObjectStore<'a> { &mut self, parent: ObjectID, child: ObjectID, - child_ty: &Type, child_move_type: MoveObjectType, child_value: Value, ) -> PartialVMResult<()> { @@ -568,7 +552,10 @@ impl<'a> ChildObjectStore<'a> { )); }; - let mut value = if let Some(ChildObject { ty, value, .. }) = self.store.remove(&child) { + let mut value = if let Some(ChildObject { + move_type, value, .. + }) = self.store.remove(&child) + { if value.exists()? { return Err( PartialVMError::new(StatusCode::UNKNOWN_INVARIANT_VIOLATION_ERROR) @@ -583,7 +570,8 @@ impl<'a> ChildObjectStore<'a> { } if self.inner.protocol_config.loaded_child_object_format() { // double check format did not change - if !self.inner.protocol_config.loaded_child_object_format_type() && child_ty != &ty + if !self.inner.protocol_config.loaded_child_object_format_type() + && child_move_type != move_type { let msg = format!("Type changed for child {child} when setting the value back"); return Err( @@ -607,7 +595,6 @@ impl<'a> ChildObjectStore<'a> { } let child_object = ChildObject { owner: parent, - ty: child_ty.clone(), move_type: child_move_type, value, }; @@ -619,7 +606,6 @@ impl<'a> ChildObjectStore<'a> { &mut self, config_id: ObjectID, name_df_id: ObjectID, - _field_setting_ty: &Type, field_setting_layout: &R::MoveTypeLayout, field_setting_object_type: &MoveObjectType, ) -> PartialVMResult>> { @@ -672,11 +658,7 @@ impl<'a> ChildObjectStore<'a> { setting } }; - let value = setting.value.copy_value().map_err(|e| { - PartialVMError::new(StatusCode::UNKNOWN_INVARIANT_VIOLATION_ERROR).with_message( - format!("Failed to copy value for config setting {child}, with error {e}",), - ) - })?; + let value = setting.value.copy_value(); Ok(ObjectResult::Loaded(Some(value))) } @@ -720,8 +702,7 @@ impl<'a> ChildObjectStore<'a> { .filter_map(|(id, child_object)| { let ChildObject { owner, - ty, - move_type: _, + move_type: ty, value, } = child_object; let effect = value.into_effect()?; @@ -750,7 +731,6 @@ impl<'a> ChildObjectStore<'a> { ActiveChildObject { id, owner: &child_object.owner, - ty: &child_object.ty, move_type: &child_object.move_type, copied_value: copied_child_value, } diff --git a/sui-execution/latest/sui-move-natives/src/random.rs b/sui-execution/latest/sui-move-natives/src/random.rs index 3281edbd839928..db5fad76ef1a48 100644 --- a/sui-execution/latest/sui-move-natives/src/random.rs +++ b/sui-execution/latest/sui-move-natives/src/random.rs @@ -3,10 +3,8 @@ use crate::legacy_test_cost; use move_binary_format::errors::PartialVMResult; -use move_vm_runtime::native_functions::NativeContext; -use move_vm_types::{ - loaded_data::runtime_types::Type, natives::function::NativeResult, values::Value, -}; +use move_vm_runtime::execution::{values::Value, Type}; +use move_vm_runtime::natives::functions::{NativeContext, NativeResult}; use rand::Rng; use smallvec::smallvec; use std::collections::VecDeque; diff --git a/sui-execution/latest/sui-move-natives/src/test_scenario.rs b/sui-execution/latest/sui-move-natives/src/test_scenario.rs index 12926664855711..5611a797d018f9 100644 --- a/sui-execution/latest/sui-move-natives/src/test_scenario.rs +++ b/sui-execution/latest/sui-move-natives/src/test_scenario.rs @@ -15,22 +15,26 @@ use move_core_types::{ language_storage::StructTag, vm_status::StatusCode, }; -use move_vm_runtime::native_functions::NativeContext; -use move_vm_types::{ - loaded_data::runtime_types::Type, - natives::function::NativeResult, +use move_vm_runtime::natives::{ + extensions::NativeContextMut, + functions::{NativeContext, NativeResult}, +}; +use move_vm_runtime::{ + execution::{ + values::{self, StructRef, Value}, + Type, + }, pop_arg, - values::{self, StructRef, Value}, }; use smallvec::smallvec; use std::{ borrow::Borrow, - cell::RefCell, + cell::{RefCell, RefMut}, collections::{BTreeMap, BTreeSet, VecDeque}, thread::LocalKey, }; use sui_types::{ - base_types::{ObjectID, SequenceNumber, SuiAddress}, + base_types::{MoveObjectType, ObjectID, SequenceNumber, SuiAddress}, config, digests::{ObjectDigest, TransactionDigest}, dynamic_field::DynamicFieldInfo, @@ -96,7 +100,10 @@ pub fn end_transaction( ) -> PartialVMResult { assert!(ty_args.is_empty()); assert!(args.is_empty()); - let object_runtime_ref: &mut ObjectRuntime = context.extensions_mut().get_mut(); + let mut object_runtime_ref: RefMut = context + .extensions() + .get::>() + .get_mut(); let taken_shared_or_imm: BTreeMap<_, _> = object_runtime_ref .test_inventories .taken @@ -154,7 +161,6 @@ pub fn end_transaction( )); } }; - let object_runtime_ref: &mut ObjectRuntime = context.extensions_mut().get_mut(); let all_active_child_objects_with_values = object_runtime_ref .all_active_child_objects() .filter(|child| child.copied_value.is_some()) @@ -192,7 +198,7 @@ pub fn end_transaction( let mut written = vec![]; for (id, (owner, ty, value)) in writes { // write configs to cache - new_object_values.insert(id, (ty.clone(), value.copy_value().unwrap())); + new_object_values.insert(id, (ty.clone(), value.copy_value())); transferred.push((id, owner.clone())); incorrect_shared_or_imm_handling = incorrect_shared_or_imm_handling || taken_shared_or_imm @@ -210,7 +216,7 @@ pub fn end_transaction( .address_inventories .entry(a) .or_default() - .entry(ty) + .entry(ty.into()) .or_default() .insert(id); } @@ -218,14 +224,14 @@ pub fn end_transaction( Owner::Shared { .. } => { inventories .shared_inventory - .entry(ty) + .entry(ty.into()) .or_default() .insert(id); } Owner::Immutable => { inventories .immutable_inventory - .entry(ty) + .entry(ty.into()) .or_default() .insert(id); } @@ -236,7 +242,7 @@ pub fn end_transaction( .address_inventories .entry(*authenticator.as_single_owner()) .or_default() - .entry(ty) + .entry(ty.into()) .or_default() .insert(id); } @@ -269,7 +275,10 @@ pub fn end_transaction( } // find all wrapped objects let mut all_wrapped = BTreeSet::new(); - let object_runtime_ref: &ObjectRuntime = context.extensions().get(); + let mut object_runtime_ref: RefMut = context + .extensions() + .get::>() + .get_mut(); find_all_wrapped_objects( context, &mut all_wrapped, @@ -282,7 +291,7 @@ pub fn end_transaction( &mut all_wrapped, object_runtime_ref .all_active_child_objects() - .filter_map(|child| Some((child.id, child.ty, child.copied_value?))), + .filter_map(|child| Some((child.id, child.move_type, child.copied_value?))), ); // mark as "incorrect" if a shared/imm object was wrapped or is a child object incorrect_shared_or_imm_handling = incorrect_shared_or_imm_handling @@ -303,7 +312,6 @@ pub fn end_transaction( } // new input objects are remaining taken objects not written/deleted - let object_runtime_ref: &mut ObjectRuntime = context.extensions_mut().get_mut(); let mut config_settings = vec![]; for child in object_runtime_ref.all_active_child_objects() { let s: StructTag = child.move_type.clone().into(); @@ -334,7 +342,7 @@ pub fn end_transaction( if let Some(prev_value) = object_runtime_ref .test_inventories .taken_immutable_values - .get(&ty) + .get(&ty.into()) .and_then(|values| values.get(&id)) { if !value.equals(prev_value)? { @@ -379,14 +387,19 @@ pub fn take_from_address_by_id( let account: SuiAddress = pop_arg!(args, AccountAddress).into(); pop_arg!(args, StructRef); assert!(args.is_empty()); - let object_runtime: &mut ObjectRuntime = context.extensions_mut().get_mut(); + let specified_obj_ty = context.type_to_type_tag(&specified_ty)?; + // TODO(tzakian): double check this + let object_runtime: &mut ObjectRuntime = &mut context + .extensions() + .get::>() + .get_mut(); let inventories = &mut object_runtime.test_inventories; let res = take_from_inventory( |x| { inventories .address_inventories .get(&account) - .and_then(|inv| inv.get(&specified_ty)) + .and_then(|inv| inv.get(&specified_obj_ty)) .map(|s| s.contains(x)) .unwrap_or(false) }, @@ -411,12 +424,16 @@ pub fn ids_for_address( let specified_ty = get_specified_ty(ty_args); let account: SuiAddress = pop_arg!(args, AccountAddress).into(); assert!(args.is_empty()); - let object_runtime: &mut ObjectRuntime = context.extensions_mut().get_mut(); + let specified_obj_ty = context.type_to_type_tag(&specified_ty)?; + let mut object_runtime: RefMut = context + .extensions() + .get::>() + .get_mut(); let inventories = &mut object_runtime.test_inventories; let ids = inventories .address_inventories .get(&account) - .and_then(|inv| inv.get(&specified_ty)) + .and_then(|inv| inv.get(&specified_obj_ty)) .map(|s| s.iter().map(|id| pack_id(*id)).collect::>()) .unwrap_or_default(); let ids_vector = Value::vector_for_testing_only(ids); @@ -432,11 +449,15 @@ pub fn most_recent_id_for_address( let specified_ty = get_specified_ty(ty_args); let account: SuiAddress = pop_arg!(args, AccountAddress).into(); assert!(args.is_empty()); - let object_runtime: &mut ObjectRuntime = context.extensions_mut().get_mut(); + let specified_obj_ty = context.type_to_type_tag(&specified_ty)?; + let mut object_runtime: RefMut = context + .extensions() + .get::>() + .get_mut(); let inventories = &mut object_runtime.test_inventories; let most_recent_id = match inventories.address_inventories.get(&account) { None => pack_option(None), - Some(inv) => most_recent_at_ty(&inventories.taken, inv, specified_ty), + Some(inv) => most_recent_at_ty(&inventories.taken, inv, specified_obj_ty), }; Ok(NativeResult::ok( legacy_test_cost(), @@ -454,7 +475,10 @@ pub fn was_taken_from_address( let id = pop_id(&mut args)?; let account: SuiAddress = pop_arg!(args, AccountAddress).into(); assert!(args.is_empty()); - let object_runtime: &mut ObjectRuntime = context.extensions_mut().get_mut(); + let mut object_runtime: RefMut = context + .extensions() + .get::>() + .get_mut(); let inventories = &mut object_runtime.test_inventories; let was_taken = inventories .taken @@ -477,13 +501,17 @@ pub fn take_immutable_by_id( let id = pop_id(&mut args)?; pop_arg!(args, StructRef); assert!(args.is_empty()); - let object_runtime: &mut ObjectRuntime = context.extensions_mut().get_mut(); + let specified_obj_ty = context.type_to_type_tag(&specified_ty)?; + let object_runtime: &mut ObjectRuntime = &mut context + .extensions() + .get::>() + .get_mut(); let inventories = &mut object_runtime.test_inventories; let res = take_from_inventory( |x| { inventories .immutable_inventory - .get(&specified_ty) + .get(&specified_obj_ty) .map(|s| s.contains(x)) .unwrap_or(false) }, @@ -497,9 +525,9 @@ pub fn take_immutable_by_id( Ok(value) => { inventories .taken_immutable_values - .entry(specified_ty) + .entry(specified_obj_ty) .or_default() - .insert(id, value.copy_value().unwrap()); + .insert(id, value.copy_value()); NativeResult::ok(legacy_test_cost(), smallvec![value]) } Err(native_err) => native_err, @@ -514,12 +542,16 @@ pub fn most_recent_immutable_id( ) -> PartialVMResult { let specified_ty = get_specified_ty(ty_args); assert!(args.is_empty()); - let object_runtime: &mut ObjectRuntime = context.extensions_mut().get_mut(); + let specified_obj_ty = context.type_to_type_tag(&specified_ty)?; + let mut object_runtime: RefMut = context + .extensions() + .get::>() + .get_mut(); let inventories = &mut object_runtime.test_inventories; let most_recent_id = most_recent_at_ty( &inventories.taken, &inventories.immutable_inventory, - specified_ty, + specified_obj_ty, ); Ok(NativeResult::ok( legacy_test_cost(), @@ -536,7 +568,10 @@ pub fn was_taken_immutable( assert!(ty_args.is_empty()); let id = pop_id(&mut args)?; assert!(args.is_empty()); - let object_runtime: &mut ObjectRuntime = context.extensions_mut().get_mut(); + let mut object_runtime: RefMut = context + .extensions() + .get::>() + .get_mut(); let inventories = &mut object_runtime.test_inventories; let was_taken = inventories .taken @@ -559,13 +594,17 @@ pub fn take_shared_by_id( let id = pop_id(&mut args)?; pop_arg!(args, StructRef); assert!(args.is_empty()); - let object_runtime: &mut ObjectRuntime = context.extensions_mut().get_mut(); + let specified_obj_ty = context.type_to_type_tag(&specified_ty)?; + let object_runtime: &mut ObjectRuntime = &mut context + .extensions() + .get::>() + .get_mut(); let inventories = &mut object_runtime.test_inventories; let res = take_from_inventory( |x| { inventories .shared_inventory - .get(&specified_ty) + .get(&specified_obj_ty) .map(|s| s.contains(x)) .unwrap_or(false) }, @@ -589,12 +628,16 @@ pub fn most_recent_id_shared( ) -> PartialVMResult { let specified_ty = get_specified_ty(ty_args); assert!(args.is_empty()); - let object_runtime: &mut ObjectRuntime = context.extensions_mut().get_mut(); + let specified_obj_ty = context.type_to_type_tag(&specified_ty)?; + let mut object_runtime: RefMut = context + .extensions() + .get::>() + .get_mut(); let inventories = &mut object_runtime.test_inventories; let most_recent_id = most_recent_at_ty( &inventories.taken, &inventories.shared_inventory, - specified_ty, + specified_obj_ty, ); Ok(NativeResult::ok( legacy_test_cost(), @@ -611,7 +654,10 @@ pub fn was_taken_shared( assert!(ty_args.is_empty()); let id = pop_id(&mut args)?; assert!(args.is_empty()); - let object_runtime: &mut ObjectRuntime = context.extensions_mut().get_mut(); + let mut object_runtime: RefMut = context + .extensions() + .get::>() + .get_mut(); let inventories = &mut object_runtime.test_inventories; let was_taken = inventories .taken @@ -639,7 +685,10 @@ pub fn allocate_receiving_ticket_for_object( E_UNABLE_TO_ALLOCATE_RECEIVING_TICKET, )); }; - let object_runtime: &mut ObjectRuntime = context.extensions_mut().get_mut(); + let mut object_runtime: RefMut = context + .extensions() + .get::>() + .get_mut(); let object_version = SequenceNumber::new(); let inventories = &mut object_runtime.test_inventories; if inventories.allocated_tickets.contains_key(&id) { @@ -716,7 +765,10 @@ pub fn deallocate_receiving_ticket_for_object( ) -> PartialVMResult { let id = pop_id(&mut args)?; - let object_runtime: &mut ObjectRuntime = context.extensions_mut().get_mut(); + let mut object_runtime: RefMut = context + .extensions() + .get::>() + .get_mut(); let inventories = &mut object_runtime.test_inventories; // Deallocate the ticket -- we should never hit this scenario let Some((_, value)) = inventories.allocated_tickets.remove(&id) else { @@ -766,21 +818,21 @@ fn take_from_inventory( taken.insert(id, owner.clone()); input_objects.insert(id, owner); let obj = obj_opt.unwrap(); - Ok(obj.copy_value().unwrap()) + Ok(obj.copy_value()) } fn most_recent_at_ty( taken: &BTreeMap, - inv: &BTreeMap>, - ty: Type, + inv: &BTreeMap>, + ty: TypeTag, ) -> Value { pack_option(most_recent_at_ty_opt(taken, inv, ty)) } fn most_recent_at_ty_opt( taken: &BTreeMap, - inv: &BTreeMap>, - ty: Type, + inv: &BTreeMap>, + ty: TypeTag, ) -> Option { let s = inv.get(&ty)?; let most_recent_id = s.iter().filter(|id| !taken.contains_key(id)).last()?; @@ -884,7 +936,7 @@ fn pack_option(opt: Option) -> Value { fn find_all_wrapped_objects<'a, 'i>( context: &NativeContext, ids: &'i mut BTreeSet, - new_object_values: impl IntoIterator)>, + new_object_values: impl IntoIterator)>, ) { #[derive(Copy, Clone)] enum LookingFor { @@ -960,12 +1012,14 @@ fn find_all_wrapped_objects<'a, 'i>( let uid = UID::layout(); for (_id, ty, value) in new_object_values { - let Ok(Some(layout)) = context.type_to_type_layout(ty) else { + let Ok(Some(layout)) = context.typetag_to_type_layout(&ty.clone().into()) else { debug_assert!(false); continue; }; - let Ok(Some(annotated_layout)) = context.type_to_fully_annotated_layout(ty) else { + let Ok(Some(annotated_layout)) = + context.typetag_to_annotated_type_layout(&ty.clone().into()) + else { debug_assert!(false); continue; }; diff --git a/sui-execution/latest/sui-move-natives/src/test_utils.rs b/sui-execution/latest/sui-move-natives/src/test_utils.rs index 7072e6c42b7ebe..4d9f482b79562d 100644 --- a/sui-execution/latest/sui-move-natives/src/test_utils.rs +++ b/sui-execution/latest/sui-move-natives/src/test_utils.rs @@ -4,10 +4,11 @@ use crate::{legacy_test_cost, types::is_otw_struct}; use move_binary_format::errors::PartialVMResult; use move_core_types::{gas_algebra::InternalGas, runtime_value::MoveTypeLayout}; -use move_vm_runtime::native_functions::NativeContext; -use move_vm_types::{ - loaded_data::runtime_types::Type, natives::function::NativeResult, values::Value, +use move_vm_runtime::execution::{ + values::{Struct, Value}, + Type, }; +use move_vm_runtime::natives::functions::{NativeContext, NativeResult}; use smallvec::smallvec; use std::collections::VecDeque; @@ -40,9 +41,7 @@ pub fn create_one_time_witness( if is_otw_struct(&struct_layout, &type_tag, hardened_check) { Ok(NativeResult::ok( legacy_test_cost(), - smallvec![Value::struct_(move_vm_types::values::Struct::pack(vec![ - Value::bool(true) - ]))], + smallvec![Value::struct_(Struct::pack(vec![Value::bool(true)]))], )) } else { Ok(NativeResult::err(InternalGas::new(1), 1)) diff --git a/sui-execution/latest/sui-move-natives/src/transfer.rs b/sui-execution/latest/sui-move-natives/src/transfer.rs index 2a1ba3e2443910..8893dc7ee93e54 100644 --- a/sui-execution/latest/sui-move-natives/src/transfer.rs +++ b/sui-execution/latest/sui-move-natives/src/transfer.rs @@ -11,11 +11,13 @@ use move_core_types::{ account_address::AccountAddress, gas_algebra::InternalGas, language_storage::TypeTag, vm_status::StatusCode, }; -use move_vm_runtime::{native_charge_gas_early_exit, native_functions::NativeContext}; -use move_vm_types::{ - loaded_data::runtime_types::Type, natives::function::NativeResult, pop_arg, values::Value, +use move_vm_runtime::natives::extensions::NativeContextMut; +use move_vm_runtime::{ + execution::values::Value, execution::Type, natives::functions::NativeResult, pop_arg, }; +use move_vm_runtime::{native_charge_gas_early_exit, natives::functions::NativeContext}; use smallvec::smallvec; +use std::cell::RefMut; use std::collections::VecDeque; use sui_types::{ base_types::{MoveObjectType, ObjectID, SequenceNumber}, @@ -60,7 +62,7 @@ pub fn receive_object_internal( let child_receiver_object_id = args.pop_back().unwrap(); let parent = pop_arg!(args, AccountAddress).into(); assert!(args.is_empty()); - let child_id: ObjectID = get_receiver_object_id(child_receiver_object_id.copy_value().unwrap()) + let child_id: ObjectID = get_receiver_object_id(child_receiver_object_id.copy_value()) .unwrap() .value_as::() .unwrap() @@ -74,12 +76,14 @@ pub fn receive_object_internal( )); }; - let object_runtime: &mut ObjectRuntime = context.extensions_mut().get_mut(); + let mut object_runtime: RefMut = context + .extensions() + .get::>() + .get_mut(); let child = match object_runtime.receive_object( parent, child_id, child_receiver_sequence_number, - &child_ty, &layout, &annotated_layout, MoveObjectType::from(tag), @@ -233,13 +237,20 @@ fn object_runtime_transfer( ty: Type, obj: Value, ) -> PartialVMResult { - if !matches!(context.type_to_type_tag(&ty)?, TypeTag::Struct(_)) { - return Err( - PartialVMError::new(StatusCode::UNKNOWN_INVARIANT_VIOLATION_ERROR) - .with_message("Sui verifier guarantees this is a struct".to_string()), - ); - } - - let obj_runtime: &mut ObjectRuntime = context.extensions_mut().get_mut(); - obj_runtime.transfer(owner, ty, obj) + let type_tag = context.type_to_type_tag(&ty)?; + let object_type = match type_tag { + TypeTag::Struct(s) => MoveObjectType::from(*s), + _ => { + return Err( + PartialVMError::new(StatusCode::UNKNOWN_INVARIANT_VIOLATION_ERROR) + .with_message("Sui verifier guarantees this is a struct".to_string()), + ); + } + }; + + let mut obj_runtime: RefMut = context + .extensions() + .get::>() + .get_mut(); + obj_runtime.transfer(owner, object_type, obj) } diff --git a/sui-execution/latest/sui-move-natives/src/tx_context.rs b/sui-execution/latest/sui-move-natives/src/tx_context.rs index 1a208098b409d9..871662e23d25df 100644 --- a/sui-execution/latest/sui-move-natives/src/tx_context.rs +++ b/sui-execution/latest/sui-move-natives/src/tx_context.rs @@ -3,12 +3,14 @@ use move_binary_format::errors::PartialVMResult; use move_core_types::{account_address::AccountAddress, gas_algebra::InternalGas}; -use move_vm_runtime::{native_charge_gas_early_exit, native_functions::NativeContext}; -use move_vm_types::{ - loaded_data::runtime_types::Type, natives::function::NativeResult, pop_arg, values::Value, +use move_vm_runtime::{ + execution::{values::Value, Type}, + natives::{extensions::NativeContextMut, functions::NativeResult}, + pop_arg, }; +use move_vm_runtime::{native_charge_gas_early_exit, natives::functions::NativeContext}; use smallvec::smallvec; -use std::{collections::VecDeque, convert::TryFrom}; +use std::{cell::RefMut, collections::VecDeque, convert::TryFrom}; use sui_types::base_types::{ObjectID, TransactionDigest}; use crate::{object_runtime::ObjectRuntime, NativesCostTable}; @@ -46,7 +48,10 @@ pub fn derive_id( // unwrap safe because all digests in Move are serialized from the Rust `TransactionDigest` let digest = TransactionDigest::try_from(tx_hash.as_slice()).unwrap(); let address = AccountAddress::from(ObjectID::derive_id(digest, ids_created)); - let obj_runtime: &mut ObjectRuntime = context.extensions_mut().get_mut(); + let mut obj_runtime: RefMut = context + .extensions() + .get::>() + .get_mut(); obj_runtime.new_id(address.into())?; Ok(NativeResult::ok( diff --git a/sui-execution/latest/sui-move-natives/src/types.rs b/sui-execution/latest/sui-move-natives/src/types.rs index 41b1ae6a916d6c..ea2182017a2a9c 100644 --- a/sui-execution/latest/sui-move-natives/src/types.rs +++ b/sui-execution/latest/sui-move-natives/src/types.rs @@ -7,10 +7,10 @@ use move_core_types::{ language_storage::TypeTag, runtime_value::{MoveStructLayout, MoveTypeLayout}, }; -use move_vm_runtime::{native_charge_gas_early_exit, native_functions::NativeContext}; -use move_vm_types::{ - loaded_data::runtime_types::Type, natives::function::NativeResult, values::Value, +use move_vm_runtime::{ + execution::values::Value, execution::Type, natives::functions::NativeResult, }; +use move_vm_runtime::{native_charge_gas_early_exit, natives::functions::NativeContext}; use smallvec::smallvec; use std::collections::VecDeque; diff --git a/sui-execution/latest/sui-move-natives/src/validator.rs b/sui-execution/latest/sui-move-natives/src/validator.rs index 6a46b167aec75b..70a9c07c6fcfda 100644 --- a/sui-execution/latest/sui-move-natives/src/validator.rs +++ b/sui-execution/latest/sui-move-natives/src/validator.rs @@ -4,10 +4,10 @@ use crate::NativesCostTable; use move_binary_format::errors::{PartialVMError, PartialVMResult}; use move_core_types::{gas_algebra::InternalGas, vm_status::StatusCode}; -use move_vm_runtime::{native_charge_gas_early_exit, native_functions::NativeContext}; -use move_vm_types::{ - loaded_data::runtime_types::Type, natives::function::NativeResult, pop_arg, values::Value, +use move_vm_runtime::{ + execution::values::Value, execution::Type, natives::functions::NativeResult, pop_arg, }; +use move_vm_runtime::{native_charge_gas_early_exit, natives::functions::NativeContext}; use smallvec::smallvec; use std::collections::VecDeque; use sui_types::sui_system_state::sui_system_state_inner_v1::ValidatorMetadataV1;