Skip to content

Commit

Permalink
feat(benchmarks) pallet token gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
MrishoLukamba committed Dec 19, 2024
1 parent ecf4b9c commit dbd3668
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 424 deletions.
100 changes: 64 additions & 36 deletions modules/ismp/pallets/token-gateway/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@

use crate::*;
use frame_benchmarking::v2::*;
use frame_support::{traits::EnsureOrigin, BoundedVec};
use frame_support::BoundedVec;
use frame_system::RawOrigin;
use ismp::host::StateMachine;
use pallet_balances::AdjustmentDirection;
use scale_info::prelude::collections::BTreeMap;
use sp_runtime::{traits::StaticLookup, AccountId32};
use token_gateway_primitives::{GatewayAssetRegistration, GatewayAssetUpdate};

fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
frame_system::Pallet::<T>::assert_last_event(generic_event.into());
}

fn dummy_teleport_asset<T>(
) -> TeleportParams<AssetId<T>, <<T as Config>::NativeCurrency as Currency<T::AccountId>>::Balance>
where
Expand All @@ -26,10 +23,10 @@ where
),
destination: StateMachine::Evm(100),
recepient: H256::from([1u8; 32]),
amount: 2u128.into(),
amount: 1100000000u128.into(),
timeout: 10,
token_gateway: vec![1, 2, 3, 4, 5],
relayer_fee: 1u128.into(),
relayer_fee: 1000000002u128.into(),
}
}

Expand All @@ -39,18 +36,13 @@ fn create_dummy_asset<T: Config>(
where
<<T as Config>::Assets as fungibles::Inspect<T::AccountId>>::AssetId: From<H256>,
{
AssetRegistration {
local_id: <<T as Config>::Assets as fungibles::Inspect<T::AccountId>>::AssetId::from(
H256::zero(),
),
reg: asset_details,
}
AssetRegistration { local_id: H256::zero().into(), reg: asset_details }
}

#[benchmarks(
where
T: pallet_balances::Config<Balance = u128>,
T: pallet_assets::Config<AssetIdParameter = sp_core::H256>,
T: pallet_assets::Config<AssetIdParameter = sp_core::H256,Balance = u128>,
<<T as Config>::Assets as fungibles::Inspect<T::AccountId>>::AssetId: From<H256>,
<<T as Config>::NativeCurrency as Currency<T::AccountId>>::Balance: From<u128>,
<T as frame_system::Config>::AccountId: From<[u8; 32]>,
Expand All @@ -66,6 +58,8 @@ mod benches {

#[benchmark]
fn create_erc6160_asset() -> Result<(), BenchmarkError> {
let account: T::AccountId = whitelisted_caller();

let asset_details = GatewayAssetRegistration {
name: BoundedVec::try_from(b"Spectre".to_vec()).unwrap(),
symbol: BoundedVec::try_from(b"SPC".to_vec()).unwrap(),
Expand All @@ -74,14 +68,30 @@ mod benches {
};
let asset = create_dummy_asset::<T>(asset_details);

// Set balances
let ed = <T as pallet_balances::Config>::ExistentialDeposit::get();

// Adjust total issuance
pallet_balances::Pallet::<T>::force_adjust_total_issuance(
RawOrigin::Root.into(),
AdjustmentDirection::Increase,
ed * 1000,
)?;

let acc = <<T as frame_system::Config>::Lookup as StaticLookup>::unlookup(account.clone());

pallet_balances::Pallet::<T>::force_set_balance(RawOrigin::Root.into(), acc, ed * 100u128)?;

#[extrinsic_call]
_(RawOrigin::Signed(AccountId32::from([0u8; 32])), asset);
_(RawOrigin::Signed(account), asset);

Ok(())
}

#[benchmark]
fn teleport() -> Result<(), BenchmarkError> {
let account: T::AccountId = whitelisted_caller();

let asset_details = GatewayAssetRegistration {
name: BoundedVec::try_from(b"Spectre".to_vec()).unwrap(),
symbol: BoundedVec::try_from(b"SPC".to_vec()).unwrap(),
Expand All @@ -90,15 +100,30 @@ mod benches {
};
let asset = create_dummy_asset::<T>(asset_details);

Pallet::<T>::create_erc6160_asset(
RawOrigin::Signed(AccountId32::from([0u8; 32])).into(),
asset,
)?;
Pallet::<T>::create_erc6160_asset(RawOrigin::Signed(account.clone()).into(), asset)?;

let dummy_teleport_params = dummy_teleport_asset::<T>();

// Set balances
let ed = <T as pallet_balances::Config>::ExistentialDeposit::get();

// Adjust total issuance
pallet_balances::Pallet::<T>::force_adjust_total_issuance(
RawOrigin::Root.into(),
AdjustmentDirection::Increase,
ed * 1000,
)?;

let acc = <<T as frame_system::Config>::Lookup as StaticLookup>::unlookup(account.clone());

pallet_balances::Pallet::<T>::force_set_balance(
RawOrigin::Root.into(),
acc.clone(),
ed * 100u128,
)?;

#[extrinsic_call]
teleport(RawOrigin::Signed(AccountId32::from([0u8; 32])), dummy_teleport_params);
teleport(RawOrigin::Signed(account), dummy_teleport_params);
Ok(())
}

Expand All @@ -111,13 +136,13 @@ mod benches {
}

#[extrinsic_call]
_(RawOrigin::Signed(AccountId32::from([0u8; 32])), addresses);
_(RawOrigin::Root, addresses);
Ok(())
}

#[benchmark]
fn update_erc6160_asset() -> Result<(), BenchmarkError> {
let origin = RawOrigin::Signed(AccountId32::from([0u8; 32]));
let acc_origin: T::AccountId = whitelisted_caller();

let asset_details = GatewayAssetRegistration {
name: BoundedVec::try_from(b"Spectre".to_vec()).unwrap(),
Expand All @@ -126,24 +151,29 @@ mod benches {
minimum_balance: None,
};
let asset = create_dummy_asset::<T>(asset_details);

let asset_id: H256 = sp_io::hashing::keccak_256(asset.reg.symbol.as_ref()).into();

let owner =
<T::Assets as fungibles::roles::Inspect<T::AccountId>>::admin(H256::zero().into());
log::info!("owner: {owner:?}");

// set balances
let account = T::AccountId::from([0u8; 32]);
let acc = <<T as frame_system::Config>::Lookup as StaticLookup>::unlookup(account.clone());
pallet_balances::Pallet::<T>::force_set_balance(RawOrigin::Root.into(), acc, 1000u128)?;

let bal = <T::NativeCurrency as Currency<T::AccountId>>::total_balance(&account.clone());
log::info!("bal: {bal:?}");
let acc_o =
<<T as frame_system::Config>::Lookup as StaticLookup>::unlookup(acc_origin.clone());
let ed = <T as pallet_balances::Config>::ExistentialDeposit::get();
pallet_balances::Pallet::<T>::force_set_balance(
RawOrigin::Root.into(),
acc_o.clone(),
ed * 100u128,
)?;

// set asset balance
pallet_assets::Pallet::<T>::create(
RawOrigin::Signed(acc_origin.clone()).into(),
H256::zero().into(),
acc_o.clone(),
1000000000,
)?;

Pallet::<T>::create_erc6160_asset(
RawOrigin::Signed(AccountId32::from([0u8; 32])).into(),
RawOrigin::Signed(acc_origin.clone()).into(),
asset.clone(),
)?;

Expand All @@ -155,9 +185,7 @@ mod benches {
};

#[extrinsic_call]
_(origin, asset_update);
_(RawOrigin::Signed(acc_origin), asset_update);
Ok(())
}

impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ex(), crate::mock::Test);
}
24 changes: 11 additions & 13 deletions modules/ismp/pallets/token-gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ extern crate alloc;

mod benchmarking;
pub mod impls;
#[cfg(test)]
pub mod mock;

pub mod types;
pub mod weights;
pub use weights::*;

use crate::impls::{convert_to_balance, convert_to_erc20};
use alloy_sol_types::SolValue;
use anyhow::anyhow;
use codec::Decode;
use frame_support::{
ensure,
pallet_prelude::Weight,
traits::{
fungibles::{self, Mutate},
tokens::{fungible::Mutate as FungibleMutate, Preservation},
Expand Down Expand Up @@ -117,6 +117,9 @@ pub mod pallet {
/// The decimals of the native currency
#[pallet::constant]
type Decimals: Get<u8>;

type WeightInfo: WeightInfo;

}

/// Assets supported by this instance of token gateway
Expand Down Expand Up @@ -222,7 +225,7 @@ pub mod pallet {
/// Teleports a registered asset
/// locks the asset and dispatches a request to token gateway on the destination
#[pallet::call_index(0)]
#[pallet::weight(weight())]
#[pallet::weight(T::WeightInfo::teleport())]
pub fn teleport(
origin: OriginFor<T>,
params: TeleportParams<
Expand Down Expand Up @@ -305,13 +308,12 @@ pub mod pallet {

/// Set the token gateway address for specified chains
#[pallet::call_index(1)]
#[pallet::weight(weight())]
#[pallet::weight(T::WeightInfo::set_token_gateway_addresses())]
pub fn set_token_gateway_addresses(
origin: OriginFor<T>,
addresses: BTreeMap<StateMachine, Vec<u8>>,
) -> DispatchResult {
ensure_signed(origin)?;
//T::AdminOrigin::ensure_origin(origin)?;
T::AdminOrigin::ensure_origin(origin)?;
for (chain, address) in addresses {
TokenGatewayAddresses::<T>::insert(chain, address.clone());
}
Expand All @@ -323,7 +325,7 @@ pub mod pallet {
/// This works by dispatching a request to the TokenGateway module on each requested chain
/// to create the asset.
#[pallet::call_index(2)]
#[pallet::weight(weight())]
#[pallet::weight(T::WeightInfo::create_erc6160_asset())]
pub fn create_erc6160_asset(
origin: OriginFor<T>,
asset: AssetRegistration<AssetId<T>>,
Expand Down Expand Up @@ -376,7 +378,7 @@ pub mod pallet {
/// This works by dispatching a request to the TokenGateway module on each requested chain
/// to create the asset.
#[pallet::call_index(3)]
#[pallet::weight(weight())]
#[pallet::weight(T::WeightInfo::update_erc6160_asset())]
pub fn update_erc6160_asset(
origin: OriginFor<T>,
asset: GatewayAssetUpdate,
Expand Down Expand Up @@ -659,10 +661,6 @@ where
}
}

/// Static weights because benchmarks suck, and we'll be getting PolkaVM soon anyways
fn weight() -> Weight {
Weight::from_parts(300_000_000, 0)
}

impl<T: Config> Pallet<T> {
/// Ensure the signer is the asset admin
Expand Down
Loading

0 comments on commit dbd3668

Please sign in to comment.