From 84b96a5331cfa2ef1e7500f4436b9613546e7cc2 Mon Sep 17 00:00:00 2001 From: Matt Ketmo Date: Thu, 21 Nov 2024 08:44:33 +0100 Subject: [PATCH] chore: simplify latestBlock variable in block watcher (#90) --- README.md | 4 ++-- pkg/watcher/block.go | 33 +++++++++++++++------------------ 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index ee7509a..9008cb5 100644 --- a/README.md +++ b/README.md @@ -107,16 +107,16 @@ Metrics (without prefix) | Description `commission` | Earned validator commission `consecutive_missed_blocks` | Number of consecutive missed blocks per validator (for a bonded validator) `downtime_jail_duration` | Duration of the jail period for a validator in seconds +`empty_blocks` | Number of empty blocks (blocks with zero transactions) proposed by validator `is_bonded` | Set to 1 if the validator is bonded `is_jailed` | Set to 1 if the validator is jailed `min_signed_blocks_per_window` | Minimum number of blocks required to be signed per signing window -`missed_blocks` | Number of missed blocks per validator (for a bonded validator) `missed_blocks_window` | Number of missed blocks per validator for the current signing window (for a bonded validator) +`missed_blocks` | Number of missed blocks per validator (for a bonded validator) `node_block_height` | Latest fetched block height for each node `node_synced` | Set to 1 is the node is synced (ie. not catching-up) `proposal_end_time` | Timestamp of the voting end time of a proposal `proposed_blocks` | Number of proposed blocks per validator (for a bonded validator) -`empty_blocks` | Number of empty blocks (blocks with zero transactions) proposed by validator `rank` | Rank of the validator `seat_price` | Min seat price to be in the active set (ie. bonded tokens of the latest validator) `signed_blocks_window` | Number of blocks per signing window diff --git a/pkg/watcher/block.go b/pkg/watcher/block.go index e0a2b18..ba38bde 100644 --- a/pkg/watcher/block.go +++ b/pkg/watcher/block.go @@ -24,16 +24,14 @@ type BlockWebhook struct { } type BlockWatcher struct { - trackedValidators []TrackedValidator - metrics *metrics.Metrics - writer io.Writer - blockChan chan *BlockInfo - validatorSet atomic.Value // []*types.Validator - latestBlockHeight int64 - latestBlockProposer string - latestBlockTransactions int - webhook *webhook.Webhook - customWebhooks []BlockWebhook + trackedValidators []TrackedValidator + metrics *metrics.Metrics + writer io.Writer + blockChan chan *BlockInfo + validatorSet atomic.Value // []*types.Validator + latestBlock BlockInfo + webhook *webhook.Webhook + customWebhooks []BlockWebhook } 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 func (w *BlockWatcher) handleBlockInfo(ctx context.Context, block *BlockInfo) { chainId := block.ChainID - if w.latestBlockHeight >= block.Height { + if w.latestBlock.Height >= block.Height { // Skip already processed blocks return } @@ -188,8 +186,8 @@ func (w *BlockWatcher) handleBlockInfo(ctx context.Context, block *BlockInfo) { } w.metrics.SkippedBlocks.WithLabelValues(chainId) - blockDiff := block.Height - w.latestBlockHeight - if w.latestBlockHeight > 0 && blockDiff > 1 { + blockDiff := block.Height - w.latestBlock.Height + if w.latestBlock.Height > 0 && blockDiff > 1 { log.Warn().Msgf("skipped %d unknown blocks", blockDiff-1) w.metrics.SkippedBlocks.WithLabelValues(chainId).Add(float64(blockDiff)) } @@ -203,9 +201,9 @@ func (w *BlockWatcher) handleBlockInfo(ctx context.Context, block *BlockInfo) { validatorStatus := []string{} for _, res := range block.ValidatorStatus { icon := "⚪️" - if w.latestBlockProposer == res.Address { + if w.latestBlock.ProposerAddress == res.Address { // Check if this is an empty block - if w.latestBlockTransactions == 0 { + if w.latestBlock.Transactions == 0 { icon = "🟡" w.metrics.EmptyBlocks.WithLabelValues(block.ChainID, res.Address, res.Label).Inc() } else { @@ -241,9 +239,8 @@ func (w *BlockWatcher) handleBlockInfo(ctx context.Context, block *BlockInfo) { // Handle webhooks w.handleWebhooks(ctx, block) - w.latestBlockHeight = block.Height - w.latestBlockProposer = block.ProposerAddress - w.latestBlockTransactions = block.Transactions + // Save latest block + w.latestBlock = *block } func (w *BlockWatcher) computeValidatorStatus(block *types.Block) []ValidatorStatus {