pallet_elections_phragmen/migrations/v5.rs
1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18use super::super::*;
19use alloc::{boxed::Box, vec::Vec};
20
21/// Migrate the locks and vote stake on accounts (as specified with param `to_migrate`) that have
22/// more than their free balance locked.
23///
24/// This migration addresses a bug were a voter could lock up to their reserved balance + free
25/// balance. Since locks are only designed to operate on free balance, this put those affected in a
26/// situation where they could increase their free balance but still not be able to use their funds
27/// because they were less than the lock.
28pub fn migrate<T: Config>(to_migrate: Vec<T::AccountId>) -> Weight {
29 let mut weight = Weight::zero();
30
31 for who in to_migrate.iter() {
32 if let Ok(mut voter) = Voting::<T>::try_get(who) {
33 let free_balance = T::Currency::free_balance(who);
34
35 weight = weight.saturating_add(T::DbWeight::get().reads(2));
36
37 if voter.stake > free_balance {
38 voter.stake = free_balance;
39 Voting::<T>::insert(&who, voter);
40
41 let pallet_id = T::PalletId::get();
42 T::Currency::set_lock(pallet_id, who, free_balance, WithdrawReasons::all());
43
44 weight = weight.saturating_add(T::DbWeight::get().writes(2));
45 }
46 }
47 }
48
49 weight
50}
51
52/// Given the list of voters to migrate return a function that does some checks and information
53/// prior to migration. This can be linked to [`frame_support::traits::OnRuntimeUpgrade::
54/// pre_upgrade`] for further testing.
55pub fn pre_migrate_fn<T: Config>(to_migrate: Vec<T::AccountId>) -> Box<dyn Fn() -> ()> {
56 Box::new(move || {
57 for who in to_migrate.iter() {
58 if let Ok(voter) = Voting::<T>::try_get(who) {
59 let free_balance = T::Currency::free_balance(who);
60
61 if voter.stake > free_balance {
62 // all good
63 } else {
64 log::warn!("pre-migrate elections-phragmen: voter={:?} has less stake then free balance", who);
65 }
66 } else {
67 log::warn!("pre-migrate elections-phragmen: cannot find voter={:?}", who);
68 }
69 }
70 log::info!("pre-migrate elections-phragmen complete");
71 })
72}
73
74/// Some checks for after migration. This can be linked to
75/// `frame_support::traits::OnRuntimeUpgrade::post_upgrade` for further testing.
76///
77/// Panics if anything goes wrong.
78pub fn post_migrate<T: crate::Config>() {
79 for (who, voter) in Voting::<T>::iter() {
80 let free_balance = T::Currency::free_balance(&who);
81
82 assert!(voter.stake <= free_balance, "migration should have made locked <= free_balance");
83 // Ideally we would also check that the locks and AccountData.misc_frozen where correctly
84 // updated, but since both of those are generic we can't do that without further bounding T.
85 }
86
87 log::info!("post-migrate elections-phragmen complete");
88}