16
16
17
17
// If you feel like getting in touch with us, you can do so at info@botlabs.org
18
18
19
- use crate :: { migrations:: StakingStorageVersion , CandidateCount , CandidatePool , Config , StorageVersion } ;
20
- use frame_support:: { dispatch:: Weight , storage:: StorageValue , traits:: Get } ;
19
+ use crate :: {
20
+ migrations:: StakingStorageVersion ,
21
+ set:: OrderedSet ,
22
+ types:: { BalanceOf , Candidate , Stake , TotalStake } ,
23
+ CandidateCount , CandidatePool , Config , StorageVersion , TopCandidates , TotalCollatorStake ,
24
+ } ;
25
+ use frame_support:: {
26
+ dispatch:: Weight ,
27
+ storage:: { IterableStorageMap , StorageValue } ,
28
+ traits:: Get ,
29
+ } ;
21
30
22
31
#[ cfg( feature = "try-runtime" ) ]
23
32
pub ( crate ) fn pre_migrate < T : Config > ( ) -> Result < ( ) , & ' static str > {
@@ -31,21 +40,57 @@ pub(crate) fn migrate<T: Config>() -> Weight {
31
40
// Kill selected candidates list
32
41
old:: SelectedCandidates :: < T > :: kill ( ) ;
33
42
34
- // count candidates
35
- let counter: u32 = CandidatePool :: < T > :: iter ( ) . fold ( 0 , |acc, _| acc. saturating_add ( 1 ) ) ;
43
+ // migrate CandidatePool to TopCandidates
44
+ // DANGER: this needs to happen before we write to the new CandidatePool!
45
+ let old_candidate_pool = old:: CandidatePool :: < T > :: get ( ) ;
46
+ TopCandidates :: < T > :: put ( old_candidate_pool) ;
47
+ old:: CandidatePool :: < T > :: kill ( ) ;
48
+
49
+ // Copy over total
50
+ let total = old:: Total :: < T > :: get ( ) ;
51
+ TotalCollatorStake :: < T > :: set ( total) ;
52
+ old:: Total :: < T > :: kill ( ) ;
53
+
54
+ // kill, copy & count candidates
55
+ let counter: u32 = old:: CollatorState :: < T > :: drain ( ) . fold ( 0 , |acc, ( key, candidate) | {
56
+ CandidatePool :: < T > :: insert ( & key, candidate) ;
57
+ acc. saturating_add ( 1 )
58
+ } ) ;
36
59
CandidateCount :: < T > :: put ( counter) ;
37
60
38
61
// update storage version
39
62
StorageVersion :: < T > :: put ( StakingStorageVersion :: V5 ) ;
40
63
log:: info!( "Completed staking migration to StakingStorageVersion::V5" ) ;
41
64
42
- T :: DbWeight :: get ( ) . reads_writes ( counter. saturating_add ( 2 ) . into ( ) , 3 )
65
+ // Writes: 3 * Kill, 3 * Put, `counter` * inserts, at max 32 additional kills <
66
+ // counter + 38 Reads: (counter + 2) * get
67
+ T :: DbWeight :: get ( ) . reads_writes ( counter. saturating_add ( 38 ) . into ( ) , counter. saturating_add ( 2 ) . into ( ) )
43
68
}
44
69
45
70
#[ cfg( feature = "try-runtime" ) ]
46
71
pub ( crate ) fn post_migrate < T : Config > ( ) -> Result < ( ) , & ' static str > {
72
+ use sp_runtime:: SaturatedConversion ;
73
+
47
74
assert_eq ! ( StorageVersion :: <T >:: get( ) , StakingStorageVersion :: V5 ) ;
48
- assert ! ( CandidateCount :: <T >:: get( ) > T :: MinCollators :: get( ) ) ;
75
+ log:: info!(
76
+ "CandidateCount = {} >= {} = MinCollators" ,
77
+ CandidateCount :: <T >:: get( ) ,
78
+ T :: MinCollators :: get( )
79
+ ) ;
80
+ assert ! ( CandidateCount :: <T >:: get( ) >= T :: MinCollators :: get( ) ) ;
81
+ log:: info!(
82
+ "TopCandidates = {} >= {} = MinCollators" ,
83
+ TopCandidates :: <T >:: get( ) . len( ) ,
84
+ T :: MinCollators :: get( )
85
+ ) ;
86
+ assert ! ( TopCandidates :: <T >:: get( ) . len( ) . saturated_into:: <u32 >( ) >= T :: MinCollators :: get( ) ) ;
87
+ assert ! ( TopCandidates :: <T >:: get( ) . len( ) . saturated_into:: <u32 >( ) <= CandidateCount :: <T >:: get( ) ) ;
88
+ assert ! (
89
+ TotalCollatorStake :: <T >:: get( ) . collators
90
+ >= CandidateCount :: <T >:: get( ) . saturated_into:: <BalanceOf <T >>( ) * T :: MinCollatorStake :: get( )
91
+ ) ;
92
+ let counter: u32 = old:: CollatorState :: < T > :: iter ( ) . fold ( 0 , |acc, ( _, _) | acc. saturating_add ( 1 ) ) ;
93
+ assert ! ( counter == 0 ) ;
49
94
Ok ( ( ) )
50
95
}
51
96
@@ -60,7 +105,10 @@ pub(crate) mod old {
60
105
61
106
decl_storage ! {
62
107
trait Store for OldPallet <T : Config > as ParachainStaking {
108
+ pub ( crate ) Total : TotalStake <BalanceOf <T >>;
63
109
pub ( crate ) SelectedCandidates : Vec <T :: AccountId >;
110
+ pub ( crate ) CollatorState : map hasher( twox_64_concat) T :: AccountId => Option <Candidate <T :: AccountId , BalanceOf <T >, T :: MaxDelegatorsPerCollator >>;
111
+ pub ( crate ) CandidatePool : OrderedSet <Stake <T :: AccountId , BalanceOf <T >>, T :: MaxTopCandidates >;
64
112
}
65
113
}
66
114
}
0 commit comments