tests: update accounts/abi tests

This commit is contained in:
Miro 2025-04-10 13:41:34 -04:00
parent 8663d9268e
commit 25661b25a9
6 changed files with 128 additions and 120 deletions

View file

@ -22,10 +22,10 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"math/big"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/holiman/uint256"
) )
// The ABI holds information about a contract's context and available // The ABI holds information about a contract's context and available
@ -299,7 +299,7 @@ func UnpackRevert(data []byte) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
pCode := unpacked[0].(*big.Int) pCode := unpacked[0].(*uint256.Int)
// uint64 safety check for future // uint64 safety check for future
// but the code is not bigger than MAX(uint64) now // but the code is not bigger than MAX(uint64) now
if pCode.IsUint64() { if pCode.IsUint64() {

View file

@ -30,6 +30,7 @@ import (
"github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/internal/testrand" "github.com/ethereum/go-ethereum/internal/testrand"
"github.com/holiman/uint256"
) )
const jsondata = ` const jsondata = `
@ -185,7 +186,7 @@ func TestConstructor(t *testing.T) {
t.Error("Missing expected constructor") t.Error("Missing expected constructor")
} }
// Test pack/unpack // 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 { if err != nil {
t.Error(err) t.Error(err)
} }
@ -194,10 +195,10 @@ func TestConstructor(t *testing.T) {
t.Error(err) 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") 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") t.Error("Unable to pack/unpack from constructor")
} }
} }
@ -328,11 +329,11 @@ func TestCustomErrorUnpackIntoInterface(t *testing.T) {
} }
type MyError struct { type MyError struct {
Sender common.Address Sender common.Address
Balance *big.Int Balance *uint256.Int
} }
sender := testrand.Address() 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) encoded, err := abi.Errors[errorName].Inputs.Pack(sender, balance)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -802,7 +803,7 @@ func TestUnpackEvent(t *testing.T) {
type ReceivedEvent struct { type ReceivedEvent struct {
Sender common.Address Sender common.Address
Amount *big.Int Amount *uint256.Int
Memo []byte Memo []byte
} }
var ev ReceivedEvent var ev ReceivedEvent
@ -842,7 +843,7 @@ func TestUnpackEventIntoMap(t *testing.T) {
receivedMap := map[string]interface{}{} receivedMap := map[string]interface{}{}
expectedReceivedMap := map[string]interface{}{ expectedReceivedMap := map[string]interface{}{
"sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"), "sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"),
"amount": big.NewInt(1), "amount": uint256.NewInt(1),
"memo": []byte{88}, "memo": []byte{88},
} }
if err := abi.UnpackIntoMap(receivedMap, "received", data); err != nil { if err := abi.UnpackIntoMap(receivedMap, "received", data); err != nil {
@ -854,7 +855,7 @@ func TestUnpackEventIntoMap(t *testing.T) {
if receivedMap["sender"] != expectedReceivedMap["sender"] { if receivedMap["sender"] != expectedReceivedMap["sender"] {
t.Error("unpacked `received` map does not match expected map") 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") t.Error("unpacked `received` map does not match expected map")
} }
if !bytes.Equal(receivedMap["memo"].([]byte), expectedReceivedMap["memo"].([]byte)) { if !bytes.Equal(receivedMap["memo"].([]byte), expectedReceivedMap["memo"].([]byte)) {
@ -906,7 +907,7 @@ func TestUnpackMethodIntoMap(t *testing.T) {
if len(sendMap) != 1 { if len(sendMap) != 1 {
t.Error("unpacked `send` map expected to have length 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") t.Error("unpacked `send` map expected `amount` value of 1")
} }
@ -988,7 +989,7 @@ func TestUnpackIntoMapNamingConflict(t *testing.T) {
} }
expectedReceivedMap := map[string]interface{}{ expectedReceivedMap := map[string]interface{}{
"sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"), "sender": common.HexToAddress("0x376c47978271565f56DEB45495afa69E59c16Ab2"),
"amount": big.NewInt(1), "amount": uint256.NewInt(1),
"memo": []byte{88}, "memo": []byte{88},
} }
if err = abi.UnpackIntoMap(receivedMap, "Received", data); err != nil { if err = abi.UnpackIntoMap(receivedMap, "Received", data); err != nil {
@ -1000,7 +1001,7 @@ func TestUnpackIntoMapNamingConflict(t *testing.T) {
if receivedMap["sender"] != expectedReceivedMap["sender"] { if receivedMap["sender"] != expectedReceivedMap["sender"] {
t.Error("unpacked `received` map does not match expected map") 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") t.Error("unpacked `received` map does not match expected map")
} }
if !bytes.Equal(receivedMap["memo"].([]byte), expectedReceivedMap["memo"].([]byte)) { if !bytes.Equal(receivedMap["memo"].([]byte), expectedReceivedMap["memo"].([]byte)) {

View file

@ -20,13 +20,13 @@ import (
"bytes" "bytes"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"math/big"
"reflect" "reflect"
"strings" "strings"
"testing" "testing"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/holiman/uint256"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -166,33 +166,33 @@ func TestEventMultiValueWithArrayUnpack(t *testing.T) {
func TestEventTupleUnpack(t *testing.T) { func TestEventTupleUnpack(t *testing.T) {
t.Parallel() t.Parallel()
type EventTransfer struct { type EventTransfer struct {
Value *big.Int Value *uint256.Int
} }
type EventTransferWithTag struct { type EventTransferWithTag struct {
// this is valid because `value` is not exportable, // this is valid because `value` is not exportable,
// so value is only unmarshalled into `Value1`. // so value is only unmarshalled into `Value1`.
value *big.Int //lint:ignore U1000 unused field is part of test value *uint256.Int //lint:ignore U1000 unused field is part of test
Value1 *big.Int `abi:"value"` Value1 *uint256.Int `abi:"value"`
} }
type BadEventTransferWithSameFieldAndTag struct { type BadEventTransferWithSameFieldAndTag struct {
Value *big.Int Value *uint256.Int
Value1 *big.Int `abi:"value"` Value1 *uint256.Int `abi:"value"`
} }
type BadEventTransferWithDuplicatedTag struct { type BadEventTransferWithDuplicatedTag struct {
Value1 *big.Int `abi:"value"` Value1 *uint256.Int `abi:"value"`
Value2 *big.Int `abi:"value"` Value2 *uint256.Int `abi:"value"`
} }
type BadEventTransferWithEmptyTag struct { type BadEventTransferWithEmptyTag struct {
Value *big.Int `abi:""` Value *uint256.Int `abi:""`
} }
type EventPledge struct { type EventPledge struct {
Who common.Address Who common.Address
Wad *big.Int Wad *uint256.Int
Currency [3]byte Currency [3]byte
} }
@ -203,15 +203,15 @@ func TestEventTupleUnpack(t *testing.T) {
} }
type EventMixedCase struct { type EventMixedCase struct {
Value1 *big.Int `abi:"value"` Value1 *uint256.Int `abi:"value"`
Value2 *big.Int `abi:"_value"` Value2 *uint256.Int `abi:"_value"`
Value3 *big.Int `abi:"Value"` Value3 *uint256.Int `abi:"Value"`
} }
bigint := new(big.Int) UInt256 := new(uint256.Int)
bigintExpected := big.NewInt(1000000) UInt256Expected := uint256.NewInt(1000000)
bigintExpected2 := big.NewInt(2218516807680) UInt256Expected2 := uint256.NewInt(2218516807680)
bigintExpected3 := big.NewInt(1000001) UInt256Expected3 := uint256.NewInt(1000001)
addr := common.HexToAddress("0x00Ce0d46d924CC8437c806721496599FC3FFA268") addr := common.HexToAddress("0x00Ce0d46d924CC8437c806721496599FC3FFA268")
var testCases = []struct { var testCases = []struct {
data string data string
@ -223,21 +223,21 @@ func TestEventTupleUnpack(t *testing.T) {
}{{ }{{
transferData1, transferData1,
&EventTransfer{}, &EventTransfer{},
&EventTransfer{Value: bigintExpected}, &EventTransfer{Value: UInt256Expected},
jsonEventTransfer, jsonEventTransfer,
"", "",
"Can unpack ERC20 Transfer event into structure", "Can unpack ERC20 Transfer event into structure",
}, { }, {
transferData1, transferData1,
&[]interface{}{&bigint}, &[]interface{}{&UInt256},
&[]interface{}{&bigintExpected}, &[]interface{}{&UInt256Expected},
jsonEventTransfer, jsonEventTransfer,
"", "",
"Can unpack ERC20 Transfer event into slice", "Can unpack ERC20 Transfer event into slice",
}, { }, {
transferData1, transferData1,
&EventTransferWithTag{}, &EventTransferWithTag{},
&EventTransferWithTag{Value1: bigintExpected}, &EventTransferWithTag{Value1: UInt256Expected},
jsonEventTransfer, jsonEventTransfer,
"", "",
"Can unpack ERC20 Transfer event into structure with abi: tag", "Can unpack ERC20 Transfer event into structure with abi: tag",
@ -267,27 +267,27 @@ func TestEventTupleUnpack(t *testing.T) {
&EventPledge{}, &EventPledge{},
&EventPledge{ &EventPledge{
addr, addr,
bigintExpected2, UInt256Expected2,
[3]byte{'u', 's', 'd'}}, [3]byte{'u', 's', 'd'}},
jsonEventPledge, jsonEventPledge,
"", "",
"Can unpack Pledge event into structure", "Can unpack Pledge event into structure",
}, { }, {
pledgeData1, pledgeData1,
&[]interface{}{&common.Address{}, &bigint, &[3]byte{}}, &[]interface{}{&common.Address{}, &UInt256, &[3]byte{}},
&[]interface{}{ &[]interface{}{
&addr, &addr,
&bigintExpected2, &UInt256Expected2,
&[3]byte{'u', 's', 'd'}}, &[3]byte{'u', 's', 'd'}},
jsonEventPledge, jsonEventPledge,
"", "",
"Can unpack Pledge event into slice", "Can unpack Pledge event into slice",
}, { }, {
pledgeData1, pledgeData1,
&[3]interface{}{&common.Address{}, &bigint, &[3]byte{}}, &[3]interface{}{&common.Address{}, &UInt256, &[3]byte{}},
&[3]interface{}{ &[3]interface{}{
&addr, &addr,
&bigintExpected2, &UInt256Expected2,
&[3]byte{'u', 's', 'd'}}, &[3]byte{'u', 's', 'd'}},
jsonEventPledge, jsonEventPledge,
"", "",
@ -308,7 +308,7 @@ func TestEventTupleUnpack(t *testing.T) {
"Can not unpack Pledge event into struct with wrong filed types", "Can not unpack Pledge event into struct with wrong filed types",
}, { }, {
pledgeData1, pledgeData1,
&[]interface{}{common.Address{}, new(big.Int)}, &[]interface{}{common.Address{}, new(uint256.Int)},
&[]interface{}{}, &[]interface{}{},
jsonEventPledge, jsonEventPledge,
"abi: insufficient number of arguments for unpack, want 3, got 2", "abi: insufficient number of arguments for unpack, want 3, got 2",
@ -323,7 +323,7 @@ func TestEventTupleUnpack(t *testing.T) {
}, { }, {
mixedCaseData1, mixedCaseData1,
&EventMixedCase{}, &EventMixedCase{},
&EventMixedCase{Value1: bigintExpected, Value2: bigintExpected2, Value3: bigintExpected3}, &EventMixedCase{Value1: UInt256Expected, Value2: UInt256Expected2, Value3: UInt256Expected3},
jsonEventMixedCase, jsonEventMixedCase,
"", "",
"Can unpack abi variables with mixed case", "Can unpack abi variables with mixed case",

View file

@ -20,6 +20,7 @@ import (
"math/big" "math/big"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/holiman/uint256"
) )
type packUnpackTest struct { type packUnpackTest struct {
@ -70,7 +71,7 @@ var packUnpackTests = []packUnpackTest{
{ {
def: `[{"type": "uint17"}]`, def: `[{"type": "uint17"}]`,
packed: "0000000000000000000000000000000000000000000000000000000000000001", packed: "0000000000000000000000000000000000000000000000000000000000000001",
unpacked: big.NewInt(1), unpacked: uint256.NewInt(1),
}, },
{ {
def: `[{"type": "uint32"}]`, def: `[{"type": "uint32"}]`,
@ -100,12 +101,12 @@ var packUnpackTests = []packUnpackTest{
}, },
{ {
def: `[{"type": "uint256"}]`, def: `[{"type": "uint256"}]`,
unpacked: big.NewInt(2), unpacked: uint256.NewInt(2),
packed: "0000000000000000000000000000000000000000000000000000000000000002", packed: "0000000000000000000000000000000000000000000000000000000000000002",
}, },
{ {
def: `[{"type": "uint256[]"}]`, def: `[{"type": "uint256[]"}]`,
unpacked: []*big.Int{big.NewInt(1), big.NewInt(2)}, unpacked: []*uint256.Int{uint256.NewInt(1), uint256.NewInt(2)},
packed: "0000000000000000000000000000000000000000000000000000000000000020" + packed: "0000000000000000000000000000000000000000000000000000000000000020" +
"0000000000000000000000000000000000000000000000000000000000000002" + "0000000000000000000000000000000000000000000000000000000000000002" +
"0000000000000000000000000000000000000000000000000000000000000001" + "0000000000000000000000000000000000000000000000000000000000000001" +
@ -408,7 +409,7 @@ var packUnpackTests = []packUnpackTest{
def: `[{"type": "uint256[]"}]`, def: `[{"type": "uint256[]"}]`,
packed: "0000000000000000000000000000000000000000000000000000000000000020" + packed: "0000000000000000000000000000000000000000000000000000000000000020" +
"0000000000000000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000000000000000",
unpacked: []*big.Int{}, unpacked: []*uint256.Int{},
}, },
{ {
def: `[{"type": "uint8[2]"}]`, def: `[{"type": "uint8[2]"}]`,
@ -652,14 +653,14 @@ var packUnpackTests = []packUnpackTest{
"0000000000000000000000000000000000000000000000000000000000000002" + "0000000000000000000000000000000000000000000000000000000000000002" +
"0000000000000000000000000000000000000000000000000000000000000001" + "0000000000000000000000000000000000000000000000000000000000000001" +
"0000000000000000000000000000000000000000000000000000000000000002", "0000000000000000000000000000000000000000000000000000000000000002",
unpacked: []*big.Int{big.NewInt(1), big.NewInt(2)}, unpacked: []*uint256.Int{uint256.NewInt(1), uint256.NewInt(2)},
}, },
{ {
def: `[{"type": "uint256[3]"}]`, def: `[{"type": "uint256[3]"}]`,
packed: "0000000000000000000000000000000000000000000000000000000000000001" + packed: "0000000000000000000000000000000000000000000000000000000000000001" +
"0000000000000000000000000000000000000000000000000000000000000002" + "0000000000000000000000000000000000000000000000000000000000000002" +
"0000000000000000000000000000000000000000000000000000000000000003", "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]"}]`, def: `[{"type": "string[4]"}]`,
@ -718,7 +719,7 @@ var packUnpackTests = []packUnpackTest{
"00000000000000000000000000000000000000000000000000000000000000c8" + "00000000000000000000000000000000000000000000000000000000000000c8" +
"0000000000000000000000000000000000000000000000000000000000000001" + "0000000000000000000000000000000000000000000000000000000000000001" +
"00000000000000000000000000000000000000000000000000000000000003e8", "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 // struct outputs
{ {
@ -901,16 +902,16 @@ var packUnpackTests = []packUnpackTest{
{"name": "b","type": "uint256[]"}], "type": "tuple"}]`, {"name": "b","type": "uint256[]"}], "type": "tuple"}]`,
unpacked: struct { unpacked: struct {
A struct { A struct {
A *big.Int A *uint256.Int
B []*big.Int B []*uint256.Int
} }
B []*big.Int B []*uint256.Int
}{ }{
A: struct { A: struct {
A *big.Int A *uint256.Int
B []*big.Int B []*uint256.Int
}{big.NewInt(1), []*big.Int{big.NewInt(1), big.NewInt(2)}}, }{uint256.NewInt(1), []*uint256.Int{uint256.NewInt(1), uint256.NewInt(2)}},
B: []*big.Int{big.NewInt(1), big.NewInt(2)}}, B: []*uint256.Int{uint256.NewInt(1), uint256.NewInt(2)}},
packed: "0000000000000000000000000000000000000000000000000000000000000020" + // struct a packed: "0000000000000000000000000000000000000000000000000000000000000020" + // struct a
"0000000000000000000000000000000000000000000000000000000000000040" + // a offset "0000000000000000000000000000000000000000000000000000000000000040" + // a offset
"00000000000000000000000000000000000000000000000000000000000000e0" + // b offset "00000000000000000000000000000000000000000000000000000000000000e0" + // b offset

View file

@ -23,6 +23,7 @@ import (
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/holiman/uint256"
) )
// typeWithoutStringer is an alias for the Type type which simply doesn't implement // 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), ""}, {"int16", nil, int16(1), ""},
{"int32", nil, int32(1), ""}, {"int32", nil, int32(1), ""},
{"int64", nil, int64(1), ""}, {"int64", nil, int64(1), ""},
{"uint24", nil, big.NewInt(1), ""}, {"uint24", nil, uint256.NewInt(1), ""},
{"uint40", nil, big.NewInt(1), ""}, {"uint40", nil, uint256.NewInt(1), ""},
{"uint48", nil, big.NewInt(1), ""}, {"uint48", nil, uint256.NewInt(1), ""},
{"uint56", nil, big.NewInt(1), ""}, {"uint56", nil, uint256.NewInt(1), ""},
{"uint72", nil, big.NewInt(1), ""}, {"uint72", nil, uint256.NewInt(1), ""},
{"uint80", nil, big.NewInt(1), ""}, {"uint80", nil, uint256.NewInt(1), ""},
{"uint88", nil, big.NewInt(1), ""}, {"uint88", nil, uint256.NewInt(1), ""},
{"uint96", nil, big.NewInt(1), ""}, {"uint96", nil, uint256.NewInt(1), ""},
{"uint104", nil, big.NewInt(1), ""}, {"uint104", nil, uint256.NewInt(1), ""},
{"uint112", nil, big.NewInt(1), ""}, {"uint112", nil, uint256.NewInt(1), ""},
{"uint120", nil, big.NewInt(1), ""}, {"uint120", nil, uint256.NewInt(1), ""},
{"uint128", nil, big.NewInt(1), ""}, {"uint128", nil, uint256.NewInt(1), ""},
{"uint136", nil, big.NewInt(1), ""}, {"uint136", nil, uint256.NewInt(1), ""},
{"uint144", nil, big.NewInt(1), ""}, {"uint144", nil, uint256.NewInt(1), ""},
{"uint152", nil, big.NewInt(1), ""}, {"uint152", nil, uint256.NewInt(1), ""},
{"uint160", nil, big.NewInt(1), ""}, {"uint160", nil, uint256.NewInt(1), ""},
{"uint168", nil, big.NewInt(1), ""}, {"uint168", nil, uint256.NewInt(1), ""},
{"uint176", nil, big.NewInt(1), ""}, {"uint176", nil, uint256.NewInt(1), ""},
{"uint184", nil, big.NewInt(1), ""}, {"uint184", nil, uint256.NewInt(1), ""},
{"uint192", nil, big.NewInt(1), ""}, {"uint192", nil, uint256.NewInt(1), ""},
{"uint200", nil, big.NewInt(1), ""}, {"uint200", nil, uint256.NewInt(1), ""},
{"uint208", nil, big.NewInt(1), ""}, {"uint208", nil, uint256.NewInt(1), ""},
{"uint216", nil, big.NewInt(1), ""}, {"uint216", nil, uint256.NewInt(1), ""},
{"uint224", nil, big.NewInt(1), ""}, {"uint224", nil, uint256.NewInt(1), ""},
{"uint232", nil, big.NewInt(1), ""}, {"uint232", nil, uint256.NewInt(1), ""},
{"uint240", nil, big.NewInt(1), ""}, {"uint240", nil, uint256.NewInt(1), ""},
{"uint248", nil, big.NewInt(1), ""}, {"uint248", nil, uint256.NewInt(1), ""},
{"int24", nil, big.NewInt(1), ""}, {"int24", nil, big.NewInt(1), ""},
{"int40", nil, big.NewInt(1), ""}, {"int40", nil, big.NewInt(1), ""},
{"int48", nil, big.NewInt(1), ""}, {"int48", nil, big.NewInt(1), ""},
@ -269,19 +270,19 @@ func TestTypeCheck(t *testing.T) {
{"invalidSlice[]", nil, "", "unsupported arg type: invalidSlice"}, {"invalidSlice[]", nil, "", "unsupported arg type: invalidSlice"},
// simple tuple // simple tuple
{"tuple", []ArgumentMarshaling{{Name: "a", Type: "uint256"}, {Name: "b", Type: "uint256"}}, struct { {"tuple", []ArgumentMarshaling{{Name: "a", Type: "uint256"}, {Name: "b", Type: "uint256"}}, struct {
A *big.Int A *uint256.Int
B *big.Int B *uint256.Int
}{}, ""}, }{}, ""},
// tuple slice // tuple slice
{"tuple[]", []ArgumentMarshaling{{Name: "a", Type: "uint256"}, {Name: "b", Type: "uint256"}}, []struct { {"tuple[]", []ArgumentMarshaling{{Name: "a", Type: "uint256"}, {Name: "b", Type: "uint256"}}, []struct {
A *big.Int A *uint256.Int
B *big.Int B *uint256.Int
}{}, ""}, }{}, ""},
// tuple array // tuple array
{"tuple[2]", []ArgumentMarshaling{{Name: "a", Type: "uint256"}, {Name: "b", Type: "uint256"}}, []struct { {"tuple[2]", []ArgumentMarshaling{{Name: "a", Type: "uint256"}, {Name: "b", Type: "uint256"}}, []struct {
A *big.Int A *uint256.Int
B *big.Int B *uint256.Int
}{{big.NewInt(0), big.NewInt(0)}, {big.NewInt(0), big.NewInt(0)}}, ""}, }{{uint256.NewInt(0), uint256.NewInt(0)}, {uint256.NewInt(0), uint256.NewInt(0)}}, ""},
} { } {
typ, err := NewType(test.typ, "", test.components) typ, err := NewType(test.typ, "", test.components)
if err != nil && len(test.err) == 0 { if err != nil && len(test.err) == 0 {

View file

@ -28,6 +28,7 @@ import (
"testing" "testing"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/holiman/uint256"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -143,7 +144,7 @@ var unpackTests = []unpackTest{
def: `[{"type": "uint17"}]`, def: `[{"type": "uint17"}]`,
enc: "0000000000000000000000000000000000000000000000000000000000000001", enc: "0000000000000000000000000000000000000000000000000000000000000001",
want: uint16(0), want: uint16(0),
err: "abi: cannot unmarshal *big.Int in to uint16", err: "abi: cannot unmarshal *uint256.Int in to uint16",
}, },
{ {
def: `[{"type": "int32"}]`, def: `[{"type": "int32"}]`,
@ -343,14 +344,14 @@ func TestUnpackIntoInterfaceSetDynamicArrayOutput(t *testing.T) {
} }
type methodMultiOutput struct { type methodMultiOutput struct {
Int *big.Int Int *uint256.Int
String string String string
} }
func methodMultiReturn(require *require.Assertions) (ABI, []byte, methodMultiOutput) { func methodMultiReturn(require *require.Assertions) (ABI, []byte, methodMultiOutput) {
const definition = `[ const definition = `[
{ "name" : "multi", "type": "function", "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]` { "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)) abi, err := JSON(strings.NewReader(definition))
require.NoError(err) require.NoError(err)
@ -367,7 +368,7 @@ func TestMethodMultiReturn(t *testing.T) {
t.Parallel() t.Parallel()
type reversed struct { type reversed struct {
String string String string
Int *big.Int Int *uint256.Int
} }
newInterfaceSlice := func(len int) interface{} { newInterfaceSlice := func(len int) interface{} {
@ -376,7 +377,7 @@ func TestMethodMultiReturn(t *testing.T) {
} }
abi, data, expected := methodMultiReturn(require.New(t)) abi, data, expected := methodMultiReturn(require.New(t))
bigint := new(big.Int) UInt := new(uint256.Int)
var testCases = []struct { var testCases = []struct {
dest interface{} dest interface{}
expected interface{} expected interface{}
@ -393,17 +394,17 @@ func TestMethodMultiReturn(t *testing.T) {
"", "",
"Can unpack into reversed structure", "Can unpack into reversed structure",
}, { }, {
&[]interface{}{&bigint, new(string)}, &[]interface{}{&UInt, new(string)},
&[]interface{}{&expected.Int, &expected.String}, &[]interface{}{&expected.Int, &expected.String},
"", "",
"Can unpack into a slice", "Can unpack into a slice",
}, { }, {
&[]interface{}{&bigint, ""}, &[]interface{}{&UInt, ""},
&[]interface{}{&expected.Int, expected.String}, &[]interface{}{&expected.Int, expected.String},
"", "",
"Can unpack into a slice without indirection", "Can unpack into a slice without indirection",
}, { }, {
&[2]interface{}{&bigint, new(string)}, &[2]interface{}{&UInt, new(string)},
&[2]interface{}{&expected.Int, &expected.String}, &[2]interface{}{&expected.Int, &expected.String},
"", "",
"Can unpack into an array", "Can unpack into an array",
@ -420,7 +421,7 @@ func TestMethodMultiReturn(t *testing.T) {
}, { }, {
&[]interface{}{new(int), new(int)}, &[]interface{}{new(int), new(int)},
&[]interface{}{&expected.Int, &expected.String}, &[]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", "Can not unpack into a slice with wrong types",
}, { }, {
&[]interface{}{new(int)}, &[]interface{}{new(int)},
@ -475,8 +476,9 @@ func TestMultiReturnWithStringArray(t *testing.T) {
} }
buff := new(bytes.Buffer) buff := new(bytes.Buffer)
buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000005c1b78ea0000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000001a055690d9db80000000000000000000000000000ab1257528b3782fb40d7ed5f72e624b744dffb2f00000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000008457468657265756d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001048656c6c6f2c20457468657265756d2100000000000000000000000000000000")) buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000005c1b78ea0000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000001a055690d9db80000000000000000000000000000ab1257528b3782fb40d7ed5f72e624b744dffb2f00000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000008457468657265756d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001048656c6c6f2c20457468657265756d2100000000000000000000000000000000"))
temp, _ := new(big.Int).SetString("30000000000000000000", 10) 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") ret2, ret2Exp := new(common.Address), common.HexToAddress("ab1257528b3782fb40d7ed5f72e624b744dffb2f")
ret3, ret3Exp := new([2]string), [2]string{"Ethereum", "Hello, Ethereum!"} ret3, ret3Exp := new([2]string), [2]string{"Ethereum", "Hello, Ethereum!"}
ret4, ret4Exp := new(bool), false 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("0000000000000000000000000000000000000000000000000000000000000064")) // output[1][0] value
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000065")) // output[1][1] value buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000065")) // output[1][1] value
ret1, ret1Exp := new([]string), []string{"ethereum", "go-ethereum"} 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 { if err := abi.UnpackIntoInterface(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -614,13 +616,13 @@ func TestUnmarshal(t *testing.T) {
} }
// marshal int // marshal int
var Int *big.Int var Int *uint256.Int
err = abi.UnpackIntoInterface(&Int, "int", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) err = abi.UnpackIntoInterface(&Int, "int", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
if err != nil { if err != nil {
t.Error(err) 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) 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("0000000000000000000000000000000000000000000000000000000000000002"))
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003")) buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003"))
// marshal int array // marshal int array
var intArray [3]*big.Int var intArray [3]*uint256.Int
err = abi.UnpackIntoInterface(&intArray, "intArraySingle", buff.Bytes()) err = abi.UnpackIntoInterface(&intArray, "intArraySingle", buff.Bytes())
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
var testAgainstIntArray [3]*big.Int var testAgainstIntArray [3]*uint256.Int
testAgainstIntArray[0] = big.NewInt(1) testAgainstIntArray[0] = uint256.NewInt(1)
testAgainstIntArray[1] = big.NewInt(2) testAgainstIntArray[1] = uint256.NewInt(2)
testAgainstIntArray[2] = big.NewInt(3) testAgainstIntArray[2] = uint256.NewInt(3)
for i, Int := range intArray { for i, Int := range intArray {
if Int.Cmp(testAgainstIntArray[i]) != 0 { if Int.Cmp(testAgainstIntArray[i]) != 0 {
@ -884,42 +886,42 @@ func TestUnpackTuple(t *testing.T) {
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // s.C[1].Y buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // s.C[1].Y
type T struct { type T struct {
X *big.Int `abi:"x"` X *uint256.Int `abi:"x"`
Z *big.Int `abi:"y"` // Test whether the abi tag works. Z *uint256.Int `abi:"y"` // Test whether the abi tag works.
} }
type S struct { type S struct {
A *big.Int A *uint256.Int
B []*big.Int B []*uint256.Int
C []T C []T
} }
type Ret struct { type Ret struct {
FieldS S `abi:"s"` FieldS S `abi:"s"`
FieldT T `abi:"t"` FieldT T `abi:"t"`
A *big.Int A *uint256.Int
} }
var ret Ret var ret Ret
var expected = Ret{ var expected = Ret{
FieldS: S{ FieldS: S{
A: big.NewInt(1), A: uint256.NewInt(1),
B: []*big.Int{big.NewInt(1), big.NewInt(2)}, B: []*uint256.Int{uint256.NewInt(1), uint256.NewInt(2)},
C: []T{ C: []T{
{big.NewInt(1), big.NewInt(2)}, {uint256.NewInt(1), uint256.NewInt(2)},
{big.NewInt(2), big.NewInt(1)}, {uint256.NewInt(2), uint256.NewInt(1)},
}, },
}, },
FieldT: T{ 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()) err = abi.UnpackIntoInterface(&ret, "tuple", buff.Bytes())
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
if reflect.DeepEqual(ret, expected) { if !reflect.DeepEqual(ret, expected) {
t.Error("unexpected unpack value") t.Error("unexpected unpack value")
} }
} }
@ -1011,6 +1013,9 @@ func TestPackAndUnpackIncompatibleNumber(t *testing.T) {
panic("bug") panic("bug")
} }
maxU64Plus1 := new(big.Int).Add(maxU64, big.NewInt(1)) maxU64Plus1 := new(big.Int).Add(maxU64, big.NewInt(1))
// maxU256 := uint256.MustFromHex("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
cases := []struct { cases := []struct {
decodeType string decodeType string
inputValue *big.Int inputValue *big.Int
@ -1065,7 +1070,7 @@ func TestPackAndUnpackIncompatibleNumber(t *testing.T) {
decodeType: "uint256", decodeType: "uint256",
inputValue: maxU64Plus1, inputValue: maxU64Plus1,
err: nil, err: nil,
expectValue: maxU64Plus1, expectValue: uint256.MustFromBig(maxU64Plus1),
}, },
{ {
decodeType: "int8", decodeType: "int8",