This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathsnapshot.rs
More file actions
96 lines (85 loc) · 3.23 KB
/
snapshot.rs
File metadata and controls
96 lines (85 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! Test to execute the snapshot using the voter bag.
use frame_election_provider_support::{ElectionBounds, SortedListProvider};
use frame_support::traits::PalletInfoAccess;
use remote_externalities::{Builder, Mode, OnlineConfig};
use sp_runtime::{traits::Block as BlockT, DeserializeOwned};
/// Execute create a snapshot from pallet-staking.
pub async fn execute<Runtime, Block>(
election_bounds: ElectionBounds,
currency_unit: u64,
ws_url: String,
) where
Runtime: crate::RuntimeT<pallet_bags_list::Instance1>,
Block: BlockT + DeserializeOwned,
Block::Header: DeserializeOwned,
{
use frame_support::storage::generator::StorageMap;
let mut ext = Builder::<Block>::new()
.mode(Mode::Online(OnlineConfig {
transport: ws_url.to_string().into(),
// NOTE: we don't scrape pallet-staking, this kinda ensures that the source of the data
// is bags-list.
pallets: vec![pallet_bags_list::Pallet::<Runtime, pallet_bags_list::Instance1>::name()
.to_string()],
at: None,
hashed_prefixes: vec![
<pallet_staking::Bonded<Runtime>>::prefix_hash(),
<pallet_staking::Ledger<Runtime>>::prefix_hash(),
<pallet_staking::Validators<Runtime>>::map_storage_final_prefix(),
<pallet_staking::Nominators<Runtime>>::map_storage_final_prefix(),
],
hashed_keys: vec![
<pallet_staking::Validators<Runtime>>::counter_storage_final_key().to_vec(),
<pallet_staking::Nominators<Runtime>>::counter_storage_final_key().to_vec(),
],
..Default::default()
}))
.build()
.await
.unwrap();
ext.execute_with(|| {
use frame_election_provider_support::ElectionDataProvider;
log::info!(
target: crate::LOG_TARGET,
"{} nodes in bags list.",
<Runtime as pallet_staking::Config>::VoterList::count(),
);
let voters = <pallet_staking::Pallet<Runtime> as ElectionDataProvider>::electing_voters(
election_bounds.voters,
)
.unwrap();
let mut voters_nominator_only = voters
.iter()
.filter(|(v, _, _)| pallet_staking::Nominators::<Runtime>::contains_key(v))
.cloned()
.collect::<Vec<_>>();
voters_nominator_only.sort_by_key(|(_, w, _)| *w);
let currency_unit = currency_unit as f64;
let min_voter = voters_nominator_only
.first()
.map(|(x, y, _)| (x.clone(), *y as f64 / currency_unit));
let max_voter = voters_nominator_only
.last()
.map(|(x, y, _)| (x.clone(), *y as f64 / currency_unit));
log::info!(
target: crate::LOG_TARGET,
"a snapshot with limits {:?} has been created, {} voters are taken. min nominator: {:?}, max: {:?}",
election_bounds,
voters.len(),
min_voter,
max_voter
);
});
}