Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
marun committed Dec 19, 2023
1 parent 62d29c8 commit ea837ea
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
3 changes: 1 addition & 2 deletions rpc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"encoding/json"
"fmt"
"math"
"strconv"
"strings"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -238,7 +237,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()
Expand Down
21 changes: 21 additions & 0 deletions rpc/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,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)
}
}
}

0 comments on commit ea837ea

Please sign in to comment.