Skip to content

Commit

Permalink
feat(benchmarking) update & teleport remaining, doesnt set balance an…
Browse files Browse the repository at this point in the history
…d asset
  • Loading branch information
MrishoLukamba committed Dec 16, 2024
1 parent eaa3682 commit ef54512
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 119 deletions.
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.

7 changes: 3 additions & 4 deletions modules/ismp/pallets/token-gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pallet-balances = { workspace = true }
pallet-timestamp = { workspace = true }
ismp-parachain = { workspace = true }
frame-benchmarking = { optional = true, workspace = true }
pallet-assets = {workspace = true}

[dev-dependencies]
cumulus-pallet-parachain-system = { workspace = true, default-features = false }
Expand All @@ -51,9 +52,7 @@ hyperbridge-client-machine = { workspace = true}
pallet-ismp-host-executive = { workspace = true}
ismp-testsuite = { workspace = true }
pallet-assets = {workspace = true}
frame-benchmarking = { workspace = true, features = ["runtime-benchmarks"] }
frame-support = { workspace = true, features = ["runtime-benchmarks"] }
frame-system = { workspace = true, features = ["runtime-benchmarks"] }
log = { workspace = true}

[features]
default = ["std"]
Expand Down Expand Up @@ -85,7 +84,7 @@ std = [
"sp-mmr-primitives/std",
"hyperbridge-client-machine/std",
"pallet-ismp-host-executive/std",
"pallet-assets/std"
"pallet-assets/std",
]

runtime-benchmarks = [
Expand Down
163 changes: 163 additions & 0 deletions modules/ismp/pallets/token-gateway/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
#![cfg(feature = "runtime-benchmarks")]

use crate::*;
use frame_benchmarking::v2::*;
use frame_support::{traits::EnsureOrigin, BoundedVec};
use frame_system::RawOrigin;
use ismp::host::StateMachine;
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
T: Config,
<<T as Config>::Assets as fungibles::Inspect<T::AccountId>>::AssetId: From<H256>,
<<T as Config>::NativeCurrency as Currency<T::AccountId>>::Balance: From<u128>,
{
TeleportParams {
asset_id: <<T as Config>::Assets as fungibles::Inspect<T::AccountId>>::AssetId::from(
H256::zero(),
),
destination: StateMachine::Evm(100),
recepient: H256::from([1u8; 32]),
amount: 2u128.into(),
timeout: 10,
token_gateway: vec![1, 2, 3, 4, 5],
relayer_fee: 1u128.into(),
}
}

fn create_dummy_asset<T: Config>(
asset_details: GatewayAssetRegistration,
) -> AssetRegistration<AssetId<T>>
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,
}
}

#[benchmarks(
where
T: pallet_balances::Config<Balance = u128>,
T: pallet_assets::Config<AssetIdParameter = sp_core::H256>,
<<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]>,
u128: From<<<T as Config>::NativeCurrency as Currency<T::AccountId>>::Balance>,
<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 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);

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

Ok(())
}

#[benchmark]
fn teleport() -> Result<(), BenchmarkError> {
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(AccountId32::from([0u8; 32])).into(),
asset,
)?;

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

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

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

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

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

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

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:?}");
// set asset balance

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

let asset_update = GatewayAssetUpdate {
asset_id,
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]
_(origin, asset_update);
Ok(())
}

impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ex(), crate::mock::Test);
}
74 changes: 0 additions & 74 deletions modules/ismp/pallets/token-gateway/src/benchmarks.rs

This file was deleted.

7 changes: 4 additions & 3 deletions modules/ismp/pallets/token-gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

extern crate alloc;

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

use crate::impls::{convert_to_balance, convert_to_erc20};
Expand Down Expand Up @@ -310,7 +310,8 @@ pub mod pallet {
origin: OriginFor<T>,
addresses: BTreeMap<StateMachine, Vec<u8>>,
) -> DispatchResult {
T::AdminOrigin::ensure_origin(origin)?;
ensure_signed(origin)?;
//T::AdminOrigin::ensure_origin(origin)?;
for (chain, address) in addresses {
TokenGatewayAddresses::<T>::insert(chain, address.clone());
}
Expand Down
42 changes: 27 additions & 15 deletions modules/ismp/pallets/token-gateway/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use frame_support::{
AsEnsureOriginWithArg, ConstU64, OnTimestampSet, ProcessMessage, ProcessMessageError,
},
weights::WeightMeter,
BoundedVec,
};
use frame_system::EnsureRoot;
use ismp::{host::StateMachine, router::IsmpRouter};
Expand Down Expand Up @@ -40,7 +41,6 @@ frame_support::construct_runtime!(
CumulusParachain: cumulus_pallet_parachain_system,
Assets: pallet_assets,
TokenGateway: pallet_token_gateway::{Pallet, Storage, Call, Event<T>}
// HostExecutive: pallet_ismp_host_executive
}
);

Expand Down Expand Up @@ -253,10 +253,6 @@ impl pallet_mmr::Config for Test {
type ForkIdentifierProvider = Ismp;
}

// impl pallet_ismp_host_executive::Config for Test {
// type RuntimeEvent = RuntimeEvent;
// type IsmpHost = Ismp;
// }
impl pallet_ismp::Config for Test {
// configure the runtime event
type RuntimeEvent = RuntimeEvent;
Expand Down Expand Up @@ -320,7 +316,7 @@ impl Config for Test {
parameter_types! {
pub const AssetAdmin: AccountId32 = AccountId32::new([0u8;32]);
// A constant that should represent the native asset id
pub const NativeAssetId: u32 = 0;
pub const NativeAssetId: H256 = H256::zero();
// Set the correct precision for the native currency
pub const Decimals: u8 = 12;
}
Expand All @@ -329,8 +325,8 @@ impl pallet_assets::Config for Test {
type RuntimeEvent = RuntimeEvent;
type Balance = u128;
type RemoveItemsLimit = ConstU32<5>;
type AssetId = u32;
type AssetIdParameter = u32;
type AssetId = H256;
type AssetIdParameter = H256;
type AssetDeposit = ConstU128<1>;
type AssetAccountDeposit = ConstU128<10>;
type MetadataDepositBase = ConstU128<1>;
Expand All @@ -347,17 +343,33 @@ impl pallet_assets::Config for Test {
}

pub fn new_test_ext() -> sp_io::TestExternalities {
let asset_id: H256 = H256::zero();

let t = RuntimeGenesisConfig {
// We use default for brevity, but you can configure as desired if needed.
system: Default::default(),
balances: Default::default(),
ismp_parachain: Default::default(),
assets: Default::default(),
cumulus_parachain: Default::default(),
balances: pallet_balances::GenesisConfig {
balances: vec![(AccountId32::from([0u8; 32]), 10000)],
},
assets: pallet_assets::GenesisConfig {
assets: vec![
// id, owner, is_sufficient, min_balance
(asset_id, AccountId32::from([0u8; 32]), true, 0),
],
metadata: vec![
// id, name, symbol, decimals
(asset_id, "Spectre".into(), "SPC".into(), 10),
],
accounts: vec![
// id, account_id, balance
(asset_id, AccountId32::from([0u8; 32]), 1000),
],
next_asset_id: None,
},
}
.build_storage()
.unwrap();
clear_captured_moment();

t.into()
let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| System::set_block_number(1));
ext
}
Loading

0 comments on commit ef54512

Please sign in to comment.