Skip to content

Commit

Permalink
feat: add UnifiedAddressMapper trait impl bechmarks (#1066)
Browse files Browse the repository at this point in the history
* feat: update bench for uma

* feat: replace hardcoded weights

* feat: apply code suggestions

* feat: use enum in uma trait

* fix: benchmarks

* feat: update weights from benchmarks
  • Loading branch information
ashutoshvarma authored Oct 29, 2023
1 parent a8525aa commit 0589b5f
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 76 deletions.
2 changes: 2 additions & 0 deletions chain-extensions/types/unified-accounts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ pub enum Command {
pub enum UnifiedAddress<T: Encode + Decode> {
/// 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),
}
53 changes: 17 additions & 36 deletions chain-extensions/unified-accounts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,19 @@

#![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;

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,
};
pub use unified_accounts_chain_extension_types::Command::{self, *};

type UAWeight<T> = <T as pallet_unified_accounts::Config>::WeightInfo;

#[derive(DefaultNoBound)]
pub struct UnifiedAccountsExtension<T, UA>(PhantomData<(T, UA)>);
Expand All @@ -54,49 +50,34 @@ 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>::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::<T>::to_h160_or_default())?;

let base_weight = <T as frame_system::Config>::DbWeight::get().reads(1);
env.charge_weight(base_weight)?;

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()?;

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

// 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))
};
// charge weight
env.charge_weight(UAWeight::<T>::to_account_id_or_default())?;

// 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))
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::to_h160(),
);

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

#[benchmark]
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::<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 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::<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 to_h160() {
let caller: T::AccountId = whitelisted_caller();
assert_ok!(T::Currency::mint_into(
&caller,
T::AccountMappingStorageFee::get()
));
// 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 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::<T>::claim_default_evm_address(
RawOrigin::Signed(caller.clone()).into()
));

#[block]
{
let _ = <Pallet<T> as UnifiedAddressMapper<T::AccountId>>::to_h160_or_default(&caller);
}
}
}
30 changes: 17 additions & 13 deletions pallets/unified-accounts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@

use astar_primitives::{
ethereum_checked::AccountMapping,
evm::{EvmAddress, UnifiedAddressMapper},
evm::{EvmAddress, UnifiedAddress, UnifiedAddressMapper},
Balance,
};
use frame_support::{
Expand Down Expand Up @@ -351,11 +351,12 @@ impl<T: Config> UnifiedAddressMapper<T::AccountId> for Pallet<T> {
EvmToNative::<T>::get(evm_address)
}

fn to_account_id_or_default(evm_address: &EvmAddress) -> T::AccountId {
EvmToNative::<T>::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<T::AccountId> {
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 {
Expand All @@ -366,11 +367,12 @@ impl<T: Config> UnifiedAddressMapper<T::AccountId> for Pallet<T> {
NativeToEvm::<T>::get(account_id)
}

fn to_h160_or_default(account_id: &T::AccountId) -> EvmAddress {
NativeToEvm::<T>::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<EvmAddress> {
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 {
Expand All @@ -381,14 +383,15 @@ impl<T: Config> UnifiedAddressMapper<T::AccountId> for Pallet<T> {
/// AccountMapping wrapper implementation
impl<T: Config> AccountMapping<T::AccountId> for Pallet<T> {
fn into_h160(account: T::AccountId) -> H160 {
<Self as UnifiedAddressMapper<T::AccountId>>::to_h160_or_default(&account)
<Self as UnifiedAddressMapper<T::AccountId>>::to_h160_or_default(&account).into_address()
}
}

/// AddressMapping wrapper implementation
impl<T: Config> AddressMapping<T::AccountId> for Pallet<T> {
fn into_account_id(evm_address: H160) -> T::AccountId {
<Self as UnifiedAddressMapper<T::AccountId>>::to_account_id_or_default(&evm_address)
.into_address()
}
}

Expand All @@ -415,7 +418,8 @@ impl<T: Config> StaticLookup for Pallet<T> {
MultiAddress::Address20(i) => Ok(
<Self as UnifiedAddressMapper<T::AccountId>>::to_account_id_or_default(
&EvmAddress::from_slice(&i),
),
)
.into_address(),
),
_ => Err(LookupError),
}
Expand Down
Loading

0 comments on commit 0589b5f

Please sign in to comment.