From 013b6069a31074caa380235fa58735a8a8f64171 Mon Sep 17 00:00:00 2001 From: NathanBSC Date: Thu, 3 Aug 2023 20:00:28 +0800 Subject: [PATCH] client: add FinalizedHeader/Block to use the fast finality --- ethclient/ethclient.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/ethclient/ethclient.go b/ethclient/ethclient.go index 745a371e6a..4e5db4e840 100644 --- a/ethclient/ethclient.go +++ b/ethclient/ethclient.go @@ -188,6 +188,32 @@ func (ec *Client) HeaderByNumber(ctx context.Context, number *big.Int) (*types.H return head, err } +// GetFinalizedHeader returns the requested finalized block header. +// - probabilisticFinalized should be in range [2,21], +// then the block header with number `max(fastFinalized, latest-probabilisticFinalized)` is returned +func (ec *Client) FinalizedHeader(ctx context.Context, probabilisticFinalized *big.Int) (*types.Header, error) { + var head *types.Header + err := ec.c.CallContext(ctx, &head, "eth_getFinalizedHeader", toBlockNumArg(probabilisticFinalized)) + if err == nil && head == nil { + err = ethereum.NotFound + } + return head, err +} + +// GetFinalizedBlock returns the requested finalized block. +// - probabilisticFinalized should be in range [2,21], +// then the block with number `max(fastFinalized, latest-probabilisticFinalized)` is returned +// - When fullTx is true all transactions in the block are returned, otherwise +// only the transaction hash is returned. +func (ec *Client) FinalizedBlock(ctx context.Context, probabilisticFinalized *big.Int, fullTx bool) (*types.Block, error) { + var block *types.Block + err := ec.c.CallContext(ctx, &block, "eth_getFinalizedBlock", toBlockNumArg(probabilisticFinalized), fullTx) + if err == nil && block == nil { + err = ethereum.NotFound + } + return block, err +} + // GetDiffAccounts returns changed accounts in a specific block number. func (ec *Client) GetDiffAccounts(ctx context.Context, number *big.Int) ([]common.Address, error) { accounts := make([]common.Address, 0)