dumpblocks

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: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DumpblocksCmd = &cobra.Command{
	Use:   "dumpblocks start end",
	Short: "Export a range of blocks from a JSON-RPC endpoint.",
	Long:  usage,
	PreRunE: func(cmd *cobra.Command, args []string) error {
		return checkFlags()
	},
	RunE: func(cmd *cobra.Command, args []string) error {
		ctx := cmd.Context()
		ec, err := ethrpc.DialContext(ctx, inputDumpblocks.RpcUrl)
		if err != nil {
			return err
		}

		var wg sync.WaitGroup
		log.Info().Uint("thread", inputDumpblocks.Threads).Msg("Thread count")
		var pool = make(chan bool, inputDumpblocks.Threads)
		start := inputDumpblocks.Start
		end := inputDumpblocks.End

		for start < end {
			rangeStart := start
			rangeEnd := rangeStart + inputDumpblocks.BatchSize

			if rangeEnd > end {
				rangeEnd = end
			}

			pool <- true
			wg.Add(1)
			log.Info().Uint64("start", rangeStart).Uint64("end", rangeEnd).Msg("Getting range")
			go func() {
				defer wg.Done()
				for {
					failCount := 0
					blocks, err := util.GetBlockRange(ctx, rangeStart, rangeEnd, ec)
					if err != nil {
						failCount = failCount + 1
						if failCount > 5 {
							log.Error().Uint64("rangeStart", rangeStart).Uint64("rangeEnd", rangeEnd).Msg("Unable to fetch blocks")
							break
						}
						time.Sleep(5 * time.Second)
						continue
					}

					blocks = filterBlocks(blocks)

					if inputDumpblocks.ShouldDumpBlocks {
						err = writeResponses(blocks, "block")
						if err != nil {
							log.Error().Err(err).Msg("Error writing blocks")
						}
					}

					if inputDumpblocks.ShouldDumpReceipts {
						failCount = 0
						receipts, err := util.GetReceipts(ctx, blocks, ec, inputDumpblocks.BatchSize)
						if err != nil {
							failCount = failCount + 1
							if failCount > 5 {
								log.Error().Uint64("rangeStart", rangeStart).Uint64("rangeEnd", rangeEnd).Msg("Unable to fetch receipts")
								break
							}
							time.Sleep(5 * time.Second)
							continue
						}

						err = writeResponses(receipts, "transaction")
						if err != nil {
							log.Error().Err(err).Msg("Error writing receipts")
						}
					}

					break
				}
				<-pool
			}()
			start = rangeEnd
		}

		log.Info().Msg("Finished requesting data starting to wait")
		wg.Wait()
		log.Info().Msg("Done")

		return nil
	},
	Args: func(cmd *cobra.Command, args []string) error {
		if len(args) < 2 {
			return fmt.Errorf("command needs at least two arguments. A start block and an end block")
		}

		start, err := strconv.ParseInt(args[0], 10, 64)
		if err != nil {
			return err
		}
		end, err := strconv.ParseInt(args[1], 10, 64)
		if err != nil {
			return err
		}
		if start < 0 || end < 0 {
			return fmt.Errorf("the start and end parameters need to be positive")
		}
		if end < start {
			start, end = end, start
		}

		inputDumpblocks.Start = uint64(start)
		inputDumpblocks.End = uint64(end)

		if inputDumpblocks.Threads == 0 {
			inputDumpblocks.Threads = 1
		}
		if !slices.Contains([]string{"json", "proto"}, inputDumpblocks.Mode) {
			return fmt.Errorf("output format must one of [json, proto]")
		}

		if err := json.Unmarshal([]byte(inputDumpblocks.FilterStr), &inputDumpblocks.filter); err != nil {
			return fmt.Errorf("could not unmarshal filter string")
		}

		for i := 0; i < len(inputDumpblocks.filter.To); i++ {
			inputDumpblocks.filter.To[i] = strings.ToLower(inputDumpblocks.filter.To[i])
		}
		for i := 0; i < len(inputDumpblocks.filter.From); i++ {
			inputDumpblocks.filter.From[i] = strings.ToLower(inputDumpblocks.filter.From[i])
		}

		return nil
	},
}

dumpblocksCmd represents the dumpblocks command

Functions

This section is empty.

Types

type Filter added in v0.1.17

type Filter struct {
	To   []string `json:"to"`
	From []string `json:"from"`
}

Jump to

Keyboard shortcuts

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