From 7f02ff81f35895182252668f5db87b431202d683 Mon Sep 17 00:00:00 2001 From: Igor Mandrigin <mandrigin@users.noreply.github.com> Date: Fri, 20 May 2022 11:04:24 +0200 Subject: [PATCH] Fix `rpc.BlockNumberOrHash` unmarshaling (#4218) * add test * fix unmarshaling bug Co-authored-by: Igor Mandrigin <i@mandrigin.ru> --- rpc/types.go | 3 +++ rpc/types_test.go | 2 ++ 2 files changed, 5 insertions(+) diff --git a/rpc/types.go b/rpc/types.go index 86652f023b..d6ddd664c3 100644 --- a/rpc/types.go +++ b/rpc/types.go @@ -136,6 +136,9 @@ func (bnh *BlockNumberOrHash) UnmarshalJSON(data []byte) error { if e.BlockNumber != nil && e.BlockHash != nil { return fmt.Errorf("cannot specify both BlockHash and BlockNumber, choose one or the other") } + if e.BlockNumber == nil && e.BlockHash == nil { + return fmt.Errorf("at least one of BlockNumber or BlockHash is needed if a dictionary is provided") + } bnh.BlockNumber = e.BlockNumber bnh.BlockHash = e.BlockHash bnh.RequireCanonical = e.RequireCanonical diff --git a/rpc/types_test.go b/rpc/types_test.go index 6b204d101f..a5ce42daac 100644 --- a/rpc/types_test.go +++ b/rpc/types_test.go @@ -98,6 +98,8 @@ func TestBlockNumberOrHash_UnmarshalJSON(t *testing.T) { 23: {`{"blockNumber":"latest"}`, false, BlockNumberOrHashWithNumber(LatestBlockNumber)}, 24: {`{"blockNumber":"earliest"}`, false, BlockNumberOrHashWithNumber(EarliestBlockNumber)}, 25: {`{"blockNumber":"0x1", "blockHash":"0x0000000000000000000000000000000000000000000000000000000000000000"}`, true, BlockNumberOrHash{}}, + 26: {`{}`, true, BlockNumberOrHash{}}, + 27: {`{"jsonrpc":"2.0","result":{"code":418,"message":"blabla"},"id":""}]`, true, BlockNumberOrHash{}}, } for i, test := range tests { -- GitLab