Skip to content

Commit

Permalink
chore: simplify latestBlock variable in block watcher (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
MattKetmo authored Nov 21, 2024
1 parent c4d7ca1 commit 84b96a5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 15 additions & 18 deletions pkg/watcher/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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))
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 84b96a5

Please sign in to comment.