1717
1818//! Migrations to version [`3.0.0`], as denoted by the changelog.
1919
20+ use crate :: { Config , Pallet } ;
2021use codec:: { Decode , Encode , FullCodec } ;
2122use frame_support:: {
22- pallet_prelude:: ValueQuery ,
23- traits:: { PalletInfoAccess , StorageVersion } ,
24- weights:: Weight ,
25- RuntimeDebug , Twox64Concat ,
23+ pallet_prelude:: ValueQuery , traits:: StorageVersion , weights:: Weight , RuntimeDebug , Twox64Concat ,
2624} ;
2725use sp_std:: prelude:: * ;
2826
@@ -42,40 +40,38 @@ struct Voter<AccountId, Balance> {
4240
4341/// Trait to implement to give information about types used for migration
4442pub trait V2ToV3 {
45- /// The elections-phragmen pallet.
46- type Pallet : ' static + PalletInfoAccess ;
47-
4843 /// System config account id
4944 type AccountId : ' static + FullCodec ;
5045
5146 /// Elections-phragmen currency balance.
5247 type Balance : ' static + FullCodec + Copy ;
5348}
5449
55- frame_support:: generate_storage_alias!(
56- PhragmenElection , Candidates <T : V2ToV3 > => Value <
57- Vec <( T :: AccountId , T :: Balance ) >,
58- ValueQuery
59- >
60- ) ;
61- frame_support:: generate_storage_alias!(
62- PhragmenElection , Members <T : V2ToV3 > => Value <
63- Vec <SeatHolder <T :: AccountId , T :: Balance >>,
64- ValueQuery
65- >
66- ) ;
67- frame_support:: generate_storage_alias!(
68- PhragmenElection , RunnersUp <T : V2ToV3 > => Value <
69- Vec <SeatHolder <T :: AccountId , T :: Balance >>,
70- ValueQuery
71- >
72- ) ;
73- frame_support:: generate_storage_alias!(
74- PhragmenElection , Voting <T : V2ToV3 > => Map <
75- ( Twox64Concat , T :: AccountId ) ,
76- Voter <T :: AccountId , T :: Balance >
77- >
78- ) ;
50+ #[ frame_support:: storage_alias]
51+ type Candidates < V , T : Config > =
52+ StorageValue < Pallet < T > , Vec < ( <V as V2ToV3 >:: AccountId , <V as V2ToV3 >:: Balance ) > , ValueQuery > ;
53+
54+ #[ frame_support:: storage_alias]
55+ type Members < V , T : Config > = StorageValue <
56+ Pallet < T > ,
57+ Vec < SeatHolder < <V as V2ToV3 >:: AccountId , <V as V2ToV3 >:: Balance > > ,
58+ ValueQuery ,
59+ > ;
60+
61+ #[ frame_support:: storage_alias]
62+ type RunnersUp < V , T : Config > = StorageValue <
63+ Pallet < T > ,
64+ Vec < SeatHolder < <V as V2ToV3 >:: AccountId , <V as V2ToV3 >:: Balance > > ,
65+ ValueQuery ,
66+ > ;
67+
68+ #[ frame_support:: storage_alias]
69+ type Voting < V , T : Config > = StorageMap <
70+ Pallet < T > ,
71+ Twox64Concat ,
72+ <V as V2ToV3 >:: AccountId ,
73+ Voter < <V as V2ToV3 >:: AccountId , <V as V2ToV3 >:: Balance > ,
74+ > ;
7975
8076/// Apply all of the migrations from 2 to 3.
8177///
@@ -86,21 +82,24 @@ frame_support::generate_storage_alias!(
8682///
8783/// Be aware that this migration is intended to be used only for the mentioned versions. Use
8884/// with care and run at your own risk.
89- pub fn apply < T : V2ToV3 > ( old_voter_bond : T :: Balance , old_candidacy_bond : T :: Balance ) -> Weight {
90- let storage_version = StorageVersion :: get :: < T :: Pallet > ( ) ;
85+ pub fn apply < V : V2ToV3 , T : Config > (
86+ old_voter_bond : V :: Balance ,
87+ old_candidacy_bond : V :: Balance ,
88+ ) -> Weight {
89+ let storage_version = StorageVersion :: get :: < Pallet < T > > ( ) ;
9190 log:: info!(
9291 target: "runtime::elections-phragmen" ,
9392 "Running migration for elections-phragmen with storage version {:?}" ,
9493 storage_version,
9594 ) ;
9695
9796 if storage_version <= 2 {
98- migrate_voters_to_recorded_deposit :: < T > ( old_voter_bond) ;
99- migrate_candidates_to_recorded_deposit :: < T > ( old_candidacy_bond) ;
100- migrate_runners_up_to_recorded_deposit :: < T > ( old_candidacy_bond) ;
101- migrate_members_to_recorded_deposit :: < T > ( old_candidacy_bond) ;
97+ migrate_voters_to_recorded_deposit :: < V , T > ( old_voter_bond) ;
98+ migrate_candidates_to_recorded_deposit :: < V , T > ( old_candidacy_bond) ;
99+ migrate_runners_up_to_recorded_deposit :: < V , T > ( old_candidacy_bond) ;
100+ migrate_members_to_recorded_deposit :: < V , T > ( old_candidacy_bond) ;
102101
103- StorageVersion :: new ( 3 ) . put :: < T :: Pallet > ( ) ;
102+ StorageVersion :: new ( 3 ) . put :: < Pallet < T > > ( ) ;
104103
105104 Weight :: max_value ( )
106105 } else {
@@ -114,21 +113,21 @@ pub fn apply<T: V2ToV3>(old_voter_bond: T::Balance, old_candidacy_bond: T::Balan
114113}
115114
116115/// Migrate from the old legacy voting bond (fixed) to the new one (per-vote dynamic).
117- pub fn migrate_voters_to_recorded_deposit < T : V2ToV3 > ( old_deposit : T :: Balance ) {
118- <Voting < T > >:: translate :: < ( T :: Balance , Vec < T :: AccountId > ) , _ > ( |_who, ( stake, votes) | {
116+ pub fn migrate_voters_to_recorded_deposit < V : V2ToV3 , T : Config > ( old_deposit : V :: Balance ) {
117+ <Voting < V , T > >:: translate :: < ( V :: Balance , Vec < V :: AccountId > ) , _ > ( |_who, ( stake, votes) | {
119118 Some ( Voter { votes, stake, deposit : old_deposit } )
120119 } ) ;
121120
122121 log:: info!(
123122 target: "runtime::elections-phragmen" ,
124123 "migrated {} voter accounts." ,
125- <Voting <T >>:: iter( ) . count( ) ,
124+ <Voting <V , T >>:: iter( ) . count( ) ,
126125 ) ;
127126}
128127
129128/// Migrate all candidates to recorded deposit.
130- pub fn migrate_candidates_to_recorded_deposit < T : V2ToV3 > ( old_deposit : T :: Balance ) {
131- let _ = <Candidates < T > >:: translate :: < Vec < T :: AccountId > , _ > ( |maybe_old_candidates| {
129+ pub fn migrate_candidates_to_recorded_deposit < V : V2ToV3 , T : Config > ( old_deposit : V :: Balance ) {
130+ let _ = <Candidates < V , T > >:: translate :: < Vec < V :: AccountId > , _ > ( |maybe_old_candidates| {
132131 maybe_old_candidates. map ( |old_candidates| {
133132 log:: info!(
134133 target: "runtime::elections-phragmen" ,
@@ -141,8 +140,8 @@ pub fn migrate_candidates_to_recorded_deposit<T: V2ToV3>(old_deposit: T::Balance
141140}
142141
143142/// Migrate all members to recorded deposit.
144- pub fn migrate_members_to_recorded_deposit < T : V2ToV3 > ( old_deposit : T :: Balance ) {
145- let _ = <Members < T > >:: translate :: < Vec < ( T :: AccountId , T :: Balance ) > , _ > ( |maybe_old_members| {
143+ pub fn migrate_members_to_recorded_deposit < V : V2ToV3 , T : Config > ( old_deposit : V :: Balance ) {
144+ let _ = <Members < V , T > >:: translate :: < Vec < ( V :: AccountId , V :: Balance ) > , _ > ( |maybe_old_members| {
146145 maybe_old_members. map ( |old_members| {
147146 log:: info!(
148147 target: "runtime::elections-phragmen" ,
@@ -158,9 +157,9 @@ pub fn migrate_members_to_recorded_deposit<T: V2ToV3>(old_deposit: T::Balance) {
158157}
159158
160159/// Migrate all runners-up to recorded deposit.
161- pub fn migrate_runners_up_to_recorded_deposit < T : V2ToV3 > ( old_deposit : T :: Balance ) {
162- let _ =
163- < RunnersUp < T > > :: translate :: < Vec < ( T :: AccountId , T :: Balance ) > , _ > ( |maybe_old_runners_up| {
160+ pub fn migrate_runners_up_to_recorded_deposit < V : V2ToV3 , T : Config > ( old_deposit : V :: Balance ) {
161+ let _ = < RunnersUp < V , T > > :: translate :: < Vec < ( V :: AccountId , V :: Balance ) > , _ > (
162+ |maybe_old_runners_up| {
164163 maybe_old_runners_up. map ( |old_runners_up| {
165164 log:: info!(
166165 target: "runtime::elections-phragmen" ,
@@ -172,5 +171,6 @@ pub fn migrate_runners_up_to_recorded_deposit<T: V2ToV3>(old_deposit: T::Balance
172171 . map ( |( who, stake) | SeatHolder { who, stake, deposit : old_deposit } )
173172 . collect :: < Vec < _ > > ( )
174173 } )
175- } ) ;
174+ } ,
175+ ) ;
176176}
0 commit comments