referrerpolicy=no-referrer-when-downgrade

pallet_bags_list_remote_tests/
snapshot.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Substrate.
3// SPDX-License-Identifier: Apache-2.0
4
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// 	http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17//! Test to execute the snapshot using the voter bag.
18
19use 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
30/// Execute create a snapshot from pallet-staking.
31pub 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			// NOTE: we don't scrape pallet-staking, this kinda ensures that the source of the data
43			// is bags-list.
44			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		// single page voter snapshot, thus page index == 0.
77		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}