Skip to content

Commit

Permalink
fix pallet_moonbeam_foreign_assets benchmarks (#3166)
Browse files Browse the repository at this point in the history
  • Loading branch information
RomarQ authored Feb 4, 2025
1 parent 26f41f7 commit 089df7a
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 110 deletions.
73 changes: 40 additions & 33 deletions pallets/moonbeam-foreign-assets/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,8 @@ use sp_runtime::traits::ConstU32;
use sp_runtime::BoundedVec;
use xcm::latest::prelude::*;

fn create_n_foreign_asset<T: Config>(n: u32) -> DispatchResult {
for i in 1..=n {
Pallet::<T>::create_foreign_asset(
RawOrigin::Root.into(),
i as u128,
location_of(i),
18,
str_to_bv("MT"),
str_to_bv("Mytoken"),
)?;
assert_eq!(Pallet::<T>::assets_by_id(i as u128), Some(location_of(i)));
}

Ok(())
}

fn location_of(n: u32) -> Location {
Location::new(0, [Junction::GeneralIndex(n as u128)])
fn location_of(n: u128) -> Location {
Location::new(0, [Junction::GeneralIndex(n)])
}

fn str_to_bv(str_: &str) -> BoundedVec<u8, ConstU32<256>> {
Expand All @@ -51,7 +35,6 @@ fn str_to_bv(str_: &str) -> BoundedVec<u8, ConstU32<256>> {
benchmarks! {
// Worst case scenario: MaxForeignAssets minus one already exists
create_foreign_asset {
create_n_foreign_asset::<T>(T::MaxForeignAssets::get().saturating_sub(1))?;
let asset_id = T::MaxForeignAssets::get() as u128;
}: _(RawOrigin::Root, asset_id, Location::parent(), 18, str_to_bv("MT"), str_to_bv("Mytoken"))
verify {
Expand All @@ -63,45 +46,69 @@ benchmarks! {

// Worst case scenario: MaxForeignAssets already exists
change_xcm_location {
create_n_foreign_asset::<T>(T::MaxForeignAssets::get())?;
}: _(RawOrigin::Root, 1, Location::here())
let asset_id = T::MaxForeignAssets::get() as u128;
Pallet::<T>::create_foreign_asset(
RawOrigin::Root.into(),
asset_id,
location_of(asset_id),
18,
str_to_bv("MT"),
str_to_bv("Mytoken"),
)?;
}: _(RawOrigin::Root, asset_id, Location::here())
verify {
assert_eq!(
Pallet::<T>::assets_by_id(1),
Pallet::<T>::assets_by_id(asset_id),
Some(Location::here())
);
}

// Worst case scenario: MaxForeignAssets already exists
freeze_foreign_asset {
create_n_foreign_asset::<T>(T::MaxForeignAssets::get())?;
}: _(RawOrigin::Root, 1, true)
let asset_id = T::MaxForeignAssets::get() as u128;
Pallet::<T>::create_foreign_asset(
RawOrigin::Root.into(),
asset_id,
location_of(asset_id),
18,
str_to_bv("MT"),
str_to_bv("Mytoken"),
)?;
}: _(RawOrigin::Root, asset_id, true)
verify {
assert_eq!(
Pallet::<T>::assets_by_location(location_of(1)),
Some((1, AssetStatus::FrozenXcmDepositAllowed))
Pallet::<T>::assets_by_location(location_of(asset_id)),
Some((asset_id, AssetStatus::FrozenXcmDepositAllowed))
);
}

// Worst case scenario:
// - MaxForeignAssets already exists
// - The asset to unfreeze is already frozen (to avoid early error)
unfreeze_foreign_asset {
create_n_foreign_asset::<T>(T::MaxForeignAssets::get())?;
let asset_id = T::MaxForeignAssets::get() as u128;
Pallet::<T>::create_foreign_asset(
RawOrigin::Root.into(),
asset_id,
location_of(asset_id),
18,
str_to_bv("MT"),
str_to_bv("Mytoken"),
)?;
Pallet::<T>::freeze_foreign_asset(
RawOrigin::Root.into(),
1,
asset_id,
true
)?;
assert_eq!(
Pallet::<T>::assets_by_location(location_of(1)),
Some((1, AssetStatus::FrozenXcmDepositAllowed))
Pallet::<T>::assets_by_location(location_of(asset_id)),
Some((asset_id, AssetStatus::FrozenXcmDepositAllowed))
);
}: _(RawOrigin::Root, 1)
}: _(RawOrigin::Root, asset_id)
verify {
assert_eq!(
Pallet::<T>::assets_by_location(location_of(1)),
Some((1, AssetStatus::Active))
Pallet::<T>::assets_by_location(location_of(asset_id)),
Some((asset_id.into(), AssetStatus::Active))
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion pallets/moonbeam-foreign-assets/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const ERC20_CALL_MAX_CALLDATA_SIZE: usize = 4 + 32 + 32; // selector + address +
const ERC20_CREATE_MAX_CALLDATA_SIZE: usize = 16 * 1024; // 16Ko

// Hardcoded gas limits (from manual binary search)
const ERC20_CREATE_GAS_LIMIT: u64 = 3_410_000; // highest failure: 3_406_000
const ERC20_CREATE_GAS_LIMIT: u64 = 3_367_000; // highest failure: 3_366_468
pub(crate) const ERC20_BURN_FROM_GAS_LIMIT: u64 = 155_000; // highest failure: 154_000
pub(crate) const ERC20_MINT_INTO_GAS_LIMIT: u64 = 155_000; // highest failure: 154_000
const ERC20_PAUSE_GAS_LIMIT: u64 = 150_500; // highest failure: 150_500
Expand Down
32 changes: 16 additions & 16 deletions runtime/moonbase/src/weights/pallet_moonbeam_foreign_assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ impl<T: frame_system::Config> pallet_moonbeam_foreign_assets::WeightInfo for Wei
/// Proof: `Ethereum::Pending` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
fn create_foreign_asset() -> Weight {
// Proof Size summary in bytes:
// Measured: `3272387`
// Estimated: `3283277`
// Minimum execution time: 4_298_810_000 picoseconds.
Weight::from_parts(4_386_304_000, 3283277)
// Measured: `2124`
// Estimated: `13014`
// Minimum execution time: 531_000_000 picoseconds.
Weight::from_parts(534_000_000, 13014)
.saturating_add(T::DbWeight::get().reads(19_u64))
.saturating_add(T::DbWeight::get().writes(13_u64))
}
Expand All @@ -89,10 +89,10 @@ impl<T: frame_system::Config> pallet_moonbeam_foreign_assets::WeightInfo for Wei
/// Proof: `EvmForeignAssets::AssetsByLocation` (`max_values`: None, `max_size`: None, mode: `Measured`)
fn change_xcm_location() -> Weight {
// Proof Size summary in bytes:
// Measured: `2461`
// Estimated: `8401`
// Minimum execution time: 53_007_000 picoseconds.
Weight::from_parts(58_736_000, 8401)
// Measured: `149`
// Estimated: `6089`
// Minimum execution time: 22_000_000 picoseconds.
Weight::from_parts(23_000_000, 6089)
.saturating_add(T::DbWeight::get().reads(3_u64))
.saturating_add(T::DbWeight::get().writes(3_u64))
}
Expand Down Expand Up @@ -126,10 +126,10 @@ impl<T: frame_system::Config> pallet_moonbeam_foreign_assets::WeightInfo for Wei
/// Proof: `Ethereum::Pending` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
fn freeze_foreign_asset() -> Weight {
// Proof Size summary in bytes:
// Measured: `3294569`
// Estimated: `3300509`
// Minimum execution time: 3_499_593_000 picoseconds.
Weight::from_parts(3_862_006_000, 3300509)
// Measured: `24167`
// Estimated: `30107`
// Minimum execution time: 192_000_000 picoseconds.
Weight::from_parts(196_000_000, 30107)
.saturating_add(T::DbWeight::get().reads(15_u64))
.saturating_add(T::DbWeight::get().writes(5_u64))
}
Expand Down Expand Up @@ -163,10 +163,10 @@ impl<T: frame_system::Config> pallet_moonbeam_foreign_assets::WeightInfo for Wei
/// Proof: `Ethereum::Pending` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
fn unfreeze_foreign_asset() -> Weight {
// Proof Size summary in bytes:
// Measured: `3295628`
// Estimated: `3301568`
// Minimum execution time: 3_498_878_000 picoseconds.
Weight::from_parts(3_882_795_000, 3301568)
// Measured: `25226`
// Estimated: `31166`
// Minimum execution time: 194_000_000 picoseconds.
Weight::from_parts(196_000_000, 31166)
.saturating_add(T::DbWeight::get().reads(15_u64))
.saturating_add(T::DbWeight::get().writes(5_u64))
}
Expand Down
60 changes: 30 additions & 30 deletions runtime/moonbeam/src/weights/pallet_moonbeam_foreign_assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,38 +62,38 @@ impl<T: frame_system::Config> pallet_moonbeam_foreign_assets::WeightInfo for Wei
/// Proof: `EthereumChainId::ChainId` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `MaxEncodedLen`)
/// Storage: `TransactionPayment::NextFeeMultiplier` (r:1 w:0)
/// Proof: `TransactionPayment::NextFeeMultiplier` (`max_values`: Some(1), `max_size`: Some(16), added: 511, mode: `MaxEncodedLen`)
/// Storage: `EVM::AccountCodes` (r:2 w:1)
/// Proof: `EVM::AccountCodes` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `EVM::AccountCodesMetadata` (r:2 w:1)
/// Proof: `EVM::AccountCodesMetadata` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `System::Account` (r:2 w:2)
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(116), added: 2591, mode: `MaxEncodedLen`)
/// Storage: `EVM::AccountCodesMetadata` (r:1 w:1)
/// Proof: `EVM::AccountCodesMetadata` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `EVM::AccountStorages` (r:4 w:4)
/// Proof: `EVM::AccountStorages` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `EVM::Suicided` (r:1 w:0)
/// Proof: `EVM::Suicided` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `EVM::AccountCodes` (r:1 w:1)
/// Proof: `EVM::AccountCodes` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `Ethereum::Pending` (r:1 w:1)
/// Proof: `Ethereum::Pending` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
fn create_foreign_asset() -> Weight {
// Proof Size summary in bytes:
// Measured: `3272387`
// Estimated: `3283277`
// Minimum execution time: 3_610_969_000 picoseconds.
Weight::from_parts(3_661_242_000, 3283277)
// Measured: `2124`
// Estimated: `13014`
// Minimum execution time: 531_000_000 picoseconds.
Weight::from_parts(534_000_000, 13014)
.saturating_add(T::DbWeight::get().reads(19_u64))
.saturating_add(T::DbWeight::get().writes(13_u64))
}
/// Storage: `EvmForeignAssets::AssetsById` (r:1 w:1)
/// Proof: `EvmForeignAssets::AssetsById` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `EvmForeignAssets::AssetsByLocation` (r:1 w:2)
/// Storage: `EvmForeignAssets::AssetsByLocation` (r:2 w:2)
/// Proof: `EvmForeignAssets::AssetsByLocation` (`max_values`: None, `max_size`: None, mode: `Measured`)
fn change_xcm_location() -> Weight {
// Proof Size summary in bytes:
// Measured: `2020`
// Estimated: `5485`
// Minimum execution time: 41_219_000 picoseconds.
Weight::from_parts(43_570_000, 5485)
.saturating_add(T::DbWeight::get().reads(2_u64))
// Measured: `149`
// Estimated: `6089`
// Minimum execution time: 22_000_000 picoseconds.
Weight::from_parts(23_000_000, 6089)
.saturating_add(T::DbWeight::get().reads(3_u64))
.saturating_add(T::DbWeight::get().writes(3_u64))
}
/// Storage: `EvmForeignAssets::AssetsById` (r:1 w:0)
Expand All @@ -110,14 +110,14 @@ impl<T: frame_system::Config> pallet_moonbeam_foreign_assets::WeightInfo for Wei
/// Proof: `EthereumChainId::ChainId` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `MaxEncodedLen`)
/// Storage: `TransactionPayment::NextFeeMultiplier` (r:1 w:0)
/// Proof: `TransactionPayment::NextFeeMultiplier` (`max_values`: Some(1), `max_size`: Some(16), added: 511, mode: `MaxEncodedLen`)
/// Storage: `EVM::AccountCodes` (r:2 w:0)
/// Proof: `EVM::AccountCodes` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `EVM::AccountCodesMetadata` (r:2 w:0)
/// Proof: `EVM::AccountCodesMetadata` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `System::Account` (r:1 w:1)
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(116), added: 2591, mode: `MaxEncodedLen`)
/// Storage: `System::Digest` (r:1 w:0)
/// Proof: `System::Digest` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `EVM::AccountCodesMetadata` (r:1 w:0)
/// Proof: `EVM::AccountCodesMetadata` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `EVM::AccountCodes` (r:1 w:0)
/// Proof: `EVM::AccountCodes` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `Assets::Asset` (r:1 w:0)
/// Proof: `Assets::Asset` (`max_values`: None, `max_size`: Some(174), added: 2649, mode: `MaxEncodedLen`)
/// Storage: `EVM::AccountStorages` (r:1 w:1)
Expand All @@ -126,10 +126,10 @@ impl<T: frame_system::Config> pallet_moonbeam_foreign_assets::WeightInfo for Wei
/// Proof: `Ethereum::Pending` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
fn freeze_foreign_asset() -> Weight {
// Proof Size summary in bytes:
// Measured: `3294569`
// Estimated: `3300509`
// Minimum execution time: 3_594_457_000 picoseconds.
Weight::from_parts(3_644_553_000, 3300509)
// Measured: `24167`
// Estimated: `30107`
// Minimum execution time: 192_000_000 picoseconds.
Weight::from_parts(196_000_000, 30107)
.saturating_add(T::DbWeight::get().reads(15_u64))
.saturating_add(T::DbWeight::get().writes(5_u64))
}
Expand All @@ -147,14 +147,14 @@ impl<T: frame_system::Config> pallet_moonbeam_foreign_assets::WeightInfo for Wei
/// Proof: `EthereumChainId::ChainId` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `MaxEncodedLen`)
/// Storage: `TransactionPayment::NextFeeMultiplier` (r:1 w:0)
/// Proof: `TransactionPayment::NextFeeMultiplier` (`max_values`: Some(1), `max_size`: Some(16), added: 511, mode: `MaxEncodedLen`)
/// Storage: `EVM::AccountCodes` (r:2 w:0)
/// Proof: `EVM::AccountCodes` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `EVM::AccountCodesMetadata` (r:2 w:0)
/// Proof: `EVM::AccountCodesMetadata` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `System::Account` (r:1 w:1)
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(116), added: 2591, mode: `MaxEncodedLen`)
/// Storage: `System::Digest` (r:1 w:0)
/// Proof: `System::Digest` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `EVM::AccountCodesMetadata` (r:1 w:0)
/// Proof: `EVM::AccountCodesMetadata` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `EVM::AccountCodes` (r:1 w:0)
/// Proof: `EVM::AccountCodes` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `Assets::Asset` (r:1 w:0)
/// Proof: `Assets::Asset` (`max_values`: None, `max_size`: Some(174), added: 2649, mode: `MaxEncodedLen`)
/// Storage: `EVM::AccountStorages` (r:1 w:1)
Expand All @@ -163,10 +163,10 @@ impl<T: frame_system::Config> pallet_moonbeam_foreign_assets::WeightInfo for Wei
/// Proof: `Ethereum::Pending` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
fn unfreeze_foreign_asset() -> Weight {
// Proof Size summary in bytes:
// Measured: `3295628`
// Estimated: `3301568`
// Minimum execution time: 3_632_781_000 picoseconds.
Weight::from_parts(3_692_099_000, 3301568)
// Measured: `25226`
// Estimated: `31166`
// Minimum execution time: 194_000_000 picoseconds.
Weight::from_parts(196_000_000, 31166)
.saturating_add(T::DbWeight::get().reads(15_u64))
.saturating_add(T::DbWeight::get().writes(5_u64))
}
Expand Down
Loading

0 comments on commit 089df7a

Please sign in to comment.