/
export.go
221 lines (177 loc) · 7.21 KB
/
export.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package app
import (
"fmt"
stdlog "log"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/codec"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
sdk "github.com/cosmos/cosmos-sdk/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)
func (app *Application) ExportAppStateAndValidators(forZeroHeight bool, jailWhiteList []string, modulesToExport []string) (servertypes.ExportedApp, error) {
context := app.NewContext(true, tmproto.Header{Height: app.LastBlockHeight()})
height := app.LastBlockHeight() + 1
if forZeroHeight {
height = 0
app.prepForZeroHeightGenesis(context, jailWhiteList)
}
genesisState := app.moduleManager.ExportGenesisForModules(context, app.appCodec, modulesToExport)
applicationState, Error := codec.MarshalJSONIndent(app.legacyAmino, genesisState)
if Error != nil {
return servertypes.ExportedApp{}, Error
}
validators, err := staking.WriteValidators(context, app.StakingKeeper)
return servertypes.ExportedApp{
AppState: applicationState,
Validators: validators,
Height: height,
ConsensusParams: app.BaseApp.GetConsensusParams(context),
}, err
}
func (app *Application) prepForZeroHeightGenesis(context sdk.Context, jailWhiteList []string) {
/* Just to be safe, assert the invariants on current state. */
app.CrisisKeeper.AssertInvariants(context)
/* Handle fee distribution state. */
// withdraw all validator commission
app.StakingKeeper.IterateValidators(context, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
_, _ = app.DistributionKeeper.WithdrawValidatorCommission(context, val.GetOperator())
return false
})
delegations := app.StakingKeeper.GetAllDelegations(context)
app.withdrawDelegationRewards(context, delegations)
app.DistributionKeeper.DeleteAllValidatorSlashEvents(context)
app.DistributionKeeper.DeleteAllValidatorHistoricalRewards(context)
// set context height to zero
height := context.BlockHeight()
context = context.WithBlockHeight(0)
app.reinitializeValidators(context)
app.reinitializeDelegations(context, delegations)
// reset context height
context = context.WithBlockHeight(height)
/* Handle staking state. */
// reset creation height for redelegations & unbonding delegations
app.resetCreationHeightForDelEntries(context)
if err := app.resetValidatorBondHeights(context, jailWhiteList); err != nil {
app.Logger().Error(err.Error())
return
}
/* Handle slashing state. */
// reset start height on signing infos
app.SlashingKeeper.IterateValidatorSigningInfos(
context,
func(validatorConsAddress sdk.ConsAddress, validatorSigningInfo slashingtypes.ValidatorSigningInfo) (stop bool) {
validatorSigningInfo.StartHeight = 0
app.SlashingKeeper.SetValidatorSigningInfo(context, validatorConsAddress, validatorSigningInfo)
return false
},
)
}
func (app *Application) withdrawDelegationRewards(context sdk.Context, delegations []stakingtypes.Delegation) {
for _, delegation := range delegations {
validatorAddress, err := sdk.ValAddressFromBech32(delegation.ValidatorAddress)
if err != nil {
panic(err)
}
delegatorAddress, err := sdk.AccAddressFromBech32(delegation.DelegatorAddress)
if err != nil {
panic(err)
}
_, err = app.DistributionKeeper.WithdrawDelegationRewards(context, delegatorAddress, validatorAddress)
if err != nil {
panic(err)
}
}
}
func (app *Application) reinitializeValidators(context sdk.Context) {
app.StakingKeeper.IterateValidators(context, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
scraps := app.DistributionKeeper.GetValidatorOutstandingRewardsCoins(context, val.GetOperator())
feePool := app.DistributionKeeper.GetFeePool(context)
feePool.CommunityPool = feePool.CommunityPool.Add(scraps...)
app.DistributionKeeper.SetFeePool(context, feePool)
if err := app.DistributionKeeper.Hooks().AfterValidatorCreated(context, val.GetOperator()); err != nil {
// never called as AfterValidatorCreated always returns nil
panic(err)
}
return false
})
}
func (app *Application) reinitializeDelegations(context sdk.Context, delegations []stakingtypes.Delegation) {
for _, delegation := range delegations {
validatorAddress, err := sdk.ValAddressFromBech32(delegation.ValidatorAddress)
if err != nil {
panic(err)
}
delegatorAddress, err := sdk.AccAddressFromBech32(delegation.DelegatorAddress)
if err != nil {
panic(err)
}
if err := app.DistributionKeeper.Hooks().BeforeDelegationCreated(context, delegatorAddress, validatorAddress); err != nil {
// never called as BeforeDelegationCreated always returns nil
panic(fmt.Errorf("error while incrementing period: %w", err))
}
if err := app.DistributionKeeper.Hooks().AfterDelegationModified(context, delegatorAddress, validatorAddress); err != nil {
// never called as AfterDelegationModified always returns nil
panic(fmt.Errorf("error while creating a new delegation period record: %w", err))
}
}
}
func (app *Application) resetCreationHeightForDelEntries(context sdk.Context) {
// iterate through redelegations, reset creation height
app.StakingKeeper.IterateRedelegations(context, func(_ int64, redelegation stakingtypes.Redelegation) (stop bool) {
for i := range redelegation.Entries {
redelegation.Entries[i].CreationHeight = 0
}
app.StakingKeeper.SetRedelegation(context, redelegation)
return false
})
// iterate through unbonding delegations, reset creation height
app.StakingKeeper.IterateUnbondingDelegations(context, func(_ int64, unbondingDelegation stakingtypes.UnbondingDelegation) (stop bool) {
for i := range unbondingDelegation.Entries {
unbondingDelegation.Entries[i].CreationHeight = 0
}
app.StakingKeeper.SetUnbondingDelegation(context, unbondingDelegation)
return false
})
}
func (app *Application) resetValidatorBondHeights(context sdk.Context, jailWhiteList []string) error {
applyWhiteList := len(jailWhiteList) > 0
whiteListMap := getWhilteListMap(jailWhiteList)
// Iterate through validators by power descending, reset bond heights, and
// update bond intra-tx counters.
store := context.KVStore(app.GetKVStoreKey()[stakingtypes.StoreKey])
iter := sdk.KVStoreReversePrefixIterator(store, stakingtypes.ValidatorsKey)
counter := int16(0)
for ; iter.Valid(); iter.Next() {
addr := sdk.ValAddress(stakingtypes.AddressFromValidatorsKey(iter.Key()))
validator, found := app.StakingKeeper.GetValidator(context, addr)
if !found {
panic("Validator not found!")
}
validator.UnbondingHeight = 0
if applyWhiteList && !whiteListMap[addr.String()] {
validator.Jailed = true
}
app.StakingKeeper.SetValidator(context, validator)
counter++
}
if err := iter.Close(); err != nil {
return fmt.Errorf("error while closing the key-value store reverse prefix iterator: %w", err)
}
_, err := app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(context)
if err != nil {
stdlog.Fatal(err)
}
return nil
}
func getWhilteListMap(jailWhiteList []string) map[string]bool {
whiteListMap := make(map[string]bool)
for _, address := range jailWhiteList {
if _, err := sdk.ValAddressFromBech32(address); err != nil {
panic(err)
}
whiteListMap[address] = true
}
return whiteListMap
}