Skip to content

Commit

Permalink
fix: read lock when reading from block cache (#191)
Browse files Browse the repository at this point in the history
* fix: read lock when reading from block cache

* chore: improve readability

Co-authored-by: Minh Vu <[email protected]>

* chore: lint

---------

Co-authored-by: Minh Vu <[email protected]>
  • Loading branch information
leovct and minhd-vu authored Jan 22, 2024
1 parent 2221ac2 commit ad350b7
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions cmd/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,8 +502,11 @@ 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) > 0 {
i := len(renderedBlocks) - 1
transactionInfo.Rows = ui.GetBlockTxTable(renderedBlocks[i], ms.ChainID)
transactionInfo.Title = fmt.Sprintf("Latest Transactions for Block #%s", renderedBlocks[i].Number().String())
}
}

termui.Render(grid)
Expand Down Expand Up @@ -592,7 +595,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 +634,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 +749,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 ad350b7

Please sign in to comment.