Skip to content

Commit

Permalink
add transaction preview
Browse files Browse the repository at this point in the history
  • Loading branch information
gatsbyz committed Jan 2, 2024
1 parent f0b34ad commit 5f04c15
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 46 deletions.
66 changes: 40 additions & 26 deletions cmd/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,18 @@ var (

type (
monitorStatus struct {
TopDisplayedBlock *big.Int
UpperBlock *big.Int
LowerBlock *big.Int
ChainID *big.Int
HeadBlock *big.Int
PeerCount uint64
GasPrice *big.Int
PendingCount uint64
SelectedBlock rpctypes.PolyBlock
BlockCache *lru.Cache `json:"-"`
BlocksLock sync.RWMutex `json:"-"`
TopDisplayedBlock *big.Int
UpperBlock *big.Int
LowerBlock *big.Int
ChainID *big.Int
HeadBlock *big.Int
PeerCount uint64
GasPrice *big.Int
PendingCount uint64
SelectedBlock rpctypes.PolyBlock
SelectedTransaction rpctypes.PolyTransaction
BlockCache *lru.Cache `json:"-"`
BlocksLock sync.RWMutex `json:"-"`
}
chainState struct {
HeadBlock uint64
Expand Down Expand Up @@ -319,17 +320,6 @@ func renderMonitorUI(ctx context.Context, ec *ethclient.Client, ms *monitorStatu
var renderedBlocks rpctypes.SortableBlocks

redraw := func(ms *monitorStatus, force ...bool) {
log.Debug().
Str("TopDisplayedBlock", ms.TopDisplayedBlock.String()).
Int("BatchSize", batchSize.Get()).
Str("UpperBlock", ms.UpperBlock.String()).
Str("LowerBlock", ms.LowerBlock.String()).
Str("ChainID", ms.ChainID.String()).
Str("HeadBlock", ms.HeadBlock.String()).
Uint64("PeerCount", ms.PeerCount).
Str("GasPrice", ms.GasPrice.String()).
Uint64("PendingCount", ms.PendingCount).
Msg("Redrawing")

if currentMode == monitorModeHelp {
// TODO add some help context?
Expand All @@ -340,11 +330,33 @@ func renderMonitorUI(ctx context.Context, ec *ethclient.Client, ms *monitorStatu
skeleton.TransactionList.Rows = rows
skeleton.TransactionList.Title = title

baseFee := ms.SelectedBlock.BaseFee()
if skeleton.TransactionList.SelectedRow != 0 {
skeleton.TransactionInformationList.Rows = ui.GetSimpleTxFields(ms.SelectedBlock.Transactions()[skeleton.TransactionList.SelectedRow-1], ms.ChainID, baseFee)

}
termui.Clear()
termui.Render(blockGrid)

log.Debug().
Int("skeleton.TransactionList.SelectedRow", skeleton.TransactionList.SelectedRow).
Msg("Redrawing block mode")

return
}

log.Debug().
Str("TopDisplayedBlock", ms.TopDisplayedBlock.String()).
Int("BatchSize", batchSize.Get()).
Str("UpperBlock", ms.UpperBlock.String()).
Str("LowerBlock", ms.LowerBlock.String()).
Str("ChainID", ms.ChainID.String()).
Str("HeadBlock", ms.HeadBlock.String()).
Uint64("PeerCount", ms.PeerCount).
Str("GasPrice", ms.GasPrice.String()).
Uint64("PendingCount", ms.PendingCount).
Msg("Redrawing")

if blockTable.SelectedRow == 0 {
bottomBlockNumber := new(big.Int).Sub(ms.HeadBlock, big.NewInt(int64(windowSize-1)))
if bottomBlockNumber.Cmp(zero) < 0 {
Expand Down Expand Up @@ -391,6 +403,7 @@ func renderMonitorUI(ctx context.Context, ec *ethclient.Client, ms *monitorStatu

blockTable.TextStyle = termui.NewStyle(termui.ColorWhite)
blockTable.SelectedRowStyle = termui.NewStyle(termui.ColorWhite, termui.ColorRed, termui.ModifierBold)
transactionColumnRatio := []int{30, 5, 20, 20, 5, 10}
if blockTable.SelectedRow > 0 && blockTable.SelectedRow <= len(blockTable.Rows) {
// Only changed the selected block when the user presses the up down keys.
// Otherwise this will adjust when the table is updated automatically.
Expand All @@ -402,8 +415,7 @@ func renderMonitorUI(ctx context.Context, ec *ethclient.Client, ms *monitorStatu

ms.SelectedBlock = renderedBlocks[len(renderedBlocks)-blockTable.SelectedRow]
blockInfo.Rows = ui.GetSimpleBlockFields(ms.SelectedBlock)
columnRatio := []int{30, 5, 20, 20, 5, 10}
transactionInfo.ColumnWidths = getColumnWidths(columnRatio, transactionInfo.Dx())
transactionInfo.ColumnWidths = getColumnWidths(transactionColumnRatio, transactionInfo.Dx())
transactionInfo.Rows = ui.GetBlockTxTable(ms.SelectedBlock, ms.ChainID)
transactionInfo.Title = fmt.Sprintf("Latest Transactions for Block #%s", ms.SelectedBlock.Number().String())

Expand All @@ -412,9 +424,11 @@ func renderMonitorUI(ctx context.Context, ec *ethclient.Client, ms *monitorStatu
}
} else {
ms.SelectedBlock = nil
transactionInfo.Title = "Latest Transactions"
blockInfo.Rows = []string{}
transactionInfo.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())
}

termui.Render(grid)
Expand Down
47 changes: 27 additions & 20 deletions cmd/monitor/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ import (
)

type UiSkeleton struct {
Current *widgets.Paragraph
TxPerBlockChart *widgets.Sparkline
GasPriceChart *widgets.Sparkline
BlockSizeChart *widgets.Sparkline
PendingTxChart *widgets.Sparkline
GasChart *widgets.Sparkline
BlockInfo *widgets.List
TransactionList *widgets.List
Current *widgets.Paragraph
TxPerBlockChart *widgets.Sparkline
GasPriceChart *widgets.Sparkline
BlockSizeChart *widgets.Sparkline
PendingTxChart *widgets.Sparkline
GasChart *widgets.Sparkline
BlockInfo *widgets.List
TransactionList *widgets.List
TransactionInformationList *widgets.List
}

func GetCurrentBlockInfo(headBlock *big.Int, gasPrice *big.Int, peerCount uint64, pendingCount uint64, chainID *big.Int, blocks []rpctypes.PolyBlock) string {
Expand All @@ -52,7 +53,7 @@ func GetBlocksList(blocks []rpctypes.PolyBlock) ([]string, string) {
zone, _ := time.Now().Zone()
headerVariables := []string{"#", fmt.Sprintf("TIME (%s)", zone), "BLK TIME", "TXN #", "GAS USED", "HASH", "AUTHOR"}

proportion := []int{10, 10, 5, 5, 10, 20}
proportion := []int{10, 10, 2, 2, 10, 12}

header := ""
for i, prop := range proportion {
Expand Down Expand Up @@ -237,16 +238,16 @@ func GetTransactionsList(block rpctypes.PolyBlock, chainID *big.Int) ([]string,
return records, header
}

func GetSimpleBlockTxFields(block rpctypes.PolyBlock, chainID *big.Int) []string {
fields := make([]string, 0)
blank := ""
for _, tx := range block.Transactions() {
txFields := GetSimpleTxFields(tx, chainID, block.BaseFee())
fields = append(fields, blank)
fields = append(fields, txFields...)
}
return fields
}
// func GetSimpleBlockTxFields(block rpctypes.PolyBlock, chainID *big.Int) []string {
// fields := make([]string, 0)
// blank := ""
// for _, tx := range block.Transactions() {
// txFields := GetSimpleTxFields(tx, chainID, block.BaseFee())
// fields = append(fields, blank)
// fields = append(fields, txFields...)
// }
// return fields
// }

func GetSimpleTxFields(tx rpctypes.PolyTransaction, chainID, baseFee *big.Int) []string {
fields := make([]string, 0)
Expand Down Expand Up @@ -347,10 +348,16 @@ func SetUISkeleton() (blockList *widgets.List, blockInfo *widgets.List, transact
termUi.TransactionList.TextStyle = ui.NewStyle(ui.ColorGreen)
termUi.TransactionList.WrapText = true

termUi.TransactionInformationList = widgets.NewList()
termUi.TransactionInformationList.Title = "Transaction Information"
termUi.TransactionInformationList.TextStyle = ui.NewStyle(ui.ColorWhite)
termUi.TransactionInformationList.WrapText = true

blockGrid.Set(
// ui.NewRow(1.0/10, b0),
ui.NewRow(2.0/10, termUi.BlockInfo),
ui.NewRow(8.0/10, termUi.TransactionList),
ui.NewRow(6.0/10, termUi.TransactionList),
ui.NewRow(2.0/10, termUi.TransactionInformationList),
)

grid.Set(
Expand Down

0 comments on commit 5f04c15

Please sign in to comment.