monitor

package
v0.1.27 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 21, 2023 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Overview

Copyright © 2022 Polygon <engineering@polygon.technology>

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

Index

Constants

This section is empty.

Variables

View Source
var MonitorCmd = &cobra.Command{
	Use:   "monitor [rpc-url]",
	Short: "A simple terminal monitor for a blockchain",
	Args:  cobra.MinimumNArgs(1),
	PreRunE: func(cmd *cobra.Command, args []string) error {

		_, err := url.Parse(args[0])
		if err != nil {
			return err
		}

		if batchSize == 0 {
			return fmt.Errorf("batch-size can't be equal to zero")
		}

		if windowSize <= 0 {
			return fmt.Errorf("window-size must be greater than zero")
		}

		if interval, err = time.ParseDuration(intervalStr); err != nil {
			return err
		}

		return nil
	},
	RunE: func(cmd *cobra.Command, args []string) error {
		ctx := cmd.Context()

		rpc, err := ethrpc.DialContext(ctx, args[0])
		if err != nil {
			log.Error().Err(err).Msg("Unable to dial rpc")
			return err
		}
		ec := ethclient.NewClient(rpc)

		ms := new(monitorStatus)

		ms.MaxBlockRetrieved = big.NewInt(0)
		ms.BlocksLock.Lock()
		ms.Blocks = make(map[string]rpctypes.PolyBlock, 0)
		ms.BlocksLock.Unlock()
		ms.ChainID = big.NewInt(0)

		isUiRendered := false
		errChan := make(chan error)
		go func() {
			for {
				var cs *chainState
				cs, err = getChainState(ctx, ec)
				if err != nil {
					log.Error().Err(err).Msg("Encountered issue fetching network information")
					time.Sleep(interval)
					continue
				}

				ms.HeadBlock = new(big.Int).SetUint64(cs.HeadBlock)
				ms.ChainID = cs.ChainID
				ms.PeerCount = cs.PeerCount
				ms.GasPrice = cs.GasPrice
				batchSize := new(big.Int).SetUint64(batchSize - 1)
				from := new(big.Int).Sub(ms.HeadBlock, batchSize)

				if ms.MaxBlockRetrieved.Cmp(from) == 1 {
					from.Add(ms.MaxBlockRetrieved, big.NewInt(1))
				}

				if from.Cmp(zero) < 0 {
					from.SetInt64(0)
				}

				log.Debug().
					Int64("from", from.Int64()).
					Int64("to", ms.HeadBlock.Int64()).
					Int64("max", ms.MaxBlockRetrieved.Int64()).
					Msg("Fetching latest blocks")

				err = ms.getBlockRange(ctx, from, ms.HeadBlock, rpc)
				if err != nil {
					log.Error().Err(err).Msg("There was an issue fetching the block range")
				}

				to := new(big.Int).Sub(ms.MinBlockRetrieved, one)
				if from = new(big.Int).Sub(to, batchSize); from.Cmp(zero) < 0 {
					from.SetInt64(0)
				}

				log.Debug().
					Int64("from", from.Int64()).
					Int64("to", to.Int64()).
					Int64("min", ms.MinBlockRetrieved.Int64()).
					Msg("Fetching older blocks")

				err = ms.getBlockRange(ctx, from, to, rpc)
				if err != nil {
					log.Error().Err(err).Msg("There was an issue fetching the block range")
				}

				if !isUiRendered {
					go func() {
						errChan <- renderMonitorUI(ms)
					}()
					isUiRendered = true
				}

				time.Sleep(interval)
			}
		}()

		err = <-errChan
		return err
	},
}

monitorCmd represents the monitor command

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL