1+ use super :: * ;
2+
3+ use frame_support:: weights:: Weight ;
4+
5+ /// Deprecated storages used for migration only.
6+ mod deprecated {
7+ use crate :: { Trait , BalanceOf , SessionIndex , Exposure } ;
8+ use codec:: { Encode , Decode } ;
9+ use frame_support:: { decl_module, decl_storage} ;
10+ use sp_std:: prelude:: * ;
11+
12+ // edgeware uses `u64` for `Moment`
13+ type Moment = u64 ;
14+
15+ /// Reward points of an era. Used to split era total payout between validators.
16+ #[ derive( Encode , Decode , Default ) ]
17+ pub struct EraPoints {
18+ /// Total number of points. Equals the sum of reward points for each validator.
19+ pub total : u32 ,
20+ /// The reward points earned by a given validator. The index of this vec corresponds to the
21+ /// index into the current validator set.
22+ pub individual : Vec < u32 > ,
23+ }
24+
25+ decl_module ! {
26+ pub struct Module <T : Trait > for enum Call where origin: T :: Origin { }
27+ }
28+
29+ decl_storage ! {
30+ pub trait Store for Module <T : Trait > as Staking {
31+ pub SlotStake : BalanceOf <T >;
32+
33+ /// The currently elected validator set keyed by stash account ID.
34+ pub CurrentElected : Vec <T :: AccountId >;
35+
36+ /// The start of the current era.
37+ pub CurrentEraStart : Moment ;
38+
39+ /// The session index at which the current era started.
40+ pub CurrentEraStartSessionIndex : SessionIndex ;
41+
42+ /// Rewards for the current era. Using indices of current elected set.
43+ pub CurrentEraPointsEarned : EraPoints ;
44+
45+ /// Nominators for a particular account that is in action right now. You can't iterate
46+ /// through validators here, but you can find them in the Session module.
47+ ///
48+ /// This is keyed by the stash account.
49+ pub Stakers : map hasher( opaque_blake2_256) T :: AccountId => Exposure <T :: AccountId , BalanceOf <T >>;
50+ }
51+ }
52+ }
53+
54+ #[ derive( PartialEq , Eq , Clone , Encode , Decode , RuntimeDebug ) ]
55+ struct OldStakingLedger < AccountId , Balance : HasCompact > {
56+ pub stash : AccountId ,
57+ #[ codec( compact) ]
58+ pub total : Balance ,
59+ #[ codec( compact) ]
60+ pub active : Balance ,
61+ pub unlocking : Vec < UnlockChunk < Balance > > ,
62+ }
63+
64+ /// Update storages to current version
65+ ///
66+ /// In old version the staking module has several issue about handling session delay, the
67+ /// current era was always considered the active one.
68+ ///
69+ /// After the migration the current era will still be considered the active one for the era of
70+ /// the upgrade. And the delay issue will be fixed when planning the next era.
71+ // * create:
72+ // * ActiveEraStart
73+ // * ErasRewardPoints
74+ // * ActiveEra
75+ // * ErasStakers
76+ // * ErasStakersClipped
77+ // * ErasValidatorPrefs
78+ // * ErasTotalStake
79+ // * ErasStartSessionIndex
80+ // * translate StakingLedger
81+ // * removal of:
82+ // * Stakers
83+ // * SlotStake
84+ // * CurrentElected
85+ // * CurrentEraStart
86+ // * CurrentEraStartSessionIndex
87+ // * CurrentEraPointsEarned
88+ pub fn migrate_to_simple_payouts < T : Trait > ( ) -> Weight {
89+ sp_runtime:: print ( "🕊️ Migrating Staking..." ) ;
90+ let current_era_start_index = deprecated:: CurrentEraStartSessionIndex :: get ( ) ;
91+ let current_era = <Module < T > as Store >:: CurrentEra :: get ( ) . unwrap_or ( 0 ) ;
92+ let current_era_start = deprecated:: CurrentEraStart :: get ( ) ;
93+ <Module < T > as Store >:: ErasStartSessionIndex :: insert ( current_era, current_era_start_index) ;
94+ <Module < T > as Store >:: ActiveEra :: put ( ActiveEraInfo {
95+ index : current_era,
96+ start : Some ( current_era_start) ,
97+ } ) ;
98+
99+ let current_elected = deprecated:: CurrentElected :: < T > :: get ( ) ;
100+ let mut current_total_stake = <BalanceOf < T > >:: zero ( ) ;
101+ for validator in & current_elected {
102+ let exposure = deprecated:: Stakers :: < T > :: get ( validator) ;
103+ current_total_stake += exposure. total ;
104+ <Module < T > as Store >:: ErasStakers :: insert ( current_era, validator, & exposure) ;
105+
106+ let mut exposure_clipped = exposure;
107+ let clipped_max_len = T :: MaxNominatorRewardedPerValidator :: get ( ) as usize ;
108+ if exposure_clipped. others . len ( ) > clipped_max_len {
109+ exposure_clipped. others . sort_unstable_by ( |a, b| a. value . cmp ( & b. value ) . reverse ( ) ) ;
110+ exposure_clipped. others . truncate ( clipped_max_len) ;
111+ }
112+ <Module < T > as Store >:: ErasStakersClipped :: insert ( current_era, validator, exposure_clipped) ;
113+
114+ let pref = <Module < T > as Store >:: Validators :: get ( validator) ;
115+ <Module < T > as Store >:: ErasValidatorPrefs :: insert ( current_era, validator, pref) ;
116+ }
117+ <Module < T > as Store >:: ErasTotalStake :: insert ( current_era, current_total_stake) ;
118+
119+ let points = deprecated:: CurrentEraPointsEarned :: get ( ) ;
120+ <Module < T > as Store >:: ErasRewardPoints :: insert ( current_era, EraRewardPoints {
121+ total : points. total ,
122+ individual : current_elected. iter ( ) . cloned ( ) . zip ( points. individual . iter ( ) . cloned ( ) ) . collect ( ) ,
123+ } ) ;
124+
125+ let res = <Module < T > as Store >:: Ledger :: translate_values (
126+ |old : OldStakingLedger < T :: AccountId , BalanceOf < T > > | StakingLedger {
127+ stash : old. stash ,
128+ total : old. total ,
129+ active : old. active ,
130+ unlocking : old. unlocking ,
131+ claimed_rewards : vec ! [ ] ,
132+ }
133+ ) ;
134+ if let Err ( e) = res {
135+ frame_support:: print ( "Encountered error in migration of Staking::Ledger map." ) ;
136+ frame_support:: print ( "The number of removed key/value is:" ) ;
137+ frame_support:: print ( e) ;
138+ }
139+
140+ // Kill old storages
141+ deprecated:: Stakers :: < T > :: remove_all ( ) ;
142+ deprecated:: SlotStake :: < T > :: kill ( ) ;
143+ deprecated:: CurrentElected :: < T > :: kill ( ) ;
144+ deprecated:: CurrentEraStart :: kill ( ) ;
145+ deprecated:: CurrentEraStartSessionIndex :: kill ( ) ;
146+ deprecated:: CurrentEraPointsEarned :: kill ( ) ;
147+
148+ sp_runtime:: print ( "🕊️ Done Staking." ) ;
149+ // TODO: determine actual weight?
150+ T :: MaximumBlockWeight :: get ( )
151+ }
0 commit comments