Skip to content

Commit 84b96a5

Browse files
authored
chore: simplify latestBlock variable in block watcher (#90)
1 parent c4d7ca1 commit 84b96a5

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,16 @@ Metrics (without prefix) | Description
107107
`commission` | Earned validator commission
108108
`consecutive_missed_blocks` | Number of consecutive missed blocks per validator (for a bonded validator)
109109
`downtime_jail_duration` | Duration of the jail period for a validator in seconds
110+
`empty_blocks` | Number of empty blocks (blocks with zero transactions) proposed by validator
110111
`is_bonded` | Set to 1 if the validator is bonded
111112
`is_jailed` | Set to 1 if the validator is jailed
112113
`min_signed_blocks_per_window` | Minimum number of blocks required to be signed per signing window
113-
`missed_blocks` | Number of missed blocks per validator (for a bonded validator)
114114
`missed_blocks_window` | Number of missed blocks per validator for the current signing window (for a bonded validator)
115+
`missed_blocks` | Number of missed blocks per validator (for a bonded validator)
115116
`node_block_height` | Latest fetched block height for each node
116117
`node_synced` | Set to 1 is the node is synced (ie. not catching-up)
117118
`proposal_end_time` | Timestamp of the voting end time of a proposal
118119
`proposed_blocks` | Number of proposed blocks per validator (for a bonded validator)
119-
`empty_blocks` | Number of empty blocks (blocks with zero transactions) proposed by validator
120120
`rank` | Rank of the validator
121121
`seat_price` | Min seat price to be in the active set (ie. bonded tokens of the latest validator)
122122
`signed_blocks_window` | Number of blocks per signing window

pkg/watcher/block.go

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,14 @@ type BlockWebhook struct {
2424
}
2525

2626
type BlockWatcher struct {
27-
trackedValidators []TrackedValidator
28-
metrics *metrics.Metrics
29-
writer io.Writer
30-
blockChan chan *BlockInfo
31-
validatorSet atomic.Value // []*types.Validator
32-
latestBlockHeight int64
33-
latestBlockProposer string
34-
latestBlockTransactions int
35-
webhook *webhook.Webhook
36-
customWebhooks []BlockWebhook
27+
trackedValidators []TrackedValidator
28+
metrics *metrics.Metrics
29+
writer io.Writer
30+
blockChan chan *BlockInfo
31+
validatorSet atomic.Value // []*types.Validator
32+
latestBlock BlockInfo
33+
webhook *webhook.Webhook
34+
customWebhooks []BlockWebhook
3735
}
3836

3937
func NewBlockWatcher(validators []TrackedValidator, metrics *metrics.Metrics, writer io.Writer, webhook *webhook.Webhook, customWebhooks []BlockWebhook) *BlockWatcher {
@@ -173,7 +171,7 @@ func (w *BlockWatcher) syncValidatorSet(ctx context.Context, n *rpc.Node) error
173171
func (w *BlockWatcher) handleBlockInfo(ctx context.Context, block *BlockInfo) {
174172
chainId := block.ChainID
175173

176-
if w.latestBlockHeight >= block.Height {
174+
if w.latestBlock.Height >= block.Height {
177175
// Skip already processed blocks
178176
return
179177
}
@@ -188,8 +186,8 @@ func (w *BlockWatcher) handleBlockInfo(ctx context.Context, block *BlockInfo) {
188186
}
189187
w.metrics.SkippedBlocks.WithLabelValues(chainId)
190188

191-
blockDiff := block.Height - w.latestBlockHeight
192-
if w.latestBlockHeight > 0 && blockDiff > 1 {
189+
blockDiff := block.Height - w.latestBlock.Height
190+
if w.latestBlock.Height > 0 && blockDiff > 1 {
193191
log.Warn().Msgf("skipped %d unknown blocks", blockDiff-1)
194192
w.metrics.SkippedBlocks.WithLabelValues(chainId).Add(float64(blockDiff))
195193
}
@@ -203,9 +201,9 @@ func (w *BlockWatcher) handleBlockInfo(ctx context.Context, block *BlockInfo) {
203201
validatorStatus := []string{}
204202
for _, res := range block.ValidatorStatus {
205203
icon := "⚪️"
206-
if w.latestBlockProposer == res.Address {
204+
if w.latestBlock.ProposerAddress == res.Address {
207205
// Check if this is an empty block
208-
if w.latestBlockTransactions == 0 {
206+
if w.latestBlock.Transactions == 0 {
209207
icon = "🟡"
210208
w.metrics.EmptyBlocks.WithLabelValues(block.ChainID, res.Address, res.Label).Inc()
211209
} else {
@@ -241,9 +239,8 @@ func (w *BlockWatcher) handleBlockInfo(ctx context.Context, block *BlockInfo) {
241239
// Handle webhooks
242240
w.handleWebhooks(ctx, block)
243241

244-
w.latestBlockHeight = block.Height
245-
w.latestBlockProposer = block.ProposerAddress
246-
w.latestBlockTransactions = block.Transactions
242+
// Save latest block
243+
w.latestBlock = *block
247244
}
248245

249246
func (w *BlockWatcher) computeValidatorStatus(block *types.Block) []ValidatorStatus {

0 commit comments

Comments
 (0)