Skip to content

Commit

Permalink
add unbond and chill benches
Browse files Browse the repository at this point in the history
  • Loading branch information
JesseAbram committed Aug 27, 2024
1 parent 612efcf commit 8af8e2e
Show file tree
Hide file tree
Showing 4 changed files with 259 additions and 27 deletions.
70 changes: 68 additions & 2 deletions pallets/staking/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,72 @@ benchmarks! {
assert_last_event::<T>(Event::<T>::ThresholdAccountChanged(bonder, server_info).into());
}

unbond {
let c in 0 .. MAX_SIGNERS as u32;

let caller: T::AccountId = whitelisted_caller();
let validator_id_res = <T as pallet_session::Config>::ValidatorId::try_from(caller.clone()).or(Err(Error::<T>::InvalidValidatorId)).unwrap();
let bonder: T::AccountId = account("bond", 0, SEED);
let threshold: T::AccountId = account("threshold", 0, SEED);

let signers = vec![validator_id_res.clone(); c as usize];
Signers::<T>::put(signers.clone());
NextSigners::<T>::put(NextSignerInfo {
next_signers: signers,
confirmations: vec![],
});

prep_bond_and_validate::<T>(true, caller.clone(), bonder.clone(), threshold, NULL_ARR);
let bond = <T as pallet_staking::Config>::Currency::minimum_balance() * 10u32.into();

// assume fully unbonded as slightly more weight, but not enough to handle partial unbond
assert_ok!(<FrameStaking<T>>::unbond(
RawOrigin::Signed(bonder.clone()).into(),
bond,
));


}: _(RawOrigin::Signed(bonder.clone()), 0u32.into())
verify {
// TODO: JA fix, pretty much benching this pathway requiers moving the session forward
// This is diffcult, from the test we were able to mock it but benchamrks use runtime configs
// It is fine for now but should come back to it
// assert_last_event::<T>(Event::NodeInfoRemoved(caller).into());
}

chill {
let c in 0 .. MAX_SIGNERS as u32;

let caller: T::AccountId = whitelisted_caller();
let validator_id_res = <T as pallet_session::Config>::ValidatorId::try_from(caller.clone()).or(Err(Error::<T>::InvalidValidatorId)).unwrap();
let bonder: T::AccountId = account("bond", 0, SEED);
let threshold: T::AccountId = account("threshold", 0, SEED);

let signers = vec![validator_id_res.clone(); c as usize];
Signers::<T>::put(signers.clone());
NextSigners::<T>::put(NextSignerInfo {
next_signers: signers,
confirmations: vec![],
});

prep_bond_and_validate::<T>(true, caller.clone(), bonder.clone(), threshold, NULL_ARR);
let bond = <T as pallet_staking::Config>::Currency::minimum_balance() * 10u32.into();

// assume fully unbonded as slightly more weight, but not enough to handle partial unbond
assert_ok!(<FrameStaking<T>>::unbond(
RawOrigin::Signed(bonder.clone()).into(),
bond,
));


}: _(RawOrigin::Signed(bonder.clone()))
verify {
// TODO: JA fix, pretty much benching this pathway requiers moving the session forward
// This is diffcult, from the test we were able to mock it but benchamrks use runtime configs
// It is fine for now but should come back to it
// assert_last_event::<T>(Event::NodeInfoRemoved(caller).into());
}


withdraw_unbonded {
let c in 0 .. MAX_SIGNERS as u32;
Expand All @@ -132,7 +198,7 @@ benchmarks! {
let bonder: T::AccountId = account("bond", 0, SEED);
let threshold: T::AccountId = account("threshold", 0, SEED);
let validator_id_res = <T as pallet_session::Config>::ValidatorId::try_from(caller.clone()).or(Err(Error::<T>::InvalidValidatorId)).unwrap();

let signers = vec![validator_id_res.clone(); c as usize];
Signers::<T>::put(signers.clone());
NextSigners::<T>::put(NextSignerInfo {
Expand All @@ -142,7 +208,7 @@ benchmarks! {

prep_bond_and_validate::<T>(true, caller.clone(), bonder.clone(), threshold, NULL_ARR);
let bond = <T as pallet_staking::Config>::Currency::minimum_balance() * 10u32.into();

// assume fully unbonded as slightly more weight, but not enough to handle partial unbond
assert_ok!(<FrameStaking<T>>::unbond(
RawOrigin::Signed(bonder.clone()).into(),
Expand Down
21 changes: 12 additions & 9 deletions pallets/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,9 @@ pub mod pallet {
Ok(())
}

/// Wraps's substrate unbond but checks to make targeted validator sure not signer or next signe
#[pallet::call_index(2)]
// TODO
#[pallet::weight(<T as Config>::WeightInfo::withdraw_unbonded(MAX_SIGNERS as u32))]
#[pallet::weight(<T as Config>::WeightInfo::unbond(MAX_SIGNERS as u32))]
pub fn unbond(
origin: OriginFor<T>,
#[pallet::compact] value: BalanceOf<T>,
Expand All @@ -414,7 +414,9 @@ pub mod pallet {
let validator_id = <T as pallet_session::Config>::ValidatorId::try_from(ledger.stash)
.or(Err(Error::<T>::InvalidValidatorId))?;

ensure!(!Self::signers().contains(&validator_id), Error::<T>::NoUnbodingWhenSigner);
let signers = Self::signers();
ensure!(!signers.contains(&validator_id), Error::<T>::NoUnbodingWhenSigner);

let next_signers = Self::next_signers();
if next_signers.is_some() {
ensure!(
Expand All @@ -428,12 +430,12 @@ pub mod pallet {

pallet_staking::Pallet::<T>::unbond(origin, value)?;

Ok(().into())
Ok(Some(<T as Config>::WeightInfo::unbond(signers.len() as u32)).into())
}

/// Wraps's substrate chill but checks to make targeted validator sure not signer or next signer
#[pallet::call_index(3)]
// TODO
#[pallet::weight(<T as Config>::WeightInfo::withdraw_unbonded(MAX_SIGNERS as u32))]
#[pallet::weight(<T as Config>::WeightInfo::chill(MAX_SIGNERS as u32))]
pub fn chill(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
let controller = ensure_signed(origin.clone())?;
let ledger =
Expand All @@ -442,8 +444,10 @@ pub mod pallet {

let validator_id = <T as pallet_session::Config>::ValidatorId::try_from(ledger.stash)
.or(Err(Error::<T>::InvalidValidatorId))?;

let signers = Self::signers();
ensure!(!signers.contains(&validator_id), Error::<T>::NoUnbodingWhenSigner);

ensure!(!Self::signers().contains(&validator_id), Error::<T>::NoUnbodingWhenSigner);
let next_signers = Self::next_signers();
if next_signers.is_some() {
ensure!(
Expand All @@ -457,12 +461,11 @@ pub mod pallet {

pallet_staking::Pallet::<T>::chill(origin)?;

Ok(().into())
Ok(Some(<T as Config>::WeightInfo::chill(signers.len() as u32)).into())
}

/// Wraps's substrate withdraw unbonded but clears extra state if fully unbonded
#[pallet::call_index(4)]
// TODO: add contains O(n) to bench
#[pallet::weight(<T as Config>::WeightInfo::withdraw_unbonded(MAX_SIGNERS as u32))]
pub fn withdraw_unbonded(
origin: OriginFor<T>,
Expand Down
108 changes: 108 additions & 0 deletions pallets/staking/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ use core::marker::PhantomData;
pub trait WeightInfo {
fn change_endpoint() -> Weight;
fn change_threshold_accounts() -> Weight;
fn chill(c: u32) -> Weight;
fn unbond(c: u32) -> Weight;
fn withdraw_unbonded(c: u32) -> Weight;
fn validate() -> Weight;
fn declare_synced() -> Weight;
Expand Down Expand Up @@ -94,6 +96,59 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
.saturating_add(T::DbWeight::get().reads(2_u64))
.saturating_add(T::DbWeight::get().writes(2_u64))
}
/// Storage: `Staking::Ledger` (r:1 w:0)
/// Proof: `Staking::Ledger` (`max_values`: None, `max_size`: Some(1091), added: 3566, mode: `MaxEncodedLen`)
/// Storage: `Staking::Bonded` (r:1 w:0)
/// Proof: `Staking::Bonded` (`max_values`: None, `max_size`: Some(72), added: 2547, mode: `MaxEncodedLen`)
/// Storage: `StakingExtension::Signers` (r:1 w:0)
/// Proof: `StakingExtension::Signers` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `StakingExtension::NextSigners` (r:1 w:0)
/// Proof: `StakingExtension::NextSigners` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// The range of component `c` is `[0, 15]`.
fn unbond(c: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `1153 + c * (64 ±0)`
// Estimated: `4556 + c * (64 ±0)`
// Minimum execution time: 30_000_000 picoseconds.
Weight::from_parts(31_919_889, 0)
.saturating_add(Weight::from_parts(0, 4556))
// Standard Error: 69_754
.saturating_add(Weight::from_parts(122_237, 0).saturating_mul(c.into()))
.saturating_add(T::DbWeight::get().reads(4))
.saturating_add(Weight::from_parts(0, 64).saturating_mul(c.into()))
}
/// Storage: `Staking::Ledger` (r:1 w:0)
/// Proof: `Staking::Ledger` (`max_values`: None, `max_size`: Some(1091), added: 3566, mode: `MaxEncodedLen`)
/// Storage: `Staking::Bonded` (r:1 w:0)
/// Proof: `Staking::Bonded` (`max_values`: None, `max_size`: Some(72), added: 2547, mode: `MaxEncodedLen`)
/// Storage: `StakingExtension::Signers` (r:1 w:0)
/// Proof: `StakingExtension::Signers` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `StakingExtension::NextSigners` (r:1 w:0)
/// Proof: `StakingExtension::NextSigners` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `Staking::Validators` (r:1 w:1)
/// Proof: `Staking::Validators` (`max_values`: None, `max_size`: Some(45), added: 2520, mode: `MaxEncodedLen`)
/// Storage: `Staking::CounterForValidators` (r:1 w:1)
/// Proof: `Staking::CounterForValidators` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
/// Storage: `BagsList::ListNodes` (r:2 w:2)
/// Proof: `BagsList::ListNodes` (`max_values`: None, `max_size`: Some(154), added: 2629, mode: `MaxEncodedLen`)
/// Storage: `BagsList::ListBags` (r:1 w:1)
/// Proof: `BagsList::ListBags` (`max_values`: None, `max_size`: Some(82), added: 2557, mode: `MaxEncodedLen`)
/// Storage: `BagsList::CounterForListNodes` (r:1 w:1)
/// Proof: `BagsList::CounterForListNodes` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
/// Storage: `Staking::Nominators` (r:1 w:0)
/// Proof: `Staking::Nominators` (`max_values`: None, `max_size`: Some(558), added: 3033, mode: `MaxEncodedLen`)
/// The range of component `c` is `[0, 15]`.
fn chill(c: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `2093 + c * (64 ±0)`
// Estimated: `6248 + c * (64 ±0)`
// Minimum execution time: 56_000_000 picoseconds.
Weight::from_parts(60_113_259, 0)
.saturating_add(Weight::from_parts(0, 6248))
.saturating_add(T::DbWeight::get().reads(11))
.saturating_add(T::DbWeight::get().writes(6))
.saturating_add(Weight::from_parts(0, 64).saturating_mul(c.into()))
}
/// Storage: `Staking::Ledger` (r:1 w:1)
/// Proof: `Staking::Ledger` (`max_values`: None, `max_size`: Some(1091), added: 3566, mode: `MaxEncodedLen`)
/// Storage: `Staking::Bonded` (r:1 w:0)
Expand Down Expand Up @@ -278,6 +333,59 @@ impl WeightInfo for () {
.saturating_add(RocksDbWeight::get().reads(2_u64))
.saturating_add(RocksDbWeight::get().writes(2_u64))
}
/// Storage: `Staking::Ledger` (r:1 w:0)
/// Proof: `Staking::Ledger` (`max_values`: None, `max_size`: Some(1091), added: 3566, mode: `MaxEncodedLen`)
/// Storage: `Staking::Bonded` (r:1 w:0)
/// Proof: `Staking::Bonded` (`max_values`: None, `max_size`: Some(72), added: 2547, mode: `MaxEncodedLen`)
/// Storage: `StakingExtension::Signers` (r:1 w:0)
/// Proof: `StakingExtension::Signers` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `StakingExtension::NextSigners` (r:1 w:0)
/// Proof: `StakingExtension::NextSigners` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// The range of component `c` is `[0, 15]`.
fn unbond(c: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `1153 + c * (64 ±0)`
// Estimated: `4556 + c * (64 ±0)`
// Minimum execution time: 30_000_000 picoseconds.
Weight::from_parts(31_919_889, 0)
.saturating_add(Weight::from_parts(0, 4556))
// Standard Error: 69_754
.saturating_add(Weight::from_parts(122_237, 0).saturating_mul(c.into()))
.saturating_add(RocksDbWeight::get().reads(4))
.saturating_add(Weight::from_parts(0, 64).saturating_mul(c.into()))
}
/// Storage: `Staking::Ledger` (r:1 w:0)
/// Proof: `Staking::Ledger` (`max_values`: None, `max_size`: Some(1091), added: 3566, mode: `MaxEncodedLen`)
/// Storage: `Staking::Bonded` (r:1 w:0)
/// Proof: `Staking::Bonded` (`max_values`: None, `max_size`: Some(72), added: 2547, mode: `MaxEncodedLen`)
/// Storage: `StakingExtension::Signers` (r:1 w:0)
/// Proof: `StakingExtension::Signers` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `StakingExtension::NextSigners` (r:1 w:0)
/// Proof: `StakingExtension::NextSigners` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `Staking::Validators` (r:1 w:1)
/// Proof: `Staking::Validators` (`max_values`: None, `max_size`: Some(45), added: 2520, mode: `MaxEncodedLen`)
/// Storage: `Staking::CounterForValidators` (r:1 w:1)
/// Proof: `Staking::CounterForValidators` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
/// Storage: `BagsList::ListNodes` (r:2 w:2)
/// Proof: `BagsList::ListNodes` (`max_values`: None, `max_size`: Some(154), added: 2629, mode: `MaxEncodedLen`)
/// Storage: `BagsList::ListBags` (r:1 w:1)
/// Proof: `BagsList::ListBags` (`max_values`: None, `max_size`: Some(82), added: 2557, mode: `MaxEncodedLen`)
/// Storage: `BagsList::CounterForListNodes` (r:1 w:1)
/// Proof: `BagsList::CounterForListNodes` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
/// Storage: `Staking::Nominators` (r:1 w:0)
/// Proof: `Staking::Nominators` (`max_values`: None, `max_size`: Some(558), added: 3033, mode: `MaxEncodedLen`)
/// The range of component `c` is `[0, 15]`.
fn chill(c: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `2093 + c * (64 ±0)`
// Estimated: `6248 + c * (64 ±0)`
// Minimum execution time: 56_000_000 picoseconds.
Weight::from_parts(60_113_259, 0)
.saturating_add(Weight::from_parts(0, 6248))
.saturating_add(RocksDbWeight::get().reads(11))
.saturating_add(RocksDbWeight::get().writes(6))
.saturating_add(Weight::from_parts(0, 64).saturating_mul(c.into()))
}
/// Storage: `Staking::Ledger` (r:1 w:1)
/// Proof: `Staking::Ledger` (`max_values`: None, `max_size`: Some(1091), added: 3566, mode: `MaxEncodedLen`)
/// Storage: `Staking::Bonded` (r:1 w:0)
Expand Down
Loading

0 comments on commit 8af8e2e

Please sign in to comment.