From d6f10b10916565ba5a6ddd5fa093ac4b7da4d52a Mon Sep 17 00:00:00 2001 From: Ashutosh Varma Date: Tue, 24 Oct 2023 14:00:35 +0530 Subject: [PATCH 1/6] feat: update bench for uma --- pallets/unified-accounts/src/benchmarking.rs | 60 ++++++++++++++ pallets/unified-accounts/src/weights.rs | 84 ++++++++++++++++++++ 2 files changed, 144 insertions(+) diff --git a/pallets/unified-accounts/src/benchmarking.rs b/pallets/unified-accounts/src/benchmarking.rs index e1a88290e3..f40dbc31ce 100644 --- a/pallets/unified-accounts/src/benchmarking.rs +++ b/pallets/unified-accounts/src/benchmarking.rs @@ -83,4 +83,64 @@ mod benchmarks { .into(), ); } + + #[benchmark] + fn uam_to_account_id() { + let caller: T::AccountId = whitelisted_caller(); + let evm_address = T::DefaultNativeToEvm::into_h160(caller.clone()); + // claim mapping + assert_ok!(Pallet::::claim_default_evm_address( + RawOrigin::Signed(caller.clone()).into() + )); + + #[block] + { + let _ = as UnifiedAddressMapper>::to_account_id(&evm_address); + } + } + + #[benchmark] + fn uam_to_account_id_or_default() { + let caller: T::AccountId = whitelisted_caller(); + let evm_address = T::DefaultNativeToEvm::into_h160(caller.clone()); + // claim mapping + assert_ok!(Pallet::::claim_default_evm_address( + RawOrigin::Signed(caller.clone()).into() + )); + + #[block] + { + let _ = as UnifiedAddressMapper>::to_account_id_or_default( + &evm_address, + ); + } + } + + #[benchmark] + fn uam_to_h160() { + let caller: T::AccountId = whitelisted_caller(); + // claim mapping + assert_ok!(Pallet::::claim_default_evm_address( + RawOrigin::Signed(caller.clone()).into() + )); + + #[block] + { + let _ = as UnifiedAddressMapper>::to_h160(&caller); + } + } + + #[benchmark] + fn uam_to_h160_or_default() { + let caller: T::AccountId = whitelisted_caller(); + // claim mapping + assert_ok!(Pallet::::claim_default_evm_address( + RawOrigin::Signed(caller.clone()).into() + )); + + #[block] + { + let _ = as UnifiedAddressMapper>::to_h160_or_default(&caller); + } + } } diff --git a/pallets/unified-accounts/src/weights.rs b/pallets/unified-accounts/src/weights.rs index 81ea31fd29..4838a8d0e4 100644 --- a/pallets/unified-accounts/src/weights.rs +++ b/pallets/unified-accounts/src/weights.rs @@ -51,6 +51,10 @@ use core::marker::PhantomData; pub trait WeightInfo { fn claim_evm_address() -> Weight; fn claim_default_evm_address() -> Weight; + fn uam_to_account_id() -> Weight; + fn uam_to_account_id_or_default() -> Weight; + fn uam_to_h160() -> Weight; + fn uam_to_h160_or_default() -> Weight; } /// Weights for pallet_unified_accounts using the Substrate node and recommended hardware. @@ -88,6 +92,46 @@ impl WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().writes(2_u64)) } + /// Storage: UnifiedAccounts EvmToNative (r:1 w:0) + /// Proof: UnifiedAccounts EvmToNative (max_values: None, max_size: Some(68), added: 2543, mode: MaxEncodedLen) + fn uam_to_account_id() -> Weight { + // Proof Size summary in bytes: + // Measured: `170` + // Estimated: `3533` + // Minimum execution time: 6_000_000 picoseconds. + Weight::from_parts(7_000_000, 3533) + .saturating_add(T::DbWeight::get().reads(1_u64)) + } + /// Storage: UnifiedAccounts EvmToNative (r:1 w:0) + /// Proof: UnifiedAccounts EvmToNative (max_values: None, max_size: Some(68), added: 2543, mode: MaxEncodedLen) + fn uam_to_account_id_or_default() -> Weight { + // Proof Size summary in bytes: + // Measured: `170` + // Estimated: `3533` + // Minimum execution time: 6_000_000 picoseconds. + Weight::from_parts(7_000_000, 3533) + .saturating_add(T::DbWeight::get().reads(1_u64)) + } + /// Storage: UnifiedAccounts NativeToEvm (r:1 w:0) + /// Proof: UnifiedAccounts NativeToEvm (max_values: None, max_size: Some(68), added: 2543, mode: MaxEncodedLen) + fn uam_to_h160() -> Weight { + // Proof Size summary in bytes: + // Measured: `170` + // Estimated: `3533` + // Minimum execution time: 6_000_000 picoseconds. + Weight::from_parts(6_000_000, 3533) + .saturating_add(T::DbWeight::get().reads(1_u64)) + } + /// Storage: UnifiedAccounts NativeToEvm (r:1 w:0) + /// Proof: UnifiedAccounts NativeToEvm (max_values: None, max_size: Some(68), added: 2543, mode: MaxEncodedLen) + fn uam_to_h160_or_default() -> Weight { + // Proof Size summary in bytes: + // Measured: `170` + // Estimated: `3533` + // Minimum execution time: 6_000_000 picoseconds. + Weight::from_parts(6_000_000, 3533) + .saturating_add(T::DbWeight::get().reads(1_u64)) + } } // For backwards compatibility and tests @@ -124,4 +168,44 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(2_u64)) .saturating_add(RocksDbWeight::get().writes(2_u64)) } + /// Storage: UnifiedAccounts EvmToNative (r:1 w:0) + /// Proof: UnifiedAccounts EvmToNative (max_values: None, max_size: Some(68), added: 2543, mode: MaxEncodedLen) + fn uam_to_account_id() -> Weight { + // Proof Size summary in bytes: + // Measured: `170` + // Estimated: `3533` + // Minimum execution time: 6_000_000 picoseconds. + Weight::from_parts(7_000_000, 3533) + .saturating_add(RocksDbWeight::get().reads(1_u64)) + } + /// Storage: UnifiedAccounts EvmToNative (r:1 w:0) + /// Proof: UnifiedAccounts EvmToNative (max_values: None, max_size: Some(68), added: 2543, mode: MaxEncodedLen) + fn uam_to_account_id_or_default() -> Weight { + // Proof Size summary in bytes: + // Measured: `170` + // Estimated: `3533` + // Minimum execution time: 6_000_000 picoseconds. + Weight::from_parts(7_000_000, 3533) + .saturating_add(RocksDbWeight::get().reads(1_u64)) + } + /// Storage: UnifiedAccounts NativeToEvm (r:1 w:0) + /// Proof: UnifiedAccounts NativeToEvm (max_values: None, max_size: Some(68), added: 2543, mode: MaxEncodedLen) + fn uam_to_h160() -> Weight { + // Proof Size summary in bytes: + // Measured: `170` + // Estimated: `3533` + // Minimum execution time: 6_000_000 picoseconds. + Weight::from_parts(6_000_000, 3533) + .saturating_add(RocksDbWeight::get().reads(1_u64)) + } + /// Storage: UnifiedAccounts NativeToEvm (r:1 w:0) + /// Proof: UnifiedAccounts NativeToEvm (max_values: None, max_size: Some(68), added: 2543, mode: MaxEncodedLen) + fn uam_to_h160_or_default() -> Weight { + // Proof Size summary in bytes: + // Measured: `170` + // Estimated: `3533` + // Minimum execution time: 6_000_000 picoseconds. + Weight::from_parts(6_000_000, 3533) + .saturating_add(RocksDbWeight::get().reads(1_u64)) + } } From 8abdd65ff8ebe35c7d65b6e92c0363d502a22542 Mon Sep 17 00:00:00 2001 From: Ashutosh Varma Date: Tue, 24 Oct 2023 14:11:55 +0530 Subject: [PATCH 2/6] feat: replace hardcoded weights --- chain-extensions/unified-accounts/src/lib.rs | 25 ++++++++++---------- chain-extensions/xvm/src/lib.rs | 8 ++++--- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/chain-extensions/unified-accounts/src/lib.rs b/chain-extensions/unified-accounts/src/lib.rs index 17ab43f4e4..b4c60a18af 100644 --- a/chain-extensions/unified-accounts/src/lib.rs +++ b/chain-extensions/unified-accounts/src/lib.rs @@ -25,17 +25,20 @@ use astar_primitives::{ use core::marker::PhantomData; use sp_runtime::DispatchError; -use frame_support::{traits::Get, DefaultNoBound}; +use frame_support::DefaultNoBound; use pallet_contracts::chain_extension::{ ChainExtension, Environment, Ext, InitState, Result as DispatchResult, RetVal, }; use pallet_evm::AddressMapping; +use pallet_unified_accounts::WeightInfo; use parity_scale_codec::Encode; pub use unified_accounts_chain_extension_types::{ Command::{self, *}, UnifiedAddress, }; +type UAWeight = ::WeightInfo; + #[derive(DefaultNoBound)] pub struct UnifiedAccountsExtension(PhantomData<(T, UA)>); @@ -54,17 +57,15 @@ where })? { GetEvmAddress => { let account_id: T::AccountId = env.read_as()?; - - let base_weight = ::DbWeight::get().reads(1); - env.charge_weight(base_weight)?; + // charge weight + env.charge_weight(UAWeight::::uam_to_h160())?; // write to buffer UA::to_h160(&account_id).using_encoded(|r| env.write(r, false, None))?; } GetEvmAddressOrDefault => { let account_id: T::AccountId = env.read_as()?; - - let base_weight = ::DbWeight::get().reads(1); - env.charge_weight(base_weight)?; + // charge weight + env.charge_weight(UAWeight::::uam_to_h160_or_default())?; let evm_address = if let Some(h160) = UA::to_h160(&account_id) { UnifiedAddress::Mapped(h160) @@ -76,17 +77,15 @@ where } GetNativeAddress => { let evm_address: EvmAddress = env.read_as()?; - - let base_weight = ::DbWeight::get().reads(1); - env.charge_weight(base_weight)?; + // charge weight + env.charge_weight(UAWeight::::uam_to_account_id())?; // write to buffer UA::to_account_id(&evm_address).using_encoded(|r| env.write(r, false, None))?; } GetNativeAddressOrDefault => { let evm_address: EvmAddress = env.read_as()?; - - let base_weight = ::DbWeight::get().reads(1); - env.charge_weight(base_weight)?; + // charge weight + env.charge_weight(UAWeight::::uam_to_account_id_or_default())?; // read the storage item let native_address = if let Some(native) = UA::to_account_id(&evm_address) { diff --git a/chain-extensions/xvm/src/lib.rs b/chain-extensions/xvm/src/lib.rs index e86ea687ce..6fb194bedc 100644 --- a/chain-extensions/xvm/src/lib.rs +++ b/chain-extensions/xvm/src/lib.rs @@ -25,7 +25,7 @@ use astar_primitives::{ evm::UnifiedAddressMapper, xvm::{Context, VmId, XvmCall}, }; -use frame_support::{dispatch::Encode, traits::Get, weights::Weight}; +use frame_support::{dispatch::Encode, weights::Weight}; use frame_system::RawOrigin; use pallet_contracts::chain_extension::{ ChainExtension, Environment, Ext, InitState, RetVal, ReturnFlags, @@ -98,8 +98,10 @@ where // Claim the default evm address if needed. let mut actual_weight = Weight::zero(); if value > 0 { - // `UA::to_h160` 1 DB read. - actual_weight.saturating_accrue(T::DbWeight::get().reads(1)); + // `UA::to_h160`. + actual_weight.saturating_accrue( + ::WeightInfo::uam_to_h160(), + ); if UA::to_h160(&source).is_none() { let weight_of_claim = ::WeightInfo::claim_default_evm_address(); From 62d5104b81ee4920a0f913e860e92d7bea776fc7 Mon Sep 17 00:00:00 2001 From: Ashutosh Varma Date: Wed, 25 Oct 2023 09:39:41 +0530 Subject: [PATCH 3/6] feat: apply code suggestions --- chain-extensions/unified-accounts/src/lib.rs | 8 +++---- chain-extensions/xvm/src/lib.rs | 2 +- pallets/unified-accounts/src/benchmarking.rs | 8 +++---- pallets/unified-accounts/src/weights.rs | 24 ++++++++++---------- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/chain-extensions/unified-accounts/src/lib.rs b/chain-extensions/unified-accounts/src/lib.rs index b4c60a18af..57d73a9922 100644 --- a/chain-extensions/unified-accounts/src/lib.rs +++ b/chain-extensions/unified-accounts/src/lib.rs @@ -58,14 +58,14 @@ where GetEvmAddress => { let account_id: T::AccountId = env.read_as()?; // charge weight - env.charge_weight(UAWeight::::uam_to_h160())?; + env.charge_weight(UAWeight::::to_h160())?; // write to buffer UA::to_h160(&account_id).using_encoded(|r| env.write(r, false, None))?; } GetEvmAddressOrDefault => { let account_id: T::AccountId = env.read_as()?; // charge weight - env.charge_weight(UAWeight::::uam_to_h160_or_default())?; + env.charge_weight(UAWeight::::to_h160_or_default())?; let evm_address = if let Some(h160) = UA::to_h160(&account_id) { UnifiedAddress::Mapped(h160) @@ -78,14 +78,14 @@ where GetNativeAddress => { let evm_address: EvmAddress = env.read_as()?; // charge weight - env.charge_weight(UAWeight::::uam_to_account_id())?; + env.charge_weight(UAWeight::::to_account_id())?; // write to buffer UA::to_account_id(&evm_address).using_encoded(|r| env.write(r, false, None))?; } GetNativeAddressOrDefault => { let evm_address: EvmAddress = env.read_as()?; // charge weight - env.charge_weight(UAWeight::::uam_to_account_id_or_default())?; + env.charge_weight(UAWeight::::to_account_id_or_default())?; // read the storage item let native_address = if let Some(native) = UA::to_account_id(&evm_address) { diff --git a/chain-extensions/xvm/src/lib.rs b/chain-extensions/xvm/src/lib.rs index 6fb194bedc..ac232e4cc0 100644 --- a/chain-extensions/xvm/src/lib.rs +++ b/chain-extensions/xvm/src/lib.rs @@ -100,7 +100,7 @@ where if value > 0 { // `UA::to_h160`. actual_weight.saturating_accrue( - ::WeightInfo::uam_to_h160(), + ::WeightInfo::to_h160(), ); if UA::to_h160(&source).is_none() { diff --git a/pallets/unified-accounts/src/benchmarking.rs b/pallets/unified-accounts/src/benchmarking.rs index f40dbc31ce..bec5f65d59 100644 --- a/pallets/unified-accounts/src/benchmarking.rs +++ b/pallets/unified-accounts/src/benchmarking.rs @@ -85,7 +85,7 @@ mod benchmarks { } #[benchmark] - fn uam_to_account_id() { + fn to_account_id() { let caller: T::AccountId = whitelisted_caller(); let evm_address = T::DefaultNativeToEvm::into_h160(caller.clone()); // claim mapping @@ -100,7 +100,7 @@ mod benchmarks { } #[benchmark] - fn uam_to_account_id_or_default() { + fn to_account_id_or_default() { let caller: T::AccountId = whitelisted_caller(); let evm_address = T::DefaultNativeToEvm::into_h160(caller.clone()); // claim mapping @@ -117,7 +117,7 @@ mod benchmarks { } #[benchmark] - fn uam_to_h160() { + fn to_h160() { let caller: T::AccountId = whitelisted_caller(); // claim mapping assert_ok!(Pallet::::claim_default_evm_address( @@ -131,7 +131,7 @@ mod benchmarks { } #[benchmark] - fn uam_to_h160_or_default() { + fn to_h160_or_default() { let caller: T::AccountId = whitelisted_caller(); // claim mapping assert_ok!(Pallet::::claim_default_evm_address( diff --git a/pallets/unified-accounts/src/weights.rs b/pallets/unified-accounts/src/weights.rs index 4838a8d0e4..6b0602a59d 100644 --- a/pallets/unified-accounts/src/weights.rs +++ b/pallets/unified-accounts/src/weights.rs @@ -51,10 +51,10 @@ use core::marker::PhantomData; pub trait WeightInfo { fn claim_evm_address() -> Weight; fn claim_default_evm_address() -> Weight; - fn uam_to_account_id() -> Weight; - fn uam_to_account_id_or_default() -> Weight; - fn uam_to_h160() -> Weight; - fn uam_to_h160_or_default() -> Weight; + fn to_account_id() -> Weight; + fn to_account_id_or_default() -> Weight; + fn to_h160() -> Weight; + fn to_h160_or_default() -> Weight; } /// Weights for pallet_unified_accounts using the Substrate node and recommended hardware. @@ -94,7 +94,7 @@ impl WeightInfo for SubstrateWeight { } /// Storage: UnifiedAccounts EvmToNative (r:1 w:0) /// Proof: UnifiedAccounts EvmToNative (max_values: None, max_size: Some(68), added: 2543, mode: MaxEncodedLen) - fn uam_to_account_id() -> Weight { + fn to_account_id() -> Weight { // Proof Size summary in bytes: // Measured: `170` // Estimated: `3533` @@ -104,7 +104,7 @@ impl WeightInfo for SubstrateWeight { } /// Storage: UnifiedAccounts EvmToNative (r:1 w:0) /// Proof: UnifiedAccounts EvmToNative (max_values: None, max_size: Some(68), added: 2543, mode: MaxEncodedLen) - fn uam_to_account_id_or_default() -> Weight { + fn to_account_id_or_default() -> Weight { // Proof Size summary in bytes: // Measured: `170` // Estimated: `3533` @@ -114,7 +114,7 @@ impl WeightInfo for SubstrateWeight { } /// Storage: UnifiedAccounts NativeToEvm (r:1 w:0) /// Proof: UnifiedAccounts NativeToEvm (max_values: None, max_size: Some(68), added: 2543, mode: MaxEncodedLen) - fn uam_to_h160() -> Weight { + fn to_h160() -> Weight { // Proof Size summary in bytes: // Measured: `170` // Estimated: `3533` @@ -124,7 +124,7 @@ impl WeightInfo for SubstrateWeight { } /// Storage: UnifiedAccounts NativeToEvm (r:1 w:0) /// Proof: UnifiedAccounts NativeToEvm (max_values: None, max_size: Some(68), added: 2543, mode: MaxEncodedLen) - fn uam_to_h160_or_default() -> Weight { + fn to_h160_or_default() -> Weight { // Proof Size summary in bytes: // Measured: `170` // Estimated: `3533` @@ -170,7 +170,7 @@ impl WeightInfo for () { } /// Storage: UnifiedAccounts EvmToNative (r:1 w:0) /// Proof: UnifiedAccounts EvmToNative (max_values: None, max_size: Some(68), added: 2543, mode: MaxEncodedLen) - fn uam_to_account_id() -> Weight { + fn to_account_id() -> Weight { // Proof Size summary in bytes: // Measured: `170` // Estimated: `3533` @@ -180,7 +180,7 @@ impl WeightInfo for () { } /// Storage: UnifiedAccounts EvmToNative (r:1 w:0) /// Proof: UnifiedAccounts EvmToNative (max_values: None, max_size: Some(68), added: 2543, mode: MaxEncodedLen) - fn uam_to_account_id_or_default() -> Weight { + fn to_account_id_or_default() -> Weight { // Proof Size summary in bytes: // Measured: `170` // Estimated: `3533` @@ -190,7 +190,7 @@ impl WeightInfo for () { } /// Storage: UnifiedAccounts NativeToEvm (r:1 w:0) /// Proof: UnifiedAccounts NativeToEvm (max_values: None, max_size: Some(68), added: 2543, mode: MaxEncodedLen) - fn uam_to_h160() -> Weight { + fn to_h160() -> Weight { // Proof Size summary in bytes: // Measured: `170` // Estimated: `3533` @@ -200,7 +200,7 @@ impl WeightInfo for () { } /// Storage: UnifiedAccounts NativeToEvm (r:1 w:0) /// Proof: UnifiedAccounts NativeToEvm (max_values: None, max_size: Some(68), added: 2543, mode: MaxEncodedLen) - fn uam_to_h160_or_default() -> Weight { + fn to_h160_or_default() -> Weight { // Proof Size summary in bytes: // Measured: `170` // Estimated: `3533` From 608cc5a9aeefa9e7407e971495bf30341a62015a Mon Sep 17 00:00:00 2001 From: Ashutosh Varma Date: Wed, 25 Oct 2023 14:39:50 +0530 Subject: [PATCH 4/6] feat: use enum in uma trait --- .../types/unified-accounts/src/lib.rs | 2 ++ chain-extensions/unified-accounts/src/lib.rs | 28 ++++------------- pallets/unified-accounts/src/lib.rs | 30 +++++++++++-------- primitives/src/evm.rs | 28 +++++++++++++++-- tests/integration/src/unified_accounts.rs | 6 ++-- 5 files changed, 53 insertions(+), 41 deletions(-) diff --git a/chain-extensions/types/unified-accounts/src/lib.rs b/chain-extensions/types/unified-accounts/src/lib.rs index 7371e3a9e0..c090ba32e8 100644 --- a/chain-extensions/types/unified-accounts/src/lib.rs +++ b/chain-extensions/types/unified-accounts/src/lib.rs @@ -39,8 +39,10 @@ pub enum Command { pub enum UnifiedAddress { /// The address fetched from the mappings and the account /// is unified + #[codec(index = 0)] Mapped(T), /// The default address associated with account as there /// is no mapping found and accounts are not unified + #[codec(index = 1)] Default(T), } diff --git a/chain-extensions/unified-accounts/src/lib.rs b/chain-extensions/unified-accounts/src/lib.rs index 57d73a9922..b4d1efda16 100644 --- a/chain-extensions/unified-accounts/src/lib.rs +++ b/chain-extensions/unified-accounts/src/lib.rs @@ -18,10 +18,7 @@ #![cfg_attr(not(feature = "std"), no_std)] -use astar_primitives::{ - ethereum_checked::AccountMapping, - evm::{EvmAddress, UnifiedAddressMapper}, -}; +use astar_primitives::evm::{EvmAddress, UnifiedAddressMapper}; use core::marker::PhantomData; use sp_runtime::DispatchError; @@ -29,13 +26,9 @@ use frame_support::DefaultNoBound; use pallet_contracts::chain_extension::{ ChainExtension, Environment, Ext, InitState, Result as DispatchResult, RetVal, }; -use pallet_evm::AddressMapping; use pallet_unified_accounts::WeightInfo; use parity_scale_codec::Encode; -pub use unified_accounts_chain_extension_types::{ - Command::{self, *}, - UnifiedAddress, -}; +pub use unified_accounts_chain_extension_types::Command::{self, *}; type UAWeight = ::WeightInfo; @@ -67,13 +60,8 @@ where // charge weight env.charge_weight(UAWeight::::to_h160_or_default())?; - let evm_address = if let Some(h160) = UA::to_h160(&account_id) { - UnifiedAddress::Mapped(h160) - } else { - UnifiedAddress::Default(T::DefaultNativeToEvm::into_h160(account_id)) - }; // write to buffer - evm_address.using_encoded(|r| env.write(r, false, None))?; + UA::to_h160_or_default(&account_id).using_encoded(|r| env.write(r, false, None))?; } GetNativeAddress => { let evm_address: EvmAddress = env.read_as()?; @@ -87,15 +75,9 @@ where // charge weight env.charge_weight(UAWeight::::to_account_id_or_default())?; - // read the storage item - let native_address = if let Some(native) = UA::to_account_id(&evm_address) { - UnifiedAddress::Mapped(native) - } else { - UnifiedAddress::Default(T::DefaultEvmToNative::into_account_id(evm_address)) - }; - // write to buffer - native_address.using_encoded(|r| env.write(r, false, None))?; + UA::to_account_id_or_default(&evm_address) + .using_encoded(|r| env.write(r, false, None))?; } }; Ok(RetVal::Converging(0)) diff --git a/pallets/unified-accounts/src/lib.rs b/pallets/unified-accounts/src/lib.rs index cb804d2f3b..8811159619 100644 --- a/pallets/unified-accounts/src/lib.rs +++ b/pallets/unified-accounts/src/lib.rs @@ -67,7 +67,7 @@ use astar_primitives::{ ethereum_checked::AccountMapping, - evm::{EvmAddress, UnifiedAddressMapper}, + evm::{EvmAddress, UnifiedAddress, UnifiedAddressMapper}, Balance, }; use frame_support::{ @@ -351,11 +351,12 @@ impl UnifiedAddressMapper for Pallet { EvmToNative::::get(evm_address) } - fn to_account_id_or_default(evm_address: &EvmAddress) -> T::AccountId { - EvmToNative::::get(evm_address).unwrap_or_else(|| { - // fallback to default account_id - T::DefaultEvmToNative::into_account_id(evm_address.clone()) - }) + fn to_account_id_or_default(evm_address: &EvmAddress) -> UnifiedAddress { + if let Some(native) = Self::to_account_id(&evm_address) { + UnifiedAddress::Mapped(native) + } else { + UnifiedAddress::Default(T::DefaultEvmToNative::into_account_id(evm_address.clone())) + } } fn to_default_account_id(evm_address: &EvmAddress) -> T::AccountId { @@ -366,11 +367,12 @@ impl UnifiedAddressMapper for Pallet { NativeToEvm::::get(account_id) } - fn to_h160_or_default(account_id: &T::AccountId) -> EvmAddress { - NativeToEvm::::get(account_id).unwrap_or_else(|| { - // fallback to default account_id - T::DefaultNativeToEvm::into_h160(account_id.clone()) - }) + fn to_h160_or_default(account_id: &T::AccountId) -> UnifiedAddress { + if let Some(h160) = Self::to_h160(&account_id) { + UnifiedAddress::Mapped(h160) + } else { + UnifiedAddress::Default(T::DefaultNativeToEvm::into_h160(account_id.clone())) + } } fn to_default_h160(account_id: &T::AccountId) -> EvmAddress { @@ -381,7 +383,7 @@ impl UnifiedAddressMapper for Pallet { /// AccountMapping wrapper implementation impl AccountMapping for Pallet { fn into_h160(account: T::AccountId) -> H160 { - >::to_h160_or_default(&account) + >::to_h160_or_default(&account).into_address() } } @@ -389,6 +391,7 @@ impl AccountMapping for Pallet { impl AddressMapping for Pallet { fn into_account_id(evm_address: H160) -> T::AccountId { >::to_account_id_or_default(&evm_address) + .into_address() } } @@ -415,7 +418,8 @@ impl StaticLookup for Pallet { MultiAddress::Address20(i) => Ok( >::to_account_id_or_default( &EvmAddress::from_slice(&i), - ), + ) + .into_address(), ), _ => Err(LookupError), } diff --git a/primitives/src/evm.rs b/primitives/src/evm.rs index 766f7bf7a8..9b1a5ae6ee 100644 --- a/primitives/src/evm.rs +++ b/primitives/src/evm.rs @@ -19,6 +19,8 @@ use crate::{AccountId, AssetId}; use frame_support::ensure; +use parity_scale_codec::{Decode, Encode, MaxEncodedLen}; +use scale_info::TypeInfo; use sp_core::H160; use sp_std::marker::PhantomData; @@ -64,7 +66,7 @@ pub trait UnifiedAddressMapper { fn to_account_id(evm_address: &EvmAddress) -> Option; /// Gets the account id associated with given evm address. /// If no mapping exists, then return the default evm address. - fn to_account_id_or_default(evm_address: &EvmAddress) -> AccountId; + fn to_account_id_or_default(evm_address: &EvmAddress) -> UnifiedAddress; /// Gets the default account id which is associated with given evm address. fn to_default_account_id(evm_address: &EvmAddress) -> AccountId; @@ -72,7 +74,29 @@ pub trait UnifiedAddressMapper { fn to_h160(account_id: &AccountId) -> Option; /// Gets the evm address associated with given account id. /// If no mapping exists, then return the default account id. - fn to_h160_or_default(account_id: &AccountId) -> EvmAddress; + fn to_h160_or_default(account_id: &AccountId) -> UnifiedAddress; /// Gets the default evm address which is associated with given account id. fn to_default_h160(account_id: &AccountId) -> EvmAddress; } + +#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, MaxEncodedLen, TypeInfo)] +pub enum UnifiedAddress
{ + /// The address fetched from the mappings and the account + /// is unified + #[codec(index = 0)] + Mapped(Address), + /// The default address associated with account as there + /// is no mapping found and accounts are not unified + #[codec(index = 1)] + Default(Address), +} + +impl
UnifiedAddress
{ + /// Get the underlying address + pub fn into_address(self) -> Address { + match self { + Self::Default(a) => a, + Self::Mapped(a) => a, + } + } +} diff --git a/tests/integration/src/unified_accounts.rs b/tests/integration/src/unified_accounts.rs index 3029f4dbb5..d1ffa322af 100644 --- a/tests/integration/src/unified_accounts.rs +++ b/tests/integration/src/unified_accounts.rs @@ -17,9 +17,9 @@ // along with Astar. If not, see . use crate::setup::*; +use astar_primitives::evm::UnifiedAddress; use parity_scale_codec::Encode; use sp_io::hashing::keccak_256; -use unified_accounts_chain_extension_types::UnifiedAddress; const AU_CE_GETTER: &'static str = "au_ce_getters"; @@ -70,7 +70,7 @@ fn unified_accounts_chain_extension_works() { contract_id.clone(), [GET_H160_OR_DEFAULT.to_vec(), ALICE.encode()].concat() ), - UnifiedAddress::Default(UnifiedAccounts::to_h160_or_default(&ALICE)) + UnifiedAccounts::to_h160_or_default(&ALICE) ); // mapped native address should be None assert_eq!( @@ -88,7 +88,7 @@ fn unified_accounts_chain_extension_works() { contract_id.clone(), [GET_NATIVE_OR_DEFAULT.to_vec(), alith().encode()].concat() ), - UnifiedAddress::Default(UnifiedAccounts::to_account_id_or_default(&alith())) + UnifiedAccounts::to_account_id_or_default(&alith()) ); // From e702a4d1195e3ec48d30c861186877256835b708 Mon Sep 17 00:00:00 2001 From: Ashutosh Varma Date: Fri, 27 Oct 2023 15:51:59 +0530 Subject: [PATCH 5/6] fix: benchmarks --- pallets/unified-accounts/src/benchmarking.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pallets/unified-accounts/src/benchmarking.rs b/pallets/unified-accounts/src/benchmarking.rs index bec5f65d59..0ea2fe6e15 100644 --- a/pallets/unified-accounts/src/benchmarking.rs +++ b/pallets/unified-accounts/src/benchmarking.rs @@ -88,6 +88,10 @@ mod benchmarks { fn to_account_id() { let caller: T::AccountId = whitelisted_caller(); let evm_address = T::DefaultNativeToEvm::into_h160(caller.clone()); + assert_ok!(T::Currency::mint_into( + &caller, + T::AccountMappingStorageFee::get() + )); // claim mapping assert_ok!(Pallet::::claim_default_evm_address( RawOrigin::Signed(caller.clone()).into() @@ -103,6 +107,10 @@ mod benchmarks { fn to_account_id_or_default() { let caller: T::AccountId = whitelisted_caller(); let evm_address = T::DefaultNativeToEvm::into_h160(caller.clone()); + assert_ok!(T::Currency::mint_into( + &caller, + T::AccountMappingStorageFee::get() + )); // claim mapping assert_ok!(Pallet::::claim_default_evm_address( RawOrigin::Signed(caller.clone()).into() @@ -119,6 +127,10 @@ mod benchmarks { #[benchmark] fn to_h160() { let caller: T::AccountId = whitelisted_caller(); + assert_ok!(T::Currency::mint_into( + &caller, + T::AccountMappingStorageFee::get() + )); // claim mapping assert_ok!(Pallet::::claim_default_evm_address( RawOrigin::Signed(caller.clone()).into() @@ -133,6 +145,10 @@ mod benchmarks { #[benchmark] fn to_h160_or_default() { let caller: T::AccountId = whitelisted_caller(); + assert_ok!(T::Currency::mint_into( + &caller, + T::AccountMappingStorageFee::get() + )); // claim mapping assert_ok!(Pallet::::claim_default_evm_address( RawOrigin::Signed(caller.clone()).into() From 06113098d2816eb70c1d528288ad83dd77b33045 Mon Sep 17 00:00:00 2001 From: Ashutosh Varma Date: Fri, 27 Oct 2023 20:44:23 +0530 Subject: [PATCH 6/6] feat: update weights from benchmarks --- pallets/unified-accounts/src/weights.rs | 69 ++++++++++++------------- 1 file changed, 34 insertions(+), 35 deletions(-) diff --git a/pallets/unified-accounts/src/weights.rs b/pallets/unified-accounts/src/weights.rs index 6b0602a59d..4d87c151a2 100644 --- a/pallets/unified-accounts/src/weights.rs +++ b/pallets/unified-accounts/src/weights.rs @@ -1,4 +1,3 @@ - // This file is part of Astar. // Copyright (C) 2019-2023 Stake Technologies Pte.Ltd. @@ -20,7 +19,7 @@ //! Autogenerated weights for pallet_unified_accounts //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2023-09-26, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2023-10-27, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]` //! WORST CASE MAP SIZE: `1000000` //! HOSTNAME: `devserver-01`, CPU: `Intel(R) Xeon(R) E-2236 CPU @ 3.40GHz` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("shibuya-dev"), DB CACHE: 1024 @@ -37,7 +36,7 @@ // --execution=wasm // --wasm-execution=compiled // --heap-pages=4096 -// --output=./benchmark-results/unified_accounts_weights.rs +// --output=./benchmark-results/shibuya-dev/unified_accounts_weights.rs // --template=./scripts/templates/weight-template.hbs #![cfg_attr(rustfmt, rustfmt_skip)] @@ -60,10 +59,10 @@ pub trait WeightInfo { /// Weights for pallet_unified_accounts using the Substrate node and recommended hardware. pub struct SubstrateWeight(PhantomData); impl WeightInfo for SubstrateWeight { - /// Storage: UnifiedAccounts EvmToNative (r:1 w:1) - /// Proof: UnifiedAccounts EvmToNative (max_values: None, max_size: Some(68), added: 2543, mode: MaxEncodedLen) /// Storage: UnifiedAccounts NativeToEvm (r:1 w:1) /// Proof: UnifiedAccounts NativeToEvm (max_values: None, max_size: Some(68), added: 2543, mode: MaxEncodedLen) + /// Storage: UnifiedAccounts EvmToNative (r:1 w:1) + /// Proof: UnifiedAccounts EvmToNative (max_values: None, max_size: Some(68), added: 2543, mode: MaxEncodedLen) /// Storage: EVMChainId ChainId (r:1 w:0) /// Proof: EVMChainId ChainId (max_values: Some(1), max_size: Some(8), added: 503, mode: MaxEncodedLen) /// Storage: System BlockHash (r:1 w:0) @@ -74,21 +73,21 @@ impl WeightInfo for SubstrateWeight { // Proof Size summary in bytes: // Measured: `256` // Estimated: `3593` - // Minimum execution time: 64_843_000 picoseconds. - Weight::from_parts(65_508_000, 3593) + // Minimum execution time: 91_231_000 picoseconds. + Weight::from_parts(91_688_000, 3593) .saturating_add(T::DbWeight::get().reads(5_u64)) .saturating_add(T::DbWeight::get().writes(2_u64)) } - /// Storage: UnifiedAccounts EvmToNative (r:1 w:1) - /// Proof: UnifiedAccounts EvmToNative (max_values: None, max_size: Some(68), added: 2543, mode: MaxEncodedLen) /// Storage: UnifiedAccounts NativeToEvm (r:1 w:1) /// Proof: UnifiedAccounts NativeToEvm (max_values: None, max_size: Some(68), added: 2543, mode: MaxEncodedLen) + /// Storage: UnifiedAccounts EvmToNative (r:1 w:1) + /// Proof: UnifiedAccounts EvmToNative (max_values: None, max_size: Some(68), added: 2543, mode: MaxEncodedLen) fn claim_default_evm_address() -> Weight { // Proof Size summary in bytes: // Measured: `42` // Estimated: `3533` - // Minimum execution time: 16_399_000 picoseconds. - Weight::from_parts(16_806_000, 3533) + // Minimum execution time: 40_749_000 picoseconds. + Weight::from_parts(41_411_000, 3533) .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().writes(2_u64)) } @@ -98,8 +97,8 @@ impl WeightInfo for SubstrateWeight { // Proof Size summary in bytes: // Measured: `170` // Estimated: `3533` - // Minimum execution time: 6_000_000 picoseconds. - Weight::from_parts(7_000_000, 3533) + // Minimum execution time: 5_478_000 picoseconds. + Weight::from_parts(5_661_000, 3533) .saturating_add(T::DbWeight::get().reads(1_u64)) } /// Storage: UnifiedAccounts EvmToNative (r:1 w:0) @@ -108,8 +107,8 @@ impl WeightInfo for SubstrateWeight { // Proof Size summary in bytes: // Measured: `170` // Estimated: `3533` - // Minimum execution time: 6_000_000 picoseconds. - Weight::from_parts(7_000_000, 3533) + // Minimum execution time: 5_515_000 picoseconds. + Weight::from_parts(5_700_000, 3533) .saturating_add(T::DbWeight::get().reads(1_u64)) } /// Storage: UnifiedAccounts NativeToEvm (r:1 w:0) @@ -118,8 +117,8 @@ impl WeightInfo for SubstrateWeight { // Proof Size summary in bytes: // Measured: `170` // Estimated: `3533` - // Minimum execution time: 6_000_000 picoseconds. - Weight::from_parts(6_000_000, 3533) + // Minimum execution time: 5_626_000 picoseconds. + Weight::from_parts(5_827_000, 3533) .saturating_add(T::DbWeight::get().reads(1_u64)) } /// Storage: UnifiedAccounts NativeToEvm (r:1 w:0) @@ -128,18 +127,18 @@ impl WeightInfo for SubstrateWeight { // Proof Size summary in bytes: // Measured: `170` // Estimated: `3533` - // Minimum execution time: 6_000_000 picoseconds. - Weight::from_parts(6_000_000, 3533) + // Minimum execution time: 5_578_000 picoseconds. + Weight::from_parts(5_719_000, 3533) .saturating_add(T::DbWeight::get().reads(1_u64)) } } // For backwards compatibility and tests impl WeightInfo for () { - /// Storage: UnifiedAccounts EvmToNative (r:1 w:1) - /// Proof: UnifiedAccounts EvmToNative (max_values: None, max_size: Some(68), added: 2543, mode: MaxEncodedLen) /// Storage: UnifiedAccounts NativeToEvm (r:1 w:1) /// Proof: UnifiedAccounts NativeToEvm (max_values: None, max_size: Some(68), added: 2543, mode: MaxEncodedLen) + /// Storage: UnifiedAccounts EvmToNative (r:1 w:1) + /// Proof: UnifiedAccounts EvmToNative (max_values: None, max_size: Some(68), added: 2543, mode: MaxEncodedLen) /// Storage: EVMChainId ChainId (r:1 w:0) /// Proof: EVMChainId ChainId (max_values: Some(1), max_size: Some(8), added: 503, mode: MaxEncodedLen) /// Storage: System BlockHash (r:1 w:0) @@ -150,21 +149,21 @@ impl WeightInfo for () { // Proof Size summary in bytes: // Measured: `256` // Estimated: `3593` - // Minimum execution time: 64_843_000 picoseconds. - Weight::from_parts(65_508_000, 3593) + // Minimum execution time: 91_231_000 picoseconds. + Weight::from_parts(91_688_000, 3593) .saturating_add(RocksDbWeight::get().reads(5_u64)) .saturating_add(RocksDbWeight::get().writes(2_u64)) } - /// Storage: UnifiedAccounts EvmToNative (r:1 w:1) - /// Proof: UnifiedAccounts EvmToNative (max_values: None, max_size: Some(68), added: 2543, mode: MaxEncodedLen) /// Storage: UnifiedAccounts NativeToEvm (r:1 w:1) /// Proof: UnifiedAccounts NativeToEvm (max_values: None, max_size: Some(68), added: 2543, mode: MaxEncodedLen) + /// Storage: UnifiedAccounts EvmToNative (r:1 w:1) + /// Proof: UnifiedAccounts EvmToNative (max_values: None, max_size: Some(68), added: 2543, mode: MaxEncodedLen) fn claim_default_evm_address() -> Weight { // Proof Size summary in bytes: // Measured: `42` // Estimated: `3533` - // Minimum execution time: 16_399_000 picoseconds. - Weight::from_parts(16_806_000, 3533) + // Minimum execution time: 40_749_000 picoseconds. + Weight::from_parts(41_411_000, 3533) .saturating_add(RocksDbWeight::get().reads(2_u64)) .saturating_add(RocksDbWeight::get().writes(2_u64)) } @@ -174,8 +173,8 @@ impl WeightInfo for () { // Proof Size summary in bytes: // Measured: `170` // Estimated: `3533` - // Minimum execution time: 6_000_000 picoseconds. - Weight::from_parts(7_000_000, 3533) + // Minimum execution time: 5_478_000 picoseconds. + Weight::from_parts(5_661_000, 3533) .saturating_add(RocksDbWeight::get().reads(1_u64)) } /// Storage: UnifiedAccounts EvmToNative (r:1 w:0) @@ -184,8 +183,8 @@ impl WeightInfo for () { // Proof Size summary in bytes: // Measured: `170` // Estimated: `3533` - // Minimum execution time: 6_000_000 picoseconds. - Weight::from_parts(7_000_000, 3533) + // Minimum execution time: 5_515_000 picoseconds. + Weight::from_parts(5_700_000, 3533) .saturating_add(RocksDbWeight::get().reads(1_u64)) } /// Storage: UnifiedAccounts NativeToEvm (r:1 w:0) @@ -194,8 +193,8 @@ impl WeightInfo for () { // Proof Size summary in bytes: // Measured: `170` // Estimated: `3533` - // Minimum execution time: 6_000_000 picoseconds. - Weight::from_parts(6_000_000, 3533) + // Minimum execution time: 5_626_000 picoseconds. + Weight::from_parts(5_827_000, 3533) .saturating_add(RocksDbWeight::get().reads(1_u64)) } /// Storage: UnifiedAccounts NativeToEvm (r:1 w:0) @@ -204,8 +203,8 @@ impl WeightInfo for () { // Proof Size summary in bytes: // Measured: `170` // Estimated: `3533` - // Minimum execution time: 6_000_000 picoseconds. - Weight::from_parts(6_000_000, 3533) + // Minimum execution time: 5_578_000 picoseconds. + Weight::from_parts(5_719_000, 3533) .saturating_add(RocksDbWeight::get().reads(1_u64)) } }