forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genesis.go
206 lines (163 loc) · 6.4 KB
/
genesis.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
package keeper
import (
"fmt"
abci "github.com/tendermint/tendermint/abci/types"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/staking/types"
)
// InitGenesis sets the pool and parameters for the provided keeper. For each
// validator in data, it sets that validator in the keeper along with manually
// setting the indexes. In addition, it also sets any delegations found in
// data. Finally, it updates the bonded validators.
// Returns final validator set after applying all declaration and delegations
func (k Keeper) InitGenesis(ctx sdk.Context, data *types.GenesisState) (res []abci.ValidatorUpdate) {
bondedTokens := math.ZeroInt()
notBondedTokens := math.ZeroInt()
// We need to pretend to be "n blocks before genesis", where "n" is the
// validator update delay, so that e.g. slashing periods are correctly
// initialized for the validator set e.g. with a one-block offset - the
// first TM block is at height 1, so state updates applied from
// genesis.json are in block 0.
ctx = ctx.WithBlockHeight(1 - sdk.ValidatorUpdateDelay)
if err := k.SetParams(ctx, data.Params); err != nil {
panic(err)
}
k.SetLastTotalPower(ctx, data.LastTotalPower)
for _, validator := range data.Validators {
k.SetValidator(ctx, validator)
// Manually set indices for the first time
k.SetValidatorByConsAddr(ctx, validator)
k.SetValidatorByPowerIndex(ctx, validator)
// Call the creation hook if not exported
if !data.Exported {
if err := k.AfterValidatorCreated(ctx, validator.GetOperator()); err != nil {
panic(err)
}
}
// update timeslice if necessary
if validator.IsUnbonding() {
k.InsertUnbondingValidatorQueue(ctx, validator)
}
switch validator.GetStatus() {
case types.Bonded:
bondedTokens = bondedTokens.Add(validator.GetTokens())
case types.Unbonding, types.Unbonded:
notBondedTokens = notBondedTokens.Add(validator.GetTokens())
default:
panic("invalid validator status")
}
}
for _, delegation := range data.Delegations {
delegatorAddress := sdk.MustAccAddressFromBech32(delegation.DelegatorAddress)
// Call the before-creation hook if not exported
if !data.Exported {
if err := k.BeforeDelegationCreated(ctx, delegatorAddress, delegation.GetValidatorAddr()); err != nil {
panic(err)
}
}
k.SetDelegation(ctx, delegation)
// Call the after-modification hook if not exported
if !data.Exported {
if err := k.AfterDelegationModified(ctx, delegatorAddress, delegation.GetValidatorAddr()); err != nil {
panic(err)
}
}
}
for _, ubd := range data.UnbondingDelegations {
k.SetUnbondingDelegation(ctx, ubd)
for _, entry := range ubd.Entries {
k.InsertUBDQueue(ctx, ubd, entry.CompletionTime)
notBondedTokens = notBondedTokens.Add(entry.Balance)
}
}
for _, red := range data.Redelegations {
k.SetRedelegation(ctx, red)
for _, entry := range red.Entries {
k.InsertRedelegationQueue(ctx, red, entry.CompletionTime)
}
}
bondedCoins := sdk.NewCoins(sdk.NewCoin(data.Params.BondDenom, bondedTokens))
notBondedCoins := sdk.NewCoins(sdk.NewCoin(data.Params.BondDenom, notBondedTokens))
// check if the unbonded and bonded pools accounts exists
bondedPool := k.GetBondedPool(ctx)
if bondedPool == nil {
panic(fmt.Sprintf("%s module account has not been set", types.BondedPoolName))
}
// TODO: remove with genesis 2-phases refactor https://github.com/cosmos/cosmos-sdk/issues/2862
bondedBalance := k.bankKeeper.GetAllBalances(ctx, bondedPool.GetAddress())
if bondedBalance.IsZero() {
k.authKeeper.SetModuleAccount(ctx, bondedPool)
}
// if balance is different from bonded coins panic because genesis is most likely malformed
if !bondedBalance.IsEqual(bondedCoins) {
panic(fmt.Sprintf("bonded pool balance is different from bonded coins: %s <-> %s", bondedBalance, bondedCoins))
}
notBondedPool := k.GetNotBondedPool(ctx)
if notBondedPool == nil {
panic(fmt.Sprintf("%s module account has not been set", types.NotBondedPoolName))
}
notBondedBalance := k.bankKeeper.GetAllBalances(ctx, notBondedPool.GetAddress())
if notBondedBalance.IsZero() {
k.authKeeper.SetModuleAccount(ctx, notBondedPool)
}
// If balance is different from non bonded coins panic because genesis is most
// likely malformed.
if !notBondedBalance.IsEqual(notBondedCoins) {
panic(fmt.Sprintf("not bonded pool balance is different from not bonded coins: %s <-> %s", notBondedBalance, notBondedCoins))
}
// don't need to run Tendermint updates if we exported
if data.Exported {
for _, lv := range data.LastValidatorPowers {
valAddr, err := sdk.ValAddressFromBech32(lv.Address)
if err != nil {
panic(err)
}
k.SetLastValidatorPower(ctx, valAddr, lv.Power)
validator, found := k.GetValidator(ctx, valAddr)
if !found {
panic(fmt.Sprintf("validator %s not found", lv.Address))
}
update := validator.ABCIValidatorUpdate(k.PowerReduction(ctx))
update.Power = lv.Power // keep the next-val-set offset, use the last power for the first block
res = append(res, update)
}
} else {
var err error
res, err = k.ApplyAndReturnValidatorSetUpdates(ctx)
if err != nil {
panic(err)
}
}
return res
}
// ExportGenesis returns a GenesisState for a given context and keeper. The
// GenesisState will contain the pool, params, validators, and bonds found in
// the keeper.
func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
var unbondingDelegations []types.UnbondingDelegation
k.IterateUnbondingDelegations(ctx, func(_ int64, ubd types.UnbondingDelegation) (stop bool) {
unbondingDelegations = append(unbondingDelegations, ubd)
return false
})
var redelegations []types.Redelegation
k.IterateRedelegations(ctx, func(_ int64, red types.Redelegation) (stop bool) {
redelegations = append(redelegations, red)
return false
})
var lastValidatorPowers []types.LastValidatorPower
k.IterateLastValidatorPowers(ctx, func(addr sdk.ValAddress, power int64) (stop bool) {
lastValidatorPowers = append(lastValidatorPowers, types.LastValidatorPower{Address: addr.String(), Power: power})
return false
})
return &types.GenesisState{
Params: k.GetParams(ctx),
LastTotalPower: k.GetLastTotalPower(ctx),
LastValidatorPowers: lastValidatorPowers,
Validators: k.GetAllValidators(ctx),
Delegations: k.GetAllDelegations(ctx),
UnbondingDelegations: unbondingDelegations,
Redelegations: redelegations,
Exported: true,
}
}