-
Notifications
You must be signed in to change notification settings - Fork 250
/
handle_genesis.go
274 lines (228 loc) · 8.92 KB
/
handle_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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package staking
import (
"encoding/json"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx"
"github.com/forbole/bdjuno/database"
"github.com/forbole/bdjuno/modules/staking/utils"
"github.com/forbole/bdjuno/types"
tmtypes "github.com/tendermint/tendermint/types"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/rs/zerolog/log"
)
func HandleGenesis(
doc *tmtypes.GenesisDoc, appState map[string]json.RawMessage, cdc codec.Marshaler, db *database.Db,
) error {
log.Debug().Str("module", "staking").Msg("parsing genesis")
// Read the genesis state
var genState stakingtypes.GenesisState
err := cdc.UnmarshalJSON(appState[stakingtypes.ModuleName], &genState)
if err != nil {
return fmt.Errorf("error while unmarshaling staking state: %s", err)
}
// Save the params
err = saveParams(doc.InitialHeight, genState.Params, db)
if err != nil {
return fmt.Errorf("error while storing staking genesis params: %s", err)
}
// Parse genesis transactions
err = parseGenesisTransactions(doc, appState, cdc, db)
if err != nil {
return fmt.Errorf("error while storing genesis transactions: %s", err)
}
// Save the validators
err = saveValidators(doc, genState.Validators, cdc, db)
if err != nil {
return fmt.Errorf("error while storing staking genesis validators: %s", err)
}
// Save the delegations
err = saveDelegations(doc, genState, db)
if err != nil {
return fmt.Errorf("error while storing staking genesis delegations: %s", err)
}
// Save the unbonding delegations
err = saveUnbondingDelegations(doc, genState, db)
if err != nil {
return fmt.Errorf("error while storing staking genesis unbonding delegations: %s", err)
}
// Save the re-delegations
err = saveRedelegations(doc, genState, db)
if err != nil {
return fmt.Errorf("error while storing staking genesis redelegations: %s", err)
}
// Save the description
err = saveValidatorDescription(doc, genState.Validators, db)
if err != nil {
return fmt.Errorf("error while storing staking genesis validator descriptions: %s", err)
}
err = saveValidatorsCommissions(doc.InitialHeight, genState.Validators, db)
if err != nil {
return fmt.Errorf("error while storing staking genesis validators commissions: %s", err)
}
return nil
}
func parseGenesisTransactions(
doc *tmtypes.GenesisDoc, appState map[string]json.RawMessage, cdc codec.Marshaler, db *database.Db,
) error {
var genUtilState genutiltypes.GenesisState
err := cdc.UnmarshalJSON(appState[genutiltypes.ModuleName], &genUtilState)
if err != nil {
return fmt.Errorf("error while unmarhsaling genutil state: %s", err)
}
for _, genTxBz := range genUtilState.GetGenTxs() {
// Unmarshal the transaction
var genTx tx.Tx
err = cdc.UnmarshalJSON(genTxBz, &genTx)
if err != nil {
return fmt.Errorf("error while unmashasling genesis tx: %s", err)
}
for _, msg := range genTx.GetMsgs() {
// Handle the message properly
createValMsg, ok := msg.(*stakingtypes.MsgCreateValidator)
if !ok {
continue
}
err = utils.StoreValidatorFromMsgCreateValidator(doc.InitialHeight, createValMsg, cdc, db)
if err != nil {
return fmt.Errorf("error while storing validators from MsgCreateValidator: %s", err)
}
}
}
return nil
}
// -------------------------------------------------------------------------------------------------------------------
// saveParams saves the given params into the database
func saveParams(height int64, params stakingtypes.Params, db *database.Db) error {
return db.SaveStakingParams(types.NewStakingParams(params, height))
}
// --------------------------------------------------------------------------------------------------------------------
// saveValidators stores the validators data present inside the given genesis state
func saveValidators(
doc *tmtypes.GenesisDoc, validators stakingtypes.Validators, cdc codec.Marshaler, db *database.Db,
) error {
vals := make([]types.Validator, len(validators))
for i, val := range validators {
validator, err := utils.ConvertValidator(cdc, val, doc.InitialHeight)
if err != nil {
return err
}
vals[i] = validator
}
return db.SaveValidatorsData(vals)
}
// saveValidatorDescription saves the description for the given validators
func saveValidatorDescription(doc *tmtypes.GenesisDoc, validators stakingtypes.Validators, db *database.Db) error {
for _, account := range validators {
description, err := utils.ConvertValidatorDescription(
account.OperatorAddress,
account.Description,
doc.InitialHeight,
)
if err != nil {
return fmt.Errorf("error while converting validator description: %s", err)
}
err = db.SaveValidatorDescription(description)
if err != nil {
return err
}
}
return nil
}
// --------------------------------------------------------------------------------------------------------------------
// saveDelegations stores the delegations data present inside the given genesis state
func saveDelegations(doc *tmtypes.GenesisDoc, genState stakingtypes.GenesisState, db *database.Db) error {
var delegations []types.Delegation
for _, validator := range genState.Validators {
tokens := validator.Tokens
delegatorShares := validator.DelegatorShares
for _, delegation := range findDelegations(genState.Delegations, validator.OperatorAddress) {
delegationAmount := tokens.ToDec().Mul(delegation.Shares).Quo(delegatorShares).TruncateInt()
delegations = append(delegations, types.NewDelegation(
delegation.DelegatorAddress,
validator.OperatorAddress,
sdk.NewCoin(genState.Params.BondDenom, delegationAmount),
doc.InitialHeight,
))
}
}
return db.SaveDelegations(delegations)
}
// findDelegations returns the list of all the delegations that are
// related to the validator having the given validator address
func findDelegations(genData stakingtypes.Delegations, valAddr string) stakingtypes.Delegations {
var delegations stakingtypes.Delegations
for _, delegation := range genData {
if delegation.ValidatorAddress == valAddr {
delegations = append(delegations, delegation)
}
}
return delegations
}
// --------------------------------------------------------------------------------------------------------------------
// saveUnbondingDelegations stores the unbonding delegations data present inside the given genesis state
func saveUnbondingDelegations(doc *tmtypes.GenesisDoc, genState stakingtypes.GenesisState, db *database.Db) error {
var unbondingDelegations []types.UnbondingDelegation
for _, validator := range genState.Validators {
valUD := findUnbondingDelegations(genState.UnbondingDelegations, validator.OperatorAddress)
for _, ud := range valUD {
for _, entry := range ud.Entries {
unbondingDelegations = append(unbondingDelegations, types.NewUnbondingDelegation(
ud.DelegatorAddress,
validator.OperatorAddress,
sdk.NewCoin(genState.Params.BondDenom, entry.InitialBalance),
entry.CompletionTime,
doc.InitialHeight,
))
}
}
}
return db.SaveUnbondingDelegations(unbondingDelegations)
}
// findUnbondingDelegations returns the list of all the unbonding delegations
// that are related to the validator having the given validator address
func findUnbondingDelegations(genData stakingtypes.UnbondingDelegations, valAddr string) stakingtypes.UnbondingDelegations {
var unbondingDelegations stakingtypes.UnbondingDelegations
for _, unbondingDelegation := range genData {
if unbondingDelegation.ValidatorAddress == valAddr {
unbondingDelegations = append(unbondingDelegations, unbondingDelegation)
}
}
return unbondingDelegations
}
// --------------------------------------------------------------------------------------------------------------------
// saveRedelegations stores the redelegations data present inside the given genesis state
func saveRedelegations(doc *tmtypes.GenesisDoc, genState stakingtypes.GenesisState, db *database.Db) error {
var redelegations []types.Redelegation
for _, redelegation := range genState.Redelegations {
for _, entry := range redelegation.Entries {
redelegations = append(redelegations, types.NewRedelegation(
redelegation.DelegatorAddress,
redelegation.ValidatorSrcAddress,
redelegation.ValidatorDstAddress,
sdk.NewCoin(genState.Params.BondDenom, entry.InitialBalance),
entry.CompletionTime,
doc.InitialHeight,
))
}
}
return db.SaveRedelegations(redelegations)
}
// --------------------------------------------------------------------------------------------------------------------
// saveValidatorsCommissions save the initial commission for each validator
func saveValidatorsCommissions(height int64, validators stakingtypes.Validators, db *database.Db) error {
for _, account := range validators {
err := db.SaveValidatorCommission(types.NewValidatorCommission(
account.OperatorAddress,
&account.Commission.Rate,
&account.MinSelfDelegation,
height,
))
if err != nil {
return err
}
}
return nil
}