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.7k
Expand file tree
/
Copy pathmigrations_3_0_0.rs
More file actions
199 lines (180 loc) · 5.77 KB
/
migrations_3_0_0.rs
File metadata and controls
199 lines (180 loc) · 5.77 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// This file is part of Substrate.
// Copyright (C) 2019-2020 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Migrations to version [`3.0.0`], as denoted by the changelog.
use codec::{Encode, Decode, FullCodec};
use sp_std::prelude::*;
use frame_support::{
RuntimeDebug, weights::Weight, Twox64Concat,
storage::types::{StorageMap, StorageValue},
traits::{GetPalletVersion, PalletVersion},
};
#[derive(Encode, Decode, Clone, Default, RuntimeDebug, PartialEq)]
struct SeatHolder<AccountId, Balance> {
who: AccountId,
stake: Balance,
deposit: Balance,
}
#[derive(Encode, Decode, Clone, Default, RuntimeDebug, PartialEq)]
struct Voter<AccountId, Balance> {
votes: Vec<AccountId>,
stake: Balance,
deposit: Balance,
}
pub trait V2ToV3 {
type Module: GetPalletVersion;
type AccountId: 'static + FullCodec;
type Balance: 'static + FullCodec + Copy;
}
struct __PhragmenElection;
impl frame_support::traits::PalletInfo for __PhragmenElection {
fn index<P: 'static>() -> Option<usize> { unreachable!() }
fn name<P: 'static>() -> Option<&'static str> { Some("PhragmenElection") }
}
struct __Candidates;
impl frame_support::traits::StorageInstance for __Candidates {
type Pallet = ();
type PalletInfo = __PhragmenElection;
const STORAGE_PREFIX: &'static str = "Candidates";
}
#[allow(type_alias_bounds)]
type Candidates<T: V2ToV3> = StorageValue<__Candidates, Vec<(T::AccountId, T::Balance)>>;
struct __Members;
impl frame_support::traits::StorageInstance for __Members {
type Pallet = ();
type PalletInfo = __PhragmenElection;
const STORAGE_PREFIX: &'static str = "Members";
}
#[allow(type_alias_bounds)]
type Members<T: V2ToV3> = StorageValue<__Members, Vec<SeatHolder<T::AccountId, T::Balance>>>;
struct __RunnersUp;
impl frame_support::traits::StorageInstance for __RunnersUp {
type Pallet = ();
type PalletInfo = __PhragmenElection;
const STORAGE_PREFIX: &'static str = "RunnersUp";
}
#[allow(type_alias_bounds)]
type RunnersUp<T: V2ToV3> = StorageValue<__RunnersUp, Vec<SeatHolder<T::AccountId, T::Balance>>>;
struct __Voting;
impl frame_support::traits::StorageInstance for __Voting {
type Pallet = ();
type PalletInfo = __PhragmenElection;
const STORAGE_PREFIX: &'static str = "Voting";
}
#[allow(type_alias_bounds)]
type Voting<T: V2ToV3> = StorageMap<__Voting, Twox64Concat, T::AccountId, Voter<T::AccountId, T::Balance>>;
/// Apply all of the migrations from 2_0_0 to 3_0_0.
///
/// ### Warning
///
/// This code will **ONLY** check that the storage version is less than 2_0_0. Further check
/// might be needed at the user runtime.
///
/// Be aware that this migration is intended to be used only for the mentioned versions. Use
/// with care and run at your own risk.
pub fn apply<T: V2ToV3>(
old_voter_bond: T::Balance,
old_candidacy_bond: T::Balance,
) -> Weight {
let maybe_storage_version = <T::Module as GetPalletVersion>::storage_version();
match maybe_storage_version {
Some(storage_version) if storage_version <= PalletVersion::new(2, 0, 0) => {
migrate_voters_to_recorded_deposit::<T>(old_voter_bond);
migrate_candidates_to_recorded_deposit::<T>(
old_candidacy_bond,
);
migrate_runners_up_to_recorded_deposit::<T>(
old_candidacy_bond,
);
migrate_members_to_recorded_deposit::<T>(
old_candidacy_bond,
);
Weight::max_value()
}
_ => 0,
}
}
/// Migrate from the old legacy voting bond (fixed) to the new one (per-vote dynamic).
pub fn migrate_voters_to_recorded_deposit<T: V2ToV3>(old_deposit: T::Balance) {
<Voting<T>>::translate::<(T::Balance, Vec<T::AccountId>), _>(
|_who, (stake, votes)| {
let deposit = old_deposit;
Some(Voter {
votes,
stake,
deposit,
})
},
);
frame_support::debug::info!(
"migrated {} voter accounts.",
<Voting<T>>::iter().count(),
);
}
/// Migrate all candidates to recorded deposit.
pub fn migrate_candidates_to_recorded_deposit<T: V2ToV3>(old_deposit: T::Balance) {
let _ = <Candidates<T>>::translate::<Vec<T::AccountId>, _>(
|maybe_old_candidates| {
maybe_old_candidates.map(|old_candidates| {
frame_support::debug::info!(
"migrated {} candidate accounts.",
old_candidates.len()
);
old_candidates
.into_iter()
.map(|c| (c, old_deposit))
.collect::<Vec<_>>()
})
},
);
}
/// Migrate all members to recorded deposit.
pub fn migrate_members_to_recorded_deposit<T: V2ToV3>(deposit: T::Balance) {
let _ = <Members<T>>::translate::<Vec<(T::AccountId, T::Balance)>, _>(
|maybe_old_members| {
maybe_old_members.map(|old_members| {
frame_support::debug::info!("migrated {} member accounts.", old_members.len());
old_members
.into_iter()
.map(|(who, stake)| SeatHolder {
who,
stake,
deposit,
})
.collect::<Vec<_>>()
})
},
);
}
/// Migrate all runners-up to recorded deposit.
pub fn migrate_runners_up_to_recorded_deposit<T: V2ToV3>(deposit: T::Balance) {
let _ = <RunnersUp<T>>::translate::<Vec<(T::AccountId, T::Balance)>, _>(
|maybe_old_runners_up| {
maybe_old_runners_up.map(|old_runners_up| {
frame_support::debug::info!(
"migrated {} runner-up accounts.",
old_runners_up.len()
);
old_runners_up
.into_iter()
.map(|(who, stake)| SeatHolder {
who,
stake,
deposit,
})
.collect::<Vec<_>>()
})
},
);
}