-
Notifications
You must be signed in to change notification settings - Fork 179
/
export.go
180 lines (148 loc) · 5.56 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
package app
import (
"encoding/json"
"log"
abci "github.com/tendermint/tendermint/abci/types"
tmtypes "github.com/tendermint/tendermint/types"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/okex/exchain/x/slashing"
"github.com/okex/exchain/x/staking"
"github.com/okex/exchain/x/staking/exported"
ethcdc "github.com/okex/exchain/app/codec"
)
// NewDefaultGenesisState generates the default state for the application.
func NewDefaultGenesisState() simapp.GenesisState {
_ = ethcdc.MakeCodec(ModuleBasics)
return ModuleBasics.DefaultGenesis()
}
// ExportAppStateAndValidators exports the state of the application for a genesis
// file.
func (app *OKExChainApp) ExportAppStateAndValidators(
forZeroHeight bool, jailWhiteList []string,
) (appState json.RawMessage, validators []tmtypes.GenesisValidator, err error) {
// Creates context with current height and checks txs for ctx to be usable by start of next block
ctx := app.NewContext(true, abci.Header{Height: app.LastBlockHeight()})
if forZeroHeight {
app.prepForZeroHeightGenesis(ctx, jailWhiteList)
}
// Export genesis to be used by SDK modules
genState := app.mm.ExportGenesis(ctx)
appState, err = codec.MarshalJSONIndent(app.cdc, genState)
if err != nil {
return nil, nil, err
}
// Write validators to staking module to be used by TM node
//todo need resolve
// validators = staking.WriteValidators(ctx, app.StakingKeeper)
return appState, validators, nil
}
// prepare for fresh start at zero height
// NOTE zero height genesis is a temporary feature which will be deprecated
// in favour of export at a block height
func (app *OKExChainApp) prepForZeroHeightGenesis(ctx sdk.Context, jailWhiteList []string) {
applyWhiteList := false
//Check if there is a whitelist
if len(jailWhiteList) > 0 {
applyWhiteList = true
}
whiteListMap := make(map[string]bool)
for _, addr := range jailWhiteList {
_, err := sdk.ValAddressFromBech32(addr)
if err != nil {
log.Fatal(err)
}
whiteListMap[addr] = true
}
/* Just to be safe, assert the invariants on current state. */
app.CrisisKeeper.AssertInvariants(ctx)
/* Handle fee distribution state. */
// withdraw all validator commission
app.StakingKeeper.IterateValidators(ctx, func(_ int64, val exported.ValidatorI) (stop bool) {
_, _ = app.DistrKeeper.WithdrawValidatorCommission(ctx, val.GetOperator())
return false
})
//todo need resolved later
// withdraw all delegator rewards
//dels := app.StakingKeeper.GetAllDelegations(ctx)
//for _, delegation := range dels {
// _, _ = app.DistrKeeper.WithdrawDelegationRewards(ctx, delegation.DelegatorAddress, delegation.ValidatorAddress)
//}
// clear validator slash events
//app.DistrKeeper.DeleteAllValidatorSlashEvents(ctx)
// clear validator historical rewards
//app.DistrKeeper.DeleteAllValidatorHistoricalRewards(ctx)
// set context height to zero
//height := ctx.BlockHeight()
//ctx = ctx.WithBlockHeight(0)
//
//// reinitialize all validators
//app.StakingKeeper.IterateValidators(ctx, func(_ int64, val exported.ValidatorI) (stop bool) {
// // donate any unwithdrawn outstanding reward fraction tokens to the community pool
// scraps := app.DistrKeeper.GetValidatorOutstandingRewardsCoins(ctx, val.GetOperator())
// feePool := app.DistrKeeper.GetFeePool(ctx)
// feePool.CommunityPool = feePool.CommunityPool.Add(scraps...)
// app.DistrKeeper.SetFeePool(ctx, feePool)
//
// app.DistrKeeper.Hooks().AfterValidatorCreated(ctx, val.GetOperator())
// return false
//})
//todo need resolve
// reinitialize all delegations
//for _, del := range dels {
// app.DistrKeeper.Hooks().BeforeDelegationCreated(ctx, del.DelegatorAddress, del.ValidatorAddress)
// app.DistrKeeper.Hooks().AfterDelegationModified(ctx, del.DelegatorAddress, del.ValidatorAddress)
//}
// reset context height
//ctx = ctx.WithBlockHeight(height)
/* Handle staking state. */
//todo need resolve
//// iterate through redelegations, reset creation height
//app.StakingKeeper.IterateRedelegations(ctx, func(_ int64, red staking.Redelegation) (stop bool) {
// for i := range red.Entries {
// red.Entries[i].CreationHeight = 0
// }
// app.StakingKeeper.SetRedelegation(ctx, red)
// return false
//})
//
//// iterate through unbonding delegations, reset creation height
//app.StakingKeeper.IterateUnbondingDelegations(ctx, func(_ int64, ubd staking.UnbondingDelegation) (stop bool) {
// for i := range ubd.Entries {
// ubd.Entries[i].CreationHeight = 0
// }
// app.StakingKeeper.SetUnbondingDelegation(ctx, ubd)
// return false
//})
// Iterate through validators by power descending, reset bond heights, and
// update bond intra-tx counters.
store := ctx.KVStore(app.keys[staking.StoreKey])
iter := sdk.KVStoreReversePrefixIterator(store, staking.ValidatorsKey)
counter := int16(0)
for ; iter.Valid(); iter.Next() {
addr := sdk.ValAddress(iter.Key()[1:])
validator, found := app.StakingKeeper.GetValidator(ctx, addr)
if !found {
panic("expected validator, not found")
}
validator.UnbondingHeight = 0
if applyWhiteList && !whiteListMap[addr.String()] {
validator.Jailed = true
}
app.StakingKeeper.SetValidator(ctx, validator)
counter++
}
iter.Close()
_ = app.StakingKeeper.ApplyAndReturnValidatorSetUpdates(ctx)
/* Handle slashing state. */
// reset start height on signing infos
app.SlashingKeeper.IterateValidatorSigningInfos(
ctx,
func(addr sdk.ConsAddress, info slashing.ValidatorSigningInfo) (stop bool) {
info.StartHeight = 0
app.SlashingKeeper.SetValidatorSigningInfo(ctx, addr, info)
return false
},
)
}