From 9d8f10c519f6b4dacb196fa735892a4e50d02b01 Mon Sep 17 00:00:00 2001 From: steelgeek091 Date: Sat, 31 Aug 2024 22:53:40 +0800 Subject: [PATCH] use Rust macro from the rooch_framework --- Cargo.lock | 1 + crates/rooch-genesis/src/lib.rs | 5 - .../framework-release/released/8/stdlib | Bin 161222 -> 160035 bytes frameworks/rooch-nursery/Cargo.toml | 3 +- .../src/natives/gas_parameter/gas_member.rs | 26 --- .../src/natives/gas_parameter/mod.rs | 2 - .../src/natives/gas_parameter/native.rs | 152 ------------------ .../src/natives/gas_parameter/wasm.rs | 2 +- frameworks/rooch-nursery/src/natives/mod.rs | 4 +- 9 files changed, 6 insertions(+), 189 deletions(-) delete mode 100644 frameworks/rooch-nursery/src/natives/gas_parameter/gas_member.rs delete mode 100644 frameworks/rooch-nursery/src/natives/gas_parameter/native.rs diff --git a/Cargo.lock b/Cargo.lock index 4d7f97e4c0..e15fad1cb9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9605,6 +9605,7 @@ dependencies = [ "moveos-types", "moveos-wasm", "musig2", + "rooch-framework", "rooch-types", "serde_json", "smallvec", diff --git a/crates/rooch-genesis/src/lib.rs b/crates/rooch-genesis/src/lib.rs index 23a74d3aff..a6161d22a4 100644 --- a/crates/rooch-genesis/src/lib.rs +++ b/crates/rooch-genesis/src/lib.rs @@ -29,11 +29,6 @@ use rooch_framework::natives::gas_parameter::gas_member::{ }; use rooch_framework::ROOCH_FRAMEWORK_ADDRESS; use rooch_indexer::store::traits::IndexerStoreTrait; -use rooch_nursery::natives::gas_parameter::gas_member::InitialGasSchedule as RoochNurseryInitialGasSchedule; -use rooch_nursery::natives::gas_parameter::gas_member::{ - FromOnChainGasSchedule as RoochNurseryFromOnChainGasSchedule, - ToOnChainGasSchedule as RoochNurseryToOnChainGasSchedule, -}; use rooch_store::meta_store::MetaStore; use rooch_store::transaction_store::TransactionStore; use rooch_types::address::BitcoinAddress; diff --git a/frameworks/framework-release/released/8/stdlib b/frameworks/framework-release/released/8/stdlib index 2fb2d8c63e207c7ee704d5e96d35e5ca2eef1fbb..85c0bb8320ca225085c5a4b90973cc8c587967e3 100644 GIT binary patch delta 25 hcmX^1nsf0Z&JC^xj5?d$417DA&Gv0K+s8OzI{=X^3j_cF delta 1154 zcmZuwJ8KkC6h7zNJI^~iyE8j$%%cegf}jwrwo?lQi(p}4BaD;Xn`B{kHtZwOY6KhY z0x1N$U}+;*gfupSwZB2^0{#T?+}&C8+JSTCJihbYb06o^FYm{h_vO(B-u?P`WAlA+ z>>aP1DH{NM1elGON(&~ zxD?kBa5-U}$c9XuhJ!w#?~`y`@QDrd1-gI=S(ib9!uSHY2_YsZ68y2nIt;N0Im^i~ zBmhEw>Kl6 z*ZxXbeX-FZo%O#|X8BOslcP+x2mM~UtFyg@YpLn6&j<&)tFz&igRIXXs-4HC42?T; zYpK(&>wL&pKD`{BXIov}{_ye^-9Gwxf3*O@oBUY#M;@(NC!}d6_ya_9`IN~&44fLv_g@I Y*kxN$NU`zF303Ls;pxfYDIT5t1F) -> Option; -} - -/// A trait for converting to a list of entries of the on-chain gas schedule. -pub trait ToOnChainGasSchedule { - /// Converts `self` into a list of entries of the on-chain gas schedule. - /// Each entry is a key-value pair where the key is a string representing the name of the - /// parameter, where the value is the gas parameter itself. - fn to_on_chain_gas_schedule(&self) -> Vec<(String, u64)>; -} - -/// A trait for defining an initial value to be used in the genesis. -pub trait InitialGasSchedule: Sized { - /// Returns the initial value of this type, which is used in the genesis. - fn initial() -> Self; -} diff --git a/frameworks/rooch-nursery/src/natives/gas_parameter/mod.rs b/frameworks/rooch-nursery/src/natives/gas_parameter/mod.rs index 25c236eeee..828224d05f 100644 --- a/frameworks/rooch-nursery/src/natives/gas_parameter/mod.rs +++ b/frameworks/rooch-nursery/src/natives/gas_parameter/mod.rs @@ -1,6 +1,4 @@ // Copyright (c) RoochNetwork // SPDX-License-Identifier: Apache-2.0 -pub mod gas_member; -pub mod native; pub mod wasm; diff --git a/frameworks/rooch-nursery/src/natives/gas_parameter/native.rs b/frameworks/rooch-nursery/src/natives/gas_parameter/native.rs deleted file mode 100644 index 36cee537af..0000000000 --- a/frameworks/rooch-nursery/src/natives/gas_parameter/native.rs +++ /dev/null @@ -1,152 +0,0 @@ -// Copyright (c) RoochNetwork -// SPDX-License-Identifier: Apache-2.0 - -// Copyright (c) The Starcoin Core Contributors -// SPDX-License-Identifier: Apache-2.0 -pub const MUL: u64 = 1; - -#[macro_export] -macro_rules! set_first_segment { - ($params: ident . $first:ident . $($rest:ident).*, $val: ident, $param_ty: ty) => { - $params .$first.$($rest)* = Some((*$val).into()); - }; -} - -#[macro_export] -macro_rules! expand_get_impl_for_native_gas_params { - ($params: ident $(.$field: ident)+, $map: ident, $prefix: literal, optional $key: literal) => { - if let Some(val) = $map.get(&format!("{}.{}", $prefix, $key)) { - $params $(.$field)+ = Some((*val).into()); - } - }; - ($params: ident $(.$field: ident)+, $map: ident, $prefix: literal, $key: literal) => { - $params $(.$field)+ = $map.get(&format!("{}.{}", $prefix, $key)).cloned()?.into(); - }; -} - -#[macro_export] -macro_rules! expand_get_for_native_gas_params { - (test_only $(.$field: ident)+, $(optional $($dummy: ident)?)? $key: literal, $initial_val: expr, $param_ty: ty, $package_name: literal, $params: ident, $gas_schedule: ident) => { - // TODO(Gas): this is a hack to work-around issue - // https://github.com/rust-lang/rust/issues/15701 - { - #[cfg(feature = "testing")] - fn assign(params: &mut $param_ty, gas_schedule: &std::collections::BTreeMap) -> Option<()> { - $crate::natives::gas_parameter::expand_get_impl_for_native_gas_params!(params $(.$field)+, gas_schedule, $package_name, $(optional $($dummy)?)? $key); - Some(()) - } - - #[cfg(not(feature = "testing"))] - fn assign(_params: &mut $param_ty, _gas_schedule: &std::collections::BTreeMap) -> Option<()> { - Some(()) - } - - assign(&mut $params, &$gas_schedule)?; - } - }; - ($(.$field: ident)+, $(optional $($dummy: ident)?)? $key: literal, $initial_val: expr, $param_ty: ty, $package_name: literal, $params: ident, $gas_schedule: ident) => { - $crate::natives::gas_parameter::native::expand_get_impl_for_native_gas_params!($params $(.$field)+, $gas_schedule, $package_name, $(optional $($dummy)?)? $key); - } -} - -#[macro_export] -macro_rules! expand_set_for_native_gas_params { - (test_only $(.$field: ident)+, $(optional)? $key: literal, $initial_val: expr, $param_ty: ty, $package_name: literal, $params: ident) => { - { - #[cfg(feature = "testing")] - fn assign(params: &mut $param_ty) { - params $(.$field)+ = $initial_val.into(); - } - - #[cfg(not(feature = "testing"))] - fn assign(_params: &mut $param_ty) { - } - - assign(&mut $params); - } - }; - ($(.$field: ident)+, $key: literal, $initial_val: expr, $param_ty: ty, $package_name: literal, $params: ident) => { - $params $(.$field)+ = $initial_val.into() - }; - ($(.$field: ident)+, optional $key: literal, $initial_val: expr, $param_ty: ty, $package_name: literal, $params: ident) => { - $params $(.$field)+ = Some($initial_val.into()) - }; -} - -#[macro_export] -macro_rules! get_self_key_value { - (.$first:ident . $($rest:ident).*, $initial_val: expr, $self: ident, $key: literal) => { - ($key, u64::from($self .$first.$($rest)*.unwrap_or(0.into()))) - }; -} - -#[macro_export] -macro_rules! expand_kv_for_native_gas_params { - (test_only $(.$field: ident)+, $(optional)? $key: literal, $initial_val: expr, $self: ident) => { - #[cfg(feature = "testing")] - ($key, u64::from($self $(.$field)+)) - }; - ($(.$field: ident)+, $key: literal, $initial_val: expr, $self: ident) => { - ($key, u64::from($self $(.$field)+)) - }; - ($(.$field: ident)+, optional $key: literal, $initial_val: expr, $self: ident) => { - $crate::natives::gas_parameter::native::get_self_key_value!($(.$field)+, $initial_val, $self, $key) - }; -} - -#[macro_export] -macro_rules! define_gas_parameters_for_natives { - ($param_ty: ty, $package_name: literal, [$([$($t: tt)*]),* $(,)?] $(, allow_unmapped = $allow_unmapped: expr)?) => { - impl $crate::natives::gas_parameter::gas_member::FromOnChainGasSchedule for $param_ty { - fn from_on_chain_gas_schedule(gas_schedule: &std::collections::BTreeMap) -> Option { - let mut params = <$param_ty>::zeros(); - - $( - $crate::natives::gas_parameter::native::expand_get_for_native_gas_params!($($t)*, $param_ty, $package_name, params, gas_schedule); - )* - - Some(params) - } - } - - impl $crate::natives::gas_parameter::gas_member::ToOnChainGasSchedule for $param_ty { - fn to_on_chain_gas_schedule(&self) -> Vec<(String, u64)> { - [$($crate::natives::gas_parameter::native::expand_kv_for_native_gas_params!($($t)*, self)),*] - .into_iter().filter(|(_, val)| *val > 0).map(|(key, val)| (format!("{}.{}", $package_name, key), val)).collect() - } - } - - impl $crate::natives::gas_parameter::gas_member::InitialGasSchedule for $param_ty { - fn initial() -> Self { - let mut params = <$param_ty>::zeros(); - - $( - $crate::natives::gas_parameter::native::expand_set_for_native_gas_params!($($t)*, $param_ty, $package_name, params); - )* - - params - } - } - - /* - #[test] - fn keys_should_be_unique() { - let mut map = std::collections::BTreeMap::<&str, ()>::new(); - - for key in [$(crate::natives::gas_parameter::native::extract_key_for_native_gas_params!($($t)*)),*] { - if map.insert(key.clone(), ()).is_some() { - panic!("duplicated key {}", key); - } - } - } - */ - }; -} - -pub use define_gas_parameters_for_natives; -pub use expand_get_for_native_gas_params; -pub use expand_get_impl_for_native_gas_params; -pub use expand_kv_for_native_gas_params; -pub use expand_set_for_native_gas_params; -pub use get_self_key_value; -pub use set_first_segment; diff --git a/frameworks/rooch-nursery/src/natives/gas_parameter/wasm.rs b/frameworks/rooch-nursery/src/natives/gas_parameter/wasm.rs index 7480bc47fe..cc3b94cb7b 100644 --- a/frameworks/rooch-nursery/src/natives/gas_parameter/wasm.rs +++ b/frameworks/rooch-nursery/src/natives/gas_parameter/wasm.rs @@ -3,7 +3,7 @@ use crate::natives::wasm::GasParameters; -crate::natives::gas_parameter::native::define_gas_parameters_for_natives!(GasParameters, "wasm", [ +rooch_framework::natives::gas_parameter::native::define_gas_parameters_for_natives!(GasParameters, "wasm", [ [.create_instance_gas_parameter.base_create_instance, "create_instance_gas_parameter.base_create_instance", 10000], [.create_instance_gas_parameter.per_byte_instance, "create_instance_gas_parameter.per_byte_instance", 100], diff --git a/frameworks/rooch-nursery/src/natives/mod.rs b/frameworks/rooch-nursery/src/natives/mod.rs index dc24c252ee..117fd3c073 100644 --- a/frameworks/rooch-nursery/src/natives/mod.rs +++ b/frameworks/rooch-nursery/src/natives/mod.rs @@ -1,10 +1,10 @@ // Copyright (c) RoochNetwork // SPDX-License-Identifier: Apache-2.0 -use crate::natives::gas_parameter::gas_member::{ +use move_vm_runtime::native_functions::{make_table_from_iter, NativeFunctionTable}; +use rooch_framework::natives::gas_parameter::gas_member::{ FromOnChainGasSchedule, InitialGasSchedule, ToOnChainGasSchedule, }; -use move_vm_runtime::native_functions::{make_table_from_iter, NativeFunctionTable}; use rooch_types::addresses::ROOCH_NURSERY_ADDRESS; use std::collections::BTreeMap;