Skip to content

Commit

Permalink
Merge pull request #252 from OffchainLabs/upgrade-type-conditionalopt…
Browse files Browse the repository at this point in the history
…ions

Upgrade ConditionalOptions variables to type math.HexOrDecimal64 to support eth_sendRawTransactionConditional
  • Loading branch information
PlasmaPower authored Sep 1, 2023
2 parents 11e7b08 + b4bd0da commit 819425c
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 5 deletions.
10 changes: 5 additions & 5 deletions arbitrum_types/txoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"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 @@ -70,10 +70,10 @@ func (r RootHashOrSlots) MarshalJSON() ([]byte, error) {

type ConditionalOptions struct {
KnownAccounts map[common.Address]RootHashOrSlots `json:"knownAccounts"`
BlockNumberMin *hexutil.Uint64 `json:"blockNumberMin,omitempty"`
BlockNumberMax *hexutil.Uint64 `json:"blockNumberMax,omitempty"`
TimestampMin *hexutil.Uint64 `json:"timestampMin,omitempty"`
TimestampMax *hexutil.Uint64 `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
}
}
}

0 comments on commit 819425c

Please sign in to comment.