Skip to content

Commit

Permalink
Benchmarks for new relayers pallet calls (paritytech#2040)
Browse files Browse the repository at this point in the history
* slash relayer balance for invalid transactions

* require some gap before unstake is possible

* more clippy

* log priority boost

* add issue ref to TODO

* fix typo

* is_message_delivery_call -> is_receive_messages_proof_call

* moved is_receive_messages_proof_call above

* only slash relayers for priority transactions

* benchmarks for new relayers pallet calls

* generated weights

* regenerated weights afer master merge

* actually use weights
  • Loading branch information
svyatonik authored and serban300 committed Apr 10, 2024
1 parent 20fa4f0 commit 7068106
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 23 deletions.
10 changes: 7 additions & 3 deletions bridges/bin/millau/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1108,16 +1108,20 @@ impl_runtime_apis! {
}

impl RelayersConfig for Runtime {
fn prepare_environment(
fn prepare_rewards_account(
account_params: RewardsAccountParams,
reward: Balance,
) {
use frame_support::traits::fungible::Mutate;
let rewards_account = bp_relayers::PayRewardFromAccount::<
Balances,
AccountId
>::rewards_account(account_params);
Balances::mint_into(&rewards_account, reward).unwrap();
Self::deposit_account(rewards_account, reward);
}

fn deposit_account(account: AccountId, balance: Balance) {
use frame_support::traits::fungible::Mutate;
Balances::mint_into(&account, balance.saturating_add(ExistentialDeposit::get())).unwrap();
}
}

Expand Down
37 changes: 35 additions & 2 deletions bridges/modules/relayers/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use bp_messages::LaneId;
use bp_relayers::RewardsAccountOwner;
use frame_benchmarking::{benchmarks, whitelisted_caller};
use frame_system::RawOrigin;
use sp_runtime::traits::One;

/// Reward amount that is (hopefully) is larger than existential deposit across all chains.
const REWARD_AMOUNT: u32 = u32::MAX;
Expand All @@ -34,7 +35,9 @@ pub struct Pallet<T: Config>(crate::Pallet<T>);
/// Trait that must be implemented by runtime.
pub trait Config: crate::Config {
/// Prepare environment for paying given reward for serving given lane.
fn prepare_environment(account_params: RewardsAccountParams, reward: Self::Reward);
fn prepare_rewards_account(account_params: RewardsAccountParams, reward: Self::Reward);
/// Give enough balance to given account.
fn deposit_account(account: Self::AccountId, balance: Self::Reward);
}

benchmarks! {
Expand All @@ -46,7 +49,7 @@ benchmarks! {
let relayer: T::AccountId = whitelisted_caller();
let reward = T::Reward::from(REWARD_AMOUNT);

T::prepare_environment(account_params, reward);
T::prepare_rewards_account(account_params, reward);
RelayerRewards::<T>::insert(&relayer, account_params, reward);
}: _(RawOrigin::Signed(relayer), account_params)
verify {
Expand All @@ -55,5 +58,35 @@ benchmarks! {
// also completed successfully
}

// Benchmark `register` call.
register {
let relayer: T::AccountId = whitelisted_caller();
let valid_till = frame_system::Pallet::<T>::block_number()
.saturating_add(crate::Pallet::<T>::required_registration_lease())
.saturating_add(One::one())
.saturating_add(One::one());

T::deposit_account(relayer.clone(), crate::Pallet::<T>::required_stake());
}: _(RawOrigin::Signed(relayer.clone()), valid_till)
verify {
assert!(crate::Pallet::<T>::is_registration_active(&relayer));
}

// Benchmark `deregister` call.
deregister {
let relayer: T::AccountId = whitelisted_caller();
let valid_till = frame_system::Pallet::<T>::block_number()
.saturating_add(crate::Pallet::<T>::required_registration_lease())
.saturating_add(One::one())
.saturating_add(One::one());
T::deposit_account(relayer.clone(), crate::Pallet::<T>::required_stake());
crate::Pallet::<T>::register(RawOrigin::Signed(relayer.clone()).into(), valid_till).unwrap();

frame_system::Pallet::<T>::set_block_number(valid_till.saturating_add(One::one()));
}: _(RawOrigin::Signed(relayer.clone()))
verify {
assert!(!crate::Pallet::<T>::is_registration_active(&relayer));
}

impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::TestRuntime)
}
8 changes: 4 additions & 4 deletions bridges/modules/relayers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub mod pallet {
///
/// Registration allows relayer to get priority boost for its message delivery transactions.
#[pallet::call_index(1)]
#[pallet::weight(Weight::zero())] // TODO: https://github.com/paritytech/parity-bridges-common/issues/2033
#[pallet::weight(T::WeightInfo::register())]
pub fn register(origin: OriginFor<T>, valid_till: T::BlockNumber) -> DispatchResult {
let relayer = ensure_signed(origin)?;

Expand Down Expand Up @@ -175,7 +175,7 @@ pub mod pallet {
/// After this call, message delivery transactions of the relayer won't get any priority
/// boost.
#[pallet::call_index(2)]
#[pallet::weight(Weight::zero())] // TODO: https://github.com/paritytech/parity-bridges-common/issues/2033
#[pallet::weight(T::WeightInfo::deregister())]
pub fn deregister(origin: OriginFor<T>) -> DispatchResult {
let relayer = ensure_signed(origin)?;

Expand Down Expand Up @@ -326,7 +326,7 @@ pub mod pallet {
}

/// Return required registration lease.
fn required_registration_lease() -> T::BlockNumber {
pub(crate) fn required_registration_lease() -> T::BlockNumber {
<T::StakeAndSlash as StakeAndSlash<
T::AccountId,
T::BlockNumber,
Expand All @@ -335,7 +335,7 @@ pub mod pallet {
}

/// Return required stake.
fn required_stake() -> T::Reward {
pub(crate) fn required_stake() -> T::Reward {
<T::StakeAndSlash as StakeAndSlash<
T::AccountId,
T::BlockNumber,
Expand Down
9 changes: 6 additions & 3 deletions bridges/modules/relayers/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,16 @@ impl pallet_bridge_relayers::Config for TestRuntime {

#[cfg(feature = "runtime-benchmarks")]
impl pallet_bridge_relayers::benchmarking::Config for TestRuntime {
fn prepare_environment(account_params: RewardsAccountParams, reward: Balance) {
use frame_support::traits::fungible::Mutate;
fn prepare_rewards_account(account_params: RewardsAccountParams, reward: Balance) {
let rewards_account =
bp_relayers::PayRewardFromAccount::<Balances, AccountId>::rewards_account(
account_params,
);
Balances::mint_into(&rewards_account, reward).unwrap();
Self::deposit_account(rewards_account, reward);
}

fn deposit_account(account: Self::AccountId, balance: Self::Reward) {
Balances::mint_into(&account, balance.saturating_add(ExistentialDeposit::get())).unwrap();
}
}

Expand Down
106 changes: 95 additions & 11 deletions bridges/modules/relayers/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! Autogenerated weights for pallet_bridge_relayers
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2023-03-02, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! DATE: 2023-04-17, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! WORST CASE MAP SIZE: `1000000`
//! HOSTNAME: `covid`, CPU: `11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz`
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024
Expand Down Expand Up @@ -51,6 +51,8 @@ use sp_std::marker::PhantomData;
/// Weight functions needed for pallet_bridge_relayers.
pub trait WeightInfo {
fn claim_rewards() -> Weight;
fn register() -> Weight;
fn deregister() -> Weight;
}

/// Weights for `pallet_bridge_relayers` that are generated using one of the Bridge testnets.
Expand All @@ -63,16 +65,57 @@ impl<T: frame_system::Config> WeightInfo for BridgeWeight<T> {
/// Proof: BridgeRelayers RelayerRewards (max_values: None, max_size: Some(65), added: 2540,
/// mode: MaxEncodedLen)
///
/// Storage: Balances TotalIssuance (r:1 w:0)
///
/// Proof: Balances TotalIssuance (max_values: Some(1), max_size: Some(8), added: 503, mode:
/// MaxEncodedLen)
///
/// Storage: System Account (r:1 w:1)
///
/// Proof: System Account (max_values: None, max_size: Some(96), added: 2571, mode:
/// Proof: System Account (max_values: None, max_size: Some(104), added: 2579, mode:
/// MaxEncodedLen)
fn claim_rewards() -> Weight {
// Proof Size summary in bytes:
// Measured: `275`
// Estimated: `5111`
// Minimum execution time: 48_639 nanoseconds.
Weight::from_parts(49_600_000, 5111)
// Measured: `294`
// Estimated: `8592`
// Minimum execution time: 75_307 nanoseconds.
Weight::from_parts(76_564_000, 8592)
.saturating_add(T::DbWeight::get().reads(3_u64))
.saturating_add(T::DbWeight::get().writes(2_u64))
}
/// Storage: BridgeRelayers RegisteredRelayers (r:1 w:1)
///
/// Proof: BridgeRelayers RegisteredRelayers (max_values: None, max_size: Some(64), added: 2539,
/// mode: MaxEncodedLen)
///
/// Storage: Balances Reserves (r:1 w:1)
///
/// Proof: Balances Reserves (max_values: None, max_size: Some(849), added: 3324, mode:
/// MaxEncodedLen)
fn register() -> Weight {
// Proof Size summary in bytes:
// Measured: `87`
// Estimated: `7843`
// Minimum execution time: 38_270 nanoseconds.
Weight::from_parts(39_191_000, 7843)
.saturating_add(T::DbWeight::get().reads(2_u64))
.saturating_add(T::DbWeight::get().writes(2_u64))
}
/// Storage: BridgeRelayers RegisteredRelayers (r:1 w:1)
///
/// Proof: BridgeRelayers RegisteredRelayers (max_values: None, max_size: Some(64), added: 2539,
/// mode: MaxEncodedLen)
///
/// Storage: Balances Reserves (r:1 w:1)
///
/// Proof: Balances Reserves (max_values: None, max_size: Some(849), added: 3324, mode:
/// MaxEncodedLen)
fn deregister() -> Weight {
// Proof Size summary in bytes:
// Measured: `264`
// Estimated: `7843`
// Minimum execution time: 43_028 nanoseconds.
Weight::from_parts(44_098_000, 7843)
.saturating_add(T::DbWeight::get().reads(2_u64))
.saturating_add(T::DbWeight::get().writes(2_u64))
}
Expand All @@ -85,16 +128,57 @@ impl WeightInfo for () {
/// Proof: BridgeRelayers RelayerRewards (max_values: None, max_size: Some(65), added: 2540,
/// mode: MaxEncodedLen)
///
/// Storage: Balances TotalIssuance (r:1 w:0)
///
/// Proof: Balances TotalIssuance (max_values: Some(1), max_size: Some(8), added: 503, mode:
/// MaxEncodedLen)
///
/// Storage: System Account (r:1 w:1)
///
/// Proof: System Account (max_values: None, max_size: Some(96), added: 2571, mode:
/// Proof: System Account (max_values: None, max_size: Some(104), added: 2579, mode:
/// MaxEncodedLen)
fn claim_rewards() -> Weight {
// Proof Size summary in bytes:
// Measured: `275`
// Estimated: `5111`
// Minimum execution time: 48_639 nanoseconds.
Weight::from_parts(49_600_000, 5111)
// Measured: `294`
// Estimated: `8592`
// Minimum execution time: 75_307 nanoseconds.
Weight::from_parts(76_564_000, 8592)
.saturating_add(RocksDbWeight::get().reads(3_u64))
.saturating_add(RocksDbWeight::get().writes(2_u64))
}
/// Storage: BridgeRelayers RegisteredRelayers (r:1 w:1)
///
/// Proof: BridgeRelayers RegisteredRelayers (max_values: None, max_size: Some(64), added: 2539,
/// mode: MaxEncodedLen)
///
/// Storage: Balances Reserves (r:1 w:1)
///
/// Proof: Balances Reserves (max_values: None, max_size: Some(849), added: 3324, mode:
/// MaxEncodedLen)
fn register() -> Weight {
// Proof Size summary in bytes:
// Measured: `87`
// Estimated: `7843`
// Minimum execution time: 38_270 nanoseconds.
Weight::from_parts(39_191_000, 7843)
.saturating_add(RocksDbWeight::get().reads(2_u64))
.saturating_add(RocksDbWeight::get().writes(2_u64))
}
/// Storage: BridgeRelayers RegisteredRelayers (r:1 w:1)
///
/// Proof: BridgeRelayers RegisteredRelayers (max_values: None, max_size: Some(64), added: 2539,
/// mode: MaxEncodedLen)
///
/// Storage: Balances Reserves (r:1 w:1)
///
/// Proof: Balances Reserves (max_values: None, max_size: Some(849), added: 3324, mode:
/// MaxEncodedLen)
fn deregister() -> Weight {
// Proof Size summary in bytes:
// Measured: `264`
// Estimated: `7843`
// Minimum execution time: 43_028 nanoseconds.
Weight::from_parts(44_098_000, 7843)
.saturating_add(RocksDbWeight::get().reads(2_u64))
.saturating_add(RocksDbWeight::get().writes(2_u64))
}
Expand Down

0 comments on commit 7068106

Please sign in to comment.