From e1059c0b20cc18fa718a0cdfaae9ba71ea789329 Mon Sep 17 00:00:00 2001 From: Vadim Smirnov Date: Sat, 22 Jul 2023 12:06:20 +0300 Subject: [PATCH 1/5] feat(wasm-builder): Set optimisation params (#2973) --- examples/fungible-token/src/contract.rs | 33 +++++++++++++------------ utils/wasm-builder/src/optimize.rs | 1 + utils/wasm-builder/src/wasm_project.rs | 11 ++++++--- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/examples/fungible-token/src/contract.rs b/examples/fungible-token/src/contract.rs index b5cd0a39309..6980162b974 100644 --- a/examples/fungible-token/src/contract.rs +++ b/examples/fungible-token/src/contract.rs @@ -54,34 +54,36 @@ impl FungibleToken { } fn mint(&mut self, amount: u128) { + let source = msg::source(); self.balances - .entry(msg::source()) + .entry(source) .and_modify(|balance| *balance += amount) .or_insert(amount); self.total_supply += amount; msg::reply( FTEvent::Transfer { from: ZERO_ID, - to: msg::source(), + to: source, amount, }, 0, ) .unwrap(); } - + /// Executed on receiving `fungible-token-messages::BurnInput`. fn burn(&mut self, amount: u128) { - if self.balances.get(&msg::source()).unwrap_or(&0) < &amount { + let source = msg::source(); + if self.balances.get(&source).unwrap_or(&0) < &amount { panic!("Amount exceeds account balance"); } self.balances - .entry(msg::source()) + .entry(source) .and_modify(|balance| *balance -= amount); self.total_supply -= amount; msg::reply( FTEvent::Transfer { - from: msg::source(), + from: source, to: ZERO_ID, amount, }, @@ -89,7 +91,7 @@ impl FungibleToken { ) .unwrap(); } - + /// Executed on receiving `fungible-token-messages::TransferInput` or `fungible-token-messages::TransferFromInput`. /// Transfers `amount` tokens from `sender` account to `recipient` account. fn transfer(&mut self, from: &ActorId, to: &ActorId, amount: u128) { if from == &ZERO_ID || to == &ZERO_ID { @@ -119,17 +121,19 @@ impl FungibleToken { .unwrap(); } + /// Executed on receiving `fungible-token-messages::ApproveInput`. fn approve(&mut self, to: &ActorId, amount: u128) { if to == &ZERO_ID { panic!("Approve to zero address"); } + let source = msg::source(); self.allowances - .entry(msg::source()) + .entry(source) .or_default() .insert(*to, amount); msg::reply( FTEvent::Approve { - from: msg::source(), + from: source, to: *to, amount, }, @@ -139,17 +143,14 @@ impl FungibleToken { } fn can_transfer(&mut self, from: &ActorId, amount: u128) -> bool { - if from == &msg::source() || self.balances.get(&msg::source()).unwrap_or(&0) >= &amount { + let source = msg::source(); + if from == &source || self.balances.get(&source).unwrap_or(&0) >= &amount { return true; } - if let Some(allowed_amount) = self - .allowances - .get(from) - .and_then(|m| m.get(&msg::source())) - { + if let Some(allowed_amount) = self.allowances.get(from).and_then(|m| m.get(&source)) { if allowed_amount >= &amount { self.allowances.entry(*from).and_modify(|m| { - m.entry(msg::source()).and_modify(|a| *a -= amount); + m.entry(source).and_modify(|a| *a -= amount); }); return true; } diff --git a/utils/wasm-builder/src/optimize.rs b/utils/wasm-builder/src/optimize.rs index 88c3aed181f..0ad73346ecf 100644 --- a/utils/wasm-builder/src/optimize.rs +++ b/utils/wasm-builder/src/optimize.rs @@ -277,6 +277,7 @@ pub fn do_optimization( "z" => OptimizationOptions::new_optimize_for_size_aggressively(), _ => panic!("Invalid optimization level {}", optimization_level), } + .shrink_level(wasm_opt::ShrinkLevel::Level2) .add_pass(Pass::Dae) .add_pass(Pass::Vacuum) // the memory in our module is imported, `wasm-opt` needs to be told that diff --git a/utils/wasm-builder/src/wasm_project.rs b/utils/wasm-builder/src/wasm_project.rs index e1fdf957f05..579ad88445d 100644 --- a/utils/wasm-builder/src/wasm_project.rs +++ b/utils/wasm-builder/src/wasm_project.rs @@ -31,6 +31,8 @@ use std::{ }; use toml::value::Table; +const OPT_LEVEL: &str = "z"; + /// Enum defining type of binary compiling: production program or metawasm. pub enum ProjectType { Program(Option), @@ -158,11 +160,12 @@ impl WasmProject { lib.insert("crate-type".into(), vec!["cdylib".to_string()].into()); let mut dev_profile = Table::new(); - dev_profile.insert("opt-level".into(), "s".into()); + dev_profile.insert("opt-level".into(), OPT_LEVEL.into()); let mut release_profile = Table::new(); - release_profile.insert("lto".into(), true.into()); - release_profile.insert("opt-level".into(), "s".into()); + release_profile.insert("lto".into(), "fat".into()); + release_profile.insert("opt-level".into(), OPT_LEVEL.into()); + release_profile.insert("codegen-units".into(), 1.into()); let mut production_profile = Table::new(); production_profile.insert("inherits".into(), "release".into()); @@ -316,7 +319,7 @@ extern "C" fn metahash() {{ let path = optimize::optimize_wasm( original_copy_wasm_path.clone(), opt_wasm_path.clone(), - "s", + "4", true, ) .map(|res| { From f4e642cd83c04b72cfd0e265e21bf5c0d5dde8a0 Mon Sep 17 00:00:00 2001 From: clearloop <26088946+clearloop@users.noreply.github.com> Date: Sat, 22 Jul 2023 22:14:37 +0800 Subject: [PATCH 2/5] refactor(common): move LockId to gear-core (#2966) --- Cargo.lock | 2 +- common/src/gas_provider/lockable.rs | 11 +---------- core/Cargo.toml | 1 + core/src/gas.rs | 15 +++++++++++++++ gclient/Cargo.toml | 1 - gclient/src/api/calls.rs | 2 +- 6 files changed, 19 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1205f903867..5e24a8a7878 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3642,7 +3642,6 @@ dependencies = [ "env_logger", "futures", "futures-timer", - "gear-common", "gear-core", "gear-core-errors", "gear-utils", @@ -3870,6 +3869,7 @@ version = "0.1.0" dependencies = [ "blake2-rfc", "derive_more", + "enum-iterator 1.4.1", "env_logger", "gear-core-errors", "gear-wasm-instrument", diff --git a/common/src/gas_provider/lockable.rs b/common/src/gas_provider/lockable.rs index c4cb36376e5..8e48be65743 100644 --- a/common/src/gas_provider/lockable.rs +++ b/common/src/gas_provider/lockable.rs @@ -17,16 +17,7 @@ // along with this program. If not, see . use super::{scheduler::StorageType, *}; -use enum_iterator::Sequence; - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Sequence)] -#[repr(u8)] -pub enum LockId { - Mailbox, - Waitlist, - Reservation, - DispatchStash, -} +pub use gear_core::gas::LockId; /// An error indicating there is no corresponding enum variant to the one provided #[derive(Debug)] diff --git a/core/Cargo.toml b/core/Cargo.toml index 7c23048e3b5..e430f73d697 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -18,6 +18,7 @@ hex = { workspace = true, features = ["alloc"] } hashbrown.workspace = true static_assertions.workspace = true paste = { workspace = true } +enum-iterator.workspace = true [dev-dependencies] wabt.workspace = true diff --git a/core/src/gas.rs b/core/src/gas.rs index d4f3a9cd1b9..9cedb49b3a4 100644 --- a/core/src/gas.rs +++ b/core/src/gas.rs @@ -19,8 +19,23 @@ //! Gas module. use crate::costs::RuntimeCosts; +use enum_iterator::Sequence; use scale_info::scale::{Decode, Encode}; +/// The id of the gas lock. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Sequence)] +#[repr(u8)] +pub enum LockId { + /// The gas lock is provided by the mailbox. + Mailbox, + /// The gas lock is provided by the waitlist. + Waitlist, + /// The gas lock is provided by reservation. + Reservation, + /// The gas lock is provided by dispatch stash. + DispatchStash, +} + /// This trait represents a token that can be used for charging `GasCounter`. /// /// Implementing type is expected to be super lightweight hence `Copy` (`Clone` is added diff --git a/gclient/Cargo.toml b/gclient/Cargo.toml index fead831b437..10d3f74388b 100644 --- a/gclient/Cargo.toml +++ b/gclient/Cargo.toml @@ -10,7 +10,6 @@ gear-utils.workspace = true gsdk = { workspace = true, features = ["testing"] } gear-core.workspace = true gear-core-errors.workspace = true -gear-common = { workspace = true, features = ["std"] } futures.workspace = true anyhow.workspace = true diff --git a/gclient/src/api/calls.rs b/gclient/src/api/calls.rs index 37dc0121008..f8b95e77360 100644 --- a/gclient/src/api/calls.rs +++ b/gclient/src/api/calls.rs @@ -18,8 +18,8 @@ use super::{GearApi, Result}; use crate::{api::storage::account_id::IntoAccountId32, utils, Error}; -use gear_common::LockId; use gear_core::{ + gas::LockId, ids::*, memory::PageBuf, pages::{GearPage, PageNumber, PageU32Size, GEAR_PAGE_SIZE, WASM_PAGE_SIZE}, From 45418525a417e6f204a9d76d1e5709029270eab7 Mon Sep 17 00:00:00 2001 From: clearloop <26088946+clearloop@users.noreply.github.com> Date: Sat, 22 Jul 2023 23:44:28 +0800 Subject: [PATCH 3/5] feat(gsdk): allow transaction retracted (#2963) --- gsdk/src/signer/calls.rs | 12 ++++++------ gsdk/src/signer/mod.rs | 15 +++++++-------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/gsdk/src/signer/calls.rs b/gsdk/src/signer/calls.rs index 5f832e3dbef..901a41a928e 100644 --- a/gsdk/src/signer/calls.rs +++ b/gsdk/src/signer/calls.rs @@ -343,16 +343,16 @@ impl Signer { b.block_hash(), b.extrinsic_hash() ), - TxStatus::Retracted(h) => log::info!(" Status: Retracted( {h} )"), - TxStatus::FinalityTimeout(h) => log::info!(" Status: FinalityTimeout( {h} )"), + TxStatus::Retracted(h) => log::warn!(" Status: Retracted( {h} )"), + TxStatus::FinalityTimeout(h) => log::error!(" Status: FinalityTimeout( {h} )"), TxStatus::Finalized(b) => log::info!( " Status: Finalized( block hash: {}, extrinsic hash: {} )", b.block_hash(), b.extrinsic_hash() ), - TxStatus::Usurped(h) => log::info!(" Status: Usurped( {h} )"), - TxStatus::Dropped => log::info!(" Status: Dropped"), - TxStatus::Invalid => log::info!(" Status: Invalid"), + TxStatus::Usurped(h) => log::error!(" Status: Usurped( {h} )"), + TxStatus::Dropped => log::error!(" Status: Dropped"), + TxStatus::Invalid => log::error!(" Status: Invalid"), } } @@ -389,7 +389,7 @@ impl Signer { let status = status?; self.log_status(&status); match status { - Future | Ready | Broadcast(_) | InBlock(_) => (), + Future | Ready | Broadcast(_) | InBlock(_) | Retracted(_) => (), Finalized(b) => { log::info!( "Successfully submitted call {}::{} {} at {}!", diff --git a/gsdk/src/signer/mod.rs b/gsdk/src/signer/mod.rs index f0de6c90a76..c421673bc69 100644 --- a/gsdk/src/signer/mod.rs +++ b/gsdk/src/signer/mod.rs @@ -52,16 +52,15 @@ impl Signer { } /// Change inner signer. - pub fn change(self, suri: &str, passwd: Option<&str>) -> Result { - Ok(Self { - api: self.api, - signer: PairSigner::new( - Pair::from_string(suri, passwd).map_err(|_| Error::InvalidSecret)?, - ), - nonce: None, - }) + pub fn change(mut self, suri: &str, passwd: Option<&str>) -> Result { + let signer = + PairSigner::new(Pair::from_string(suri, passwd).map_err(|_| Error::InvalidSecret)?); + self.signer = signer; + + Ok(self) } + /// Set nonce of the signer pub fn set_nonce(&mut self, nonce: u32) { self.nonce = Some(nonce) } From 6e3ad0607102654368db45bea7d2caf491cc581d Mon Sep 17 00:00:00 2001 From: StackOverflowExcept1on <109800286+StackOverflowExcept1on@users.noreply.github.com> Date: Sat, 22 Jul 2023 19:12:49 +0300 Subject: [PATCH 4/5] chore(ft-benchmarks): fix wrong path and set PRNG seed (#2964) --- examples/fungible-token/tests/benchmarks.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/fungible-token/tests/benchmarks.rs b/examples/fungible-token/tests/benchmarks.rs index 10a255d8f30..63596aabce8 100644 --- a/examples/fungible-token/tests/benchmarks.rs +++ b/examples/fungible-token/tests/benchmarks.rs @@ -23,11 +23,11 @@ use ft_io::*; use gclient::{EventProcessor, GearApi, Result}; use gear_core::ids::{MessageId, ProgramId}; use gstd::{vec, ActorId, Encode, Vec}; -use rand::Rng; +use rand::{rngs::StdRng, Rng, SeedableRng}; use statrs::statistics::Statistics; /// Path to the gear node binary. -const GEAR_PATH: &str = "../../../target/release/gear"; +const GEAR_PATH: &str = "../../target/release/gear"; /// This constant defines the number of messages in the batch. /// It is calculated empirically, and 25 is considered the optimal value for @@ -206,7 +206,7 @@ async fn stress_test() -> Result<()> { #[ignore] #[tokio::test] async fn stress_transfer() -> Result<()> { - let mut rng = rand::thread_rng(); + let mut rng = StdRng::seed_from_u64(42); let api = GearApi::dev_from_path(GEAR_PATH).await?; // Use this code in comment for custom node run: From 86295f60ec851255ef423f37768779d524507f9b Mon Sep 17 00:00:00 2001 From: StackOverflowExcept1on <109800286+StackOverflowExcept1on@users.noreply.github.com> Date: Sun, 23 Jul 2023 12:05:15 +0300 Subject: [PATCH 5/5] chore(deps): bump parity-scale-codec to 3.6.4 (#2961) --- Cargo.lock | 16 ++++++++-------- Cargo.toml | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5e24a8a7878..2591c1a9022 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7941,9 +7941,9 @@ dependencies = [ [[package]] name = "parity-scale-codec" -version = "3.6.3" +version = "3.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "756d439303e94fae44f288ba881ad29670c65b0c4b0e05674ca81061bb65f2c5" +checksum = "dd8e946cc0cc711189c0b0249fb8b599cbeeab9784d83c415719368bb8d4ac64" dependencies = [ "arrayvec 0.7.4", "bitvec", @@ -7956,9 +7956,9 @@ dependencies = [ [[package]] name = "parity-scale-codec-derive" -version = "3.6.3" +version = "3.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d884d78fcf214d70b1e239fcd1c6e5e95aa3be1881918da2e488cc946c7a476" +checksum = "2a296c3079b5fefbc499e1de58dc26c09b1b9a5952d26694ee89f04a43ebbb3e" dependencies = [ "proc-macro-crate 1.1.3", "proc-macro2", @@ -8394,9 +8394,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.64" +version = "1.0.66" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78803b62cbf1f46fde80d7c0e803111524b9877184cfe7c3033659490ac7a7da" +checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9" dependencies = [ "unicode-ident", ] @@ -8621,9 +8621,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.29" +version = "1.0.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "573015e8ab27661678357f27dc26460738fd2b6c86e46f386fde94cb5d913105" +checksum = "5fe8a65d69dd0808184ebb5f836ab526bb259db23c657efa38711b1072ee47f0" dependencies = [ "proc-macro2", ] diff --git a/Cargo.toml b/Cargo.toml index f176f24afb2..fa84c9587a1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -115,7 +115,7 @@ base64 = "0.21.0" blake2-rfc = { version = "0.2.18", default-features = false } bs58 = { version = "0.4.0", default-features = false } clap = { version = "4.2.1" } -codec = { package = "parity-scale-codec", version = "3.4.0", default-features = false } +codec = { package = "parity-scale-codec", version = "3.6.4", default-features = false } color-eyre = "0.6.2" colored = "2.0.0" const-str = "0.5" @@ -136,7 +136,7 @@ lazy_static = "1.4.0" libc = { version = "0.2", default-features = false } log = { version = "0.4.17", default-features = false } once_cell = "1.17.1" -parity-scale-codec = { version = "3.6.1", default-features = false } +parity-scale-codec = { version = "3.6.4", default-features = false } parity-wasm = "0.45.0" parking_lot = "0.12.1" path-clean = "1.0.1"