mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
ethclient: Fix eth_getBlockReceipts call when using block numbers or labels
The String() version of BlockNumberOrHash uses decimal for all block numbers, including negative ones used to indicate labels. Switch to using BlockNumber.String() which encodes it correctly for use in the JSON-RPC API.
This commit is contained in:
parent
2e478aab98
commit
488f52565c
2 changed files with 31 additions and 2 deletions
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -153,3 +153,33 @@ func TestBlockNumberOrHash_WithNumber_MarshalAndUnmarshal(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBlockNumberOrHash_WithNumber_StringAndUnmarshal(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
value BlockNumberOrHash
|
||||
}{
|
||||
{"max", BlockNumberOrHashWithNumber(math.MaxInt64)},
|
||||
{"pending", BlockNumberOrHashWithNumber(PendingBlockNumber)},
|
||||
{"latest", BlockNumberOrHashWithNumber(LatestBlockNumber)},
|
||||
{"earliest", BlockNumberOrHashWithNumber(EarliestBlockNumber)},
|
||||
{"0x20", BlockNumberOrHashWithNumber(32)},
|
||||
{"hash", BlockNumberOrHashWithHash(common.Hash{0xaa}, false)},
|
||||
}
|
||||
for _, test := range tests {
|
||||
test := test
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
bnh := test.value
|
||||
// Wrap the string value in quotes to make it a JSON string.
|
||||
marshalled := []byte("\"" + bnh.String() + "\"")
|
||||
var unmarshalled BlockNumberOrHash
|
||||
err := json.Unmarshal(marshalled, &unmarshalled)
|
||||
if err != nil {
|
||||
t.Fatalf("cannot unmarshal (%v): %v", string(marshalled), err)
|
||||
}
|
||||
if !reflect.DeepEqual(bnh, unmarshalled) {
|
||||
t.Fatalf("wrong result: expected %v, got %v", bnh, unmarshalled)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue