use super::*;
use crate::{self as bags_list};
use frame_election_provider_support::VoteWeight;
use frame_support::parameter_types;
use sp_runtime::BuildStorage;
use std::collections::HashMap;
pub type AccountId = u32;
pub type Balance = u32;
parameter_types! {
pub static NextVoteWeight: VoteWeight = 0;
pub static NextVoteWeightMap: HashMap<AccountId, VoteWeight> = Default::default();
}
pub struct StakingMock;
impl frame_election_provider_support::ScoreProvider<AccountId> for StakingMock {
type Score = VoteWeight;
fn score(id: &AccountId) -> Self::Score {
*NextVoteWeightMap::get().get(id).unwrap_or(&NextVoteWeight::get())
}
frame_election_provider_support::runtime_benchmarks_fuzz_or_std_enabled! {
fn set_score_of(id: &AccountId, weight: Self::Score) {
NEXT_VOTE_WEIGHT_MAP.with(|m| m.borrow_mut().insert(*id, weight));
}
}
}
impl frame_system::Config for Runtime {
type SS58Prefix = ();
type BaseCallFilter = frame_support::traits::Everything;
type RuntimeOrigin = RuntimeOrigin;
type Nonce = u64;
type RuntimeCall = RuntimeCall;
type Hash = sp_core::H256;
type Hashing = sp_runtime::traits::BlakeTwo256;
type AccountId = AccountId;
type Lookup = sp_runtime::traits::IdentityLookup<Self::AccountId>;
type Block = Block;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = ();
type DbWeight = ();
type BlockLength = ();
type BlockWeights = ();
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<Balance>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
}
parameter_types! {
pub static BagThresholds: &'static [VoteWeight] = &[10, 20, 30, 40, 50, 60, 1_000, 2_000, 10_000];
}
impl bags_list::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
type BagThresholds = BagThresholds;
type ScoreProvider = StakingMock;
type Score = VoteWeight;
}
type Block = frame_system::mocking::MockBlock<Runtime>;
frame_support::construct_runtime!(
pub struct Runtime
{
System: frame_system::{Pallet, Call, Storage, Event<T>, Config<T>},
BagsList: bags_list::{Pallet, Call, Storage, Event<T>},
}
);
pub(crate) const GENESIS_IDS: [(AccountId, VoteWeight); 4] =
[(1, 10), (2, 1_000), (3, 1_000), (4, 1_000)];
#[derive(Default)]
pub struct ExtBuilder {
ids: Vec<(AccountId, VoteWeight)>,
skip_genesis_ids: bool,
}
#[cfg(any(feature = "runtime-benchmarks", feature = "fuzz", test))]
impl ExtBuilder {
#[cfg(test)]
pub(crate) fn skip_genesis_ids(mut self) -> Self {
self.skip_genesis_ids = true;
self
}
#[cfg(test)]
pub(crate) fn add_ids(mut self, ids: Vec<(AccountId, VoteWeight)>) -> Self {
self.ids = ids;
self
}
pub(crate) fn build(self) -> sp_io::TestExternalities {
sp_tracing::try_init_simple();
let storage = frame_system::GenesisConfig::<Runtime>::default().build_storage().unwrap();
let ids_with_weight: Vec<_> = if self.skip_genesis_ids {
self.ids.iter().collect()
} else {
GENESIS_IDS.iter().chain(self.ids.iter()).collect()
};
let mut ext = sp_io::TestExternalities::from(storage);
ext.execute_with(|| {
for (id, weight) in ids_with_weight {
frame_support::assert_ok!(List::<Runtime>::insert(*id, *weight));
StakingMock::set_score_of(id, *weight);
}
});
ext
}
pub fn build_and_execute(self, test: impl FnOnce() -> ()) {
self.build().execute_with(|| {
test();
List::<Runtime>::do_try_state().expect("do_try_state post condition failed")
})
}
#[cfg(test)]
pub(crate) fn build_and_execute_no_post_check(self, test: impl FnOnce() -> ()) {
self.build().execute_with(test)
}
}
#[cfg(test)]
pub(crate) mod test_utils {
use super::*;
use list::Bag;
pub(crate) fn bag_as_ids(bag: &Bag<Runtime>) -> Vec<AccountId> {
bag.iter().map(|n| *n.id()).collect::<Vec<_>>()
}
pub(crate) fn get_list_as_ids() -> Vec<AccountId> {
List::<Runtime>::iter().map(|n| *n.id()).collect::<Vec<_>>()
}
}