-
Notifications
You must be signed in to change notification settings - Fork 231
/
handle_genesis.go
147 lines (117 loc) · 4.16 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
package staking
import (
"encoding/json"
"fmt"
"github.com/cosmos/cosmos-sdk/types/tx"
"github.com/forbole/bdjuno/v3/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/rs/zerolog/log"
)
// HandleGenesis implements GenesisModule
func (m *Module) HandleGenesis(doc *tmtypes.GenesisDoc, appState map[string]json.RawMessage) error {
log.Debug().Str("module", "staking").Msg("parsing genesis")
// Read the genesis state
var genState stakingtypes.GenesisState
err := m.cdc.UnmarshalJSON(appState[stakingtypes.ModuleName], &genState)
if err != nil {
return fmt.Errorf("error while unmarshaling staking state: %s", err)
}
// Save the params
err = m.db.SaveStakingParams(types.NewStakingParams(genState.Params, doc.InitialHeight))
if err != nil {
return fmt.Errorf("error while storing genesis staking params: %s", err)
}
// Parse genesis transactions
err = m.parseGenesisTransactions(doc, appState)
if err != nil {
return fmt.Errorf("error while storing genesis transactions: %s", err)
}
// Save the validators
err = m.saveValidators(doc, genState.Validators)
if err != nil {
return fmt.Errorf("error while storing staking genesis validators: %s", err)
}
// Save the description
err = m.saveValidatorDescription(doc, genState.Validators)
if err != nil {
return fmt.Errorf("error while storing staking genesis validator descriptions: %s", err)
}
err = m.saveValidatorsCommissions(doc.InitialHeight, genState.Validators)
if err != nil {
return fmt.Errorf("error while storing staking genesis validators commissions: %s", err)
}
return nil
}
func (m *Module) parseGenesisTransactions(doc *tmtypes.GenesisDoc, appState map[string]json.RawMessage) error {
var genUtilState genutiltypes.GenesisState
err := m.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 = m.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 = m.StoreValidatorsFromMsgCreateValidator(doc.InitialHeight, createValMsg)
if err != nil {
return fmt.Errorf("error while storing validators from MsgCreateValidator: %s", err)
}
}
}
return nil
}
// --------------------------------------------------------------------------------------------------------------------
// saveValidators stores the validators data present inside the given genesis state
func (m *Module) saveValidators(doc *tmtypes.GenesisDoc, validators stakingtypes.Validators) error {
vals := make([]types.Validator, len(validators))
for i, val := range validators {
validator, err := m.convertValidator(doc.InitialHeight, val)
if err != nil {
return err
}
vals[i] = validator
}
return m.db.SaveValidatorsData(vals)
}
// saveValidatorDescription saves the description for the given validators
func (m *Module) saveValidatorDescription(doc *tmtypes.GenesisDoc, validators stakingtypes.Validators) error {
for _, account := range validators {
description := m.convertValidatorDescription(
doc.InitialHeight,
account.OperatorAddress,
account.Description,
)
err := m.db.SaveValidatorDescription(description)
if err != nil {
return err
}
}
return nil
}
// --------------------------------------------------------------------------------------------------------------------
// saveValidatorsCommissions save the initial commission for each validator
func (m *Module) saveValidatorsCommissions(height int64, validators stakingtypes.Validators) error {
for _, account := range validators {
err := m.db.SaveValidatorCommission(types.NewValidatorCommission(
account.OperatorAddress,
&account.Commission.Rate,
&account.MinSelfDelegation,
height,
))
if err != nil {
return err
}
}
return nil
}