Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add UnifiedAddressMapper trait impl bechmarks #1066

Merged
merged 6 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions chain-extensions/unified-accounts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> = <T as pallet_unified_accounts::Config>::WeightInfo;

#[derive(DefaultNoBound)]
pub struct UnifiedAccountsExtension<T, UA>(PhantomData<(T, UA)>);

Expand All @@ -54,17 +57,15 @@ where
})? {
GetEvmAddress => {
let account_id: T::AccountId = env.read_as()?;

let base_weight = <T as frame_system::Config>::DbWeight::get().reads(1);
env.charge_weight(base_weight)?;
// charge weight
env.charge_weight(UAWeight::<T>::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 = <T as frame_system::Config>::DbWeight::get().reads(1);
env.charge_weight(base_weight)?;
// charge weight
env.charge_weight(UAWeight::<T>::uam_to_h160_or_default())?;
ashutoshvarma marked this conversation as resolved.
Show resolved Hide resolved

let evm_address = if let Some(h160) = UA::to_h160(&account_id) {
UnifiedAddress::Mapped(h160)
Expand All @@ -76,17 +77,15 @@ where
}
GetNativeAddress => {
let evm_address: EvmAddress = env.read_as()?;

let base_weight = <T as frame_system::Config>::DbWeight::get().reads(1);
env.charge_weight(base_weight)?;
// charge weight
env.charge_weight(UAWeight::<T>::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 = <T as frame_system::Config>::DbWeight::get().reads(1);
env.charge_weight(base_weight)?;
// charge weight
env.charge_weight(UAWeight::<T>::uam_to_account_id_or_default())?;

// read the storage item
let native_address = if let Some(native) = UA::to_account_id(&evm_address) {
Expand Down
8 changes: 5 additions & 3 deletions chain-extensions/xvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
<T as pallet_unified_accounts::Config>::WeightInfo::uam_to_h160(),
);
ashutoshvarma marked this conversation as resolved.
Show resolved Hide resolved

if UA::to_h160(&source).is_none() {
let weight_of_claim = <T as pallet_unified_accounts::Config>::WeightInfo::claim_default_evm_address();
Expand Down
60 changes: 60 additions & 0 deletions pallets/unified-accounts/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,64 @@ mod benchmarks {
.into(),
);
}

#[benchmark]
fn uam_to_account_id() {
ashutoshvarma marked this conversation as resolved.
Show resolved Hide resolved
let caller: T::AccountId = whitelisted_caller();
let evm_address = T::DefaultNativeToEvm::into_h160(caller.clone());
// claim mapping
assert_ok!(Pallet::<T>::claim_default_evm_address(
RawOrigin::Signed(caller.clone()).into()
));

#[block]
{
let _ = <Pallet<T> as UnifiedAddressMapper<T::AccountId>>::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::<T>::claim_default_evm_address(
RawOrigin::Signed(caller.clone()).into()
));

#[block]
{
let _ = <Pallet<T> as UnifiedAddressMapper<T::AccountId>>::to_account_id_or_default(
&evm_address,
);
}
}

#[benchmark]
fn uam_to_h160() {
let caller: T::AccountId = whitelisted_caller();
// claim mapping
assert_ok!(Pallet::<T>::claim_default_evm_address(
RawOrigin::Signed(caller.clone()).into()
));

#[block]
{
let _ = <Pallet<T> as UnifiedAddressMapper<T::AccountId>>::to_h160(&caller);
}
}

#[benchmark]
fn uam_to_h160_or_default() {
let caller: T::AccountId = whitelisted_caller();
// claim mapping
assert_ok!(Pallet::<T>::claim_default_evm_address(
RawOrigin::Signed(caller.clone()).into()
));

#[block]
{
let _ = <Pallet<T> as UnifiedAddressMapper<T::AccountId>>::to_h160_or_default(&caller);
}
}
}
84 changes: 84 additions & 0 deletions pallets/unified-accounts/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -88,6 +92,46 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
.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
Expand Down Expand Up @@ -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))
}
}
Loading