From ca058b7a69938668f0423c7e2a4d4b395588927e Mon Sep 17 00:00:00 2001 From: Adrian Sutton Date: Wed, 25 Oct 2023 23:52:42 +1000 Subject: [PATCH] rpc: use correct stringer-method for serializing BlockNumberOrHash (#28358) Switch to using BlockNumber.String() which encodes it correctly for use in the JSON-RPC API. --- internal/ethapi/api_test.go | 2 +- rpc/types.go | 3 +-- rpc/types_test.go | 21 +++++++++++++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/internal/ethapi/api_test.go b/internal/ethapi/api_test.go index cbf9b6354..898f92855 100644 --- a/internal/ethapi/api_test.go +++ b/internal/ethapi/api_test.go @@ -1809,7 +1809,7 @@ func setupReceiptBackend(t *testing.T, genBlocks int) (*testBackend, []common.Ha } signer = types.LatestSignerForChainID(params.TestChainConfig.ChainID) txHashes = make([]common.Hash, genBlocks) - gasPrice = big.NewInt(3e9) // 3Gwei + gasPrice = big.NewInt(3e9) // 3Gwei ) backend := newTestBackend(t, genBlocks, genesis, func(i int, b *core.BlockGen) { var ( diff --git a/rpc/types.go b/rpc/types.go index 34a1451de..f88c37c59 100644 --- a/rpc/types.go +++ b/rpc/types.go @@ -21,7 +21,6 @@ import ( "encoding/json" "fmt" "math" - "strconv" "strings" "github.com/ethereum/go-ethereum/common" @@ -221,7 +220,7 @@ func (bnh *BlockNumberOrHash) Number() (BlockNumber, bool) { func (bnh *BlockNumberOrHash) String() string { if bnh.BlockNumber != nil { - return strconv.Itoa(int(*bnh.BlockNumber)) + return bnh.BlockNumber.String() } if bnh.BlockHash != nil { return bnh.BlockHash.String() diff --git a/rpc/types_test.go b/rpc/types_test.go index f110dee7c..617f441d9 100644 --- a/rpc/types_test.go +++ b/rpc/types_test.go @@ -153,3 +153,24 @@ func TestBlockNumberOrHash_WithNumber_MarshalAndUnmarshal(t *testing.T) { }) } } + +func TestBlockNumberOrHash_StringAndUnmarshal(t *testing.T) { + tests := []BlockNumberOrHash{ + BlockNumberOrHashWithNumber(math.MaxInt64), + BlockNumberOrHashWithNumber(PendingBlockNumber), + BlockNumberOrHashWithNumber(LatestBlockNumber), + BlockNumberOrHashWithNumber(EarliestBlockNumber), + BlockNumberOrHashWithNumber(32), + BlockNumberOrHashWithHash(common.Hash{0xaa}, false), + } + for _, want := range tests { + marshalled, _ := json.Marshal(want.String()) + var have BlockNumberOrHash + if err := json.Unmarshal(marshalled, &have); err != nil { + t.Fatalf("cannot unmarshal (%v): %v", string(marshalled), err) + } + if !reflect.DeepEqual(want, have) { + t.Fatalf("wrong result: have %v, want %v", have, want) + } + } +}