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

pallet-token-gateway benchmarks #364

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion modules/pallets/token-gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ readme = "README.md"
[dependencies]
frame-support = { workspace = true }
frame-system = { workspace = true }
frame-benchmarking = { workspace = true }
sp-runtime = { workspace = true }
sp-core = { workspace = true }
sp-io = { workspace = true }
Expand All @@ -39,6 +40,7 @@ default = ["std"]
std = [
"frame-support/std",
"frame-system/std",
"frame-benchmarking/std",
"sp-runtime/std",
"sp-core/std",
"sp-io/std",
Expand All @@ -50,6 +52,12 @@ std = [
"anyhow/std",
"alloy-primitives/std",
"pallet-hyperbridge/std",
"token-gateway-primitives/std"
"token-gateway-primitives/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
try-runtime = []
155 changes: 155 additions & 0 deletions modules/pallets/token-gateway/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
#![cfg(feature = "runtime-benchmarks")]

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

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

fn create_dummy_asset<T: Config>(
asset_details: GatewayAssetRegistration,
) -> AssetRegistration<AssetId<T>>
where
{
let local_id = T::AssetIdFactory::create_asset_id(asset_details.symbol.to_vec()).unwrap();
AssetRegistration { local_id, reg: asset_details, native: true }
}

#[benchmarks(
where
<<T as Config>::NativeCurrency as Currency<T::AccountId>>::Balance: From<u128>,
<T as frame_system::Config>::AccountId: From<[u8; 32]>,
u128: From<<<T as Config>::NativeCurrency as Currency<T::AccountId>>::Balance>,
T::Balance: From<u128>,
<T as pallet_ismp::Config>::Balance: From<<<T as Config>::NativeCurrency as Currency<T::AccountId>>::Balance>,
<<T as Config>::Assets as fungibles::Inspect<T::AccountId>>::Balance: From<<<T as Config>::NativeCurrency as Currency<T::AccountId>>::Balance>,
<<T as Config>::Assets as fungibles::Inspect<T::AccountId>>::Balance: From<u128>,
[u8; 32]: From<<T as frame_system::Config>::AccountId>,
<T as frame_system::Config>::RuntimeOrigin: From<frame_system::RawOrigin<AccountId32>>,
)]
mod benches {
use super::*;

#[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(),
chains: vec![StateMachine::Evm(100)],
minimum_balance: Some(10),
};
let asset = create_dummy_asset::<T>(asset_details);

<T::Currency as fungible::Mutate<T::AccountId>>::set_balance(&account, u128::MAX.into());

#[extrinsic_call]
_(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(),
chains: vec![StateMachine::Evm(100)],
minimum_balance: None,
};
let asset = create_dummy_asset::<T>(asset_details);

Pallet::<T>::create_erc6160_asset(
RawOrigin::Signed(account.clone()).into(),
asset.clone(),
)?;

let dummy_teleport_params = dummy_teleport_asset::<T>(asset.local_id);

<T::Currency as fungible::Mutate<T::AccountId>>::set_balance(&account, u128::MAX.into());

#[extrinsic_call]
teleport(RawOrigin::Signed(account), dummy_teleport_params);
Ok(())
}

#[benchmark]
fn set_token_gateway_addresses(x: Linear<5, 100>) -> Result<(), BenchmarkError> {
let mut addresses = BTreeMap::new();
for i in 0..x {
let addr = i.to_string().as_bytes().to_vec();
addresses.insert(StateMachine::Evm(100), addr);
}

#[extrinsic_call]
_(RawOrigin::Root, addresses);
Ok(())
}

#[benchmark]
fn update_erc6160_asset() -> Result<(), BenchmarkError> {
let acc_origin: 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(),
chains: vec![StateMachine::Evm(100)],
minimum_balance: None,
};
let asset = create_dummy_asset::<T>(asset_details.clone());

// set balances
<T::Currency as fungible::Mutate<T::AccountId>>::set_balance(&acc_origin, u128::MAX.into());
let asset_id = T::AssetIdFactory::create_asset_id(asset_details.symbol.to_vec()).unwrap();
<T::Assets as fungibles::Create<T::AccountId>>::create(
asset_id.into(),
acc_origin.clone(),
true,
1000000000u128.into(),
)?;

Pallet::<T>::create_erc6160_asset(
RawOrigin::Signed(acc_origin.clone()).into(),
asset.clone(),
)?;

let asset_update = GatewayAssetUpdate {
asset_id: H256::zero(),
add_chains: BoundedVec::try_from(vec![StateMachine::Evm(200)]).unwrap(),
remove_chains: BoundedVec::try_from(Vec::new()).unwrap(),
new_admins: BoundedVec::try_from(Vec::new()).unwrap(),
};

#[extrinsic_call]
_(RawOrigin::Signed(acc_origin), asset_update);
Ok(())
}
}
24 changes: 12 additions & 12 deletions modules/pallets/token-gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,22 @@ extern crate alloc;

pub mod impls;
pub mod types;

mod benchmarking;
mod weights;
use crate::impls::{convert_to_balance, convert_to_erc20};
use alloy_sol_types::SolValue;
use anyhow::anyhow;
use codec::{Decode, Encode};
use frame_support::{
ensure,
pallet_prelude::Weight,
traits::{
fungibles::{self, Mutate},
tokens::{fungible::Mutate as FungibleMutate, Preservation},
Currency, ExistenceRequirement,
},
};
pub use weights::WeightInfo;

use ismp::{
events::Meta,
Expand Down Expand Up @@ -121,6 +124,9 @@ pub mod pallet {

/// A trait that converts an evm address to a substrate account
type EvmToSubstrate: EvmToSubstrate<Self>;

/// Weight information for extrinsics in this pallet
type WeightInfo: WeightInfo;
}

/// Assets supported by this instance of token gateway
Expand Down Expand Up @@ -231,7 +237,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 @@ -354,7 +360,7 @@ 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(addresses.len() as u32))]
pub fn set_token_gateway_addresses(
origin: OriginFor<T>,
addresses: BTreeMap<StateMachine, Vec<u8>>,
Expand All @@ -372,11 +378,10 @@ pub mod pallet {
/// to create the asset.
/// `native` should be true if this asset originates from this chain
#[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>>,
native: bool,
) -> DispatchResult {
let who = ensure_signed(origin)?;

Expand All @@ -398,7 +403,7 @@ pub mod pallet {
// the mapping to its token gateway asset id

SupportedAssets::<T>::insert(asset.local_id.clone(), asset_id.clone());
NativeAssets::<T>::insert(asset.local_id.clone(), native);
NativeAssets::<T>::insert(asset.local_id.clone(), asset.native);
LocalAssets::<T>::insert(asset_id, asset.local_id.clone());
// All ERC6160 assets use 18 decimals
Decimals::<T>::insert(asset.local_id, 18);
Expand Down Expand Up @@ -427,7 +432,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 @@ -794,11 +799,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
pub fn ensure_admin(who: T::AccountId, asset_id: AssetId<T>) -> Result<(), Error<T>> {
Expand Down
2 changes: 2 additions & 0 deletions modules/pallets/token-gateway/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ pub struct AssetRegistration<AssetId> {
pub local_id: AssetId,
/// MNT Asset registration details
pub reg: token_gateway_primitives::GatewayAssetRegistration,
/// Flag for if this asset is native
pub native: bool,
}

alloy_sol_macro::sol! {
Expand Down
28 changes: 28 additions & 0 deletions modules/pallets/token-gateway/src/weights.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use frame_support::weights::Weight;

// ============================== INTERFACE ============================================ //
/// Weight functions needed for `pallet_token_gateway.
pub trait WeightInfo {
fn create_erc6160_asset() -> Weight;
fn teleport() -> Weight;
fn set_token_gateway_addresses(x: u32) -> Weight;
fn update_erc6160_asset() -> Weight;
}

impl WeightInfo for () {
fn create_erc6160_asset() -> Weight {
Weight::zero()
}

fn teleport() -> Weight {
Weight::zero()
}

fn set_token_gateway_addresses(_x: u32) -> Weight {
Weight::zero()
}

fn update_erc6160_asset() -> Weight {
Weight::zero()
}
}
Loading
Loading