Skip to content

Commit

Permalink
feat: apply benchmarks weights & code suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
ashutoshvarma committed Sep 26, 2023
1 parent 037028a commit 9fcc5a6
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 44 deletions.
2 changes: 1 addition & 1 deletion pallets/unified-accounts/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ mod benchmarks {
fn claim_default_evm_address() {
let caller: T::AccountId = whitelisted_caller();
let caller_clone = caller.clone();
let evm_address = T::DefaultAccountMapping::into_h160(caller.clone());
let evm_address = T::DefaultNativeToEvm::into_h160(caller.clone());

#[extrinsic_call]
_(RawOrigin::Signed(caller));
Expand Down
16 changes: 8 additions & 8 deletions pallets/unified-accounts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ pub mod pallet {
/// The Currency for managing evm address assets
type Currency: Mutate<Self::AccountId>;
/// Default evm address to account id conversion
type DefaultAddressMapping: AddressMapping<Self::AccountId>;
type DefaultEvmToNative: AddressMapping<Self::AccountId>;
/// Default account id to evm address conversion
type DefaultAccountMapping: AccountMapping<Self::AccountId>;
type DefaultNativeToEvm: AccountMapping<Self::AccountId>;
/// EVM chain id
type ChainId: Get<u64>;
/// Weight information for the extrinsics in this module
Expand Down Expand Up @@ -191,7 +191,7 @@ pub mod pallet {
ensure!(evm_address == address, Error::<T>::InvalidSignature);

// Check if the default account id already exists for this evm address
let default_account_id = T::DefaultAddressMapping::into_account_id(evm_address.clone());
let default_account_id = T::DefaultEvmToNative::into_account_id(evm_address.clone());
if frame_system::Pallet::<T>::account_exists(&default_account_id) {
// Transfer all the free native balance from old account id to the newly
// since this `default_account_id` will no longer be connected to evm address
Expand Down Expand Up @@ -239,7 +239,7 @@ impl<T: Config> Pallet<T> {
Error::<T>::AlreadyMapped
);
// get the default evm address
let evm_address = T::DefaultAccountMapping::into_h160(account_id.clone());
let evm_address = T::DefaultNativeToEvm::into_h160(account_id.clone());
// make sure default address is not already mapped, this should not
// happen but for sanity check.
ensure!(
Expand Down Expand Up @@ -332,12 +332,12 @@ impl<T: Config> UnifiedAddressMapper<T::AccountId> for Pallet<T> {
fn to_account_id_or_default(evm_address: &EvmAddress) -> T::AccountId {
NativeToEvm::<T>::get(evm_address).unwrap_or_else(|| {
// fallback to default account_id
T::DefaultAddressMapping::into_account_id(evm_address.clone())
T::DefaultEvmToNative::into_account_id(evm_address.clone())
})
}

fn to_default_account_id(evm_address: &EvmAddress) -> T::AccountId {
T::DefaultAddressMapping::into_account_id(evm_address.clone())
T::DefaultEvmToNative::into_account_id(evm_address.clone())
}

fn to_h160(account_id: &T::AccountId) -> Option<EvmAddress> {
Expand All @@ -347,12 +347,12 @@ impl<T: Config> UnifiedAddressMapper<T::AccountId> for Pallet<T> {
fn to_h160_or_default(account_id: &T::AccountId) -> EvmAddress {
EvmToNative::<T>::get(account_id).unwrap_or_else(|| {
// fallback to default account_id
T::DefaultAccountMapping::into_h160(account_id.clone())
T::DefaultNativeToEvm::into_h160(account_id.clone())
})
}

fn to_default_h160(account_id: &T::AccountId) -> EvmAddress {
T::DefaultAccountMapping::into_h160(account_id.clone())
T::DefaultNativeToEvm::into_h160(account_id.clone())
}
}

Expand Down
4 changes: 2 additions & 2 deletions pallets/unified-accounts/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ parameter_types! {
impl pallet_unified_accounts::Config for TestRuntime {
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type DefaultAddressMapping = HashedAddressMapping<BlakeTwo256>;
type DefaultAccountMapping = HashedAccountMapping<BlakeTwo256>;
type DefaultEvmToNative = HashedAddressMapping<BlakeTwo256>;
type DefaultNativeToEvm = HashedAccountMapping<BlakeTwo256>;
type ChainId = ChainId;
type WeightInfo = ();
}
Expand Down
4 changes: 2 additions & 2 deletions pallets/unified-accounts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ fn account_claim_should_work() {
ExtBuilder::default().build().execute_with(|| {
let alice_eth = UnifiedAccounts::eth_address(&alice_secret());
// default ss58 account associated with eth address
let alice_eth_old_account = <TestRuntime as Config>::DefaultAddressMapping::into_account_id(alice_eth.clone());
let alice_eth_old_account = <TestRuntime as Config>::DefaultEvmToNative::into_account_id(alice_eth.clone());
let signature = get_evm_signature(&ALICE, &alice_secret());

// transfer some funds to alice_eth (H160)
Expand Down Expand Up @@ -187,7 +187,7 @@ fn account_claim_should_work() {
fn account_default_claim_works() {
ExtBuilder::default().build().execute_with(|| {
let alice_default_evm =
<TestRuntime as Config>::DefaultAccountMapping::into_h160(ALICE.into());
<TestRuntime as Config>::DefaultNativeToEvm::into_h160(ALICE.into());

// claim default account
assert_ok!(UnifiedAccounts::claim_default_evm_address(
Expand Down
54 changes: 27 additions & 27 deletions pallets/unified-accounts/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
//! Autogenerated weights for pallet_unified_accounts
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2023-09-25, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! DATE: 2023-09-26, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! WORST CASE MAP SIZE: `1000000`
//! HOSTNAME: `Ashutoshs-MacBook-Pro.local`, CPU: `<UNKNOWN>`
//! 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
// Executed Command:
Expand All @@ -37,7 +37,7 @@
// --execution=wasm
// --wasm-execution=compiled
// --heap-pages=4096
// --output=./pallets/unified-accounts/src/weights.rs
// --output=./benchmark-results/unified_accounts_weights.rs
// --template=./scripts/templates/weight-template.hbs

#![cfg_attr(rustfmt, rustfmt_skip)]
Expand All @@ -57,9 +57,9 @@ pub trait WeightInfo {
pub struct SubstrateWeight<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
/// Storage: UnifiedAccounts EvmToNative (r:1 w:1)
/// Proof Skipped: UnifiedAccounts EvmToNative (max_values: None, max_size: None, mode: Measured)
/// Proof: UnifiedAccounts EvmToNative (max_values: None, max_size: Some(68), added: 2543, mode: MaxEncodedLen)
/// Storage: UnifiedAccounts NativeToEvm (r:1 w:1)
/// Proof Skipped: UnifiedAccounts NativeToEvm (max_values: None, max_size: None, mode: Measured)
/// Proof: UnifiedAccounts NativeToEvm (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)
Expand All @@ -69,33 +69,33 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
fn claim_evm_address() -> Weight {
// Proof Size summary in bytes:
// Measured: `256`
// Estimated: `3721`
// Minimum execution time: 58_000_000 picoseconds.
Weight::from_parts(59_000_000, 3721)
// Estimated: `3593`
// Minimum execution time: 64_843_000 picoseconds.
Weight::from_parts(65_508_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 Skipped: UnifiedAccounts EvmToNative (max_values: None, max_size: None, mode: Measured)
/// Storage: UnifiedAccounts NativeToEvm (r:0 w:1)
/// Proof Skipped: UnifiedAccounts NativeToEvm (max_values: None, max_size: None, mode: Measured)
/// 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)
fn claim_default_evm_address() -> Weight {
// Proof Size summary in bytes:
// Measured: `42`
// Estimated: `3507`
// Minimum execution time: 14_000_000 picoseconds.
Weight::from_parts(15_000_000, 3507)
.saturating_add(T::DbWeight::get().reads(1_u64))
// Estimated: `3533`
// Minimum execution time: 16_399_000 picoseconds.
Weight::from_parts(16_806_000, 3533)
.saturating_add(T::DbWeight::get().reads(2_u64))
.saturating_add(T::DbWeight::get().writes(2_u64))
}
}

// For backwards compatibility and tests
impl WeightInfo for () {
/// Storage: UnifiedAccounts EvmToNative (r:1 w:1)
/// Proof Skipped: UnifiedAccounts EvmToNative (max_values: None, max_size: None, mode: Measured)
/// Proof: UnifiedAccounts EvmToNative (max_values: None, max_size: Some(68), added: 2543, mode: MaxEncodedLen)
/// Storage: UnifiedAccounts NativeToEvm (r:1 w:1)
/// Proof Skipped: UnifiedAccounts NativeToEvm (max_values: None, max_size: None, mode: Measured)
/// Proof: UnifiedAccounts NativeToEvm (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)
Expand All @@ -105,23 +105,23 @@ impl WeightInfo for () {
fn claim_evm_address() -> Weight {
// Proof Size summary in bytes:
// Measured: `256`
// Estimated: `3721`
// Minimum execution time: 58_000_000 picoseconds.
Weight::from_parts(59_000_000, 3721)
// Estimated: `3593`
// Minimum execution time: 64_843_000 picoseconds.
Weight::from_parts(65_508_000, 3593)
.saturating_add(RocksDbWeight::get().reads(5_u64))
.saturating_add(RocksDbWeight::get().writes(2_u64))
}
/// Storage: UnifiedAccounts EvmToNative (r:1 w:1)
/// Proof Skipped: UnifiedAccounts EvmToNative (max_values: None, max_size: None, mode: Measured)
/// Storage: UnifiedAccounts NativeToEvm (r:0 w:1)
/// Proof Skipped: UnifiedAccounts NativeToEvm (max_values: None, max_size: None, mode: Measured)
/// 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)
fn claim_default_evm_address() -> Weight {
// Proof Size summary in bytes:
// Measured: `42`
// Estimated: `3507`
// Minimum execution time: 14_000_000 picoseconds.
Weight::from_parts(15_000_000, 3507)
.saturating_add(RocksDbWeight::get().reads(1_u64))
// Estimated: `3533`
// Minimum execution time: 16_399_000 picoseconds.
Weight::from_parts(16_806_000, 3533)
.saturating_add(RocksDbWeight::get().reads(2_u64))
.saturating_add(RocksDbWeight::get().writes(2_u64))
}
}
4 changes: 2 additions & 2 deletions runtime/local/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,8 @@ impl pallet_utility::Config for Runtime {
impl pallet_unified_accounts::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type DefaultAddressMapping = pallet_evm::HashedAddressMapping<BlakeTwo256>;
type DefaultAccountMapping = HashedAccountMapping<BlakeTwo256>;
type DefaultEvmToNative = pallet_evm::HashedAddressMapping<BlakeTwo256>;
type DefaultNativeToEvm = HashedAccountMapping<BlakeTwo256>;
type ChainId = ChainId;
type WeightInfo = pallet_unified_accounts::weights::SubstrateWeight<Self>;
}
Expand Down
4 changes: 2 additions & 2 deletions runtime/shibuya/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1206,8 +1206,8 @@ impl pallet_xc_asset_config::Config for Runtime {
impl pallet_unified_accounts::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type DefaultAddressMapping = pallet_evm::HashedAddressMapping<BlakeTwo256>;
type DefaultAccountMapping = HashedAccountMapping<BlakeTwo256>;
type DefaultEvmToNative = pallet_evm::HashedAddressMapping<BlakeTwo256>;
type DefaultNativeToEvm = HashedAccountMapping<BlakeTwo256>;
type ChainId = EVMChainId;
type WeightInfo = pallet_unified_accounts::weights::SubstrateWeight<Self>;
}
Expand Down

0 comments on commit 9fcc5a6

Please sign in to comment.