Skip to content

Commit

Permalink
Merge pull request #429 from jhkimqd/jihwan/monitor-add-forkid
Browse files Browse the repository at this point in the history
feat: add forkid to monitor
  • Loading branch information
jhkimqd authored Nov 12, 2024
2 parents 72367b1 + 4f792b4 commit d9bd47b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 14 deletions.
17 changes: 14 additions & 3 deletions cmd/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import (
"sync"
"time"

lru "github.com/hashicorp/golang-lru"
"github.com/0xPolygon/polygon-cli/util"
lru "github.com/hashicorp/golang-lru"

_ "embed"

"github.com/ethereum/go-ethereum/ethclient"
ethrpc "github.com/ethereum/go-ethereum/rpc"

"github.com/cenkalti/backoff/v4"
termui "github.com/gizak/termui/v3"
"github.com/0xPolygon/polygon-cli/cmd/monitor/ui"
"github.com/0xPolygon/polygon-cli/metrics"
"github.com/0xPolygon/polygon-cli/rpctypes"
"github.com/cenkalti/backoff/v4"
termui "github.com/gizak/termui/v3"
"github.com/rs/zerolog/log"
)

Expand Down Expand Up @@ -62,6 +62,7 @@ type (
UpperBlock *big.Int
LowerBlock *big.Int
ChainID *big.Int
ForkID uint64
HeadBlock *big.Int
PeerCount uint64
GasPrice *big.Int
Expand All @@ -79,6 +80,7 @@ type (
GasPrice *big.Int
TxPoolStatus txPoolStatus
ZkEVMBatches zkEVMBatches
ForkID uint64
}
txPoolStatus struct {
pending uint64
Expand Down Expand Up @@ -234,6 +236,11 @@ func getChainState(ctx context.Context, ec *ethclient.Client) (*chainState, erro
log.Debug().Err(err).Msg("Unable to get zkevm batches")
}

cs.ForkID, err = util.GetForkID(ec.Client())
if err != nil {
log.Debug().Err(err).Msg("Unable to get fork id")
}

return cs, nil

}
Expand Down Expand Up @@ -276,6 +283,7 @@ func fetchCurrentBlockData(ctx context.Context, ec *ethclient.Client, ms *monito
ms.GasPrice = cs.GasPrice
ms.TxPoolStatus = cs.TxPoolStatus
ms.ZkEVMBatches = cs.ZkEVMBatches
ms.ForkID = cs.ForkID

return
}
Expand Down Expand Up @@ -594,8 +602,11 @@ func renderMonitorUI(ctx context.Context, ec *ethclient.Client, ms *monitorStatu
skeleton.TxPool.Text = ui.GetTxPoolText(skeleton.TxPool, ms.TxPoolStatus.pending, ms.TxPoolStatus.queued)
}

// if zkEVMBatchesSupported == true, this means the network will also support ForkIDs.
if zkEVMBatchesSupported {
skeleton.ZkEVM.Text = ui.GetZkEVMText(skeleton.ZkEVM, ms.ZkEVMBatches.trusted, ms.ZkEVMBatches.virtual, ms.ZkEVMBatches.verified)

skeleton.Rollup.Text = ui.GetRollupText(skeleton.Rollup, ms.ForkID)
}

skeleton.TxPerBlockChart.Data = metrics.GetTxsPerBlock(renderedBlocks)
Expand Down
33 changes: 22 additions & 11 deletions cmd/monitor/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
"strings"
"time"

"github.com/0xPolygon/polygon-cli/metrics"
"github.com/0xPolygon/polygon-cli/rpctypes"
ethcommon "github.com/ethereum/go-ethereum/common"
ethrpc "github.com/ethereum/go-ethereum/rpc"
ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
"github.com/0xPolygon/polygon-cli/metrics"
"github.com/0xPolygon/polygon-cli/rpctypes"
"github.com/rs/zerolog/log"
)

Expand All @@ -25,15 +25,15 @@ var (
)

type UiSkeleton struct {
Current, TxPool, ZkEVM *widgets.Paragraph
TxPerBlockChart *widgets.Sparkline
GasPriceChart *widgets.Sparkline
BlockSizeChart *widgets.Sparkline
PendingTxChart *widgets.Sparkline
GasChart *widgets.Sparkline
BlockInfo *widgets.List
TxInfo *widgets.List
Receipts *widgets.List
Current, TxPool, ZkEVM, Rollup *widgets.Paragraph
TxPerBlockChart *widgets.Sparkline
GasPriceChart *widgets.Sparkline
BlockSizeChart *widgets.Sparkline
PendingTxChart *widgets.Sparkline
GasChart *widgets.Sparkline
BlockInfo *widgets.List
TxInfo *widgets.List
Receipts *widgets.List
}

func GetCurrentText(widget *widgets.Paragraph, headBlock *big.Int, gasPrice string, peerCount uint64, chainID *big.Int, rpcURL string) string {
Expand Down Expand Up @@ -66,6 +66,11 @@ func GetZkEVMText(widget *widgets.Paragraph, trustedBatchesCount, virtualBatches
return formatParagraph(widget, []string{trustedBatches, virtualBatches, verifiedBatches})
}

func GetRollupText(widget *widgets.Paragraph, forkID uint64) string {
forkIDString := fmt.Sprintf("ForkID: %d", forkID)
return formatParagraph(widget, []string{forkIDString})
}

func formatParagraph(widget *widgets.Paragraph, content []string) string {
dx := widget.Inner.Dx()
dy := widget.Inner.Dy()
Expand Down Expand Up @@ -524,6 +529,10 @@ func SetUISkeleton(txPoolStatusSupported, zkEVMBatchesSupported bool) (blockList
termUi.ZkEVM = widgets.NewParagraph()
termUi.ZkEVM.Title = "ZkEVM Batch No."
totalWidgets++

termUi.Rollup = widgets.NewParagraph()
termUi.Rollup.Title = "Rollup Info"
totalWidgets++
}

topRowBlocks := []interface{}{
Expand All @@ -534,6 +543,8 @@ func SetUISkeleton(txPoolStatusSupported, zkEVMBatchesSupported bool) (blockList
}
if zkEVMBatchesSupported {
topRowBlocks = append(topRowBlocks, ui.NewCol(1.0/5.0, termUi.ZkEVM))

topRowBlocks = append(topRowBlocks, ui.NewCol(1.0/5.0, termUi.Rollup))
}

termUi.TxPerBlockChart = widgets.NewSparkline()
Expand Down
12 changes: 12 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,18 @@ func GetZkEVMBatches(rpc *ethrpc.Client) (uint64, uint64, uint64, error) {
return trustedBatches, virtualBatches, verifiedBatches, nil
}

func GetForkID(rpc *ethrpc.Client) (uint64, error) {
var raw interface{}
if err := rpc.Call(&raw, "zkevm_getForkId"); err != nil {
return 0, err
}
forkID, err := hexutil.DecodeUint64(fmt.Sprintf("%v", raw))
if err != nil {
return 0, err
}
return forkID, nil
}

type batch string

const (
Expand Down

0 comments on commit d9bd47b

Please sign in to comment.