query

package
v0.1.43 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: AGPL-3.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var QueryCmd = &cobra.Command{
	Use:   "query [enode/enr]",
	Short: "Query block header(s) from node and prints the output.",
	Long: `Query header of single block or range of blocks given a single enode/enr.
	
This command will initially establish a handshake and exchange status message
from the peer. Then, it will query the node for block(s) given the start block
and the amount of blocks to query and print the results.`,
	Args: cobra.MinimumNArgs(1),
	PreRunE: func(cmd *cobra.Command, args []string) error {
		if inputQueryParams.Amount < 1 {
			return fmt.Errorf("amount must be greater than 0")
		}
		return nil
	},
	Run: func(cmd *cobra.Command, args []string) {
		node, err := p2p.ParseNode(args[0])
		if err != nil {
			log.Error().Err(err).Msg("Unable to parse enode")
			return
		}

		var (
			hello  *p2p.Hello
			status *p2p.Status
			start  uint64 = inputQueryParams.StartBlock
			amount uint64 = inputQueryParams.Amount
		)

		conn, err := p2p.Dial(node)
		if err != nil {
			log.Error().Err(err).Msg("Dial failed")
			return
		}
		defer conn.Close()
		if hello, status, err = conn.Peer(); err != nil {
			log.Error().Err(err).Msg("Peer failed")
			return
		}

		log.Info().Interface("hello", hello).Interface("status", status).Msg("Peering messages received")
		log.Info().Uint64("start", start).Uint64("amount", amount).Msg("Requesting headers")

		if err = conn.QueryHeaders(start, amount); err != nil {
			log.Error().Err(err).Msg("Failed to request header(s)")
			return
		}

		headers, err := conn.ListenHeaders()
		if err != nil {
			log.Error().Err(err).Msg("Failed to listen for header(s)")
			return
		}

		if len(headers) != int(amount) {
			log.Error().Uint64("want", amount).Int("have", len(headers)).Msg("Received less headers than requested")
			return
		}

		var (
			headerStart uint64 = headers[0].Number.Uint64()
			headerEnd   uint64 = headers[len(headers)-1].Number.Uint64()
			end         uint64 = start + amount - 1
		)
		if headerStart != start || headerEnd != end {
			log.Error().Uint64("start", start).Uint64("end", end).Uint64("header start", headerStart).Uint64("header end", headerEnd).Msg("Received headers out of range")
			return
		}

		print(headers)
	},
}

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