|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "math/big" |
| 9 | + |
| 10 | + "github.com/ethereum/go-ethereum/ethclient" |
| 11 | +) |
| 12 | + |
| 13 | +func main() { |
| 14 | + // Define command-line flags. |
| 15 | + rpcURL := flag.String("rpc", "", "RPC URL of Ethereum node") |
| 16 | + numBlocks := flag.Int("blocks", 0, "Number of blocks to fetch") |
| 17 | + flag.Parse() |
| 18 | + |
| 19 | + if *rpcURL == "" || *numBlocks <= 0 { |
| 20 | + flag.Usage() |
| 21 | + return |
| 22 | + } |
| 23 | + |
| 24 | + // Connect to the Ethereum node. |
| 25 | + client, err := ethclient.Dial(*rpcURL) |
| 26 | + if err != nil { |
| 27 | + log.Fatalf("Failed to connect to the Ethereum client: %v", err) |
| 28 | + } |
| 29 | + |
| 30 | + // Retrieve the latest block header. |
| 31 | + latestHeader, err := client.HeaderByNumber(context.Background(), nil) |
| 32 | + if err != nil { |
| 33 | + log.Fatalf("Failed to get the latest block header: %v", err) |
| 34 | + } |
| 35 | + latestBlockNumber := latestHeader.Number |
| 36 | + |
| 37 | + var totalTxs int |
| 38 | + var firstBlockTimestamp uint64 // timestamp of the latest block (first fetched) |
| 39 | + var lastBlockTimestamp uint64 // timestamp of the oldest block in our fetched list |
| 40 | + |
| 41 | + // Fetch the latest block first. |
| 42 | + block, err := client.BlockByNumber(context.Background(), latestBlockNumber) |
| 43 | + if err != nil { |
| 44 | + log.Fatalf("Failed to get block %v: %v", latestBlockNumber, err) |
| 45 | + } |
| 46 | + firstBlockTimestamp = block.Time() // assumed to be in milliseconds |
| 47 | + totalTxs += len(block.Transactions()) |
| 48 | + |
| 49 | + // Iterate to fetch older blocks. |
| 50 | + // Starting from i=1 because the latest block is already fetched. |
| 51 | + for i := 1; i < *numBlocks; i++ { |
| 52 | + blockNumber := new(big.Int).Sub(latestBlockNumber, big.NewInt(int64(i))) |
| 53 | + block, err := client.BlockByNumber(context.Background(), blockNumber) |
| 54 | + if err != nil { |
| 55 | + log.Fatalf("Failed to get block %v: %v", blockNumber, err) |
| 56 | + } |
| 57 | + totalTxs += len(block.Transactions()) |
| 58 | + // Keep updating lastBlockTimestamp; after loop, it will hold the oldest block's timestamp. |
| 59 | + lastBlockTimestamp = block.Time() |
| 60 | + } |
| 61 | + |
| 62 | + // If only one block is fetched, assign lastBlockTimestamp to firstBlockTimestamp. |
| 63 | + if *numBlocks == 1 { |
| 64 | + lastBlockTimestamp = firstBlockTimestamp |
| 65 | + } |
| 66 | + |
| 67 | + // Calculate the overall time interval between the first and last block. |
| 68 | + // Note: the latest block is expected to have a larger timestamp. |
| 69 | + var timeInterval uint64 |
| 70 | + if firstBlockTimestamp > lastBlockTimestamp { |
| 71 | + timeInterval = firstBlockTimestamp - lastBlockTimestamp |
| 72 | + } else { |
| 73 | + timeInterval = lastBlockTimestamp - firstBlockTimestamp |
| 74 | + } |
| 75 | + |
| 76 | + // Convert interval from milliseconds to seconds. |
| 77 | + timeIntervalSeconds := float64(timeInterval) / 1000.0 |
| 78 | + |
| 79 | + // Calculate TPS (transactions per second) |
| 80 | + var tps float64 |
| 81 | + if timeIntervalSeconds > 0 { |
| 82 | + tps = float64(totalTxs) / timeIntervalSeconds |
| 83 | + } else { |
| 84 | + tps = 0 |
| 85 | + } |
| 86 | + |
| 87 | + fmt.Printf("Fetched %d blocks (from block %s to %s)\n", *numBlocks, |
| 88 | + new(big.Int).Sub(latestBlockNumber, big.NewInt(int64(*numBlocks-1))).String(), |
| 89 | + latestBlockNumber.String()) |
| 90 | + fmt.Printf("Total transactions: %d\n", totalTxs) |
| 91 | + fmt.Printf("Time interval between first (latest) and last (oldest) block: %d ms (%.2f seconds)\n", timeInterval, timeIntervalSeconds) |
| 92 | + fmt.Printf("TPS (transactions per second): %.2f\n", tps) |
| 93 | +} |
0 commit comments