@@ -3,23 +3,25 @@ package app
33
44import (
55 "encoding/json"
6- "log"
76
7+ storetypes "cosmossdk.io/store/types"
8+ tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
89 servertypes "github.com/cosmos/cosmos-sdk/server/types"
910 sdk "github.com/cosmos/cosmos-sdk/types"
1011 slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
1112 "github.com/cosmos/cosmos-sdk/x/staking"
1213 stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
13- tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
1414)
1515
1616// ExportAppStateAndValidators exports the state of the application for a genesis
1717// file.
1818func (app * OnomyApp ) ExportAppStateAndValidators (
19- forZeroHeight bool , jailAllowedAddrs []string ,
19+ forZeroHeight bool ,
20+ jailAllowedAddrs []string ,
21+ modulesToExport []string ,
2022) (servertypes.ExportedApp , error ) {
21- // as if they could withdraw from the start of the next block
22- ctx := app .NewContext (true , tmproto.Header {Height : app .LastBlockHeight ()})
23+ // as if they could withdraw from the start of the next block.
24+ ctx := app .NewContextLegacy (true , tmproto.Header {Height : app .LastBlockHeight ()})
2325
2426 // We export at last height + 1, because that's the height at which
2527 // Tendermint will start InitChain.
@@ -29,31 +31,32 @@ func (app *OnomyApp) ExportAppStateAndValidators(
2931 app .prepForZeroHeightGenesis (ctx , jailAllowedAddrs )
3032 }
3133
32- genState := app .mm .ExportGenesis (ctx , app .appCodec )
33- appState , err := json .MarshalIndent (genState , "" , " " )
34+ genState , err := app .mm .ExportGenesisForModules (ctx , app .appCodec , modulesToExport )
3435 if err != nil {
3536 return servertypes.ExportedApp {}, err
3637 }
3738
38- validators , err := staking . WriteValidators ( ctx , app . StakingKeeper )
39+ appState , err := json . MarshalIndent ( genState , "" , " " )
3940 if err != nil {
4041 return servertypes.ExportedApp {}, err
4142 }
43+
44+ validators , err := staking .WriteValidators (ctx , app .StakingKeeper )
4245 return servertypes.ExportedApp {
4346 AppState : appState ,
4447 Validators : validators ,
4548 Height : height ,
4649 ConsensusParams : app .BaseApp .GetConsensusParams (ctx ),
47- }, nil
50+ }, err
4851}
4952
5053// prepare for fresh start at zero height
5154// NOTE zero height genesis is a temporary feature which will be deprecated
52- // in favor of export at a block height
53- func (app * OnomyApp ) prepForZeroHeightGenesis (ctx sdk.Context , jailAllowedAddrs []string ) { // nolint:cyclop,gocyclo,gocognit // generated by cosmos
55+ // in favor of export at a block height.
56+ func (app * OnomyApp ) prepForZeroHeightGenesis (ctx sdk.Context , jailAllowedAddrs []string ) {
5457 applyAllowedAddrs := false
5558
56- // check if there is a allowed address list
59+ // check if there is a allowed address list.
5760 if len (jailAllowedAddrs ) > 0 {
5861 applyAllowedAddrs = true
5962 }
@@ -63,7 +66,7 @@ func (app *OnomyApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs
6366 for _ , addr := range jailAllowedAddrs {
6467 _ , err := sdk .ValAddressFromBech32 (addr )
6568 if err != nil {
66- log . Fatal (err )
69+ panic (err )
6770 }
6871 allowedAddrsMap [addr ] = true
6972 }
@@ -73,114 +76,183 @@ func (app *OnomyApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs
7376
7477 /* Handle fee distribution state. */
7578
76- // withdraw all validator commission
77- app .StakingKeeper .IterateValidators (ctx , func (_ int64 , val stakingtypes.ValidatorI ) (stop bool ) {
78- _ , err := app .DistrKeeper . WithdrawValidatorCommission ( ctx , val .GetOperator ())
79+ // withdraw all validator commission.
80+ err := app .StakingKeeper .IterateValidators (ctx , func (_ int64 , val stakingtypes.ValidatorI ) (stop bool ) {
81+ valAddr , err := app .StakingKeeper . ValidatorAddressCodec (). StringToBytes ( val .GetOperator ())
7982 if err != nil {
80- panic (err )
83+ app .Logger ().Error (err .Error (), "ValOperatorAddress" , val .GetOperator ())
84+ }
85+ _ , err = app .DistrKeeper .WithdrawValidatorCommission (ctx , valAddr )
86+ if err != nil {
87+ app .Logger ().Error (err .Error (), "ValOperatorAddress" , val .GetOperator ())
8188 }
8289 return false
8390 })
91+ if err != nil {
92+ panic (err )
93+ }
8494
85- // withdraw all delegator rewards
86- dels := app .StakingKeeper .GetAllDelegations (ctx )
95+ // withdraw all delegator rewards.
96+ dels , err := app .StakingKeeper .GetAllDelegations (ctx )
97+ if err != nil {
98+ panic (err )
99+ }
87100 for _ , delegation := range dels {
88- _ , err := app .DistrKeeper .WithdrawDelegationRewards (ctx , delegation .GetDelegatorAddr (), delegation .GetValidatorAddr ())
101+ valAddr , err := sdk .ValAddressFromBech32 (delegation .ValidatorAddress )
102+ if err != nil {
103+ panic (err )
104+ }
105+
106+ delAddr , err := sdk .AccAddressFromBech32 (delegation .DelegatorAddress )
107+ if err != nil {
108+ panic (err )
109+ }
110+
111+ _ , err = app .DistrKeeper .WithdrawDelegationRewards (ctx , delAddr , valAddr )
89112 if err != nil {
90113 panic (err )
91114 }
92115 }
93116
94- // clear validator slash events
117+ // clear validator slash events.
95118 app .DistrKeeper .DeleteAllValidatorSlashEvents (ctx )
96119
97- // clear validator historical rewards
120+ // clear validator historical rewards.
98121 app .DistrKeeper .DeleteAllValidatorHistoricalRewards (ctx )
99122
100- // set context height to zero
123+ // set context height to zero.
101124 height := ctx .BlockHeight ()
102125 ctx = ctx .WithBlockHeight (0 )
103126
104- // reinitialize all validators
105- app .StakingKeeper .IterateValidators (ctx , func (_ int64 , val stakingtypes.ValidatorI ) (stop bool ) {
106- // donate any unwithdrawn outstanding reward fraction tokens to the community pool
107- scraps := app .DistrKeeper .GetValidatorOutstandingRewardsCoins (ctx , val .GetOperator ())
108- feePool := app .DistrKeeper .GetFeePool (ctx )
127+ // reinitialize all validators.
128+ err = app .StakingKeeper .IterateValidators (ctx , func (_ int64 , val stakingtypes.ValidatorI ) (stop bool ) {
129+ // donate any unwithdrawn outstanding reward fraction tokens to the community pool.
130+ valAddr , err := app .StakingKeeper .ValidatorAddressCodec ().StringToBytes (val .GetOperator ())
131+ if err != nil {
132+ panic (err )
133+ }
134+ scraps , err := app .DistrKeeper .GetValidatorOutstandingRewardsCoins (ctx , valAddr )
135+ if err != nil {
136+ panic (err )
137+ }
138+ feePool , err := app .DistrKeeper .FeePool .Get (ctx )
139+ if err != nil {
140+ panic (err )
141+ }
109142 feePool .CommunityPool = feePool .CommunityPool .Add (scraps ... )
110- app .DistrKeeper .SetFeePool (ctx , feePool )
111-
112- app .DistrKeeper .Hooks ().AfterValidatorCreated (ctx , val .GetOperator ())
143+ err = app .DistrKeeper .FeePool .Set (ctx , feePool )
144+ if err != nil {
145+ panic (err )
146+ }
147+ if err := app .DistrKeeper .Hooks ().AfterValidatorCreated (ctx , valAddr ); err != nil {
148+ panic (err )
149+ }
113150 return false
114151 })
152+ if err != nil {
153+ panic (err )
154+ }
115155
116- // reinitialize all delegations
156+ // reinitialize all delegations.
117157 for _ , del := range dels {
118- app .DistrKeeper .Hooks ().BeforeDelegationCreated (ctx , del .GetDelegatorAddr (), del .GetValidatorAddr ())
119- app .DistrKeeper .Hooks ().AfterDelegationModified (ctx , del .GetDelegatorAddr (), del .GetValidatorAddr ())
158+ valAddr , err := sdk .ValAddressFromBech32 (del .ValidatorAddress )
159+ if err != nil {
160+ panic (err )
161+ }
162+ delAddr , err := sdk .AccAddressFromBech32 (del .DelegatorAddress )
163+ if err != nil {
164+ panic (err )
165+ }
166+ if err := app .DistrKeeper .Hooks ().BeforeDelegationCreated (ctx , delAddr , valAddr ); err != nil {
167+ panic (err )
168+ }
169+ if err := app .DistrKeeper .Hooks ().AfterDelegationModified (ctx , delAddr , valAddr ); err != nil {
170+ panic (err )
171+ }
120172 }
121173
122- // reset context height
174+ // reset context height.
123175 ctx = ctx .WithBlockHeight (height )
124176
125177 /* Handle staking state. */
126178
127- // iterate through redelegations, reset creation height
128- app .StakingKeeper .IterateRedelegations (ctx , func (_ int64 , red stakingtypes.Redelegation ) (stop bool ) {
179+ // iterate through redelegations, reset creation height.
180+ err = app .StakingKeeper .IterateRedelegations (ctx , func (_ int64 , red stakingtypes.Redelegation ) (stop bool ) {
129181 for i := range red .Entries {
130182 red .Entries [i ].CreationHeight = 0
131183 }
132- app .StakingKeeper .SetRedelegation (ctx , red )
184+ if err := app .StakingKeeper .SetRedelegation (ctx , red ); err != nil {
185+ panic (err )
186+ }
133187 return false
134188 })
189+ if err != nil {
190+ panic (err )
191+ }
135192
136- // iterate through unbonding delegations, reset creation height
137- app .StakingKeeper .IterateUnbondingDelegations (ctx , func (_ int64 , ubd stakingtypes.UnbondingDelegation ) (stop bool ) {
193+ // iterate through unbonding delegations, reset creation height.
194+ err = app .StakingKeeper .IterateUnbondingDelegations (ctx , func (_ int64 , ubd stakingtypes.UnbondingDelegation ) (stop bool ) {
138195 for i := range ubd .Entries {
139196 ubd .Entries [i ].CreationHeight = 0
140197 }
141- app .StakingKeeper .SetUnbondingDelegation (ctx , ubd )
198+ if err := app .StakingKeeper .SetUnbondingDelegation (ctx , ubd ); err != nil {
199+ panic (err )
200+ }
142201 return false
143202 })
203+ if err != nil {
204+ panic (err )
205+ }
144206
145207 // Iterate through validators by power descending, reset bond heights, and
146208 // update bond intra-tx counters.
147- store := ctx .KVStore (app .keys [stakingtypes .StoreKey ])
148- iter := sdk .KVStoreReversePrefixIterator (store , stakingtypes .ValidatorsKey )
149- counter := int16 (0 )
209+ store := ctx .KVStore (app .GetKey (stakingtypes .StoreKey ))
210+ iter := storetypes .KVStoreReversePrefixIterator (store , stakingtypes .ValidatorsKey )
150211
151- for ; iter .Valid (); iter .Next () {
152- addr := sdk .ValAddress (iter .Key ()[1 :])
153- validator , found := app .StakingKeeper .GetValidator (ctx , addr )
154- if ! found {
155- panic ("expected validator, not found" )
156- }
212+ counter := int16 (0 )
157213
158- validator .UnbondingHeight = 0
159- if applyAllowedAddrs && ! allowedAddrsMap [addr .String ()] {
160- validator .Jailed = true
214+ // Closure to ensure iterator doesn't leak.
215+ func () {
216+ defer iter .Close ()
217+ for ; iter .Valid (); iter .Next () {
218+ addr := sdk .ValAddress (stakingtypes .AddressFromValidatorsKey (iter .Key ()))
219+ validator , err := app .StakingKeeper .GetValidator (ctx , addr )
220+ if err != nil {
221+ panic ("expected validator, not found" )
222+ }
223+
224+ validator .UnbondingHeight = 0
225+ if applyAllowedAddrs && ! allowedAddrsMap [addr .String ()] {
226+ validator .Jailed = true
227+ }
228+
229+ if err = app .StakingKeeper .SetValidator (ctx , validator ); err != nil {
230+ panic (err )
231+ }
232+
233+ counter ++
161234 }
235+ }()
162236
163- app .StakingKeeper .SetValidator (ctx , validator )
164- counter ++
165- }
166-
167- if err := iter .Close (); err != nil {
168- panic (err )
169- }
170-
171- if _ , err := app .StakingKeeper .ApplyAndReturnValidatorSetUpdates (ctx ); err != nil {
237+ _ , err = app .StakingKeeper .ApplyAndReturnValidatorSetUpdates (ctx )
238+ if err != nil {
172239 panic (err )
173240 }
174241
175242 /* Handle slashing state. */
176243
177- // reset start height on signing infos
178- app .SlashingKeeper .IterateValidatorSigningInfos (
244+ // reset start height on signing infos.
245+ err = app .SlashingKeeper .IterateValidatorSigningInfos (
179246 ctx ,
180247 func (addr sdk.ConsAddress , info slashingtypes.ValidatorSigningInfo ) (stop bool ) {
181248 info .StartHeight = 0
182- app .SlashingKeeper .SetValidatorSigningInfo (ctx , addr , info )
249+ if err = app .SlashingKeeper .SetValidatorSigningInfo (ctx , addr , info ); err != nil {
250+ panic (err )
251+ }
183252 return false
184253 },
185254 )
255+ if err != nil {
256+ panic (err )
257+ }
186258}
0 commit comments