Skip to content

Commit 25f1554

Browse files
authored
Merge pull request #438 from jhkimqd/jihwan/pgdn-monitor-fix
fix: monitor panic when renderedblock is nil
2 parents 2aaa905 + 935b649 commit 25f1554

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

cmd/monitor/monitor.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -609,9 +609,11 @@ func renderMonitorUI(ctx context.Context, ec *ethclient.Client, ms *monitorStatu
609609
if renderedBlocksMeanGasPrice == nil {
610610
skeleton.Current.Text = ui.GetCurrentText(skeleton.Current, ms.HeadBlock, "--", ms.PeerCount, ms.ChainID, rpcUrl)
611611
} else {
612-
// Under normal cases, the gas price will be derived from the last element of the GasPriceChart with 2 decimal places precision.
613-
gasPriceStr := strconv.FormatFloat(renderedBlocksMeanGasPrice[len(renderedBlocksMeanGasPrice)-1]/1000000000, 'f', 2, 64)
614-
skeleton.Current.Text = ui.GetCurrentText(skeleton.Current, ms.HeadBlock, gasPriceStr, ms.PeerCount, ms.ChainID, rpcUrl)
612+
if len(renderedBlocksMeanGasPrice) >= 1 {
613+
// Under normal cases, the gas price will be derived from the last element of the GasPriceChart with 2 decimal places precision.
614+
gasPriceStr := strconv.FormatFloat(renderedBlocksMeanGasPrice[len(renderedBlocksMeanGasPrice)-1]/1000000000, 'f', 2, 64)
615+
skeleton.Current.Text = ui.GetCurrentText(skeleton.Current, ms.HeadBlock, gasPriceStr, ms.PeerCount, ms.ChainID, rpcUrl)
616+
}
615617
}
616618

617619
if txPoolStatusSupported {
@@ -643,7 +645,7 @@ func renderMonitorUI(ctx context.Context, ec *ethclient.Client, ms *monitorStatu
643645
if blockTable.SelectedRow > 0 && blockTable.SelectedRow <= len(blockTable.Rows) && (len(renderedBlocks)-blockTable.SelectedRow) >= 0 {
644646
// Only changed the selected block when the user presses the up down keys.
645647
// Otherwise this will adjust when the table is updated automatically.
646-
if setBlock {
648+
if setBlock && ms.SelectedBlock != nil {
647649
log.Debug().
648650
Int("blockTable.SelectedRow", blockTable.SelectedRow).
649651
Int("renderedBlocks", len(renderedBlocks)).
@@ -837,6 +839,14 @@ func renderMonitorUI(ctx context.Context, ec *ethclient.Client, ms *monitorStatu
837839
setBlock = true
838840
case "<C-f>", "<PageDown>":
839841
// When pressing PageDown beyond the genesis block, redraw the monitor screen to avoid freezing at the previous rendered blocks.
842+
if len(renderedBlocks) == 0 {
843+
currentMode = monitorModeExplorer
844+
blockTable.SelectedRow = 0
845+
forceRedraw = true
846+
redraw(ms, true)
847+
break
848+
}
849+
840850
if renderedBlocks[0].Number().String() == "0" || renderedBlocks[0].Number().String() == "1" {
841851
blockTable.SelectedRow = len(renderedBlocks)
842852
forceRedraw = true

0 commit comments

Comments
 (0)