From 9ab6f2af0ac227b09889e0b0146e5eca541fdeff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Vincent?= <28714795+leovct@users.noreply.github.com> Date: Wed, 18 Sep 2024 13:55:36 +0200 Subject: [PATCH] fix(monitor): out of range panics (#375) * fix: memory panic when selecting txs * fix: same fix * chore: nit * chore: lint --- cmd/monitor/monitor.go | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/cmd/monitor/monitor.go b/cmd/monitor/monitor.go index 2a30e6d9..0e0ccc17 100644 --- a/cmd/monitor/monitor.go +++ b/cmd/monitor/monitor.go @@ -471,7 +471,21 @@ func renderMonitorUI(ctx context.Context, ec *ethclient.Client, ms *monitorStatu baseFee := ms.SelectedBlock.BaseFee() if transactionList.SelectedRow != 0 { - ms.SelectedTransaction = ms.SelectedBlock.Transactions()[transactionList.SelectedRow-1] + transactions := ms.SelectedBlock.Transactions() + if len(transactions) > 0 { + index := transactionList.SelectedRow - 1 + if index >= 0 && index < len(transactions) { + ms.SelectedTransaction = transactions[index] + } else { + log.Error(). + Int("row", transactionList.SelectedRow). + Msg("Selected row is out of range for transactions") + } + } else { + log.Debug(). + Int("block", int(ms.SelectedBlock.Number().Uint64())). + Msg("No transactions available in the selected block") + } transactionInformationList.Rows = ui.GetSimpleTxFields(ms.SelectedTransaction, ms.ChainID, baseFee) } termui.Clear() @@ -484,7 +498,22 @@ func renderMonitorUI(ctx context.Context, ec *ethclient.Client, ms *monitorStatu return } else if currentMode == monitorModeTransaction { baseFee := ms.SelectedBlock.BaseFee() - skeleton.TxInfo.Rows = ui.GetSimpleTxFields(ms.SelectedBlock.Transactions()[transactionList.SelectedRow-1], ms.ChainID, baseFee) + transactions := ms.SelectedBlock.Transactions() + if len(transactions) > 0 { + index := transactionList.SelectedRow - 1 + if index > 0 && index < len(transactions) { + tx := transactions[index] + skeleton.TxInfo.Rows = ui.GetSimpleTxFields(tx, ms.ChainID, baseFee) + } else { + log.Error(). + Int("row", transactionList.SelectedRow). + Msg("Selected row is out of range for transactions") + } + } else { + log.Debug(). + Int("block", int(ms.SelectedBlock.Number().Uint64())). + Msg("No transactions available in the selected block") + } skeleton.Receipts.Rows = ui.GetSimpleReceipt(ctx, rpc, ms.SelectedTransaction) termui.Clear()