pallet_bags_list_remote_tests/
snapshot.rs1use frame_election_provider_support::{
20 bounds::{CountBound, DataProviderBounds},
21 SortedListProvider,
22};
23use frame_support::traits::PalletInfoAccess;
24use remote_externalities::{Builder, Mode, OnlineConfig};
25use sp_runtime::{
26 traits::{Block as BlockT, Zero},
27 DeserializeOwned,
28};
29
30pub async fn execute<Runtime, Block>(voter_limit: Option<usize>, currency_unit: u64, ws_url: String)
32where
33 Runtime: crate::RuntimeT<pallet_bags_list::Instance1>,
34 Block: BlockT + DeserializeOwned,
35 Block::Header: DeserializeOwned,
36{
37 use frame_support::storage::generator::StorageMap;
38
39 let mut ext = Builder::<Block>::new()
40 .mode(Mode::Online(OnlineConfig {
41 transport: ws_url.to_string().into(),
42 pallets: vec![pallet_bags_list::Pallet::<Runtime, pallet_bags_list::Instance1>::name()
45 .to_string()],
46 at: None,
47 hashed_prefixes: vec![
48 <pallet_staking::Bonded<Runtime>>::prefix_hash().to_vec(),
49 <pallet_staking::Ledger<Runtime>>::prefix_hash().to_vec(),
50 <pallet_staking::Validators<Runtime>>::map_storage_final_prefix(),
51 <pallet_staking::Nominators<Runtime>>::map_storage_final_prefix(),
52 ],
53 hashed_keys: vec![
54 <pallet_staking::Validators<Runtime>>::counter_storage_final_key().to_vec(),
55 <pallet_staking::Nominators<Runtime>>::counter_storage_final_key().to_vec(),
56 ],
57 ..Default::default()
58 }))
59 .build()
60 .await
61 .unwrap();
62
63 ext.execute_with(|| {
64 use frame_election_provider_support::ElectionDataProvider;
65 log::info!(
66 target: crate::LOG_TARGET,
67 "{} nodes in bags list.",
68 <Runtime as pallet_staking::Config>::VoterList::count(),
69 );
70
71 let bounds = match voter_limit {
72 None => DataProviderBounds::default(),
73 Some(v) => DataProviderBounds { count: Some(CountBound(v as u32)), size: None },
74 };
75
76 let voters =
78 <pallet_staking::Pallet<Runtime> as ElectionDataProvider>::electing_voters(bounds, Zero::zero())
79 .unwrap();
80
81 let mut voters_nominator_only = voters
82 .iter()
83 .filter(|(v, _, _)| pallet_staking::Nominators::<Runtime>::contains_key(v))
84 .cloned()
85 .collect::<Vec<_>>();
86 voters_nominator_only.sort_by_key(|(_, w, _)| *w);
87
88 let currency_unit = currency_unit as f64;
89 let min_voter = voters_nominator_only
90 .first()
91 .map(|(x, y, _)| (x.clone(), *y as f64 / currency_unit));
92 let max_voter = voters_nominator_only
93 .last()
94 .map(|(x, y, _)| (x.clone(), *y as f64 / currency_unit));
95 log::info!(
96 target: crate::LOG_TARGET,
97 "a snapshot with limit {:?} has been created, {} voters are taken. min nominator: {:?}, max: {:?}",
98 voter_limit,
99 voters.len(),
100 min_voter,
101 max_voter
102 );
103 });
104}