Help us shape the future of Go! Take the 2026 Go Developer Survey

report

package
v0.1.109 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: AGPL-3.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultBlockRange is the default number of blocks to analyze when start/end blocks are not specified
	DefaultBlockRange = 500
	// BlockNotSet is a sentinel value to indicate a block number flag was not set by the user
	BlockNotSet = math.MaxUint64
)

Variables

View Source
var ReportCmd = &cobra.Command{
	Use:   "report",
	Short: "Generate a report analyzing a range of blocks from an Ethereum-compatible blockchain.",
	Long:  usage,
	Args:  cobra.NoArgs,
	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, inputReport.RPCURL)
		if err != nil {
			return fmt.Errorf("failed to connect to RPC: %w", err)
		}
		defer ec.Close()

		// Fetch chain ID
		var chainIDHex string
		err = ec.CallContext(ctx, &chainIDHex, "eth_chainId")
		if err != nil {
			return fmt.Errorf("failed to fetch chain ID: %w", err)
		}
		chainID := hexToUint64(chainIDHex)

		startBlock := inputReport.StartBlock
		endBlock := inputReport.EndBlock

		// Fetch latest block if needed for auto-detection
		var latestBlock uint64
		needsLatest := startBlock == BlockNotSet || endBlock == BlockNotSet
		if needsLatest {
			var latestBlockHex string
			err = ec.CallContext(ctx, &latestBlockHex, "eth_blockNumber")
			if err != nil {
				return fmt.Errorf("failed to fetch latest block number: %w", err)
			}
			latestBlock = hexToUint64(latestBlockHex)
			log.Info().Uint64("latest-block", latestBlock).Msg("Auto-detected latest block")
		}

		if startBlock == BlockNotSet && endBlock == BlockNotSet {

			endBlock = latestBlock
			if latestBlock >= DefaultBlockRange-1 {
				startBlock = latestBlock - (DefaultBlockRange - 1)
			} else {
				startBlock = 0
			}
		} else if startBlock == BlockNotSet {

			if endBlock >= DefaultBlockRange-1 {
				startBlock = endBlock - (DefaultBlockRange - 1)
			} else {
				startBlock = 0
			}
		} else if endBlock == BlockNotSet {

			endBlock = min(startBlock+(DefaultBlockRange-1), latestBlock)
		}

		log.Info().
			Str("rpc-url", inputReport.RPCURL).
			Uint64("start-block", startBlock).
			Uint64("end-block", endBlock).
			Msg("Starting block analysis")

		report := &BlockReport{
			ChainID:     chainID,
			RPCURL:      inputReport.RPCURL,
			StartBlock:  startBlock,
			EndBlock:    endBlock,
			GeneratedAt: time.Now(),
			Blocks:      []BlockInfo{},
		}

		err = generateReport(ctx, ec, report, inputReport.Concurrency, inputReport.RateLimit)
		if err != nil {
			return fmt.Errorf("failed to generate report: %w", err)
		}

		if err := outputReport(ctx, report, inputReport.Format, inputReport.OutputFile); err != nil {
			return fmt.Errorf("failed to output report: %w", err)
		}

		log.Info().Msg("Report generation completed")
		return nil
	},
}

ReportCmd represents the report command

Functions

This section is empty.

Types

type BlockInfo

type BlockInfo struct {
	Number        uint64            `json:"number"`
	Timestamp     uint64            `json:"timestamp"`
	TxCount       uint64            `json:"tx_count"`
	GasUsed       uint64            `json:"gas_used"`
	GasLimit      uint64            `json:"gas_limit"`
	BaseFeePerGas *big.Int          `json:"base_fee_per_gas,omitempty"`
	Transactions  []TransactionInfo `json:"-"` // Internal use only
}

BlockInfo contains information about a single block

type BlockReport

type BlockReport struct {
	ChainID     uint64       `json:"chain_id"`
	RPCURL      string       `json:"rpc_url"`
	StartBlock  uint64       `json:"start_block"`
	EndBlock    uint64       `json:"end_block"`
	GeneratedAt time.Time    `json:"generated_at"`
	Summary     SummaryStats `json:"summary"`
	Top10       Top10Stats   `json:"top_10"`
	Blocks      []BlockInfo  `json:"-"` // Internal use only, not exported
}

BlockReport represents the complete report for a range of blocks

type GasLimitFreq

type GasLimitFreq struct {
	GasLimit uint64 `json:"gas_limit"`
	Count    uint64 `json:"count"`
}

GasLimitFreq represents the frequency of a specific gas limit

type GasPriceFreq

type GasPriceFreq struct {
	GasPrice uint64 `json:"gas_price"`
	Count    uint64 `json:"count"`
}

GasPriceFreq represents the frequency of a specific gas price

type SummaryStats

type SummaryStats struct {
	TotalBlocks       uint64  `json:"total_blocks"`
	TotalTransactions uint64  `json:"total_transactions"`
	TotalGasUsed      uint64  `json:"total_gas_used"`
	AvgTxPerBlock     float64 `json:"avg_tx_per_block"`
	AvgGasPerBlock    float64 `json:"avg_gas_per_block"`
	AvgBaseFeePerGas  string  `json:"avg_base_fee_per_gas,omitempty"`
	UniqueSenders     uint64  `json:"unique_senders"`
	UniqueRecipients  uint64  `json:"unique_recipients"`
}

SummaryStats contains aggregate statistics for the block range

type Top10Stats

type Top10Stats struct {
	BlocksByTxCount        []TopBlock       `json:"blocks_by_tx_count"`
	BlocksByGasUsed        []TopBlock       `json:"blocks_by_gas_used"`
	TransactionsByGas      []TopTransaction `json:"transactions_by_gas"`
	TransactionsByGasLimit []TopTransaction `json:"transactions_by_gas_limit"`
	MostUsedGasPrices      []GasPriceFreq   `json:"most_used_gas_prices"`
	MostUsedGasLimits      []GasLimitFreq   `json:"most_used_gas_limits"`
}

Top10Stats contains top 10 lists for various metrics

type TopBlock

type TopBlock struct {
	Number         uint64  `json:"number"`
	TxCount        uint64  `json:"tx_count,omitempty"`
	GasUsed        uint64  `json:"gas_used,omitempty"`
	GasLimit       uint64  `json:"gas_limit,omitempty"`
	GasUsedPercent float64 `json:"gas_used_percent,omitempty"`
}

TopBlock represents a block in a top 10 list

type TopTransaction

type TopTransaction struct {
	Hash           string  `json:"hash"`
	BlockNumber    uint64  `json:"block_number"`
	GasUsed        uint64  `json:"gas_used,omitempty"`
	GasLimit       uint64  `json:"gas_limit,omitempty"`
	BlockGasLimit  uint64  `json:"block_gas_limit,omitempty"`
	GasUsedPercent float64 `json:"gas_used_percent,omitempty"`
}

TopTransaction represents a transaction in a top 10 list

type TransactionInfo

type TransactionInfo struct {
	Hash           string  `json:"hash"`
	From           string  `json:"from"`
	To             string  `json:"to"`
	BlockNumber    uint64  `json:"block_number"`
	GasUsed        uint64  `json:"gas_used"`
	GasLimit       uint64  `json:"gas_limit"`
	GasPrice       uint64  `json:"gas_price"`
	BlockGasLimit  uint64  `json:"block_gas_limit"`
	GasUsedPercent float64 `json:"gas_used_percent"`
}

TransactionInfo contains information about a single transaction

Jump to

Keyboard shortcuts

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