Skip to content

Commit

Permalink
reuse math.HexOrDecimal64
Browse files Browse the repository at this point in the history
  • Loading branch information
ganeshvanahalli committed Aug 31, 2023
1 parent f8363dc commit b4bd0da
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 112 deletions.
9 changes: 5 additions & 4 deletions arbitrum_types/txoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/rpc"
"github.com/pkg/errors"
Expand Down Expand Up @@ -69,10 +70,10 @@ func (r RootHashOrSlots) MarshalJSON() ([]byte, error) {

type ConditionalOptions struct {
KnownAccounts map[common.Address]RootHashOrSlots `json:"knownAccounts"`
BlockNumberMin *common.Uint64OrHex `json:"blockNumberMin,omitempty"`
BlockNumberMax *common.Uint64OrHex `json:"blockNumberMax,omitempty"`
TimestampMin *common.Uint64OrHex `json:"timestampMin,omitempty"`
TimestampMax *common.Uint64OrHex `json:"timestampMax,omitempty"`
BlockNumberMin *math.HexOrDecimal64 `json:"blockNumberMin,omitempty"`
BlockNumberMax *math.HexOrDecimal64 `json:"blockNumberMax,omitempty"`
TimestampMin *math.HexOrDecimal64 `json:"timestampMin,omitempty"`
TimestampMax *math.HexOrDecimal64 `json:"timestampMax,omitempty"`
}

func (o *ConditionalOptions) Check(l1BlockNumber uint64, l2Timestamp uint64, statedb *state.StateDB) error {
Expand Down
76 changes: 76 additions & 0 deletions common/math/integer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package math

import (
"encoding/json"
"testing"
)

Expand Down Expand Up @@ -114,3 +115,78 @@ func TestMustParseUint64Panic(t *testing.T) {
}()
MustParseUint64("ggg")
}

type marshalUnMarshalTest struct {
input interface{}
want interface{}
wantErr bool // if true, decoding must fail on any platform
}

var (
marshalHexOrDecimal64Tests = []marshalUnMarshalTest{
{input: uint64(0), want: "0x0"},
{input: uint64(1), want: "0x1"},
{input: uint64(16), want: "0x10"},
{input: uint64(255), want: "0xff"},
{input: uint64(0xff), want: "0xff"},
{input: uint64(0x1122334455667788), want: "0x1122334455667788"},
}

UnMarshalHexOrDecimal64Tests = []marshalUnMarshalTest{
// invalid encoding
{input: "", wantErr: true},
{input: "null", wantErr: true},
{input: `"0x"`, wantErr: true},
{input: `"0xfffffffffffffffff"`, wantErr: true},
{input: `"1ab"`, wantErr: true},
{input: `"0xx"`, wantErr: true},
{input: `"0x1zz01"`, wantErr: true},

// valid encoding
{input: `""`, want: uint64(0)},
{input: `"0"`, want: uint64(0)},
{input: `"10"`, want: uint64(10)},
{input: `"100"`, want: uint64(100)},
{input: `"12344678"`, want: uint64(12344678)},
{input: `"1111111111111111"`, want: uint64(1111111111111111)},
{input: `"0x0"`, want: uint64(0)},
{input: `"0x2"`, want: uint64(0x2)},
{input: `"0x2F2"`, want: uint64(0x2f2)},
{input: `"0x1122aaff"`, want: uint64(0x1122aaff)},
{input: `"0xbbb"`, want: uint64(0xbbb)},
{input: `"0xffffffffffffffff"`, want: uint64(0xffffffffffffffff)},
}
)

func TestMarshalHexOrDecimal64(t *testing.T) {
for _, test := range marshalHexOrDecimal64Tests {
in := test.input.(uint64)
out, err := json.Marshal(HexOrDecimal64(in))
if err != nil {
t.Errorf("%d: %v", in, err)
continue
}
if want := `"` + test.want.(string) + `"`; string(out) != want {
t.Errorf("%d: MarshalJSON output mismatch: got %q, want %q", in, out, want)
continue
}
}
}

func TestUnMarshalHexOrDecimal64(t *testing.T) {
for _, test := range UnMarshalHexOrDecimal64Tests {
var v HexOrDecimal64
err := json.Unmarshal([]byte(test.input.(string)), &v)
if test.wantErr {
if err == nil {
t.Errorf("%s: UnMarshalJSON did not error on invalid encoding: got %q, want <nil>", test.input, err)
}
continue
}

if uint64(v) != test.want.(uint64) {
t.Errorf("input %s: value mismatch: got %d, want %d", test.input, v, test.want)
continue
}
}
}
30 changes: 0 additions & 30 deletions common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,33 +462,3 @@ func (d *Decimal) UnmarshalJSON(input []byte) error {
return err
}
}

// Uint64OrHex marshals as a JSON string with 0x prefix.
// And unmarshals both hex and decimal to uint64.
type Uint64OrHex uint64

// MarshalText implements encoding.TextMarshaler.
func (b Uint64OrHex) MarshalText() ([]byte, error) {
buf := make([]byte, 2, 10)
copy(buf, `0x`)
buf = strconv.AppendUint(buf, uint64(b), 16)
return buf, nil
}

// UnmarshalJSON implements json.Unmarshaler.
func (b *Uint64OrHex) UnmarshalJSON(input []byte) error {
var decVal Decimal
err := decVal.UnmarshalJSON(input)
if err == nil {
*b = Uint64OrHex(decVal)
return nil
}

var hexVal hexutil.Uint64
err = hexVal.UnmarshalJSON(input)
if err == nil {
*b = Uint64OrHex(hexVal)
return nil
}
return errors.New("json: cannot unmarshal the given input into Go value of type common.Uint64OrHex")
}
78 changes: 0 additions & 78 deletions common/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,81 +583,3 @@ func TestAddressEIP55(t *testing.T) {
t.Fatal("Unexpected address after unmarshal")
}
}

type marshalUnMarshalTest struct {
input interface{}
want interface{}
wantErr bool // if true, decoding must fail on any platform
}

var (
marshalUint64OrHexTests = []marshalUnMarshalTest{
{input: uint64(0), want: "0x0"},
{input: uint64(1), want: "0x1"},
{input: uint64(16), want: "0x10"},
{input: uint64(255), want: "0xff"},
{input: uint64(0xff), want: "0xff"},
{input: uint64(0x1122334455667788), want: "0x1122334455667788"},
}

UnMarshalUint64OrHexTests = []marshalUnMarshalTest{
// invalid encoding
{input: "", wantErr: true},
{input: "null", wantErr: true},
{input: `"0x"`, wantErr: true},
{input: `"0x01"`, wantErr: true},
{input: `"0xfffffffffffffffff"`, wantErr: true},
{input: `"11111111111111111111"`, wantErr: true},
{input: `"1ab"`, wantErr: true},
{input: `"0xx"`, wantErr: true},
{input: `"0x1zz01"`, wantErr: true},

// valid encoding
{input: `""`, want: uint64(0)},
{input: `"0"`, want: uint64(0)},
{input: `"10"`, want: uint64(10)},
{input: `"100"`, want: uint64(100)},
{input: `"12344678"`, want: uint64(12344678)},
{input: `"1111111111111111"`, want: uint64(1111111111111111)},
{input: `"0x0"`, want: uint64(0)},
{input: `"0x2"`, want: uint64(0x2)},
{input: `"0x2F2"`, want: uint64(0x2f2)},
{input: `"0x1122aaff"`, want: uint64(0x1122aaff)},
{input: `"0xbbb"`, want: uint64(0xbbb)},
{input: `"0xffffffffffffffff"`, want: uint64(0xffffffffffffffff)},
}
)

func TestMarshalUint64OrHex(t *testing.T) {
for _, test := range marshalUint64OrHexTests {
in := test.input.(uint64)
out, err := json.Marshal(Uint64OrHex(in))
if err != nil {
t.Errorf("%d: %v", in, err)
continue
}

if want := `"` + test.want.(string) + `"`; string(out) != want {
t.Errorf("%d: MarshalJSON output mismatch: got %q, want %q", in, out, want)
continue
}
}
}

func TestUnMarshalUint64OrHex(t *testing.T) {
for _, test := range UnMarshalUint64OrHexTests {
var v Uint64OrHex
err := json.Unmarshal([]byte(test.input.(string)), &v)
if test.wantErr {
if err == nil {
t.Errorf("%s: UnMarshalJSON did not error on invalid encoding: got %q, want <nil>", test.input, err)
}
continue
}

if uint64(v) != test.want.(uint64) {
t.Errorf("input %s: value mismatch: got %d, want %d", test.input, v, test.want)
continue
}
}
}

0 comments on commit b4bd0da

Please sign in to comment.