From cf2a77af28e58359f3ec1c0376c3b834f9ad8fb5 Mon Sep 17 00:00:00 2001 From: Mason Fischer Date: Tue, 15 Sep 2020 05:29:51 -0400 Subject: [PATCH] ethclient: fix BlockNumber (#21565) It didn't actually work because it called a method that doesn't exist. This fixes it also adds a test. Co-authored-by: Felix Lange --- ethclient/ethclient.go | 2 +- ethclient/ethclient_test.go | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/ethclient/ethclient.go b/ethclient/ethclient.go index a964bd0f8d..8dc34a835e 100644 --- a/ethclient/ethclient.go +++ b/ethclient/ethclient.go @@ -91,7 +91,7 @@ func (ec *Client) BlockByNumber(ctx context.Context, number *big.Int) (*types.Bl // BlockNumber returns the most recent block number func (ec *Client) BlockNumber(ctx context.Context) (uint64, error) { var result hexutil.Uint64 - err := ec.c.CallContext(ctx, &result, "eth_getBlockNumber", nil) + err := ec.c.CallContext(ctx, &result, "eth_blockNumber") return uint64(result), err } diff --git a/ethclient/ethclient_test.go b/ethclient/ethclient_test.go index 16cf5ce61c..0ca72c6ee7 100644 --- a/ethclient/ethclient_test.go +++ b/ethclient/ethclient_test.go @@ -352,3 +352,19 @@ func TestChainID(t *testing.T) { t.Fatalf("ChainID returned wrong number: %+v", id) } } + +func TestBlockNumber(t *testing.T) { + backend, _ := newTestBackend(t) + client, _ := backend.Attach() + defer backend.Close() + defer client.Close() + ec := NewClient(client) + + blockNumber, err := ec.BlockNumber(context.Background()) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if blockNumber != 1 { + t.Fatalf("BlockNumber returned wrong number: %d", blockNumber) + } +}