@@ -2,6 +2,9 @@ package app
2
2
3
3
import (
4
4
"encoding/json"
5
+ "fmt"
6
+
7
+ cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
5
8
6
9
storetypes "cosmossdk.io/store/types"
7
10
servertypes "github.com/cosmos/cosmos-sdk/server/types"
@@ -14,7 +17,7 @@ import (
14
17
func (app * SecretNetworkApp ) ExportAppStateAndValidators (forZeroHeight bool , jailAllowedAddrs []string , modulesToExport []string ,
15
18
) (servertypes.ExportedApp , error ) {
16
19
// as if they could withdraw from the start of the next block
17
- ctx := app .BaseApp . NewContext (true )
20
+ ctx := app .NewContextLegacy (true , cmtproto. Header { Height : app . LastBlockHeight ()} )
18
21
19
22
// We export at last height + 1, because that's the height at which
20
23
// Tendermint will start InitChain.
@@ -44,7 +47,7 @@ func (app *SecretNetworkApp) ExportAppStateAndValidators(forZeroHeight bool, jai
44
47
Validators : validators ,
45
48
Height : height ,
46
49
ConsensusParams : app .BaseApp .GetConsensusParams (ctx ),
47
- }, nil
50
+ }, err
48
51
}
49
52
50
53
// prepare for fresh start at zero height
@@ -75,24 +78,31 @@ func (app *SecretNetworkApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllow
75
78
/* Handle fee distribution state. */
76
79
77
80
// withdraw all validator commission
78
- app .AppKeepers .StakingKeeper .IterateValidators (ctx , func (_ int64 , val stakingtypes.ValidatorI ) (stop bool ) {
79
- valAddr , _ := sdk .ValAddressFromBech32 (val .GetOperator ())
81
+ err := app .AppKeepers .StakingKeeper .IterateValidators (ctx , func (_ int64 , val stakingtypes.ValidatorI ) (stop bool ) {
82
+ valAddr , err := app .AppKeepers .StakingKeeper .ValidatorAddressCodec ().StringToBytes (val .GetOperator ())
83
+ if err != nil {
84
+ panic (err )
85
+ }
80
86
_ , _ = app .AppKeepers .DistrKeeper .WithdrawValidatorCommission (ctx , valAddr )
81
87
return false
82
88
})
89
+ if err != nil {
90
+ panic (err )
91
+ }
83
92
84
93
// withdraw all delegator rewards
85
- dels , _ := app .AppKeepers .StakingKeeper .GetAllDelegations (ctx )
94
+ dels , err := app .AppKeepers .StakingKeeper .GetAllDelegations (ctx )
95
+ if err != nil {
96
+ panic (err )
97
+ }
98
+
86
99
for _ , delegation := range dels {
87
100
valAddr , err := sdk .ValAddressFromBech32 (delegation .ValidatorAddress )
88
101
if err != nil {
89
102
panic (err )
90
103
}
91
104
92
- delAddr , err := sdk .AccAddressFromBech32 (delegation .DelegatorAddress )
93
- if err != nil {
94
- panic (err )
95
- }
105
+ delAddr := sdk .MustAccAddressFromBech32 (delegation .DelegatorAddress )
96
106
97
107
_ , _ = app .AppKeepers .DistrKeeper .WithdrawDelegationRewards (ctx , delAddr , valAddr )
98
108
}
@@ -108,15 +118,28 @@ func (app *SecretNetworkApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllow
108
118
ctx = ctx .WithBlockHeight (0 )
109
119
110
120
// reinitialize all validators
111
- app .AppKeepers .StakingKeeper .IterateValidators (ctx , func (_ int64 , val stakingtypes.ValidatorI ) (stop bool ) {
112
- valAddr , _ := sdk .ValAddressFromBech32 (val .GetOperator ())
121
+ err = app .AppKeepers .StakingKeeper .IterateValidators (ctx , func (_ int64 , val stakingtypes.ValidatorI ) (stop bool ) {
122
+ valAddr , err := app .AppKeepers .StakingKeeper .ValidatorAddressCodec ().StringToBytes (val .GetOperator ())
123
+ if err != nil {
124
+ panic (err )
125
+ }
113
126
// donate any unwithdrawn outstanding reward fraction tokens to the community pool
114
- scraps , _ := app .AppKeepers .DistrKeeper .GetValidatorOutstandingRewardsCoins (ctx , valAddr )
115
- feePool , _ := app .AppKeepers .DistrKeeper .FeePool .Get (ctx )
127
+ scraps , err := app .AppKeepers .DistrKeeper .GetValidatorOutstandingRewardsCoins (ctx , valAddr )
128
+ if err != nil {
129
+ panic (err )
130
+ }
131
+ feePool , err := app .AppKeepers .DistrKeeper .FeePool .Get (ctx )
132
+ if err != nil {
133
+ panic (err )
134
+ }
116
135
feePool .CommunityPool = feePool .CommunityPool .Add (scraps ... )
117
- app .AppKeepers .DistrKeeper .FeePool .Set (ctx , feePool )
136
+ if err := app .AppKeepers .DistrKeeper .FeePool .Set (ctx , feePool ); err != nil {
137
+ panic (err )
138
+ }
118
139
119
- app .AppKeepers .DistrKeeper .Hooks ().AfterValidatorCreated (ctx , valAddr )
140
+ if err := app .AppKeepers .DistrKeeper .Hooks ().AfterValidatorCreated (ctx , valAddr ); err != nil {
141
+ panic (err )
142
+ }
120
143
return false
121
144
})
122
145
@@ -126,14 +149,17 @@ func (app *SecretNetworkApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllow
126
149
if err != nil {
127
150
panic (err )
128
151
}
152
+ delAddr := sdk .MustAccAddressFromBech32 (del .DelegatorAddress )
129
153
130
- delAddr , err := sdk . AccAddressFromBech32 ( del . DelegatorAddress )
131
- if err != nil {
132
- panic (err )
154
+ if err := app . AppKeepers . DistrKeeper . Hooks (). BeforeDelegationCreated ( ctx , delAddr , valAddr ); err != nil {
155
+ // never called as BeforeDelegationCreated always returns nil
156
+ panic (fmt . Errorf ( "error while incrementing period: %w" , err ) )
133
157
}
134
158
135
- app .AppKeepers .DistrKeeper .Hooks ().BeforeDelegationCreated (ctx , delAddr , valAddr )
136
- app .AppKeepers .DistrKeeper .Hooks ().AfterDelegationModified (ctx , delAddr , valAddr )
159
+ if err := app .AppKeepers .DistrKeeper .Hooks ().AfterDelegationModified (ctx , delAddr , valAddr ); err != nil {
160
+ // never called as AfterDelegationModified always returns nil
161
+ panic (fmt .Errorf ("error while creating a new delegation period record: %w" , err ))
162
+ }
137
163
}
138
164
139
165
// reset context height
@@ -142,22 +168,34 @@ func (app *SecretNetworkApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllow
142
168
/* Handle staking state. */
143
169
144
170
// iterate through redelegations, reset creation height
145
- app .AppKeepers .StakingKeeper .IterateRedelegations (ctx , func (_ int64 , red stakingtypes.Redelegation ) (stop bool ) {
171
+ err = app .AppKeepers .StakingKeeper .IterateRedelegations (ctx , func (_ int64 , red stakingtypes.Redelegation ) (stop bool ) {
146
172
for i := range red .Entries {
147
173
red .Entries [i ].CreationHeight = 0
148
174
}
149
- app .AppKeepers .StakingKeeper .SetRedelegation (ctx , red )
175
+ err = app .AppKeepers .StakingKeeper .SetRedelegation (ctx , red )
176
+ if err != nil {
177
+ panic (err )
178
+ }
150
179
return false
151
180
})
181
+ if err != nil {
182
+ panic (err )
183
+ }
152
184
153
185
// iterate through unbonding delegations, reset creation height
154
- app .AppKeepers .StakingKeeper .IterateUnbondingDelegations (ctx , func (_ int64 , ubd stakingtypes.UnbondingDelegation ) (stop bool ) {
186
+ err = app .AppKeepers .StakingKeeper .IterateUnbondingDelegations (ctx , func (_ int64 , ubd stakingtypes.UnbondingDelegation ) (stop bool ) {
155
187
for i := range ubd .Entries {
156
188
ubd .Entries [i ].CreationHeight = 0
157
189
}
158
- app .AppKeepers .StakingKeeper .SetUnbondingDelegation (ctx , ubd )
190
+ err = app .AppKeepers .StakingKeeper .SetUnbondingDelegation (ctx , ubd )
191
+ if err != nil {
192
+ panic (err )
193
+ }
159
194
return false
160
195
})
196
+ if err != nil {
197
+ panic (err )
198
+ }
161
199
162
200
// Iterate through validators by power descending, reset bond heights, and
163
201
// update bond intra-tx counters.
@@ -166,7 +204,7 @@ func (app *SecretNetworkApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllow
166
204
counter := int16 (0 )
167
205
168
206
for ; iter .Valid (); iter .Next () {
169
- addr := sdk .ValAddress (iter .Key ()[ 1 :] )
207
+ addr := sdk .ValAddress (stakingtypes . AddressFromValidatorsKey ( iter .Key ()) )
170
208
validator , err := app .AppKeepers .StakingKeeper .GetValidator (ctx , addr )
171
209
if err != nil {
172
210
panic ("expected validator, not found" )
@@ -175,27 +213,49 @@ func (app *SecretNetworkApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllow
175
213
validator .UnbondingHeight = 0
176
214
if applyAllowedAddrs && ! allowedAddrsMap [addr .String ()] {
177
215
validator .Jailed = true
178
- app .AppKeepers .StakingKeeper .SetValidator (ctx , validator )
179
- app .AppKeepers .StakingKeeper .DeleteValidatorByPowerIndex (ctx , validator )
216
+ err = app .AppKeepers .StakingKeeper .SetValidator (ctx , validator )
217
+ if err != nil {
218
+ panic (err )
219
+ }
220
+ err = app .AppKeepers .StakingKeeper .DeleteValidatorByPowerIndex (ctx , validator )
221
+ if err != nil {
222
+ panic (err )
223
+ }
180
224
} else {
181
- app .AppKeepers .StakingKeeper .SetValidator (ctx , validator )
225
+ err = app .AppKeepers .StakingKeeper .SetValidator (ctx , validator )
226
+ if err != nil {
227
+ panic (err )
228
+ }
182
229
}
183
230
184
231
counter ++
185
232
}
186
233
187
- iter .Close ()
234
+ if err := iter .Close (); err != nil {
235
+ app .Logger ().Error ("error while closing the key-value store reverse prefix iterator: " , err )
236
+ return
237
+ }
238
+
239
+ _ , err = app .AppKeepers .StakingKeeper .ApplyAndReturnValidatorSetUpdates (ctx )
240
+ if err != nil {
241
+ panic (err )
242
+ }
188
243
189
- _ , _ = app .AppKeepers .StakingKeeper .ApplyAndReturnValidatorSetUpdates (ctx )
190
244
/* Handle slashing state. */
191
245
192
246
// reset start height on signing infos
193
- app .AppKeepers .SlashingKeeper .IterateValidatorSigningInfos (
247
+ err = app .AppKeepers .SlashingKeeper .IterateValidatorSigningInfos (
194
248
ctx ,
195
249
func (addr sdk.ConsAddress , info slashingtypes.ValidatorSigningInfo ) (stop bool ) {
196
250
info .StartHeight = 0
197
- app .AppKeepers .SlashingKeeper .SetValidatorSigningInfo (ctx , addr , info )
251
+ err = app .AppKeepers .SlashingKeeper .SetValidatorSigningInfo (ctx , addr , info )
252
+ if err != nil {
253
+ panic (err )
254
+ }
198
255
return false
199
256
},
200
257
)
258
+ if err != nil {
259
+ panic (err )
260
+ }
201
261
}
0 commit comments