ethclient: enhance the description of using block number tags #30984 (#1394)

This commit is contained in:
Daniel Liu 2025-08-28 18:59:31 +08:00 committed by GitHub
parent 47520a1316
commit bd1d41d3bc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -86,11 +86,22 @@ func (ec *Client) BlockByHash(ctx context.Context, hash common.Hash) (*types.Blo
return ec.getBlock(ctx, "eth_getBlockByHash", hash, true) return ec.getBlock(ctx, "eth_getBlockByHash", hash, true)
} }
// BlockByNumber returns a block from the current canonical chain. If number is nil, the // BlockByNumber returns a block from the current canonical chain.
// latest known block is returned. // If `number` is nil, the latest known block is returned.
// //
// Note that loading full blocks requires two requests. Use HeaderByNumber // Use `HeaderByNumber` if you don't need full transaction data or uncle headers.
// if you don't need all transactions or uncle headers. //
// Supported special block number tags:
// - `earliest` : The genesis (earliest) block
// - `latest` : The most recently included block
// - `committed` : The latest committed block
// - `pending` : The pending block
//
// Example usage:
//
// ```go
// BlockByNumber(context.Background(), big.NewInt(int64(rpc.LatestBlockNumber)))
// ```
func (ec *Client) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) { func (ec *Client) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) {
return ec.getBlock(ctx, "eth_getBlockByNumber", toBlockNumArg(number), true) return ec.getBlock(ctx, "eth_getBlockByNumber", toBlockNumArg(number), true)
} }
@ -204,8 +215,20 @@ func (ec *Client) HeaderByHash(ctx context.Context, hash common.Hash) (*types.He
return head, err return head, err
} }
// HeaderByNumber returns a block header from the current canonical chain. If number is // HeaderByNumber returns a block header from the current canonical chain.
// nil, the latest known header is returned. // If `number` is nil, the latest known block header is returned.
//
// Supported special block number tags:
// - `earliest` : The genesis (earliest) block
// - `latest` : The most recently included block
// - `committed` : The latest committed block
// - `pending` : The pending block
//
// Example usage:
//
// ```go
// HeaderByNumber(context.Background(), big.NewInt(int64(rpc.LatestBlockNumber)))
// ```
func (ec *Client) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) { func (ec *Client) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
var head *types.Header var head *types.Header
err := ec.c.CallContext(ctx, &head, "eth_getBlockByNumber", toBlockNumArg(number), false) err := ec.c.CallContext(ctx, &head, "eth_getBlockByNumber", toBlockNumArg(number), false)