tail

package
v0.1.114 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var TailCmd = &cobra.Command{
	Use:   "tail",
	Short: "Tail full blocks from a JSON-RPC endpoint as NDJSON.",
	Long:  usage,
	Args:  cobra.NoArgs,
	PreRunE: func(cmd *cobra.Command, _ []string) (err error) {

		verbosityFlag := cmd.Flag("verbosity")
		if verbosityFlag != nil && !verbosityFlag.Changed {
			util.SetLogLevel(300)
		}

		inputTail.RPCURL, err = flag.GetRPCURL(cmd)
		if err != nil {
			return err
		}
		if inputTail.BatchSize == 0 {
			return fmt.Errorf("batch-size must be greater than 0")
		}
		if inputTail.BlocksBack == 0 {
			return fmt.Errorf("blocks-back must be greater than 0")
		}
		if inputTail.PollInterval <= 0 {
			return fmt.Errorf("poll-interval must be greater than 0")
		}
		return nil
	},
	RunE: func(cmd *cobra.Command, _ []string) error {
		ctx := cmd.Context()
		ec, err := ethrpc.DialContext(ctx, inputTail.RPCURL)
		if err != nil {
			return err
		}

		latestBlock, err := getLatestBlockNumber(ctx, ec)
		if err != nil {
			return err
		}

		startBlock := uint64(0)
		if latestBlock+1 > inputTail.BlocksBack {
			startBlock = latestBlock - inputTail.BlocksBack + 1
		}

		nextBlock := startBlock
		log.Info().
			Uint64("latest", latestBlock).
			Uint64("start", startBlock).
			Bool("follow", inputTail.Follow).
			Msg("Starting tail")

		for {
			latestBlock, err = getLatestBlockNumber(ctx, ec)
			if err != nil {
				if !inputTail.Follow {
					return err
				}
				log.Warn().Err(err).Msg("Unable to fetch latest block number; retrying")
			} else if nextBlock <= latestBlock {
				if err := writeBlockRange(ctx, ec, nextBlock, latestBlock); err != nil {
					if !inputTail.Follow {
						return err
					}
					log.Warn().Err(err).Msg("Unable to fetch block range; retrying")
				} else {
					nextBlock = latestBlock + 1
				}
			}

			if !inputTail.Follow {
				return nil
			}

			select {
			case <-ctx.Done():
				return ctx.Err()
			case <-time.After(inputTail.PollInterval):
			}
		}
	},
}

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