Skip to content

Commit

Permalink
skip_to_elapsed
Browse files Browse the repository at this point in the history
  • Loading branch information
nanocryk committed Sep 4, 2023
1 parent a4d50cf commit 29c3d3f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 21 deletions.
1 change: 0 additions & 1 deletion pallets/pooled-staking/src/calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ use {
traits::{
fungible::{Mutate, MutateHold},
tokens::{Precision, Preservation},
Contains,
},
},
sp_runtime::traits::{CheckedSub, Zero},
Expand Down
4 changes: 2 additions & 2 deletions pallets/pooled-staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use {
parameter_types,
traits::{
tokens::fungible::{Inspect, InspectHold},
Contains, Everything, OnFinalize, OnInitialize,
Everything, OnFinalize, OnInitialize,
},
},
frame_system::pallet_prelude::BlockNumberFor,
Expand All @@ -36,7 +36,7 @@ use {
sp_core::{ConstU32, ConstU64, RuntimeDebug, H256},
sp_runtime::{
testing::Header,
traits::{BlakeTwo256, BlockNumberProvider, IdentityLookup},
traits::{BlakeTwo256, IdentityLookup},
Perbill,
},
};
Expand Down
8 changes: 8 additions & 0 deletions pallets/pooled-staking/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ pub trait Timer {

#[cfg(feature = "runtime-benchmarks")]
fn elapsed_instant() -> Self::Instant;

#[cfg(feature = "runtime-benchmarks")]
fn skip_to_elapsed();
}

pub struct BlockNumberTimer<T, G>(PhantomData<(T, G)>);
Expand Down Expand Up @@ -63,6 +66,11 @@ where
.checked_add(&delay)
.expect("overflow when computing valid elapsed instant")
}

#[cfg(feature = "runtime-benchmarks")]
fn skip_to_elapsed() {
frame_system::Pallet::<T>::set_block_number(Self::elapsed_instant());
}
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
Expand Down
24 changes: 6 additions & 18 deletions runtime/dancebox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ use {
EnsureRoot,
},
nimbus_primitives::NimbusId,
pallet_pooled_staking::traits::BlockNumberTimer,
pallet_registrar_runtime_api::ContainerChainGenesisData,
pallet_session::ShouldEndSession,
pallet_transaction_payment::{ConstFeeMultiplier, CurrencyAdapter, Multiplier},
Expand All @@ -65,15 +66,14 @@ use {
sp_core::{crypto::KeyTypeId, Decode, Encode, Get, MaxEncodedLen, OpaqueMetadata},
sp_runtime::{
create_runtime_str, generic, impl_opaque_keys,
traits::{
AccountIdConversion, AccountIdLookup, BlakeTwo256, Block as BlockT, BlockNumberProvider,
},
traits::{AccountIdConversion, AccountIdLookup, BlakeTwo256, Block as BlockT},
transaction_validity::{TransactionSource, TransactionValidity},
AccountId32, ApplyExtrinsicResult,
},
sp_std::prelude::*,
sp_version::RuntimeVersion,
};

pub use {
sp_runtime::{MultiAddress, Perbill, Permill},
tp_core::{AccountId, Address, Balance, BlockNumber, Hash, Header, Index, Signature},
Expand Down Expand Up @@ -733,22 +733,10 @@ parameter_types! {
pub const InitialLeavingShareValue: u128 = 1;
pub const MinimumSelfDelegation: u128 = 10 * currency::KILODANCE;
pub const RewardsCollatorCommission: Perbill = Perbill::from_percent(20);
pub const BlocksToWait: u32 = BLOCKS_TO_WAIT;
}

pub const BLOCKS_TO_WAIT: u32 = 2;
pub struct DummyRequestFilter;

impl Contains<u32> for DummyRequestFilter {
fn contains(request_block: &u32) -> bool {
let block_number = frame_system::Pallet::<Runtime>::current_block_number();

let Some(diff) = block_number.checked_sub(*request_block) else {
return false;
};

diff >= BLOCKS_TO_WAIT // must wait 2 blocks
}
}

impl pallet_pooled_staking::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
Expand All @@ -763,9 +751,9 @@ impl pallet_pooled_staking::Config for Runtime {
type MinimumSelfDelegation = MinimumSelfDelegation;
type RewardsCollatorCommission = RewardsCollatorCommission;
// TODO: Change for session boundary filter
type JoiningRequestFilter = DummyRequestFilter;
type JoiningRequestTimer = BlockNumberTimer<Self, BlocksToWait>;
// TODO: Change for proper duration
type LeavingRequestFilter = DummyRequestFilter;
type LeavingRequestTimer = BlockNumberTimer<Self, BlocksToWait>;
type EligibleCandidatesBufferSize = ConstU32<100>;
// TODO: Add check that candidate have authoring keys?
type EligibleCandidatesFilter = Everything;
Expand Down

0 comments on commit 29c3d3f

Please sign in to comment.