use crate::{RuntimeT, LOG_TARGET};
use frame_support::traits::PalletInfoAccess;
use pallet_staking::Nominators;
use remote_externalities::{Builder, Mode, OnlineConfig};
use sp_runtime::{traits::Block as BlockT, DeserializeOwned};
pub async fn execute<Runtime, Block>(
currency_unit: u64,
currency_name: &'static str,
ws_url: String,
) where
Runtime: RuntimeT<pallet_bags_list::Instance1>,
Block: BlockT + DeserializeOwned,
Block::Header: DeserializeOwned,
{
let mut ext = Builder::<Block>::new()
.mode(Mode::Online(OnlineConfig {
transport: ws_url.to_string().into(),
pallets: vec![pallet_staking::Pallet::<Runtime>::name().to_string()],
..Default::default()
}))
.build()
.await
.unwrap();
ext.execute_with(|| {
let pre_migrate_nominator_count = <Nominators<Runtime>>::iter().count() as u32;
log::info!(target: LOG_TARGET, "Nominator count: {}", pre_migrate_nominator_count);
use frame_election_provider_support::SortedListProvider;
let moved = <Runtime as pallet_staking::Config>::VoterList::unsafe_regenerate(
pallet_staking::Nominators::<Runtime>::iter().map(|(n, _)| n),
pallet_staking::Pallet::<Runtime>::weight_of_fn(),
);
log::info!(target: LOG_TARGET, "Moved {} nominators", moved);
let voter_list_len = <Runtime as pallet_staking::Config>::VoterList::iter().count() as u32;
let voter_list_count = <Runtime as pallet_staking::Config>::VoterList::count();
assert_eq!(pre_migrate_nominator_count, voter_list_len);
assert_eq!(pre_migrate_nominator_count, voter_list_count);
crate::display_and_check_bags::<Runtime>(currency_unit, currency_name);
});
}