mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-19 21:31:37 +00:00
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. Co-authored-by: Adrian Sutton <adrian@oplabs.co>
This commit is contained in:
parent
aa75ed2f6b
commit
7422a13f24
2 changed files with 31 additions and 0 deletions
10
rpc/types.go
10
rpc/types.go
|
|
@ -235,6 +235,16 @@ func (bnh *BlockNumberOrHash) Number() (BlockNumber, bool) {
|
|||
return BlockNumber(0), false
|
||||
}
|
||||
|
||||
func (bnh *BlockNumberOrHash) String() string {
|
||||
if bnh.BlockNumber != nil {
|
||||
return bnh.BlockNumber.String()
|
||||
}
|
||||
if bnh.BlockHash != nil {
|
||||
return bnh.BlockHash.String()
|
||||
}
|
||||
return "nil"
|
||||
}
|
||||
|
||||
func (bnh *BlockNumberOrHash) Hash() (common.Hash, bool) {
|
||||
if bnh.BlockHash != nil {
|
||||
return *bnh.BlockHash, true
|
||||
|
|
|
|||
|
|
@ -155,3 +155,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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue