github.com/ethereum/go-ethereum@v1.16.1/cmd/workload/client.go (about) 1 // Copyright 2025 The go-ethereum Authors 2 // This file is part of go-ethereum. 3 // 4 // go-ethereum is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // go-ethereum is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. 16 17 package main 18 19 import ( 20 "context" 21 "fmt" 22 23 "github.com/ethereum/go-ethereum/common" 24 "github.com/ethereum/go-ethereum/common/hexutil" 25 "github.com/ethereum/go-ethereum/core/types" 26 "github.com/ethereum/go-ethereum/ethclient" 27 "github.com/ethereum/go-ethereum/ethclient/gethclient" 28 "github.com/ethereum/go-ethereum/rpc" 29 "github.com/urfave/cli/v2" 30 ) 31 32 type client struct { 33 Eth *ethclient.Client 34 Geth *gethclient.Client 35 RPC *rpc.Client 36 } 37 38 func makeClient(ctx *cli.Context) *client { 39 if ctx.NArg() < 1 { 40 exit("missing RPC endpoint URL as command-line argument") 41 } 42 url := ctx.Args().First() 43 cl, err := rpc.Dial(url) 44 if err != nil { 45 exit(fmt.Errorf("could not create RPC client at %s: %v", url, err)) 46 } 47 return &client{ 48 RPC: cl, 49 Eth: ethclient.NewClient(cl), 50 Geth: gethclient.New(cl), 51 } 52 } 53 54 type simpleBlock struct { 55 Number hexutil.Uint64 `json:"number"` 56 Hash common.Hash `json:"hash"` 57 } 58 59 type simpleTransaction struct { 60 Hash common.Hash `json:"hash"` 61 TransactionIndex hexutil.Uint64 `json:"transactionIndex"` 62 } 63 64 func (c *client) getBlockByHash(ctx context.Context, arg common.Hash, fullTx bool) (*simpleBlock, error) { 65 var r *simpleBlock 66 err := c.RPC.CallContext(ctx, &r, "eth_getBlockByHash", arg, fullTx) 67 return r, err 68 } 69 70 func (c *client) getBlockByNumber(ctx context.Context, arg uint64, fullTx bool) (*simpleBlock, error) { 71 var r *simpleBlock 72 err := c.RPC.CallContext(ctx, &r, "eth_getBlockByNumber", hexutil.Uint64(arg), fullTx) 73 return r, err 74 } 75 76 func (c *client) getTransactionByBlockHashAndIndex(ctx context.Context, block common.Hash, index uint64) (*simpleTransaction, error) { 77 var r *simpleTransaction 78 err := c.RPC.CallContext(ctx, &r, "eth_getTransactionByBlockHashAndIndex", block, hexutil.Uint64(index)) 79 return r, err 80 } 81 82 func (c *client) getTransactionByBlockNumberAndIndex(ctx context.Context, block uint64, index uint64) (*simpleTransaction, error) { 83 var r *simpleTransaction 84 err := c.RPC.CallContext(ctx, &r, "eth_getTransactionByBlockNumberAndIndex", hexutil.Uint64(block), hexutil.Uint64(index)) 85 return r, err 86 } 87 88 func (c *client) getBlockTransactionCountByHash(ctx context.Context, block common.Hash) (uint64, error) { 89 var r hexutil.Uint64 90 err := c.RPC.CallContext(ctx, &r, "eth_getBlockTransactionCountByHash", block) 91 return uint64(r), err 92 } 93 94 func (c *client) getBlockTransactionCountByNumber(ctx context.Context, block uint64) (uint64, error) { 95 var r hexutil.Uint64 96 err := c.RPC.CallContext(ctx, &r, "eth_getBlockTransactionCountByNumber", hexutil.Uint64(block)) 97 return uint64(r), err 98 } 99 100 func (c *client) getBlockReceipts(ctx context.Context, arg any) ([]*types.Receipt, error) { 101 var result []*types.Receipt 102 err := c.RPC.CallContext(ctx, &result, "eth_getBlockReceipts", arg) 103 return result, err 104 }