Skip to content

Commit

Permalink
fix: read lock when reading from block cache
Browse files Browse the repository at this point in the history
  • Loading branch information
leovct committed Jan 18, 2024
1 parent a940a8f commit 4f04dde
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions cmd/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,8 +502,10 @@ func renderMonitorUI(ctx context.Context, ec *ethclient.Client, ms *monitorStatu
blockInfo.Rows = []string{}

transactionInfo.ColumnWidths = getColumnWidths(transactionColumnRatio, transactionInfo.Dx())
transactionInfo.Rows = ui.GetBlockTxTable(renderedBlocks[len(renderedBlocks)-1], ms.ChainID)
transactionInfo.Title = fmt.Sprintf("Latest Transactions for Block #%s", renderedBlocks[len(renderedBlocks)-1].Number().String())
if len(renderedBlocks) >= 1 {
transactionInfo.Rows = ui.GetBlockTxTable(renderedBlocks[len(renderedBlocks)-1], ms.ChainID)
transactionInfo.Title = fmt.Sprintf("Latest Transactions for Block #%s", renderedBlocks[len(renderedBlocks)-1].Number().String())
}
}

termui.Render(grid)
Expand Down Expand Up @@ -592,7 +594,7 @@ func renderMonitorUI(ctx context.Context, ec *ethclient.Client, ms *monitorStatu
toBlockNumber.SetInt64(0)
}

if !isBlockInCache(ms.BlockCache, toBlockNumber) {
if !ms.isBlockInCache(toBlockNumber) {
err := ms.getBlockRange(ctx, toBlockNumber, rpc)
if err != nil {
log.Warn().Err(err).Msg("Failed to fetch blocks on page down")
Expand Down Expand Up @@ -631,7 +633,7 @@ func renderMonitorUI(ctx context.Context, ec *ethclient.Client, ms *monitorStatu
}

// Fetch the blocks in the new range if they are missing
if !isBlockInCache(ms.BlockCache, nextTopBlockNumber) {
if !ms.isBlockInCache(nextTopBlockNumber) {
err := ms.getBlockRange(ctx, new(big.Int).Add(nextTopBlockNumber, big.NewInt(int64(windowSize))), rpc)
if err != nil {
log.Warn().Err(err).Msg("Failed to fetch blocks on page up")
Expand Down Expand Up @@ -746,8 +748,10 @@ func renderMonitorUI(ctx context.Context, ec *ethclient.Client, ms *monitorStatu
}
}

func isBlockInCache(cache *lru.Cache, blockNumber *big.Int) bool {
_, exists := cache.Get(blockNumber.String())
func (ms *monitorStatus) isBlockInCache(blockNumber *big.Int) bool {
ms.BlocksLock.RLock()
_, exists := ms.BlockCache.Get(blockNumber.String())
ms.BlocksLock.RUnlock()
return exists
}

Expand Down

0 comments on commit 4f04dde

Please sign in to comment.