-
Notifications
You must be signed in to change notification settings - Fork 247
/
handle_block.go
145 lines (121 loc) · 4.25 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
package staking
import (
"encoding/hex"
"fmt"
"github.com/forbole/bdjuno/v2/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
juno "github.com/forbole/juno/v2/types"
"github.com/rs/zerolog/log"
tmctypes "github.com/tendermint/tendermint/rpc/core/types"
tmtypes "github.com/tendermint/tendermint/types"
)
// HandleBlock implements BlockModule
func (m *Module) HandleBlock(
block *tmctypes.ResultBlock, res *tmctypes.ResultBlockResults, _ []*juno.Tx, vals *tmctypes.ResultValidators,
) error {
// Update the validators
validators, err := m.updateValidators(block.Block.Height)
if err != nil {
return fmt.Errorf("error while updating validators: %s", err)
}
// Update the voting powers
go m.updateValidatorVotingPower(block.Block.Height, vals)
// Update the validators statuses
go m.updateValidatorsStatus(block.Block.Height, validators)
// Updated the double sign evidences
go m.updateDoubleSignEvidence(block.Block.Height, block.Block.Evidence.Evidence)
// Update the staking pool
go m.updateStakingPool(block.Block.Height)
return nil
}
// updateValidatorsStatus updates all validators' statuses
func (m *Module) updateValidatorsStatus(height int64, validators []stakingtypes.Validator) {
log.Debug().Str("module", "staking").Int64("height", height).
Msg("updating validators statuses")
statuses, err := m.GetValidatorsStatuses(height, validators)
if err != nil {
log.Error().Str("module", "staking").Err(err).
Int64("height", height).
Send()
return
}
err = m.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 (m *Module) updateValidatorVotingPower(height int64, vals *tmctypes.ResultValidators) {
log.Debug().Str("module", "staking").Int64("height", height).
Msg("updating validators voting powers")
// Get the voting powers
votingPowers, err := m.GetValidatorsVotingPowers(height, vals)
if err != nil {
log.Error().Str("module", "staking").Err(err).Int64("height", height).
Msg("error while getting validators voting powers")
return
}
// Save all the voting powers
err = m.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 (m *Module) updateDoubleSignEvidence(height int64, evidenceList tmtypes.EvidenceList) {
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 := m.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 (m *Module) updateStakingPool(height int64) {
log.Debug().Str("module", "staking").Int64("height", height).
Msg("updating staking pool")
pool, err := m.GetStakingPool(height)
if err != nil {
log.Error().Str("module", "staking").Err(err).Int64("height", height).
Msg("error while getting staking pool")
return
}
err = m.db.SaveStakingPool(pool)
if err != nil {
log.Error().Str("module", "staking").Err(err).Int64("height", height).
Msg("error while saving staking pool")
return
}
}