From d5efccd38b45b0df5819743249415a8de4308fd4 Mon Sep 17 00:00:00 2001 From: Jesse Abramowitz Date: Tue, 20 Aug 2024 10:23:55 -0400 Subject: [PATCH] add new_session_not_adding_new_signer --- pallets/staking/src/benchmarking.rs | 39 +++++++++ pallets/staking/src/lib.rs | 7 +- pallets/staking/src/weights.rs | 82 +++++++++++++++++++ .../src/weights/pallet_staking_extension.rs | 62 +++++++++++--- 4 files changed, 179 insertions(+), 11 deletions(-) diff --git a/pallets/staking/src/benchmarking.rs b/pallets/staking/src/benchmarking.rs index f74821df2..389eac99b 100644 --- a/pallets/staking/src/benchmarking.rs +++ b/pallets/staking/src/benchmarking.rs @@ -238,6 +238,45 @@ benchmarks! { verify { assert!(NextSigners::::get().is_none()); } + + new_session_not_adding_new_signer { + let caller: T::AccountId = whitelisted_caller(); + let validator_id_res = ::ValidatorId::try_from(caller.clone()).or(Err(Error::::InvalidValidatorId)).unwrap(); + + let second_signer: T::AccountId = account("second_signer", 0, SEED); + let second_signer_id = ::ValidatorId::try_from(second_signer.clone()).or(Err(Error::::InvalidValidatorId)).unwrap(); + // full signer list leaving room for one extra validator + let mut signers = vec![second_signer_id.clone(); 5]; + Signers::::put(signers.clone()); + signers.push(validator_id_res.clone()); + + + }: { + Staking::::new_session_handler(&signers) + } + verify { + assert_eq!(NextSigners::::get().unwrap().next_signers.len(), signers.len() - 2); + } + + new_session { + let confirmation_num = MAX_SIGNERS as usize - 1; + + let caller: T::AccountId = whitelisted_caller(); + let validator_id_res = ::ValidatorId::try_from(caller.clone()).or(Err(Error::::InvalidValidatorId)).unwrap(); + + let second_signer: T::AccountId = account("second_signer", 0, SEED); + let second_signer_id = ::ValidatorId::try_from(second_signer.clone()).or(Err(Error::::InvalidValidatorId)).unwrap(); + // full signer list leaving room for one extra validator + let mut signers = vec![second_signer_id.clone(); confirmation_num as usize]; + Signers::::put(signers.clone()); + signers.push(validator_id_res.clone()); + + }: { + Staking::::new_session_handler(&signers) + } + verify { + assert!(NextSigners::::get().is_some()); + } } impl_benchmark_test_suite!(Staking, crate::mock::new_test_ext(), crate::mock::Test); diff --git a/pallets/staking/src/lib.rs b/pallets/staking/src/lib.rs index 6f8655f42..a6da5ed84 100644 --- a/pallets/staking/src/lib.rs +++ b/pallets/staking/src/lib.rs @@ -549,19 +549,23 @@ pub mod pallet { let signers_info = pallet_parameters::Pallet::::signers_info(); let mut new_signer = vec![]; + let mut weight: Weight = ::WeightInfo::new_session_not_adding_new_signer(); if current_signers_length <= signers_info.total_signers as usize { let mut randomness = Self::get_randomness(); // grab a current signer to initiate value let mut next_signer_up = ¤t_signers[0].clone(); let mut index; + let mut count = 0u32; // loops to find signer in validator that is not already signer while current_signers.contains(next_signer_up) { index = randomness.next_u32() % validators.len() as u32; next_signer_up = &validators[index as usize]; + count += 1; } current_signers.push(next_signer_up.clone()); new_signer = next_signer_up.encode(); + // TODO add last weight here } // removes first signer and pushes new signer to back if total signers not increased @@ -583,7 +587,8 @@ pub mod pallet { JumpStartProgress::::mutate(|jump_start_details| { jump_start_details.parent_key_threshold = signers_info.threshold }); - Ok(0.into()) + + Ok(weight) } } diff --git a/pallets/staking/src/weights.rs b/pallets/staking/src/weights.rs index 9ce11422b..4b32de2fc 100644 --- a/pallets/staking/src/weights.rs +++ b/pallets/staking/src/weights.rs @@ -60,6 +60,8 @@ pub trait WeightInfo { fn confirm_key_reshare_confirmed(c: u32) -> Weight; fn confirm_key_reshare_completed() -> Weight; fn new_session_validators_less_then_signers() -> Weight; + fn new_session_not_adding_new_signer() -> Weight; + fn new_session() -> Weight; } /// Weights for pallet_staking_extension using the Substrate node and recommended hardware. @@ -202,6 +204,46 @@ impl WeightInfo for SubstrateWeight { .saturating_add(Weight::from_parts(0, 1719)) .saturating_add(T::DbWeight::get().reads(1)) } + /// Storage: `StakingExtension::Signers` (r:1 w:0) + /// Proof: `StakingExtension::Signers` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + /// Storage: `Parameters::SignersInfo` (r:1 w:0) + /// Proof: `Parameters::SignersInfo` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + /// Storage: `StakingExtension::JumpStartProgress` (r:1 w:1) + /// Proof: `StakingExtension::JumpStartProgress` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + /// Storage: `StakingExtension::ReshareData` (r:0 w:1) + /// Proof: `StakingExtension::ReshareData` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + /// Storage: `StakingExtension::NextSigners` (r:0 w:1) + /// Proof: `StakingExtension::NextSigners` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + fn new_session() -> Weight { + // Proof Size summary in bytes: + // Measured: `727` + // Estimated: `2212` + // Minimum execution time: 10_000_000 picoseconds. + Weight::from_parts(10_000_000, 0) + .saturating_add(Weight::from_parts(0, 2212)) + .saturating_add(T::DbWeight::get().reads(3)) + .saturating_add(T::DbWeight::get().writes(3)) + } + /// Storage: `StakingExtension::Signers` (r:1 w:0) + /// Proof: `StakingExtension::Signers` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + /// Storage: `Parameters::SignersInfo` (r:1 w:0) + /// Proof: `Parameters::SignersInfo` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + /// Storage: `StakingExtension::JumpStartProgress` (r:1 w:1) + /// Proof: `StakingExtension::JumpStartProgress` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + /// Storage: `StakingExtension::ReshareData` (r:0 w:1) + /// Proof: `StakingExtension::ReshareData` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + /// Storage: `StakingExtension::NextSigners` (r:0 w:1) + /// Proof: `StakingExtension::NextSigners` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + fn new_session_not_adding_new_signer() -> Weight { + // Proof Size summary in bytes: + // Measured: `439` + // Estimated: `1924` + // Minimum execution time: 9_000_000 picoseconds. + Weight::from_parts(10_000_000, 0) + .saturating_add(Weight::from_parts(0, 1924)) + .saturating_add(T::DbWeight::get().reads(3)) + .saturating_add(T::DbWeight::get().writes(3)) + } } // For backwards compatibility and tests @@ -343,4 +385,44 @@ impl WeightInfo for () { .saturating_add(Weight::from_parts(0, 1719)) .saturating_add(RocksDbWeight::get().reads(1)) } + /// Storage: `StakingExtension::Signers` (r:1 w:0) + /// Proof: `StakingExtension::Signers` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + /// Storage: `Parameters::SignersInfo` (r:1 w:0) + /// Proof: `Parameters::SignersInfo` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + /// Storage: `StakingExtension::JumpStartProgress` (r:1 w:1) + /// Proof: `StakingExtension::JumpStartProgress` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + /// Storage: `StakingExtension::ReshareData` (r:0 w:1) + /// Proof: `StakingExtension::ReshareData` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + /// Storage: `StakingExtension::NextSigners` (r:0 w:1) + /// Proof: `StakingExtension::NextSigners` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + fn new_session() -> Weight { + // Proof Size summary in bytes: + // Measured: `727` + // Estimated: `2212` + // Minimum execution time: 10_000_000 picoseconds. + Weight::from_parts(10_000_000, 0) + .saturating_add(Weight::from_parts(0, 2212)) + .saturating_add(RocksDbWeight::get().reads(3)) + .saturating_add(RocksDbWeight::get().writes(3)) + } + /// Storage: `StakingExtension::Signers` (r:1 w:0) + /// Proof: `StakingExtension::Signers` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + /// Storage: `Parameters::SignersInfo` (r:1 w:0) + /// Proof: `Parameters::SignersInfo` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + /// Storage: `StakingExtension::JumpStartProgress` (r:1 w:1) + /// Proof: `StakingExtension::JumpStartProgress` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + /// Storage: `StakingExtension::ReshareData` (r:0 w:1) + /// Proof: `StakingExtension::ReshareData` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + /// Storage: `StakingExtension::NextSigners` (r:0 w:1) + /// Proof: `StakingExtension::NextSigners` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + fn new_session_not_adding_new_signer() -> Weight { + // Proof Size summary in bytes: + // Measured: `439` + // Estimated: `1924` + // Minimum execution time: 9_000_000 picoseconds. + Weight::from_parts(10_000_000, 0) + .saturating_add(Weight::from_parts(0, 1924)) + .saturating_add(RocksDbWeight::get().reads(3)) + .saturating_add(RocksDbWeight::get().writes(3)) + } } diff --git a/runtime/src/weights/pallet_staking_extension.rs b/runtime/src/weights/pallet_staking_extension.rs index 373e0769e..2016ff379 100644 --- a/runtime/src/weights/pallet_staking_extension.rs +++ b/runtime/src/weights/pallet_staking_extension.rs @@ -56,7 +56,7 @@ impl pallet_staking_extension::WeightInfo for WeightInf // Measured: `1309` // Estimated: `4774` // Minimum execution time: 25_000_000 picoseconds. - Weight::from_parts(27_000_000, 0) + Weight::from_parts(25_000_000, 0) .saturating_add(Weight::from_parts(0, 4774)) .saturating_add(T::DbWeight::get().reads(3)) .saturating_add(T::DbWeight::get().writes(1)) @@ -73,8 +73,8 @@ impl pallet_staking_extension::WeightInfo for WeightInf // Proof Size summary in bytes: // Measured: `1430` // Estimated: `4895` - // Minimum execution time: 30_000_000 picoseconds. - Weight::from_parts(33_000_000, 0) + // Minimum execution time: 29_000_000 picoseconds. + Weight::from_parts(32_000_000, 0) .saturating_add(Weight::from_parts(0, 4895)) .saturating_add(T::DbWeight::get().reads(4)) .saturating_add(T::DbWeight::get().writes(2)) @@ -95,8 +95,8 @@ impl pallet_staking_extension::WeightInfo for WeightInf // Proof Size summary in bytes: // Measured: `1262` // Estimated: `4764` - // Minimum execution time: 43_000_000 picoseconds. - Weight::from_parts(55_000_000, 0) + // Minimum execution time: 42_000_000 picoseconds. + Weight::from_parts(49_000_000, 0) .saturating_add(Weight::from_parts(0, 4764)) .saturating_add(T::DbWeight::get().reads(6)) .saturating_add(T::DbWeight::get().writes(3)) @@ -146,7 +146,7 @@ impl pallet_staking_extension::WeightInfo for WeightInf // Measured: `320` // Estimated: `3785` // Minimum execution time: 10_000_000 picoseconds. - Weight::from_parts(11_000_000, 0) + Weight::from_parts(10_000_000, 0) .saturating_add(Weight::from_parts(0, 3785)) .saturating_add(T::DbWeight::get().reads(1)) .saturating_add(T::DbWeight::get().writes(1)) @@ -161,8 +161,10 @@ impl pallet_staking_extension::WeightInfo for WeightInf // Measured: `797 + c * (32 ±0)` // Estimated: `4298 + c * (29 ±1)` // Minimum execution time: 11_000_000 picoseconds. - Weight::from_parts(12_756_906, 0) + Weight::from_parts(12_143_646, 0) .saturating_add(Weight::from_parts(0, 4298)) + // Standard Error: 177_742 + .saturating_add(Weight::from_parts(91_160, 0).saturating_mul(c.into())) .saturating_add(T::DbWeight::get().reads(2)) .saturating_add(T::DbWeight::get().writes(1)) .saturating_add(Weight::from_parts(0, 29).saturating_mul(c.into())) @@ -178,7 +180,7 @@ impl pallet_staking_extension::WeightInfo for WeightInf // Measured: `1309` // Estimated: `4774` // Minimum execution time: 12_000_000 picoseconds. - Weight::from_parts(12_000_000, 0) + Weight::from_parts(13_000_000, 0) .saturating_add(Weight::from_parts(0, 4774)) .saturating_add(T::DbWeight::get().reads(2)) .saturating_add(T::DbWeight::get().writes(2)) @@ -189,9 +191,49 @@ impl pallet_staking_extension::WeightInfo for WeightInf // Proof Size summary in bytes: // Measured: `234` // Estimated: `1719` - // Minimum execution time: 4_000_000 picoseconds. - Weight::from_parts(4_000_000, 0) + // Minimum execution time: 3_000_000 picoseconds. + Weight::from_parts(3_000_000, 0) .saturating_add(Weight::from_parts(0, 1719)) .saturating_add(T::DbWeight::get().reads(1)) } + /// Storage: `StakingExtension::Signers` (r:1 w:0) + /// Proof: `StakingExtension::Signers` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + /// Storage: `Parameters::SignersInfo` (r:1 w:0) + /// Proof: `Parameters::SignersInfo` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + /// Storage: `StakingExtension::JumpStartProgress` (r:1 w:1) + /// Proof: `StakingExtension::JumpStartProgress` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + /// Storage: `StakingExtension::ReshareData` (r:0 w:1) + /// Proof: `StakingExtension::ReshareData` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + /// Storage: `StakingExtension::NextSigners` (r:0 w:1) + /// Proof: `StakingExtension::NextSigners` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + fn new_session_not_adding_new_signer() -> Weight { + // Proof Size summary in bytes: + // Measured: `439` + // Estimated: `1924` + // Minimum execution time: 9_000_000 picoseconds. + Weight::from_parts(10_000_000, 0) + .saturating_add(Weight::from_parts(0, 1924)) + .saturating_add(T::DbWeight::get().reads(3)) + .saturating_add(T::DbWeight::get().writes(3)) + } + /// Storage: `StakingExtension::Signers` (r:1 w:0) + /// Proof: `StakingExtension::Signers` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + /// Storage: `Parameters::SignersInfo` (r:1 w:0) + /// Proof: `Parameters::SignersInfo` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + /// Storage: `StakingExtension::JumpStartProgress` (r:1 w:1) + /// Proof: `StakingExtension::JumpStartProgress` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + /// Storage: `StakingExtension::ReshareData` (r:0 w:1) + /// Proof: `StakingExtension::ReshareData` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + /// Storage: `StakingExtension::NextSigners` (r:0 w:1) + /// Proof: `StakingExtension::NextSigners` (`max_values`: Some(1), `max_size`: None, mode: `Measured`) + fn new_session() -> Weight { + // Proof Size summary in bytes: + // Measured: `727` + // Estimated: `2212` + // Minimum execution time: 10_000_000 picoseconds. + Weight::from_parts(14_000_000, 0) + .saturating_add(Weight::from_parts(0, 2212)) + .saturating_add(T::DbWeight::get().reads(3)) + .saturating_add(T::DbWeight::get().writes(3)) + } }