diff --git a/accounts/abi/abi.go b/accounts/abi/abi.go index f75278c8b1..3b5065f857 100644 --- a/accounts/abi/abi.go +++ b/accounts/abi/abi.go @@ -22,10 +22,10 @@ import ( "errors" "fmt" "io" - "math/big" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" + "github.com/holiman/uint256" ) // The ABI holds information about a contract's context and available @@ -299,7 +299,7 @@ func UnpackRevert(data []byte) (string, error) { if err != nil { return "", err } - pCode := unpacked[0].(*big.Int) + pCode := unpacked[0].(*uint256.Int) // uint64 safety check for future // but the code is not bigger than MAX(uint64) now if pCode.IsUint64() { diff --git a/accounts/abi/abi_test.go b/accounts/abi/abi_test.go index db9a4c55a5..314e0868ff 100644 --- a/accounts/abi/abi_test.go +++ b/accounts/abi/abi_test.go @@ -30,6 +30,7 @@ import ( "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/internal/testrand" + "github.com/holiman/uint256" ) const jsondata = ` @@ -185,7 +186,7 @@ func TestConstructor(t *testing.T) { t.Error("Missing expected constructor") } // Test pack/unpack - packed, err := abi.Pack("", big.NewInt(1), big.NewInt(2)) + packed, err := abi.Pack("", uint256.NewInt(1), uint256.NewInt(2)) if err != nil { t.Error(err) } @@ -194,10 +195,10 @@ func TestConstructor(t *testing.T) { t.Error(err) } - if !reflect.DeepEqual(unpacked[0], big.NewInt(1)) { + if !reflect.DeepEqual(unpacked[0], uint256.NewInt(1)) { t.Error("Unable to pack/unpack from constructor") } - if !reflect.DeepEqual(unpacked[1], big.NewInt(2)) { + if !reflect.DeepEqual(unpacked[1], uint256.NewInt(2)) { t.Error("Unable to pack/unpack from constructor") } } @@ -328,11 +329,11 @@ func TestCustomErrorUnpackIntoInterface(t *testing.T) { } type MyError struct { Sender common.Address - Balance *big.Int + Balance *uint256.Int } sender := testrand.Address() - balance := new(big.Int).SetBytes(testrand.Bytes(8)) + balance := new(uint256.Int).SetBytes(testrand.Bytes(8)) encoded, err := abi.Errors[errorName].Inputs.Pack(sender, balance) if err != nil { t.Fatal(err) @@ -802,7 +803,7 @@ func TestUnpackEvent(t *testing.T) { type ReceivedEvent struct { Sender common.Address - Amount *big.Int + Amount *uint256.Int Memo []byte } var ev ReceivedEvent @@ -842,7 +843,7 @@ func TestUnpackEventIntoMap(t *testing.T) { receivedMap := map[string]interface{}{} expectedReceivedMap := map[string]interface{}{ "sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"), - "amount": big.NewInt(1), + "amount": uint256.NewInt(1), "memo": []byte{88}, } if err := abi.UnpackIntoMap(receivedMap, "received", data); err != nil { @@ -854,7 +855,7 @@ func TestUnpackEventIntoMap(t *testing.T) { if receivedMap["sender"] != expectedReceivedMap["sender"] { t.Error("unpacked `received` map does not match expected map") } - if receivedMap["amount"].(*big.Int).Cmp(expectedReceivedMap["amount"].(*big.Int)) != 0 { + if receivedMap["amount"].(*uint256.Int).Cmp(expectedReceivedMap["amount"].(*uint256.Int)) != 0 { t.Error("unpacked `received` map does not match expected map") } if !bytes.Equal(receivedMap["memo"].([]byte), expectedReceivedMap["memo"].([]byte)) { @@ -906,7 +907,7 @@ func TestUnpackMethodIntoMap(t *testing.T) { if len(sendMap) != 1 { t.Error("unpacked `send` map expected to have length 1") } - if sendMap["amount"].(*big.Int).Cmp(big.NewInt(1)) != 0 { + if sendMap["amount"].(*uint256.Int).Cmp(uint256.NewInt(1)) != 0 { t.Error("unpacked `send` map expected `amount` value of 1") } @@ -988,7 +989,7 @@ func TestUnpackIntoMapNamingConflict(t *testing.T) { } expectedReceivedMap := map[string]interface{}{ "sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"), - "amount": big.NewInt(1), + "amount": uint256.NewInt(1), "memo": []byte{88}, } if err = abi.UnpackIntoMap(receivedMap, "Received", data); err != nil { @@ -1000,7 +1001,7 @@ func TestUnpackIntoMapNamingConflict(t *testing.T) { if receivedMap["sender"] != expectedReceivedMap["sender"] { t.Error("unpacked `received` map does not match expected map") } - if receivedMap["amount"].(*big.Int).Cmp(expectedReceivedMap["amount"].(*big.Int)) != 0 { + if receivedMap["amount"].(*uint256.Int).Cmp(expectedReceivedMap["amount"].(*uint256.Int)) != 0 { t.Error("unpacked `received` map does not match expected map") } if !bytes.Equal(receivedMap["memo"].([]byte), expectedReceivedMap["memo"].([]byte)) { diff --git a/accounts/abi/event_test.go b/accounts/abi/event_test.go index c548fd8db6..29f252e652 100644 --- a/accounts/abi/event_test.go +++ b/accounts/abi/event_test.go @@ -20,13 +20,13 @@ import ( "bytes" "encoding/hex" "encoding/json" - "math/big" "reflect" "strings" "testing" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" + "github.com/holiman/uint256" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -166,33 +166,33 @@ func TestEventMultiValueWithArrayUnpack(t *testing.T) { func TestEventTupleUnpack(t *testing.T) { t.Parallel() type EventTransfer struct { - Value *big.Int + Value *uint256.Int } type EventTransferWithTag struct { // this is valid because `value` is not exportable, // so value is only unmarshalled into `Value1`. - value *big.Int //lint:ignore U1000 unused field is part of test - Value1 *big.Int `abi:"value"` + value *uint256.Int //lint:ignore U1000 unused field is part of test + Value1 *uint256.Int `abi:"value"` } type BadEventTransferWithSameFieldAndTag struct { - Value *big.Int - Value1 *big.Int `abi:"value"` + Value *uint256.Int + Value1 *uint256.Int `abi:"value"` } type BadEventTransferWithDuplicatedTag struct { - Value1 *big.Int `abi:"value"` - Value2 *big.Int `abi:"value"` + Value1 *uint256.Int `abi:"value"` + Value2 *uint256.Int `abi:"value"` } type BadEventTransferWithEmptyTag struct { - Value *big.Int `abi:""` + Value *uint256.Int `abi:""` } type EventPledge struct { Who common.Address - Wad *big.Int + Wad *uint256.Int Currency [3]byte } @@ -203,15 +203,15 @@ func TestEventTupleUnpack(t *testing.T) { } type EventMixedCase struct { - Value1 *big.Int `abi:"value"` - Value2 *big.Int `abi:"_value"` - Value3 *big.Int `abi:"Value"` + Value1 *uint256.Int `abi:"value"` + Value2 *uint256.Int `abi:"_value"` + Value3 *uint256.Int `abi:"Value"` } - bigint := new(big.Int) - bigintExpected := big.NewInt(1000000) - bigintExpected2 := big.NewInt(2218516807680) - bigintExpected3 := big.NewInt(1000001) + UInt256 := new(uint256.Int) + UInt256Expected := uint256.NewInt(1000000) + UInt256Expected2 := uint256.NewInt(2218516807680) + UInt256Expected3 := uint256.NewInt(1000001) addr := common.HexToAddress("0x00Ce0d46d924CC8437c806721496599FC3FFA268") var testCases = []struct { data string @@ -223,21 +223,21 @@ func TestEventTupleUnpack(t *testing.T) { }{{ transferData1, &EventTransfer{}, - &EventTransfer{Value: bigintExpected}, + &EventTransfer{Value: UInt256Expected}, jsonEventTransfer, "", "Can unpack ERC20 Transfer event into structure", }, { transferData1, - &[]interface{}{&bigint}, - &[]interface{}{&bigintExpected}, + &[]interface{}{&UInt256}, + &[]interface{}{&UInt256Expected}, jsonEventTransfer, "", "Can unpack ERC20 Transfer event into slice", }, { transferData1, &EventTransferWithTag{}, - &EventTransferWithTag{Value1: bigintExpected}, + &EventTransferWithTag{Value1: UInt256Expected}, jsonEventTransfer, "", "Can unpack ERC20 Transfer event into structure with abi: tag", @@ -267,27 +267,27 @@ func TestEventTupleUnpack(t *testing.T) { &EventPledge{}, &EventPledge{ addr, - bigintExpected2, + UInt256Expected2, [3]byte{'u', 's', 'd'}}, jsonEventPledge, "", "Can unpack Pledge event into structure", }, { pledgeData1, - &[]interface{}{&common.Address{}, &bigint, &[3]byte{}}, + &[]interface{}{&common.Address{}, &UInt256, &[3]byte{}}, &[]interface{}{ &addr, - &bigintExpected2, + &UInt256Expected2, &[3]byte{'u', 's', 'd'}}, jsonEventPledge, "", "Can unpack Pledge event into slice", }, { pledgeData1, - &[3]interface{}{&common.Address{}, &bigint, &[3]byte{}}, + &[3]interface{}{&common.Address{}, &UInt256, &[3]byte{}}, &[3]interface{}{ &addr, - &bigintExpected2, + &UInt256Expected2, &[3]byte{'u', 's', 'd'}}, jsonEventPledge, "", @@ -308,7 +308,7 @@ func TestEventTupleUnpack(t *testing.T) { "Can not unpack Pledge event into struct with wrong filed types", }, { pledgeData1, - &[]interface{}{common.Address{}, new(big.Int)}, + &[]interface{}{common.Address{}, new(uint256.Int)}, &[]interface{}{}, jsonEventPledge, "abi: insufficient number of arguments for unpack, want 3, got 2", @@ -323,7 +323,7 @@ func TestEventTupleUnpack(t *testing.T) { }, { mixedCaseData1, &EventMixedCase{}, - &EventMixedCase{Value1: bigintExpected, Value2: bigintExpected2, Value3: bigintExpected3}, + &EventMixedCase{Value1: UInt256Expected, Value2: UInt256Expected2, Value3: UInt256Expected3}, jsonEventMixedCase, "", "Can unpack abi variables with mixed case", diff --git a/accounts/abi/packing_test.go b/accounts/abi/packing_test.go index eae3b0df20..eb0732dfd5 100644 --- a/accounts/abi/packing_test.go +++ b/accounts/abi/packing_test.go @@ -20,6 +20,7 @@ import ( "math/big" "github.com/ethereum/go-ethereum/common" + "github.com/holiman/uint256" ) type packUnpackTest struct { @@ -70,7 +71,7 @@ var packUnpackTests = []packUnpackTest{ { def: `[{"type": "uint17"}]`, packed: "0000000000000000000000000000000000000000000000000000000000000001", - unpacked: big.NewInt(1), + unpacked: uint256.NewInt(1), }, { def: `[{"type": "uint32"}]`, @@ -100,12 +101,12 @@ var packUnpackTests = []packUnpackTest{ }, { def: `[{"type": "uint256"}]`, - unpacked: big.NewInt(2), + unpacked: uint256.NewInt(2), packed: "0000000000000000000000000000000000000000000000000000000000000002", }, { def: `[{"type": "uint256[]"}]`, - unpacked: []*big.Int{big.NewInt(1), big.NewInt(2)}, + unpacked: []*uint256.Int{uint256.NewInt(1), uint256.NewInt(2)}, packed: "0000000000000000000000000000000000000000000000000000000000000020" + "0000000000000000000000000000000000000000000000000000000000000002" + "0000000000000000000000000000000000000000000000000000000000000001" + @@ -408,7 +409,7 @@ var packUnpackTests = []packUnpackTest{ def: `[{"type": "uint256[]"}]`, packed: "0000000000000000000000000000000000000000000000000000000000000020" + "0000000000000000000000000000000000000000000000000000000000000000", - unpacked: []*big.Int{}, + unpacked: []*uint256.Int{}, }, { def: `[{"type": "uint8[2]"}]`, @@ -652,14 +653,14 @@ var packUnpackTests = []packUnpackTest{ "0000000000000000000000000000000000000000000000000000000000000002" + "0000000000000000000000000000000000000000000000000000000000000001" + "0000000000000000000000000000000000000000000000000000000000000002", - unpacked: []*big.Int{big.NewInt(1), big.NewInt(2)}, + unpacked: []*uint256.Int{uint256.NewInt(1), uint256.NewInt(2)}, }, { def: `[{"type": "uint256[3]"}]`, packed: "0000000000000000000000000000000000000000000000000000000000000001" + "0000000000000000000000000000000000000000000000000000000000000002" + "0000000000000000000000000000000000000000000000000000000000000003", - unpacked: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)}, + unpacked: [3]*uint256.Int{uint256.NewInt(1), uint256.NewInt(2), uint256.NewInt(3)}, }, { def: `[{"type": "string[4]"}]`, @@ -718,7 +719,7 @@ var packUnpackTests = []packUnpackTest{ "00000000000000000000000000000000000000000000000000000000000000c8" + "0000000000000000000000000000000000000000000000000000000000000001" + "00000000000000000000000000000000000000000000000000000000000003e8", - unpacked: [][][2]*big.Int{{{big.NewInt(1), big.NewInt(200)}, {big.NewInt(1), big.NewInt(1000)}}, {{big.NewInt(1), big.NewInt(200)}, {big.NewInt(1), big.NewInt(1000)}}}, + unpacked: [][][2]*uint256.Int{{{uint256.NewInt(1), uint256.NewInt(200)}, {uint256.NewInt(1), uint256.NewInt(1000)}}, {{uint256.NewInt(1), uint256.NewInt(200)}, {uint256.NewInt(1), uint256.NewInt(1000)}}}, }, // struct outputs { @@ -901,16 +902,16 @@ var packUnpackTests = []packUnpackTest{ {"name": "b","type": "uint256[]"}], "type": "tuple"}]`, unpacked: struct { A struct { - A *big.Int - B []*big.Int + A *uint256.Int + B []*uint256.Int } - B []*big.Int + B []*uint256.Int }{ A: struct { - A *big.Int - B []*big.Int - }{big.NewInt(1), []*big.Int{big.NewInt(1), big.NewInt(2)}}, - B: []*big.Int{big.NewInt(1), big.NewInt(2)}}, + A *uint256.Int + B []*uint256.Int + }{uint256.NewInt(1), []*uint256.Int{uint256.NewInt(1), uint256.NewInt(2)}}, + B: []*uint256.Int{uint256.NewInt(1), uint256.NewInt(2)}}, packed: "0000000000000000000000000000000000000000000000000000000000000020" + // struct a "0000000000000000000000000000000000000000000000000000000000000040" + // a offset "00000000000000000000000000000000000000000000000000000000000000e0" + // b offset diff --git a/accounts/abi/type_test.go b/accounts/abi/type_test.go index 95922548c4..16e845b239 100644 --- a/accounts/abi/type_test.go +++ b/accounts/abi/type_test.go @@ -23,6 +23,7 @@ import ( "github.com/davecgh/go-spew/spew" "github.com/ethereum/go-ethereum/common" + "github.com/holiman/uint256" ) // typeWithoutStringer is an alias for the Type type which simply doesn't implement @@ -142,33 +143,33 @@ func TestTypeCheck(t *testing.T) { {"int16", nil, int16(1), ""}, {"int32", nil, int32(1), ""}, {"int64", nil, int64(1), ""}, - {"uint24", nil, big.NewInt(1), ""}, - {"uint40", nil, big.NewInt(1), ""}, - {"uint48", nil, big.NewInt(1), ""}, - {"uint56", nil, big.NewInt(1), ""}, - {"uint72", nil, big.NewInt(1), ""}, - {"uint80", nil, big.NewInt(1), ""}, - {"uint88", nil, big.NewInt(1), ""}, - {"uint96", nil, big.NewInt(1), ""}, - {"uint104", nil, big.NewInt(1), ""}, - {"uint112", nil, big.NewInt(1), ""}, - {"uint120", nil, big.NewInt(1), ""}, - {"uint128", nil, big.NewInt(1), ""}, - {"uint136", nil, big.NewInt(1), ""}, - {"uint144", nil, big.NewInt(1), ""}, - {"uint152", nil, big.NewInt(1), ""}, - {"uint160", nil, big.NewInt(1), ""}, - {"uint168", nil, big.NewInt(1), ""}, - {"uint176", nil, big.NewInt(1), ""}, - {"uint184", nil, big.NewInt(1), ""}, - {"uint192", nil, big.NewInt(1), ""}, - {"uint200", nil, big.NewInt(1), ""}, - {"uint208", nil, big.NewInt(1), ""}, - {"uint216", nil, big.NewInt(1), ""}, - {"uint224", nil, big.NewInt(1), ""}, - {"uint232", nil, big.NewInt(1), ""}, - {"uint240", nil, big.NewInt(1), ""}, - {"uint248", nil, big.NewInt(1), ""}, + {"uint24", nil, uint256.NewInt(1), ""}, + {"uint40", nil, uint256.NewInt(1), ""}, + {"uint48", nil, uint256.NewInt(1), ""}, + {"uint56", nil, uint256.NewInt(1), ""}, + {"uint72", nil, uint256.NewInt(1), ""}, + {"uint80", nil, uint256.NewInt(1), ""}, + {"uint88", nil, uint256.NewInt(1), ""}, + {"uint96", nil, uint256.NewInt(1), ""}, + {"uint104", nil, uint256.NewInt(1), ""}, + {"uint112", nil, uint256.NewInt(1), ""}, + {"uint120", nil, uint256.NewInt(1), ""}, + {"uint128", nil, uint256.NewInt(1), ""}, + {"uint136", nil, uint256.NewInt(1), ""}, + {"uint144", nil, uint256.NewInt(1), ""}, + {"uint152", nil, uint256.NewInt(1), ""}, + {"uint160", nil, uint256.NewInt(1), ""}, + {"uint168", nil, uint256.NewInt(1), ""}, + {"uint176", nil, uint256.NewInt(1), ""}, + {"uint184", nil, uint256.NewInt(1), ""}, + {"uint192", nil, uint256.NewInt(1), ""}, + {"uint200", nil, uint256.NewInt(1), ""}, + {"uint208", nil, uint256.NewInt(1), ""}, + {"uint216", nil, uint256.NewInt(1), ""}, + {"uint224", nil, uint256.NewInt(1), ""}, + {"uint232", nil, uint256.NewInt(1), ""}, + {"uint240", nil, uint256.NewInt(1), ""}, + {"uint248", nil, uint256.NewInt(1), ""}, {"int24", nil, big.NewInt(1), ""}, {"int40", nil, big.NewInt(1), ""}, {"int48", nil, big.NewInt(1), ""}, @@ -269,19 +270,19 @@ func TestTypeCheck(t *testing.T) { {"invalidSlice[]", nil, "", "unsupported arg type: invalidSlice"}, // simple tuple {"tuple", []ArgumentMarshaling{{Name: "a", Type: "uint256"}, {Name: "b", Type: "uint256"}}, struct { - A *big.Int - B *big.Int + A *uint256.Int + B *uint256.Int }{}, ""}, // tuple slice {"tuple[]", []ArgumentMarshaling{{Name: "a", Type: "uint256"}, {Name: "b", Type: "uint256"}}, []struct { - A *big.Int - B *big.Int + A *uint256.Int + B *uint256.Int }{}, ""}, // tuple array {"tuple[2]", []ArgumentMarshaling{{Name: "a", Type: "uint256"}, {Name: "b", Type: "uint256"}}, []struct { - A *big.Int - B *big.Int - }{{big.NewInt(0), big.NewInt(0)}, {big.NewInt(0), big.NewInt(0)}}, ""}, + A *uint256.Int + B *uint256.Int + }{{uint256.NewInt(0), uint256.NewInt(0)}, {uint256.NewInt(0), uint256.NewInt(0)}}, ""}, } { typ, err := NewType(test.typ, "", test.components) if err != nil && len(test.err) == 0 { diff --git a/accounts/abi/unpack_test.go b/accounts/abi/unpack_test.go index 74db298b3f..8361cb971c 100644 --- a/accounts/abi/unpack_test.go +++ b/accounts/abi/unpack_test.go @@ -28,6 +28,7 @@ import ( "testing" "github.com/ethereum/go-ethereum/common" + "github.com/holiman/uint256" "github.com/stretchr/testify/require" ) @@ -143,7 +144,7 @@ var unpackTests = []unpackTest{ def: `[{"type": "uint17"}]`, enc: "0000000000000000000000000000000000000000000000000000000000000001", want: uint16(0), - err: "abi: cannot unmarshal *big.Int in to uint16", + err: "abi: cannot unmarshal *uint256.Int in to uint16", }, { def: `[{"type": "int32"}]`, @@ -343,14 +344,14 @@ func TestUnpackIntoInterfaceSetDynamicArrayOutput(t *testing.T) { } type methodMultiOutput struct { - Int *big.Int + Int *uint256.Int String string } func methodMultiReturn(require *require.Assertions) (ABI, []byte, methodMultiOutput) { const definition = `[ { "name" : "multi", "type": "function", "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]` - var expected = methodMultiOutput{big.NewInt(1), "hello"} + var expected = methodMultiOutput{uint256.NewInt(1), "hello"} abi, err := JSON(strings.NewReader(definition)) require.NoError(err) @@ -367,7 +368,7 @@ func TestMethodMultiReturn(t *testing.T) { t.Parallel() type reversed struct { String string - Int *big.Int + Int *uint256.Int } newInterfaceSlice := func(len int) interface{} { @@ -376,7 +377,7 @@ func TestMethodMultiReturn(t *testing.T) { } abi, data, expected := methodMultiReturn(require.New(t)) - bigint := new(big.Int) + UInt := new(uint256.Int) var testCases = []struct { dest interface{} expected interface{} @@ -393,17 +394,17 @@ func TestMethodMultiReturn(t *testing.T) { "", "Can unpack into reversed structure", }, { - &[]interface{}{&bigint, new(string)}, + &[]interface{}{&UInt, new(string)}, &[]interface{}{&expected.Int, &expected.String}, "", "Can unpack into a slice", }, { - &[]interface{}{&bigint, ""}, + &[]interface{}{&UInt, ""}, &[]interface{}{&expected.Int, expected.String}, "", "Can unpack into a slice without indirection", }, { - &[2]interface{}{&bigint, new(string)}, + &[2]interface{}{&UInt, new(string)}, &[2]interface{}{&expected.Int, &expected.String}, "", "Can unpack into an array", @@ -420,7 +421,7 @@ func TestMethodMultiReturn(t *testing.T) { }, { &[]interface{}{new(int), new(int)}, &[]interface{}{&expected.Int, &expected.String}, - "abi: cannot unmarshal *big.Int in to int", + "abi: cannot unmarshal *uint256.Int in to int", "Can not unpack into a slice with wrong types", }, { &[]interface{}{new(int)}, @@ -475,8 +476,9 @@ func TestMultiReturnWithStringArray(t *testing.T) { } buff := new(bytes.Buffer) buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000005c1b78ea0000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000001a055690d9db80000000000000000000000000000ab1257528b3782fb40d7ed5f72e624b744dffb2f00000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000008457468657265756d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001048656c6c6f2c20457468657265756d2100000000000000000000000000000000")) + temp, _ := new(big.Int).SetString("30000000000000000000", 10) - ret1, ret1Exp := new([3]*big.Int), [3]*big.Int{big.NewInt(1545304298), big.NewInt(6), temp} + ret1, ret1Exp := new([3]*uint256.Int), [3]*uint256.Int{uint256.NewInt(1545304298), uint256.NewInt(6), uint256.MustFromBig(temp)} ret2, ret2Exp := new(common.Address), common.HexToAddress("ab1257528b3782fb40d7ed5f72e624b744dffb2f") ret3, ret3Exp := new([2]string), [2]string{"Ethereum", "Hello, Ethereum!"} ret4, ret4Exp := new(bool), false @@ -518,7 +520,7 @@ func TestMultiReturnWithStringSlice(t *testing.T) { buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000064")) // output[1][0] value buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000065")) // output[1][1] value ret1, ret1Exp := new([]string), []string{"ethereum", "go-ethereum"} - ret2, ret2Exp := new([]*big.Int), []*big.Int{big.NewInt(100), big.NewInt(101)} + ret2, ret2Exp := new([]*uint256.Int), []*uint256.Int{uint256.NewInt(100), uint256.NewInt(101)} if err := abi.UnpackIntoInterface(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil { t.Fatal(err) } @@ -614,13 +616,13 @@ func TestUnmarshal(t *testing.T) { } // marshal int - var Int *big.Int + var Int *uint256.Int err = abi.UnpackIntoInterface(&Int, "int", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) if err != nil { t.Error(err) } - if Int == nil || Int.Cmp(big.NewInt(1)) != 0 { + if Int == nil || Int.Cmp(uint256.NewInt(1)) != 0 { t.Error("expected Int to be 1 got", Int) } @@ -738,15 +740,15 @@ func TestUnmarshal(t *testing.T) { buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003")) // marshal int array - var intArray [3]*big.Int + var intArray [3]*uint256.Int err = abi.UnpackIntoInterface(&intArray, "intArraySingle", buff.Bytes()) if err != nil { t.Error(err) } - var testAgainstIntArray [3]*big.Int - testAgainstIntArray[0] = big.NewInt(1) - testAgainstIntArray[1] = big.NewInt(2) - testAgainstIntArray[2] = big.NewInt(3) + var testAgainstIntArray [3]*uint256.Int + testAgainstIntArray[0] = uint256.NewInt(1) + testAgainstIntArray[1] = uint256.NewInt(2) + testAgainstIntArray[2] = uint256.NewInt(3) for i, Int := range intArray { if Int.Cmp(testAgainstIntArray[i]) != 0 { @@ -884,42 +886,42 @@ func TestUnpackTuple(t *testing.T) { buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // s.C[1].Y type T struct { - X *big.Int `abi:"x"` - Z *big.Int `abi:"y"` // Test whether the abi tag works. + X *uint256.Int `abi:"x"` + Z *uint256.Int `abi:"y"` // Test whether the abi tag works. } type S struct { - A *big.Int - B []*big.Int + A *uint256.Int + B []*uint256.Int C []T } type Ret struct { FieldS S `abi:"s"` FieldT T `abi:"t"` - A *big.Int + A *uint256.Int } var ret Ret var expected = Ret{ FieldS: S{ - A: big.NewInt(1), - B: []*big.Int{big.NewInt(1), big.NewInt(2)}, + A: uint256.NewInt(1), + B: []*uint256.Int{uint256.NewInt(1), uint256.NewInt(2)}, C: []T{ - {big.NewInt(1), big.NewInt(2)}, - {big.NewInt(2), big.NewInt(1)}, + {uint256.NewInt(1), uint256.NewInt(2)}, + {uint256.NewInt(2), uint256.NewInt(1)}, }, }, FieldT: T{ - big.NewInt(0), big.NewInt(1), + uint256.NewInt(0), uint256.NewInt(1), }, - A: big.NewInt(1), + A: uint256.NewInt(1), } err = abi.UnpackIntoInterface(&ret, "tuple", buff.Bytes()) if err != nil { t.Error(err) } - if reflect.DeepEqual(ret, expected) { + if !reflect.DeepEqual(ret, expected) { t.Error("unexpected unpack value") } } @@ -1011,6 +1013,9 @@ func TestPackAndUnpackIncompatibleNumber(t *testing.T) { panic("bug") } maxU64Plus1 := new(big.Int).Add(maxU64, big.NewInt(1)) + + // maxU256 := uint256.MustFromHex("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff") + cases := []struct { decodeType string inputValue *big.Int @@ -1065,7 +1070,7 @@ func TestPackAndUnpackIncompatibleNumber(t *testing.T) { decodeType: "uint256", inputValue: maxU64Plus1, err: nil, - expectValue: maxU64Plus1, + expectValue: uint256.MustFromBig(maxU64Plus1), }, { decodeType: "int8",