referrerpolicy=no-referrer-when-downgrade

pallet_bags_list_remote_tests/
migration.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 check the migration of the voter bag.
18
19use crate::{RuntimeT, LOG_TARGET};
20use frame_support::traits::PalletInfoAccess;
21use pallet_staking::Nominators;
22use remote_externalities::{Builder, Mode, OnlineConfig};
23use sp_runtime::{traits::Block as BlockT, DeserializeOwned};
24
25/// Test voter bags migration. `currency_unit` is the number of planks per the the runtimes `UNITS`
26/// (i.e. number of decimal places per DOT, KSM etc)
27pub async fn execute<Runtime, Block>(
28	currency_unit: u64,
29	currency_name: &'static str,
30	ws_url: String,
31) where
32	Runtime: RuntimeT<pallet_bags_list::Instance1>,
33	Block: BlockT + DeserializeOwned,
34	Block::Header: DeserializeOwned,
35{
36	let mut ext = Builder::<Block>::new()
37		.mode(Mode::Online(OnlineConfig {
38			transport: ws_url.to_string().into(),
39			pallets: vec![pallet_staking::Pallet::<Runtime>::name().to_string()],
40			..Default::default()
41		}))
42		.build()
43		.await
44		.unwrap();
45
46	ext.execute_with(|| {
47		// get the nominator & validator count prior to migrating; these should be invariant.
48		let pre_migrate_nominator_count = <Nominators<Runtime>>::iter().count() as u32;
49		log::info!(target: LOG_TARGET, "Nominator count: {}", pre_migrate_nominator_count);
50
51		use frame_election_provider_support::SortedListProvider;
52		// run the actual migration
53		let moved = <Runtime as pallet_staking::Config>::VoterList::unsafe_regenerate(
54			pallet_staking::Nominators::<Runtime>::iter().map(|(n, _)| n),
55			Box::new(|x| Some(pallet_staking::Pallet::<Runtime>::weight_of(x))),
56		);
57		log::info!(target: LOG_TARGET, "Moved {} nominators", moved);
58
59		let voter_list_len = <Runtime as pallet_staking::Config>::VoterList::iter().count() as u32;
60		let voter_list_count = <Runtime as pallet_staking::Config>::VoterList::count();
61		// and confirm it is equal to the length of the `VoterList`.
62		assert_eq!(pre_migrate_nominator_count, voter_list_len);
63		assert_eq!(pre_migrate_nominator_count, voter_list_count);
64
65		crate::display_and_check_bags::<Runtime>(currency_unit, currency_name);
66	});
67}