-
Notifications
You must be signed in to change notification settings - Fork 231
/
handle_block.go
254 lines (211 loc) · 8.17 KB
/
handle_block.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
package staking
import (
"context"
"encoding/hex"
"fmt"
"time"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
bankutils "github.com/forbole/bdjuno/modules/bank/utils"
historyutils "github.com/forbole/bdjuno/modules/history/utils"
"github.com/forbole/bdjuno/modules/utils"
"github.com/desmos-labs/juno/client"
"github.com/forbole/bdjuno/database"
"github.com/forbole/bdjuno/types"
"github.com/cosmos/cosmos-sdk/codec"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
juno "github.com/desmos-labs/juno/types"
"github.com/rs/zerolog/log"
tmctypes "github.com/tendermint/tendermint/rpc/core/types"
tmtypes "github.com/tendermint/tendermint/types"
stakingutils "github.com/forbole/bdjuno/modules/staking/utils"
)
// HandleBlock represents a method that is called each time a new block is created
func HandleBlock(
cfg juno.Config, block *tmctypes.ResultBlock, vals *tmctypes.ResultValidators,
stakingClient stakingtypes.QueryClient, bankClient banktypes.QueryClient, distrClient distrtypes.QueryClient,
cdc codec.Marshaler, db *database.Db,
) error {
// Update the validators
validators, err := stakingutils.UpdateValidators(block.Block.Height, stakingClient, cdc, db)
if err != nil {
return fmt.Errorf("error while updating validators: %s", err)
}
// Get the params
go updateParams(block.Block.Height, stakingClient, db)
// Update the voting powers
go updateValidatorVotingPower(block.Block.Height, vals, db)
// Update the validators statuses
go updateValidatorsStatus(block.Block.Height, validators, cdc, db)
// Updated the double sign evidences
go updateDoubleSignEvidence(block.Block.Height, block.Block.Evidence.Evidence, db)
// Update the staking pool
go updateStakingPool(block.Block.Height, stakingClient, db)
// Update redelegations and unbonding delegations
go updateElapsedDelegations(cfg, block.Block.Height, block.Block.Time, stakingClient, bankClient, distrClient, db)
return nil
}
// updateParams gets the updated params and stores them inside the database
func updateParams(height int64, stakingClient stakingtypes.QueryClient, db *database.Db) {
log.Debug().Str("module", "staking").Int64("height", height).
Msg("updating params")
res, err := stakingClient.Params(
context.Background(),
&stakingtypes.QueryParamsRequest{},
client.GetHeightRequestHeader(height),
)
if err != nil {
log.Error().Str("module", "staking").Err(err).
Int64("height", height).
Msg("error while getting params")
return
}
err = db.SaveStakingParams(types.NewStakingParams(res.Params, height))
if err != nil {
log.Error().Str("module", "staking").Err(err).
Int64("height", height).
Msg("error while saving params")
return
}
}
// updateValidatorsStatus updates all validators' statuses
func updateValidatorsStatus(height int64, validators []stakingtypes.Validator, cdc codec.Marshaler, db *database.Db) {
log.Debug().Str("module", "staking").Int64("height", height).
Msg("updating validators statuses")
statuses, err := stakingutils.GetValidatorsStatuses(height, validators, cdc)
if err != nil {
log.Error().Str("module", "staking").Err(err).
Int64("height", height).
Send()
return
}
err = db.SaveValidatorsStatuses(statuses)
if err != nil {
log.Error().Str("module", "staking").Err(err).
Int64("height", height).
Msg("error while saving validators statuses")
}
}
// updateValidatorVotingPower fetches and stores into the database all the current validators' voting powers
func updateValidatorVotingPower(height int64, vals *tmctypes.ResultValidators, db *database.Db) {
log.Debug().Str("module", "staking").Int64("height", height).
Msg("updating validators voting powers")
votingPowers := stakingutils.GetValidatorsVotingPowers(height, vals, db)
err := db.SaveValidatorsVotingPowers(votingPowers)
if err != nil {
log.Error().Str("module", "staking").Err(err).Int64("height", height).
Msg("error while saving validators voting powers")
}
}
// updateDoubleSignEvidence updates the double sign evidence of all validators
func updateDoubleSignEvidence(height int64, evidenceList tmtypes.EvidenceList, db *database.Db) {
log.Debug().Str("module", "staking").Int64("height", height).
Msg("updating double sign evidence")
for _, ev := range evidenceList {
dve, ok := ev.(*tmtypes.DuplicateVoteEvidence)
if !ok {
continue
}
evidence := types.NewDoubleSignEvidence(
height,
types.NewDoubleSignVote(
int(dve.VoteA.Type),
dve.VoteA.Height,
dve.VoteA.Round,
dve.VoteA.BlockID.String(),
juno.ConvertValidatorAddressToBech32String(dve.VoteA.ValidatorAddress),
dve.VoteA.ValidatorIndex,
hex.EncodeToString(dve.VoteA.Signature),
),
types.NewDoubleSignVote(
int(dve.VoteB.Type),
dve.VoteB.Height,
dve.VoteB.Round,
dve.VoteB.BlockID.String(),
juno.ConvertValidatorAddressToBech32String(dve.VoteB.ValidatorAddress),
dve.VoteB.ValidatorIndex,
hex.EncodeToString(dve.VoteB.Signature),
),
)
err := db.SaveDoubleSignEvidence(evidence)
if err != nil {
log.Error().Str("module", "staking").Err(err).Int64("height", height).
Msg("error while saving double sign evidence")
return
}
}
}
// updateStakingPool reads from the LCD the current staking pool and stores its value inside the database
func updateStakingPool(height int64, stakingClient stakingtypes.QueryClient, db *database.Db) {
log.Debug().Str("module", "staking").Int64("height", height).
Msg("updating staking pool")
pool, err := stakingutils.GetStakingPool(height, stakingClient)
if err != nil {
log.Error().Str("module", "staking").Err(err).Int64("height", height).
Msg("error while getting staking pool")
return
}
err = db.SaveStakingPool(pool)
if err != nil {
log.Error().Str("module", "staking").Err(err).Int64("height", height).
Msg("error while saving staking pool")
return
}
}
// updateElapsedDelegations updates the redelegations and unbonding delegations that have elapsed
func updateElapsedDelegations(
cfg juno.Config, height int64, blockTime time.Time,
stakingClient stakingtypes.QueryClient, bankClient banktypes.QueryClient, distrClient distrtypes.QueryClient,
db *database.Db,
) {
log.Debug().Str("module", "staking").Int64("height", height).
Msg("updating elapsed redelegations and unbonding delegations")
deletedRedelegations, err := db.DeleteCompletedRedelegations(blockTime)
if err != nil {
log.Error().Str("module", "staking").Err(err).Int64("height", height).
Msg("error while deleting completed redelegations")
return
}
deletedUnbondingDelegations, err := db.DeleteCompletedUnbondingDelegations(blockTime)
if err != nil {
log.Error().Str("module", "staking").Err(err).Int64("height", height).
Msg("error while deleting completed unbonding delegations")
return
}
var delegators = map[string]bool{}
// Add all the delegators from the redelegations
for _, redelegation := range deletedRedelegations {
if _, ok := delegators[redelegation.DelegatorAddress]; !ok {
delegators[redelegation.DelegatorAddress] = true
}
}
// Add all the delegators from unbonding delegations
for _, delegation := range deletedUnbondingDelegations {
if _, ok := delegators[delegation.DelegatorAddress]; !ok {
delegators[delegation.DelegatorAddress] = true
}
}
// Update the delegations and balances of all the delegators
for delegator := range delegators {
err = stakingutils.RefreshDelegations(height, delegator, stakingClient, distrClient, db)
if err != nil {
log.Error().Str("module", "staking").Err(err).Int64("height", height).
Str("delegator", delegator).Msg("error while refreshing the delegations")
return
}
err = bankutils.RefreshBalances(height, []string{delegator}, bankClient, db)
if err != nil {
log.Error().Str("module", "staking").Err(err).Int64("height", height).
Str("delegator", delegator).Msg("error while refreshing the balance")
return
}
if utils.IsModuleEnabled(cfg, types.HistoryModuleName) {
err = historyutils.UpdateAccountBalanceHistoryWithTime(delegator, blockTime, db)
if err != nil {
log.Error().Str("module", "staking").Err(err).Int64("height", height).
Str("delegator", delegator).Msg("error while updating account balance history")
return
}
}
}
}