diff --git a/cmd/monitor/block.go b/cmd/monitor/block.go new file mode 100644 index 00000000..d443cee7 --- /dev/null +++ b/cmd/monitor/block.go @@ -0,0 +1 @@ +package monitor diff --git a/cmd/monitor/cmd.go b/cmd/monitor/cmd.go index 2f57cd04..f1166c5e 100644 --- a/cmd/monitor/cmd.go +++ b/cmd/monitor/cmd.go @@ -15,9 +15,10 @@ var ( usage string // flags - rpcUrl string - batchSizeValue string - intervalStr string + rpcUrl string + batchSizeValue string + blockCacheLimit int + intervalStr string ) // MonitorCmd represents the monitor command @@ -37,6 +38,7 @@ var MonitorCmd = &cobra.Command{ func init() { MonitorCmd.PersistentFlags().StringVarP(&rpcUrl, "rpc-url", "r", "http://localhost:8545", "The RPC endpoint url") MonitorCmd.PersistentFlags().StringVarP(&batchSizeValue, "batch-size", "b", "auto", "Number of requests per batch") + MonitorCmd.PersistentFlags().IntVarP(&blockCacheLimit, "cache-limit", "c", 50, "Number of cached blocks for the LRU block data structure") MonitorCmd.PersistentFlags().StringVarP(&intervalStr, "interval", "i", "5s", "Amount of time between batch block rpc calls") } @@ -66,5 +68,10 @@ func checkFlags() (err error) { } } + // Check batch-size flag. + if blockCacheLimit < 50 { + return fmt.Errorf("block-cache can't be less than 50") + } + return nil } diff --git a/cmd/monitor/monitor.go b/cmd/monitor/monitor.go index 7d793143..1079aff2 100644 --- a/cmd/monitor/monitor.go +++ b/cmd/monitor/monitor.go @@ -90,7 +90,7 @@ func monitor(ctx context.Context) error { ec := ethclient.NewClient(rpc) ms := new(monitorStatus) - ms.BlockCache, _ = lru.New(100) + ms.BlockCache, _ = lru.New(blockCacheLimit) ms.MaxBlockRetrieved = big.NewInt(0) ms.ChainID = big.NewInt(0) @@ -193,8 +193,6 @@ func prependLatestBlocks(ctx context.Context, ms *monitorStatus, rpc *ethrpc.Cli } } -const maxHistoricalPoints = 100 // set a limit to the number of historical points - func fetchBlocks(ctx context.Context, ec *ethclient.Client, ms *monitorStatus, rpc *ethrpc.Client, isUiRendered bool) (err error) { var cs *chainState cs, err = getChainState(ctx, ec) @@ -203,10 +201,6 @@ func fetchBlocks(ctx context.Context, ec *ethclient.Client, ms *monitorStatus, r time.Sleep(interval) return err } - if len(observedPendingTxs) >= maxHistoricalPoints { - // remove the oldest data point - observedPendingTxs = observedPendingTxs[1:] - } observedPendingTxs = append(observedPendingTxs, historicalDataPoint{SampleTime: time.Now(), SampleValue: float64(cs.PendingCount)}) log.Debug().Uint64("PeerCount", cs.PeerCount).Uint64("ChainID", cs.ChainID.Uint64()).Uint64("HeadBlock", cs.HeadBlock).Uint64("GasPrice", cs.GasPrice.Uint64()).Msg("Fetching blocks")