@@ -32,17 +32,6 @@ pub mod v2 {
3232 #[ cfg( feature = "try-runtime" ) ]
3333 use sp_std:: vec:: Vec ;
3434
35- /// [`UncheckedMigrationToV2`] wrapped in a
36- /// [`VersionedMigration`](frame_support::migrations::VersionedMigration), ensuring the
37- /// migration is only performed when on-chain version is 1.
38- pub type MigrationToV2 < T > = frame_support:: migrations:: VersionedMigration <
39- 1 ,
40- 2 ,
41- UncheckedMigrationToV2 < T > ,
42- Pallet < T > ,
43- <T as frame_system:: Config >:: DbWeight ,
44- > ;
45-
4635 #[ storage_alias]
4736 pub type Candidates < T : Config > = StorageValue <
4837 Pallet < T > ,
@@ -51,53 +40,64 @@ pub mod v2 {
5140 > ;
5241
5342 /// Migrate to V2.
54- pub struct UncheckedMigrationToV2 < T > ( sp_std:: marker:: PhantomData < T > ) ;
55- impl < T : Config + pallet_balances:: Config > UncheckedOnRuntimeUpgrade for UncheckedMigrationToV2 < T > {
43+ pub struct MigrationToV2 < T > ( sp_std:: marker:: PhantomData < T > ) ;
44+ impl < T : Config + pallet_balances:: Config > OnRuntimeUpgrade for MigrationToV2 < T > {
5645 fn on_runtime_upgrade ( ) -> Weight {
57- let mut weight = Weight :: zero ( ) ;
58- let mut count: u64 = 0 ;
59- // candidates who exist under the old `Candidates` key
60- let candidates = Candidates :: < T > :: take ( ) ;
61-
62- // New candidates who have registered since the upgrade. Under normal circumstances,
63- // this should not exist because the migration should be applied when the upgrade
64- // happens. But in Polkadot/Kusama we messed this up, and people registered under
65- // `CandidateList` while their funds were locked in `Candidates`.
66- let new_candidate_list = CandidateList :: < T > :: get ( ) ;
67- if new_candidate_list. len ( ) . is_zero ( ) {
68- // The new list is empty, so this is essentially being applied correctly. We just
69- // put the candidates into the new storage item.
70- CandidateList :: < T > :: put ( & candidates) ;
71- // 1 write for the new list
72- weight. saturating_accrue ( T :: DbWeight :: get ( ) . reads_writes ( 0 , 1 ) ) ;
73- } else {
74- // Oops, the runtime upgraded without the migration. There are new candidates in
75- // `CandidateList`. So, let's just refund the old ones and assume they have already
76- // started participating in the new system.
77- for candidate in candidates {
78- let err = T :: Currency :: unreserve ( & candidate. who , candidate. deposit ) ;
79- if err > Zero :: zero ( ) {
80- log:: error!(
81- target: LOG_TARGET ,
82- "{:?} balance was unable to be unreserved from {:?}" ,
83- err, & candidate. who,
84- ) ;
46+ let on_chain_version = Pallet :: < T > :: on_chain_storage_version ( ) ;
47+ if on_chain_version == 1 {
48+ let mut weight = Weight :: zero ( ) ;
49+ let mut count: u64 = 0 ;
50+ // candidates who exist under the old `Candidates` key
51+ let candidates = Candidates :: < T > :: take ( ) ;
52+
53+ // New candidates who have registered since the upgrade. Under normal circumstances,
54+ // this should not exist because the migration should be applied when the upgrade
55+ // happens. But in Polkadot/Kusama we messed this up, and people registered under
56+ // `CandidateList` while their funds were locked in `Candidates`.
57+ let new_candidate_list = CandidateList :: < T > :: get ( ) ;
58+ if new_candidate_list. len ( ) . is_zero ( ) {
59+ // The new list is empty, so this is essentially being applied correctly. We
60+ // just put the candidates into the new storage item.
61+ CandidateList :: < T > :: put ( & candidates) ;
62+ // 1 write for the new list
63+ weight. saturating_accrue ( T :: DbWeight :: get ( ) . reads_writes ( 0 , 1 ) ) ;
64+ } else {
65+ // Oops, the runtime upgraded without the migration. There are new candidates in
66+ // `CandidateList`. So, let's just refund the old ones and assume they have
67+ // already started participating in the new system.
68+ for candidate in candidates {
69+ let err = T :: Currency :: unreserve ( & candidate. who , candidate. deposit ) ;
70+ if err > Zero :: zero ( ) {
71+ log:: error!(
72+ target: LOG_TARGET ,
73+ "{:?} balance was unable to be unreserved from {:?}" ,
74+ err, & candidate. who,
75+ ) ;
76+ }
77+ count. saturating_inc ( ) ;
8578 }
86- count. saturating_inc ( ) ;
79+ weight. saturating_accrue (
80+ <<T as pallet_balances:: Config >:: WeightInfo as pallet_balances:: WeightInfo >:: force_unreserve ( ) . saturating_mul ( count. into ( ) ) ,
81+ ) ;
8782 }
88- weight. saturating_accrue (
89- <<T as pallet_balances:: Config >:: WeightInfo as pallet_balances:: WeightInfo >:: force_unreserve ( ) . saturating_mul ( count. into ( ) ) ,
90- ) ;
91- }
9283
93- log:: info!(
94- target: LOG_TARGET ,
95- "Unreserved locked bond of {} candidates, upgraded storage to version 2" ,
96- count,
97- ) ;
84+ StorageVersion :: new ( 2 ) . put :: < Pallet < T > > ( ) ;
85+
86+ log:: info!(
87+ target: LOG_TARGET ,
88+ "Unreserved locked bond of {} candidates, upgraded storage to version 2" ,
89+ count,
90+ ) ;
9891
99- weight. saturating_accrue ( T :: DbWeight :: get ( ) . reads_writes ( 3 , 2 ) ) ;
100- weight
92+ weight. saturating_accrue ( T :: DbWeight :: get ( ) . reads_writes ( 3 , 2 ) ) ;
93+ weight
94+ } else {
95+ log:: info!(
96+ target: LOG_TARGET ,
97+ "Migration did not execute. This probably should be removed"
98+ ) ;
99+ T :: DbWeight :: get ( ) . reads ( 1 )
100+ }
101101 }
102102
103103 #[ cfg( feature = "try-runtime" ) ]
0 commit comments