2018-02-14 12:49:11 +00:00
|
|
|
// Copyright 2017 The go-ethereum Authors
|
2017-06-27 08:05:33 +00:00
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package abi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-10-17 11:07:08 +00:00
|
|
|
"encoding/hex"
|
2017-06-27 08:05:33 +00:00
|
|
|
"fmt"
|
2023-02-07 13:32:27 +00:00
|
|
|
"math"
|
2017-06-27 08:05:33 +00:00
|
|
|
"math/big"
|
|
|
|
"reflect"
|
2017-12-20 14:09:23 +00:00
|
|
|
"strconv"
|
2017-06-27 08:05:33 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2017-11-23 22:50:22 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-06-27 08:05:33 +00:00
|
|
|
)
|
|
|
|
|
2020-04-27 13:07:33 +00:00
|
|
|
// TestUnpack tests the general pack/unpack tests in packing_test.go
|
|
|
|
func TestUnpack(t *testing.T) {
|
|
|
|
for i, test := range packUnpackTests {
|
2020-05-05 14:30:43 +00:00
|
|
|
t.Run(strconv.Itoa(i)+" "+test.def, func(t *testing.T) {
|
2020-04-27 13:07:33 +00:00
|
|
|
//Unpack
|
|
|
|
def := fmt.Sprintf(`[{ "name" : "method", "type": "function", "outputs": %s}]`, test.def)
|
|
|
|
abi, err := JSON(strings.NewReader(def))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("invalid ABI definition %s: %v", def, err)
|
|
|
|
}
|
|
|
|
encb, err := hex.DecodeString(test.packed)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("invalid hex %s: %v", test.packed, err)
|
|
|
|
}
|
2020-09-28 12:10:26 +00:00
|
|
|
out, err := abi.Unpack("method", encb)
|
2020-04-27 13:07:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("test %d (%v) failed: %v", i, test.def, err)
|
|
|
|
return
|
|
|
|
}
|
2020-09-28 12:10:26 +00:00
|
|
|
if !reflect.DeepEqual(test.unpacked, ConvertType(out[0], test.unpacked)) {
|
|
|
|
t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, test.unpacked, out[0])
|
2020-04-27 13:07:33 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-17 11:07:08 +00:00
|
|
|
type unpackTest struct {
|
|
|
|
def string // ABI definition JSON
|
|
|
|
enc string // evm return data
|
|
|
|
want interface{} // the expected output
|
|
|
|
err string // empty or error if expected
|
|
|
|
}
|
2017-06-27 08:05:33 +00:00
|
|
|
|
2017-10-17 11:07:08 +00:00
|
|
|
func (test unpackTest) checkError(err error) error {
|
|
|
|
if err != nil {
|
|
|
|
if len(test.err) == 0 {
|
|
|
|
return fmt.Errorf("expected no err but got: %v", err)
|
|
|
|
} else if err.Error() != test.err {
|
|
|
|
return fmt.Errorf("expected err: '%v' got err: %q", test.err, err)
|
2017-06-27 08:05:33 +00:00
|
|
|
}
|
2017-10-17 11:07:08 +00:00
|
|
|
} else if len(test.err) > 0 {
|
|
|
|
return fmt.Errorf("expected err: %v but got none", test.err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-06-27 08:05:33 +00:00
|
|
|
|
2017-10-17 11:07:08 +00:00
|
|
|
var unpackTests = []unpackTest{
|
2019-10-31 13:17:51 +00:00
|
|
|
// Bools
|
2018-04-04 11:42:36 +00:00
|
|
|
{
|
|
|
|
def: `[{ "type": "bool" }]`,
|
|
|
|
enc: "0000000000000000000000000000000000000000000000000001000000000001",
|
|
|
|
want: false,
|
|
|
|
err: "abi: improperly encoded boolean value",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
def: `[{ "type": "bool" }]`,
|
|
|
|
enc: "0000000000000000000000000000000000000000000000000000000000000003",
|
|
|
|
want: false,
|
|
|
|
err: "abi: improperly encoded boolean value",
|
|
|
|
},
|
2019-10-31 13:17:51 +00:00
|
|
|
// Integers
|
2017-10-17 11:07:08 +00:00
|
|
|
{
|
|
|
|
def: `[{"type": "uint32"}]`,
|
|
|
|
enc: "0000000000000000000000000000000000000000000000000000000000000001",
|
|
|
|
want: uint16(0),
|
|
|
|
err: "abi: cannot unmarshal uint32 in to uint16",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
def: `[{"type": "uint17"}]`,
|
|
|
|
enc: "0000000000000000000000000000000000000000000000000000000000000001",
|
|
|
|
want: uint16(0),
|
|
|
|
err: "abi: cannot unmarshal *big.Int in to uint16",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
def: `[{"type": "int32"}]`,
|
|
|
|
enc: "0000000000000000000000000000000000000000000000000000000000000001",
|
|
|
|
want: int16(0),
|
|
|
|
err: "abi: cannot unmarshal int32 in to int16",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
def: `[{"type": "int17"}]`,
|
|
|
|
enc: "0000000000000000000000000000000000000000000000000000000000000001",
|
|
|
|
want: int16(0),
|
|
|
|
err: "abi: cannot unmarshal *big.Int in to int16",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
def: `[{"type": "bytes"}]`,
|
|
|
|
enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
|
2020-05-13 15:50:18 +00:00
|
|
|
want: [32]byte{1},
|
2017-10-17 11:07:08 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
def: `[{"type": "bytes32"}]`,
|
|
|
|
enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
|
|
|
|
want: []byte(nil),
|
|
|
|
err: "abi: cannot unmarshal [32]uint8 in to []uint8",
|
|
|
|
},
|
2018-12-29 10:32:58 +00:00
|
|
|
{
|
|
|
|
def: `[{"name":"___","type":"int256"}]`,
|
|
|
|
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
|
|
|
|
want: struct {
|
|
|
|
IntOne *big.Int
|
|
|
|
Intone *big.Int
|
2020-05-13 15:50:18 +00:00
|
|
|
}{IntOne: big.NewInt(1)},
|
2018-12-29 10:32:58 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
def: `[{"name":"int_one","type":"int256"},{"name":"IntOne","type":"int256"}]`,
|
|
|
|
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
|
|
|
|
want: struct {
|
|
|
|
Int1 *big.Int
|
|
|
|
Int2 *big.Int
|
|
|
|
}{},
|
|
|
|
err: "abi: multiple outputs mapping to the same struct field 'IntOne'",
|
|
|
|
},
|
2017-12-29 21:20:02 +00:00
|
|
|
{
|
|
|
|
def: `[{"name":"int","type":"int256"},{"name":"Int","type":"int256"}]`,
|
|
|
|
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
|
|
|
|
want: struct {
|
|
|
|
Int1 *big.Int
|
|
|
|
Int2 *big.Int
|
|
|
|
}{},
|
|
|
|
err: "abi: multiple outputs mapping to the same struct field 'Int'",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
def: `[{"name":"int","type":"int256"},{"name":"_int","type":"int256"}]`,
|
|
|
|
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
|
|
|
|
want: struct {
|
|
|
|
Int1 *big.Int
|
|
|
|
Int2 *big.Int
|
|
|
|
}{},
|
|
|
|
err: "abi: multiple outputs mapping to the same struct field 'Int'",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
def: `[{"name":"Int","type":"int256"},{"name":"_int","type":"int256"}]`,
|
|
|
|
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
|
|
|
|
want: struct {
|
|
|
|
Int1 *big.Int
|
|
|
|
Int2 *big.Int
|
|
|
|
}{},
|
|
|
|
err: "abi: multiple outputs mapping to the same struct field 'Int'",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
def: `[{"name":"Int","type":"int256"},{"name":"_","type":"int256"}]`,
|
|
|
|
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
|
|
|
|
want: struct {
|
|
|
|
Int1 *big.Int
|
|
|
|
Int2 *big.Int
|
|
|
|
}{},
|
|
|
|
err: "abi: purely underscored output cannot unpack to struct",
|
|
|
|
},
|
2020-04-27 13:07:33 +00:00
|
|
|
// Make sure only the first argument is consumed
|
|
|
|
{
|
|
|
|
def: `[{"name":"int_one","type":"int256"}]`,
|
|
|
|
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
|
|
|
|
want: struct {
|
|
|
|
IntOne *big.Int
|
|
|
|
}{big.NewInt(1)},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
def: `[{"name":"int__one","type":"int256"}]`,
|
|
|
|
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
|
|
|
|
want: struct {
|
|
|
|
IntOne *big.Int
|
|
|
|
}{big.NewInt(1)},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
def: `[{"name":"int_one_","type":"int256"}]`,
|
|
|
|
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
|
|
|
|
want: struct {
|
|
|
|
IntOne *big.Int
|
|
|
|
}{big.NewInt(1)},
|
|
|
|
},
|
2022-05-06 14:45:57 +00:00
|
|
|
{
|
|
|
|
def: `[{"type":"bool"}]`,
|
|
|
|
enc: "",
|
|
|
|
want: false,
|
2023-11-20 07:44:05 +00:00
|
|
|
err: "abi: attempting to unmarshal an empty string while arguments are expected",
|
2022-05-06 14:45:57 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
def: `[{"type":"bytes32","indexed":true},{"type":"uint256","indexed":false}]`,
|
|
|
|
enc: "",
|
|
|
|
want: false,
|
2023-11-20 07:44:05 +00:00
|
|
|
err: "abi: attempting to unmarshal an empty string while arguments are expected",
|
2022-05-06 14:45:57 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
def: `[{"type":"bool","indexed":true},{"type":"uint64","indexed":true}]`,
|
|
|
|
enc: "",
|
|
|
|
want: false,
|
|
|
|
},
|
2017-10-17 11:07:08 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 13:07:33 +00:00
|
|
|
// TestLocalUnpackTests runs test specially designed only for unpacking.
|
|
|
|
// All test cases that can be used to test packing and unpacking should move to packing_test.go
|
|
|
|
func TestLocalUnpackTests(t *testing.T) {
|
2017-10-17 11:07:08 +00:00
|
|
|
for i, test := range unpackTests {
|
2017-12-20 14:09:23 +00:00
|
|
|
t.Run(strconv.Itoa(i), func(t *testing.T) {
|
2020-04-27 13:07:33 +00:00
|
|
|
//Unpack
|
2020-04-15 07:23:58 +00:00
|
|
|
def := fmt.Sprintf(`[{ "name" : "method", "type": "function", "outputs": %s}]`, test.def)
|
2017-12-20 14:09:23 +00:00
|
|
|
abi, err := JSON(strings.NewReader(def))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("invalid ABI definition %s: %v", def, err)
|
|
|
|
}
|
|
|
|
encb, err := hex.DecodeString(test.enc)
|
|
|
|
if err != nil {
|
2019-10-31 13:17:51 +00:00
|
|
|
t.Fatalf("invalid hex %s: %v", test.enc, err)
|
2017-12-20 14:09:23 +00:00
|
|
|
}
|
|
|
|
outptr := reflect.New(reflect.TypeOf(test.want))
|
2020-09-28 12:10:26 +00:00
|
|
|
err = abi.UnpackIntoInterface(outptr.Interface(), "method", encb)
|
2017-12-20 14:09:23 +00:00
|
|
|
if err := test.checkError(err); err != nil {
|
|
|
|
t.Errorf("test %d (%v) failed: %v", i, test.def, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
out := outptr.Elem().Interface()
|
|
|
|
if !reflect.DeepEqual(test.want, out) {
|
|
|
|
t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, test.want, out)
|
|
|
|
}
|
|
|
|
})
|
2017-06-27 08:05:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-28 12:10:26 +00:00
|
|
|
func TestUnpackIntoInterfaceSetDynamicArrayOutput(t *testing.T) {
|
2018-12-22 10:26:49 +00:00
|
|
|
abi, err := JSON(strings.NewReader(`[{"constant":true,"inputs":[],"name":"testDynamicFixedBytes15","outputs":[{"name":"","type":"bytes15[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"testDynamicFixedBytes32","outputs":[{"name":"","type":"bytes32[]"}],"payable":false,"stateMutability":"view","type":"function"}]`))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
marshalledReturn32 = common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000230783132333435363738393000000000000000000000000000000000000000003078303938373635343332310000000000000000000000000000000000000000")
|
|
|
|
marshalledReturn15 = common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000230783031323334350000000000000000000000000000000000000000000000003078393837363534000000000000000000000000000000000000000000000000")
|
|
|
|
|
|
|
|
out32 [][32]byte
|
|
|
|
out15 [][15]byte
|
|
|
|
)
|
|
|
|
|
|
|
|
// test 32
|
2020-09-28 12:10:26 +00:00
|
|
|
err = abi.UnpackIntoInterface(&out32, "testDynamicFixedBytes32", marshalledReturn32)
|
2018-12-22 10:26:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(out32) != 2 {
|
|
|
|
t.Fatalf("expected array with 2 values, got %d", len(out32))
|
|
|
|
}
|
|
|
|
expected := common.Hex2Bytes("3078313233343536373839300000000000000000000000000000000000000000")
|
|
|
|
if !bytes.Equal(out32[0][:], expected) {
|
|
|
|
t.Errorf("expected %x, got %x\n", expected, out32[0])
|
|
|
|
}
|
|
|
|
expected = common.Hex2Bytes("3078303938373635343332310000000000000000000000000000000000000000")
|
|
|
|
if !bytes.Equal(out32[1][:], expected) {
|
|
|
|
t.Errorf("expected %x, got %x\n", expected, out32[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
// test 15
|
2020-09-28 12:10:26 +00:00
|
|
|
err = abi.UnpackIntoInterface(&out15, "testDynamicFixedBytes32", marshalledReturn15)
|
2018-12-22 10:26:49 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(out15) != 2 {
|
|
|
|
t.Fatalf("expected array with 2 values, got %d", len(out15))
|
|
|
|
}
|
|
|
|
expected = common.Hex2Bytes("307830313233343500000000000000")
|
|
|
|
if !bytes.Equal(out15[0][:], expected) {
|
|
|
|
t.Errorf("expected %x, got %x\n", expected, out15[0])
|
|
|
|
}
|
|
|
|
expected = common.Hex2Bytes("307839383736353400000000000000")
|
|
|
|
if !bytes.Equal(out15[1][:], expected) {
|
|
|
|
t.Errorf("expected %x, got %x\n", expected, out15[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-23 22:50:22 +00:00
|
|
|
type methodMultiOutput struct {
|
|
|
|
Int *big.Int
|
|
|
|
String string
|
|
|
|
}
|
|
|
|
|
|
|
|
func methodMultiReturn(require *require.Assertions) (ABI, []byte, methodMultiOutput) {
|
2017-06-27 08:05:33 +00:00
|
|
|
const definition = `[
|
2020-04-15 07:23:58 +00:00
|
|
|
{ "name" : "multi", "type": "function", "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]`
|
2017-11-23 22:50:22 +00:00
|
|
|
var expected = methodMultiOutput{big.NewInt(1), "hello"}
|
2017-06-27 08:05:33 +00:00
|
|
|
|
|
|
|
abi, err := JSON(strings.NewReader(definition))
|
2017-11-23 22:50:22 +00:00
|
|
|
require.NoError(err)
|
2017-06-27 08:05:33 +00:00
|
|
|
// using buff to make the code readable
|
|
|
|
buff := new(bytes.Buffer)
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
|
2017-11-23 22:50:22 +00:00
|
|
|
buff.Write(common.RightPadBytes([]byte(expected.String), 32))
|
|
|
|
return abi, buff.Bytes(), expected
|
|
|
|
}
|
2017-06-27 08:05:33 +00:00
|
|
|
|
2017-11-23 22:50:22 +00:00
|
|
|
func TestMethodMultiReturn(t *testing.T) {
|
|
|
|
type reversed struct {
|
2017-06-27 08:05:33 +00:00
|
|
|
String string
|
|
|
|
Int *big.Int
|
|
|
|
}
|
|
|
|
|
2019-01-23 13:36:49 +00:00
|
|
|
newInterfaceSlice := func(len int) interface{} {
|
|
|
|
slice := make([]interface{}, len)
|
|
|
|
return &slice
|
|
|
|
}
|
|
|
|
|
2017-11-23 22:50:22 +00:00
|
|
|
abi, data, expected := methodMultiReturn(require.New(t))
|
|
|
|
bigint := new(big.Int)
|
|
|
|
var testCases = []struct {
|
|
|
|
dest interface{}
|
|
|
|
expected interface{}
|
|
|
|
error string
|
|
|
|
name string
|
|
|
|
}{{
|
|
|
|
&methodMultiOutput{},
|
|
|
|
&expected,
|
|
|
|
"",
|
|
|
|
"Can unpack into structure",
|
|
|
|
}, {
|
|
|
|
&reversed{},
|
|
|
|
&reversed{expected.String, expected.Int},
|
|
|
|
"",
|
|
|
|
"Can unpack into reversed structure",
|
|
|
|
}, {
|
|
|
|
&[]interface{}{&bigint, new(string)},
|
|
|
|
&[]interface{}{&expected.Int, &expected.String},
|
|
|
|
"",
|
|
|
|
"Can unpack into a slice",
|
2022-08-19 10:15:04 +00:00
|
|
|
}, {
|
|
|
|
&[]interface{}{&bigint, ""},
|
|
|
|
&[]interface{}{&expected.Int, expected.String},
|
|
|
|
"",
|
|
|
|
"Can unpack into a slice without indirection",
|
2017-12-01 21:32:04 +00:00
|
|
|
}, {
|
|
|
|
&[2]interface{}{&bigint, new(string)},
|
|
|
|
&[2]interface{}{&expected.Int, &expected.String},
|
|
|
|
"",
|
|
|
|
"Can unpack into an array",
|
2019-01-23 13:36:49 +00:00
|
|
|
}, {
|
|
|
|
&[2]interface{}{},
|
|
|
|
&[2]interface{}{expected.Int, expected.String},
|
|
|
|
"",
|
|
|
|
"Can unpack into interface array",
|
|
|
|
}, {
|
|
|
|
newInterfaceSlice(2),
|
|
|
|
&[]interface{}{expected.Int, expected.String},
|
|
|
|
"",
|
|
|
|
"Can unpack into interface slice",
|
2017-11-23 22:50:22 +00:00
|
|
|
}, {
|
|
|
|
&[]interface{}{new(int), new(int)},
|
|
|
|
&[]interface{}{&expected.Int, &expected.String},
|
|
|
|
"abi: cannot unmarshal *big.Int in to int",
|
|
|
|
"Can not unpack into a slice with wrong types",
|
|
|
|
}, {
|
|
|
|
&[]interface{}{new(int)},
|
|
|
|
&[]interface{}{},
|
2020-05-13 15:50:18 +00:00
|
|
|
"abi: insufficient number of arguments for unpack, want 2, got 1",
|
2017-11-23 22:50:22 +00:00
|
|
|
"Can not unpack into a slice with wrong types",
|
|
|
|
}}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
require := require.New(t)
|
2020-09-28 12:10:26 +00:00
|
|
|
err := abi.UnpackIntoInterface(tc.dest, "multi", data)
|
2017-11-23 22:50:22 +00:00
|
|
|
if tc.error == "" {
|
|
|
|
require.Nil(err, "Should be able to unpack method outputs.")
|
|
|
|
require.Equal(tc.expected, tc.dest)
|
|
|
|
} else {
|
|
|
|
require.EqualError(err, tc.error)
|
|
|
|
}
|
|
|
|
})
|
2017-06-27 08:05:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-20 14:09:23 +00:00
|
|
|
func TestMultiReturnWithArray(t *testing.T) {
|
2020-04-15 07:23:58 +00:00
|
|
|
const definition = `[{"name" : "multi", "type": "function", "outputs": [{"type": "uint64[3]"}, {"type": "uint64"}]}]`
|
2017-12-20 14:09:23 +00:00
|
|
|
abi, err := JSON(strings.NewReader(definition))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
buff := new(bytes.Buffer)
|
|
|
|
buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000900000000000000000000000000000000000000000000000000000000000000090000000000000000000000000000000000000000000000000000000000000009"))
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000008"))
|
|
|
|
|
|
|
|
ret1, ret1Exp := new([3]uint64), [3]uint64{9, 9, 9}
|
|
|
|
ret2, ret2Exp := new(uint64), uint64(8)
|
2020-09-28 12:10:26 +00:00
|
|
|
if err := abi.UnpackIntoInterface(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil {
|
accounts/abi: Abi binding support for nested arrays, fixes #15648, including nested array unpack fix (#15676)
* accounts/abi/bind: support for multi-dim arrays
Also:
- reduce usage of regexes a bit.
- fix minor Java syntax problems
Fixes #15648
* accounts/abi/bind: Add some more documentation
* accounts/abi/bind: Improve code readability
* accounts/abi: bugfix for unpacking nested arrays
The code previously assumed the arrays/slices were always 1 level
deep. While the packing supports nested arrays (!!!).
The current code for unpacking doesn't return the "consumed" length, so
this fix had to work around that by calculating it (i.e. packing and
getting resulting length) after the unpacking of the array element.
It's far from ideal, but unpacking behaviour is fixed now.
* accounts/abi: Fix unpacking of nested arrays
Removed the temporary workaround of packing to calculate size, which was
incorrect for slice-like types anyway.
Full size of nested arrays is used now.
* accounts/abi: deeply nested array unpack test
Test unpacking of an array nested more than one level.
* accounts/abi: Add deeply nested array pack test
Same as the deep nested array unpack test, but the other way around.
* accounts/abi/bind: deeply nested arrays bind test
Test the usage of bindings that were generated
for methods with multi-dimensional (and not
just a single extra dimension, like foo[2][3])
array arguments and returns.
edit: trigger rebuild, CI failed to fetch linter module.
* accounts/abi/bind: improve array binding
wrapArray uses a regex now, and arrayBindingJava is improved.
* accounts/abi: Improve naming of element size func
The full step size for unpacking an array
is now retrieved with "getFullElemSize".
* accounts/abi: support nested nested array args
Previously, the code only considered the outer-size of the array,
ignoring the size of the contents. This was fine for most types,
but nested arrays are packed directly into it, and count towards
the total size. This resulted in arguments following a nested
array to replicate some of the binary contents of the array.
The fix: for arrays, calculate their complete contents size:
count the arg.Type.Elem.Size when Elem is an Array, and
repeat when their child is an array too, etc.
The count is the number of 32 byte elements, similar to how it
previously counted, but nested.
* accounts/abi: Test deep nested arr multi-arguments
Arguments with a deeply nested array should not cause the next arguments
to be read from the wrong position.
2018-03-04 22:24:17 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(*ret1, ret1Exp) {
|
|
|
|
t.Error("array result", *ret1, "!= Expected", ret1Exp)
|
|
|
|
}
|
|
|
|
if *ret2 != ret2Exp {
|
|
|
|
t.Error("int result", *ret2, "!= Expected", ret2Exp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-28 07:43:55 +00:00
|
|
|
func TestMultiReturnWithStringArray(t *testing.T) {
|
2020-04-15 07:23:58 +00:00
|
|
|
const definition = `[{"name" : "multi", "type": "function", "outputs": [{"name": "","type": "uint256[3]"},{"name": "","type": "address"},{"name": "","type": "string[2]"},{"name": "","type": "bool"}]}]`
|
2018-12-28 07:43:55 +00:00
|
|
|
abi, err := JSON(strings.NewReader(definition))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
buff := new(bytes.Buffer)
|
|
|
|
buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000005c1b78ea0000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000001a055690d9db80000000000000000000000000000ab1257528b3782fb40d7ed5f72e624b744dffb2f00000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000008457468657265756d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001048656c6c6f2c20457468657265756d2100000000000000000000000000000000"))
|
2022-06-14 12:09:48 +00:00
|
|
|
temp, _ := new(big.Int).SetString("30000000000000000000", 10)
|
2018-12-28 07:43:55 +00:00
|
|
|
ret1, ret1Exp := new([3]*big.Int), [3]*big.Int{big.NewInt(1545304298), big.NewInt(6), temp}
|
|
|
|
ret2, ret2Exp := new(common.Address), common.HexToAddress("ab1257528b3782fb40d7ed5f72e624b744dffb2f")
|
|
|
|
ret3, ret3Exp := new([2]string), [2]string{"Ethereum", "Hello, Ethereum!"}
|
|
|
|
ret4, ret4Exp := new(bool), false
|
2020-09-28 12:10:26 +00:00
|
|
|
if err := abi.UnpackIntoInterface(&[]interface{}{ret1, ret2, ret3, ret4}, "multi", buff.Bytes()); err != nil {
|
2018-12-28 07:43:55 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(*ret1, ret1Exp) {
|
|
|
|
t.Error("big.Int array result", *ret1, "!= Expected", ret1Exp)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(*ret2, ret2Exp) {
|
|
|
|
t.Error("address result", *ret2, "!= Expected", ret2Exp)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(*ret3, ret3Exp) {
|
|
|
|
t.Error("string array result", *ret3, "!= Expected", ret3Exp)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(*ret4, ret4Exp) {
|
|
|
|
t.Error("bool result", *ret4, "!= Expected", ret4Exp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultiReturnWithStringSlice(t *testing.T) {
|
2020-04-15 07:23:58 +00:00
|
|
|
const definition = `[{"name" : "multi", "type": "function", "outputs": [{"name": "","type": "string[]"},{"name": "","type": "uint256[]"}]}]`
|
2018-12-28 07:43:55 +00:00
|
|
|
abi, err := JSON(strings.NewReader(definition))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
buff := new(bytes.Buffer)
|
2019-01-10 08:59:37 +00:00
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // output[0] offset
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000120")) // output[1] offset
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // output[0] length
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // output[0][0] offset
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // output[0][1] offset
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000008")) // output[0][0] length
|
|
|
|
buff.Write(common.Hex2Bytes("657468657265756d000000000000000000000000000000000000000000000000")) // output[0][0] value
|
|
|
|
buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000b")) // output[0][1] length
|
|
|
|
buff.Write(common.Hex2Bytes("676f2d657468657265756d000000000000000000000000000000000000000000")) // output[0][1] value
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // output[1] length
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000064")) // output[1][0] value
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000065")) // output[1][1] value
|
2018-12-28 07:43:55 +00:00
|
|
|
ret1, ret1Exp := new([]string), []string{"ethereum", "go-ethereum"}
|
|
|
|
ret2, ret2Exp := new([]*big.Int), []*big.Int{big.NewInt(100), big.NewInt(101)}
|
2020-09-28 12:10:26 +00:00
|
|
|
if err := abi.UnpackIntoInterface(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil {
|
2018-12-28 07:43:55 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(*ret1, ret1Exp) {
|
|
|
|
t.Error("string slice result", *ret1, "!= Expected", ret1Exp)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(*ret2, ret2Exp) {
|
|
|
|
t.Error("uint256 slice result", *ret2, "!= Expected", ret2Exp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
accounts/abi: Abi binding support for nested arrays, fixes #15648, including nested array unpack fix (#15676)
* accounts/abi/bind: support for multi-dim arrays
Also:
- reduce usage of regexes a bit.
- fix minor Java syntax problems
Fixes #15648
* accounts/abi/bind: Add some more documentation
* accounts/abi/bind: Improve code readability
* accounts/abi: bugfix for unpacking nested arrays
The code previously assumed the arrays/slices were always 1 level
deep. While the packing supports nested arrays (!!!).
The current code for unpacking doesn't return the "consumed" length, so
this fix had to work around that by calculating it (i.e. packing and
getting resulting length) after the unpacking of the array element.
It's far from ideal, but unpacking behaviour is fixed now.
* accounts/abi: Fix unpacking of nested arrays
Removed the temporary workaround of packing to calculate size, which was
incorrect for slice-like types anyway.
Full size of nested arrays is used now.
* accounts/abi: deeply nested array unpack test
Test unpacking of an array nested more than one level.
* accounts/abi: Add deeply nested array pack test
Same as the deep nested array unpack test, but the other way around.
* accounts/abi/bind: deeply nested arrays bind test
Test the usage of bindings that were generated
for methods with multi-dimensional (and not
just a single extra dimension, like foo[2][3])
array arguments and returns.
edit: trigger rebuild, CI failed to fetch linter module.
* accounts/abi/bind: improve array binding
wrapArray uses a regex now, and arrayBindingJava is improved.
* accounts/abi: Improve naming of element size func
The full step size for unpacking an array
is now retrieved with "getFullElemSize".
* accounts/abi: support nested nested array args
Previously, the code only considered the outer-size of the array,
ignoring the size of the contents. This was fine for most types,
but nested arrays are packed directly into it, and count towards
the total size. This resulted in arguments following a nested
array to replicate some of the binary contents of the array.
The fix: for arrays, calculate their complete contents size:
count the arg.Type.Elem.Size when Elem is an Array, and
repeat when their child is an array too, etc.
The count is the number of 32 byte elements, similar to how it
previously counted, but nested.
* accounts/abi: Test deep nested arr multi-arguments
Arguments with a deeply nested array should not cause the next arguments
to be read from the wrong position.
2018-03-04 22:24:17 +00:00
|
|
|
func TestMultiReturnWithDeeplyNestedArray(t *testing.T) {
|
|
|
|
// Similar to TestMultiReturnWithArray, but with a special case in mind:
|
|
|
|
// values of nested static arrays count towards the size as well, and any element following
|
|
|
|
// after such nested array argument should be read with the correct offset,
|
|
|
|
// so that it does not read content from the previous array argument.
|
2020-04-15 07:23:58 +00:00
|
|
|
const definition = `[{"name" : "multi", "type": "function", "outputs": [{"type": "uint64[3][2][4]"}, {"type": "uint64"}]}]`
|
accounts/abi: Abi binding support for nested arrays, fixes #15648, including nested array unpack fix (#15676)
* accounts/abi/bind: support for multi-dim arrays
Also:
- reduce usage of regexes a bit.
- fix minor Java syntax problems
Fixes #15648
* accounts/abi/bind: Add some more documentation
* accounts/abi/bind: Improve code readability
* accounts/abi: bugfix for unpacking nested arrays
The code previously assumed the arrays/slices were always 1 level
deep. While the packing supports nested arrays (!!!).
The current code for unpacking doesn't return the "consumed" length, so
this fix had to work around that by calculating it (i.e. packing and
getting resulting length) after the unpacking of the array element.
It's far from ideal, but unpacking behaviour is fixed now.
* accounts/abi: Fix unpacking of nested arrays
Removed the temporary workaround of packing to calculate size, which was
incorrect for slice-like types anyway.
Full size of nested arrays is used now.
* accounts/abi: deeply nested array unpack test
Test unpacking of an array nested more than one level.
* accounts/abi: Add deeply nested array pack test
Same as the deep nested array unpack test, but the other way around.
* accounts/abi/bind: deeply nested arrays bind test
Test the usage of bindings that were generated
for methods with multi-dimensional (and not
just a single extra dimension, like foo[2][3])
array arguments and returns.
edit: trigger rebuild, CI failed to fetch linter module.
* accounts/abi/bind: improve array binding
wrapArray uses a regex now, and arrayBindingJava is improved.
* accounts/abi: Improve naming of element size func
The full step size for unpacking an array
is now retrieved with "getFullElemSize".
* accounts/abi: support nested nested array args
Previously, the code only considered the outer-size of the array,
ignoring the size of the contents. This was fine for most types,
but nested arrays are packed directly into it, and count towards
the total size. This resulted in arguments following a nested
array to replicate some of the binary contents of the array.
The fix: for arrays, calculate their complete contents size:
count the arg.Type.Elem.Size when Elem is an Array, and
repeat when their child is an array too, etc.
The count is the number of 32 byte elements, similar to how it
previously counted, but nested.
* accounts/abi: Test deep nested arr multi-arguments
Arguments with a deeply nested array should not cause the next arguments
to be read from the wrong position.
2018-03-04 22:24:17 +00:00
|
|
|
abi, err := JSON(strings.NewReader(definition))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
buff := new(bytes.Buffer)
|
|
|
|
// construct the test array, each 3 char element is joined with 61 '0' chars,
|
|
|
|
// to from the ((3 + 61) * 0.5) = 32 byte elements in the array.
|
|
|
|
buff.Write(common.Hex2Bytes(strings.Join([]string{
|
|
|
|
"", //empty, to apply the 61-char separator to the first element as well.
|
|
|
|
"111", "112", "113", "121", "122", "123",
|
|
|
|
"211", "212", "213", "221", "222", "223",
|
|
|
|
"311", "312", "313", "321", "322", "323",
|
|
|
|
"411", "412", "413", "421", "422", "423",
|
|
|
|
}, "0000000000000000000000000000000000000000000000000000000000000")))
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000009876"))
|
|
|
|
|
|
|
|
ret1, ret1Exp := new([4][2][3]uint64), [4][2][3]uint64{
|
|
|
|
{{0x111, 0x112, 0x113}, {0x121, 0x122, 0x123}},
|
|
|
|
{{0x211, 0x212, 0x213}, {0x221, 0x222, 0x223}},
|
|
|
|
{{0x311, 0x312, 0x313}, {0x321, 0x322, 0x323}},
|
|
|
|
{{0x411, 0x412, 0x413}, {0x421, 0x422, 0x423}},
|
|
|
|
}
|
|
|
|
ret2, ret2Exp := new(uint64), uint64(0x9876)
|
2020-09-28 12:10:26 +00:00
|
|
|
if err := abi.UnpackIntoInterface(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil {
|
2017-12-20 14:09:23 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(*ret1, ret1Exp) {
|
|
|
|
t.Error("array result", *ret1, "!= Expected", ret1Exp)
|
|
|
|
}
|
|
|
|
if *ret2 != ret2Exp {
|
|
|
|
t.Error("int result", *ret2, "!= Expected", ret2Exp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-27 08:05:33 +00:00
|
|
|
func TestUnmarshal(t *testing.T) {
|
|
|
|
const definition = `[
|
2020-04-15 07:23:58 +00:00
|
|
|
{ "name" : "int", "type": "function", "outputs": [ { "type": "uint256" } ] },
|
|
|
|
{ "name" : "bool", "type": "function", "outputs": [ { "type": "bool" } ] },
|
|
|
|
{ "name" : "bytes", "type": "function", "outputs": [ { "type": "bytes" } ] },
|
|
|
|
{ "name" : "fixed", "type": "function", "outputs": [ { "type": "bytes32" } ] },
|
|
|
|
{ "name" : "multi", "type": "function", "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] },
|
|
|
|
{ "name" : "intArraySingle", "type": "function", "outputs": [ { "type": "uint256[3]" } ] },
|
|
|
|
{ "name" : "addressSliceSingle", "type": "function", "outputs": [ { "type": "address[]" } ] },
|
|
|
|
{ "name" : "addressSliceDouble", "type": "function", "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] },
|
|
|
|
{ "name" : "mixedBytes", "type": "function", "stateMutability" : "view", "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]`
|
2017-06-27 08:05:33 +00:00
|
|
|
|
|
|
|
abi, err := JSON(strings.NewReader(definition))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
buff := new(bytes.Buffer)
|
|
|
|
|
2017-10-17 11:07:08 +00:00
|
|
|
// marshall mixed bytes (mixedBytes)
|
|
|
|
p0, p0Exp := []byte{}, common.Hex2Bytes("01020000000000000000")
|
|
|
|
p1, p1Exp := [32]byte{}, common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff")
|
|
|
|
mixedBytes := []interface{}{&p0, &p1}
|
|
|
|
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff"))
|
|
|
|
buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000a"))
|
|
|
|
buff.Write(common.Hex2Bytes("0102000000000000000000000000000000000000000000000000000000000000"))
|
|
|
|
|
2020-09-28 12:10:26 +00:00
|
|
|
err = abi.UnpackIntoInterface(&mixedBytes, "mixedBytes", buff.Bytes())
|
2017-11-08 10:45:52 +00:00
|
|
|
if err != nil {
|
2017-10-17 11:07:08 +00:00
|
|
|
t.Error(err)
|
|
|
|
} else {
|
2017-12-12 18:05:47 +00:00
|
|
|
if !bytes.Equal(p0, p0Exp) {
|
2017-10-17 11:07:08 +00:00
|
|
|
t.Errorf("unexpected value unpacked: want %x, got %x", p0Exp, p0)
|
|
|
|
}
|
|
|
|
|
2017-12-12 18:05:47 +00:00
|
|
|
if !bytes.Equal(p1[:], p1Exp) {
|
2017-10-17 11:07:08 +00:00
|
|
|
t.Errorf("unexpected value unpacked: want %x, got %x", p1Exp, p1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-27 08:05:33 +00:00
|
|
|
// marshal int
|
|
|
|
var Int *big.Int
|
2020-09-28 12:10:26 +00:00
|
|
|
err = abi.UnpackIntoInterface(&Int, "int", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
|
2017-06-27 08:05:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if Int == nil || Int.Cmp(big.NewInt(1)) != 0 {
|
|
|
|
t.Error("expected Int to be 1 got", Int)
|
|
|
|
}
|
|
|
|
|
|
|
|
// marshal bool
|
|
|
|
var Bool bool
|
2020-09-28 12:10:26 +00:00
|
|
|
err = abi.UnpackIntoInterface(&Bool, "bool", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
|
2017-06-27 08:05:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !Bool {
|
|
|
|
t.Error("expected Bool to be true")
|
|
|
|
}
|
|
|
|
|
|
|
|
// marshal dynamic bytes max length 32
|
2017-10-17 11:07:08 +00:00
|
|
|
buff.Reset()
|
2017-06-27 08:05:33 +00:00
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
|
|
|
|
bytesOut := common.RightPadBytes([]byte("hello"), 32)
|
|
|
|
buff.Write(bytesOut)
|
|
|
|
|
|
|
|
var Bytes []byte
|
2020-09-28 12:10:26 +00:00
|
|
|
err = abi.UnpackIntoInterface(&Bytes, "bytes", buff.Bytes())
|
2017-06-27 08:05:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(Bytes, bytesOut) {
|
|
|
|
t.Errorf("expected %x got %x", bytesOut, Bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// marshall dynamic bytes max length 64
|
|
|
|
buff.Reset()
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
|
|
|
|
bytesOut = common.RightPadBytes([]byte("hello"), 64)
|
|
|
|
buff.Write(bytesOut)
|
|
|
|
|
2020-09-28 12:10:26 +00:00
|
|
|
err = abi.UnpackIntoInterface(&Bytes, "bytes", buff.Bytes())
|
2017-06-27 08:05:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(Bytes, bytesOut) {
|
|
|
|
t.Errorf("expected %x got %x", bytesOut, Bytes)
|
|
|
|
}
|
|
|
|
|
2017-10-17 11:07:08 +00:00
|
|
|
// marshall dynamic bytes max length 64
|
2017-06-27 08:05:33 +00:00
|
|
|
buff.Reset()
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
|
|
|
|
buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000003f"))
|
2017-10-17 11:07:08 +00:00
|
|
|
bytesOut = common.RightPadBytes([]byte("hello"), 64)
|
2017-06-27 08:05:33 +00:00
|
|
|
buff.Write(bytesOut)
|
|
|
|
|
2020-09-28 12:10:26 +00:00
|
|
|
err = abi.UnpackIntoInterface(&Bytes, "bytes", buff.Bytes())
|
2017-06-27 08:05:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2017-10-17 11:07:08 +00:00
|
|
|
if !bytes.Equal(Bytes, bytesOut[:len(bytesOut)-1]) {
|
|
|
|
t.Errorf("expected %x got %x", bytesOut[:len(bytesOut)-1], Bytes)
|
2017-06-27 08:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// marshal dynamic bytes output empty
|
2020-09-28 12:10:26 +00:00
|
|
|
err = abi.UnpackIntoInterface(&Bytes, "bytes", nil)
|
2017-06-27 08:05:33 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Error("expected error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// marshal dynamic bytes length 5
|
|
|
|
buff.Reset()
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
|
|
|
|
buff.Write(common.RightPadBytes([]byte("hello"), 32))
|
|
|
|
|
2020-09-28 12:10:26 +00:00
|
|
|
err = abi.UnpackIntoInterface(&Bytes, "bytes", buff.Bytes())
|
2017-06-27 08:05:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(Bytes, []byte("hello")) {
|
|
|
|
t.Errorf("expected %x got %x", bytesOut, Bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// marshal dynamic bytes length 5
|
|
|
|
buff.Reset()
|
|
|
|
buff.Write(common.RightPadBytes([]byte("hello"), 32))
|
|
|
|
|
|
|
|
var hash common.Hash
|
2020-09-28 12:10:26 +00:00
|
|
|
err = abi.UnpackIntoInterface(&hash, "fixed", buff.Bytes())
|
2017-06-27 08:05:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
helloHash := common.BytesToHash(common.RightPadBytes([]byte("hello"), 32))
|
|
|
|
if hash != helloHash {
|
|
|
|
t.Errorf("Expected %x to equal %x", hash, helloHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// marshal error
|
|
|
|
buff.Reset()
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
|
2020-09-28 12:10:26 +00:00
|
|
|
err = abi.UnpackIntoInterface(&Bytes, "bytes", buff.Bytes())
|
2017-06-27 08:05:33 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Error("expected error")
|
|
|
|
}
|
|
|
|
|
2020-09-28 12:10:26 +00:00
|
|
|
err = abi.UnpackIntoInterface(&Bytes, "multi", make([]byte, 64))
|
2017-06-27 08:05:33 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Error("expected error")
|
|
|
|
}
|
|
|
|
|
|
|
|
buff.Reset()
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"))
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003"))
|
|
|
|
// marshal int array
|
|
|
|
var intArray [3]*big.Int
|
2020-09-28 12:10:26 +00:00
|
|
|
err = abi.UnpackIntoInterface(&intArray, "intArraySingle", buff.Bytes())
|
2017-06-27 08:05:33 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
for i, Int := range intArray {
|
|
|
|
if Int.Cmp(testAgainstIntArray[i]) != 0 {
|
|
|
|
t.Errorf("expected %v, got %v", testAgainstIntArray[i], Int)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// marshal address slice
|
|
|
|
buff.Reset()
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) // offset
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
|
|
|
|
|
|
|
|
var outAddr []common.Address
|
2020-09-28 12:10:26 +00:00
|
|
|
err = abi.UnpackIntoInterface(&outAddr, "addressSliceSingle", buff.Bytes())
|
2017-06-27 08:05:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("didn't expect error:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(outAddr) != 1 {
|
|
|
|
t.Fatal("expected 1 item, got", len(outAddr))
|
|
|
|
}
|
|
|
|
|
|
|
|
if outAddr[0] != (common.Address{1}) {
|
|
|
|
t.Errorf("expected %x, got %x", common.Address{1}, outAddr[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
// marshal multiple address slice
|
|
|
|
buff.Reset()
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // offset
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // offset
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // size
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000200000000000000000000000000000000000000"))
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000300000000000000000000000000000000000000"))
|
|
|
|
|
|
|
|
var outAddrStruct struct {
|
|
|
|
A []common.Address
|
|
|
|
B []common.Address
|
|
|
|
}
|
2020-09-28 12:10:26 +00:00
|
|
|
err = abi.UnpackIntoInterface(&outAddrStruct, "addressSliceDouble", buff.Bytes())
|
2017-06-27 08:05:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("didn't expect error:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(outAddrStruct.A) != 1 {
|
|
|
|
t.Fatal("expected 1 item, got", len(outAddrStruct.A))
|
|
|
|
}
|
|
|
|
|
|
|
|
if outAddrStruct.A[0] != (common.Address{1}) {
|
|
|
|
t.Errorf("expected %x, got %x", common.Address{1}, outAddrStruct.A[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(outAddrStruct.B) != 2 {
|
|
|
|
t.Fatal("expected 1 item, got", len(outAddrStruct.B))
|
|
|
|
}
|
|
|
|
|
|
|
|
if outAddrStruct.B[0] != (common.Address{2}) {
|
|
|
|
t.Errorf("expected %x, got %x", common.Address{2}, outAddrStruct.B[0])
|
|
|
|
}
|
|
|
|
if outAddrStruct.B[1] != (common.Address{3}) {
|
|
|
|
t.Errorf("expected %x, got %x", common.Address{3}, outAddrStruct.B[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
// marshal invalid address slice
|
|
|
|
buff.Reset()
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000100"))
|
|
|
|
|
2020-09-28 12:10:26 +00:00
|
|
|
err = abi.UnpackIntoInterface(&outAddr, "addressSliceSingle", buff.Bytes())
|
2017-06-27 08:05:33 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected error:", err)
|
|
|
|
}
|
|
|
|
}
|
2018-01-13 15:03:24 +00:00
|
|
|
|
2019-01-10 08:59:37 +00:00
|
|
|
func TestUnpackTuple(t *testing.T) {
|
2020-04-15 07:23:58 +00:00
|
|
|
const simpleTuple = `[{"name":"tuple","type":"function","outputs":[{"type":"tuple","name":"ret","components":[{"type":"int256","name":"a"},{"type":"int256","name":"b"}]}]}]`
|
2019-01-10 08:59:37 +00:00
|
|
|
abi, err := JSON(strings.NewReader(simpleTuple))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
buff := new(bytes.Buffer)
|
|
|
|
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // ret[a] = 1
|
|
|
|
buff.Write(common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")) // ret[b] = -1
|
|
|
|
|
2019-07-03 10:17:43 +00:00
|
|
|
// If the result is single tuple, use struct as return value container directly.
|
2021-09-22 08:18:18 +00:00
|
|
|
type v struct {
|
2019-01-10 08:59:37 +00:00
|
|
|
A *big.Int
|
|
|
|
B *big.Int
|
2021-09-22 08:18:18 +00:00
|
|
|
}
|
|
|
|
type r struct {
|
|
|
|
Result v
|
|
|
|
}
|
|
|
|
var ret0 = new(r)
|
|
|
|
err = abi.UnpackIntoInterface(ret0, "tuple", buff.Bytes())
|
2019-01-10 08:59:37 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else {
|
2021-09-22 08:18:18 +00:00
|
|
|
if ret0.Result.A.Cmp(big.NewInt(1)) != 0 {
|
|
|
|
t.Errorf("unexpected value unpacked: want %x, got %x", 1, ret0.Result.A)
|
2019-01-10 08:59:37 +00:00
|
|
|
}
|
2021-09-22 08:18:18 +00:00
|
|
|
if ret0.Result.B.Cmp(big.NewInt(-1)) != 0 {
|
|
|
|
t.Errorf("unexpected value unpacked: want %x, got %x", -1, ret0.Result.B)
|
2019-01-10 08:59:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test nested tuple
|
2020-04-15 07:23:58 +00:00
|
|
|
const nestedTuple = `[{"name":"tuple","type":"function","outputs":[
|
2019-01-10 08:59:37 +00:00
|
|
|
{"type":"tuple","name":"s","components":[{"type":"uint256","name":"a"},{"type":"uint256[]","name":"b"},{"type":"tuple[]","name":"c","components":[{"name":"x", "type":"uint256"},{"name":"y","type":"uint256"}]}]},
|
|
|
|
{"type":"tuple","name":"t","components":[{"name":"x", "type":"uint256"},{"name":"y","type":"uint256"}]},
|
|
|
|
{"type":"uint256","name":"a"}
|
|
|
|
]}]`
|
|
|
|
|
|
|
|
abi, err = JSON(strings.NewReader(nestedTuple))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
buff.Reset()
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // s offset
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000000")) // t.X = 0
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // t.Y = 1
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // a = 1
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // s.A = 1
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000060")) // s.B offset
|
|
|
|
buff.Write(common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000c0")) // s.C offset
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.B length
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // s.B[0] = 1
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.B[0] = 2
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.C length
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // s.C[0].X
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.C[0].Y
|
|
|
|
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.C[1].X
|
|
|
|
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.
|
|
|
|
}
|
|
|
|
|
|
|
|
type S struct {
|
|
|
|
A *big.Int
|
|
|
|
B []*big.Int
|
|
|
|
C []T
|
|
|
|
}
|
|
|
|
|
|
|
|
type Ret struct {
|
|
|
|
FieldS S `abi:"s"`
|
|
|
|
FieldT T `abi:"t"`
|
|
|
|
A *big.Int
|
|
|
|
}
|
|
|
|
var ret Ret
|
|
|
|
var expected = Ret{
|
|
|
|
FieldS: S{
|
|
|
|
A: big.NewInt(1),
|
|
|
|
B: []*big.Int{big.NewInt(1), big.NewInt(2)},
|
|
|
|
C: []T{
|
|
|
|
{big.NewInt(1), big.NewInt(2)},
|
|
|
|
{big.NewInt(2), big.NewInt(1)},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
FieldT: T{
|
|
|
|
big.NewInt(0), big.NewInt(1),
|
|
|
|
},
|
|
|
|
A: big.NewInt(1),
|
|
|
|
}
|
|
|
|
|
2020-09-28 12:10:26 +00:00
|
|
|
err = abi.UnpackIntoInterface(&ret, "tuple", buff.Bytes())
|
2019-01-10 08:59:37 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if reflect.DeepEqual(ret, expected) {
|
|
|
|
t.Error("unexpected unpack value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-13 15:03:24 +00:00
|
|
|
func TestOOMMaliciousInput(t *testing.T) {
|
|
|
|
oomTests := []unpackTest{
|
|
|
|
{
|
|
|
|
def: `[{"type": "uint8[]"}]`,
|
|
|
|
enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000003" + // num elems
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
|
|
|
|
},
|
|
|
|
{ // Length larger than 64 bits
|
|
|
|
def: `[{"type": "uint8[]"}]`,
|
|
|
|
enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
|
|
|
|
"00ffffffffffffffffffffffffffffffffffffffffffffff0000000000000002" + // num elems
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
|
|
|
|
},
|
|
|
|
{ // Offset very large (over 64 bits)
|
|
|
|
def: `[{"type": "uint8[]"}]`,
|
|
|
|
enc: "00ffffffffffffffffffffffffffffffffffffffffffffff0000000000000020" + // offset
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000002" + // num elems
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
|
|
|
|
},
|
|
|
|
{ // Offset very large (below 64 bits)
|
|
|
|
def: `[{"type": "uint8[]"}]`,
|
|
|
|
enc: "0000000000000000000000000000000000000000000000007ffffffffff00020" + // offset
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000002" + // num elems
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
|
|
|
|
},
|
|
|
|
{ // Offset negative (as 64 bit)
|
|
|
|
def: `[{"type": "uint8[]"}]`,
|
|
|
|
enc: "000000000000000000000000000000000000000000000000f000000000000020" + // offset
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000002" + // num elems
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
|
|
|
|
},
|
|
|
|
|
|
|
|
{ // Negative length
|
|
|
|
def: `[{"type": "uint8[]"}]`,
|
|
|
|
enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
|
|
|
|
"000000000000000000000000000000000000000000000000f000000000000002" + // num elems
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
|
|
|
|
},
|
|
|
|
{ // Very large length
|
|
|
|
def: `[{"type": "uint8[]"}]`,
|
|
|
|
enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
|
|
|
|
"0000000000000000000000000000000000000000000000007fffffffff000002" + // num elems
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for i, test := range oomTests {
|
2020-04-15 07:23:58 +00:00
|
|
|
def := fmt.Sprintf(`[{ "name" : "method", "type": "function", "outputs": %s}]`, test.def)
|
2018-01-13 15:03:24 +00:00
|
|
|
abi, err := JSON(strings.NewReader(def))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("invalid ABI definition %s: %v", def, err)
|
|
|
|
}
|
|
|
|
encb, err := hex.DecodeString(test.enc)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("invalid hex: %s" + test.enc)
|
|
|
|
}
|
|
|
|
_, err = abi.Methods["method"].Outputs.UnpackValues(encb)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected error on malicious input, test %d", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-07 13:32:27 +00:00
|
|
|
|
|
|
|
func TestPackAndUnpackIncompatibleNumber(t *testing.T) {
|
|
|
|
var encodeABI Arguments
|
|
|
|
uint256Ty, err := NewType("uint256", "", nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
encodeABI = Arguments{
|
|
|
|
{Type: uint256Ty},
|
|
|
|
}
|
|
|
|
|
|
|
|
maxU64, ok := new(big.Int).SetString(strconv.FormatUint(math.MaxUint64, 10), 10)
|
|
|
|
if !ok {
|
|
|
|
panic("bug")
|
|
|
|
}
|
|
|
|
maxU64Plus1 := new(big.Int).Add(maxU64, big.NewInt(1))
|
|
|
|
cases := []struct {
|
|
|
|
decodeType string
|
|
|
|
inputValue *big.Int
|
|
|
|
err error
|
|
|
|
expectValue interface{}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
decodeType: "uint8",
|
|
|
|
inputValue: big.NewInt(math.MaxUint8 + 1),
|
|
|
|
err: errBadUint8,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
decodeType: "uint8",
|
|
|
|
inputValue: big.NewInt(math.MaxUint8),
|
|
|
|
err: nil,
|
|
|
|
expectValue: uint8(math.MaxUint8),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
decodeType: "uint16",
|
|
|
|
inputValue: big.NewInt(math.MaxUint16 + 1),
|
|
|
|
err: errBadUint16,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
decodeType: "uint16",
|
|
|
|
inputValue: big.NewInt(math.MaxUint16),
|
|
|
|
err: nil,
|
|
|
|
expectValue: uint16(math.MaxUint16),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
decodeType: "uint32",
|
|
|
|
inputValue: big.NewInt(math.MaxUint32 + 1),
|
|
|
|
err: errBadUint32,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
decodeType: "uint32",
|
|
|
|
inputValue: big.NewInt(math.MaxUint32),
|
|
|
|
err: nil,
|
|
|
|
expectValue: uint32(math.MaxUint32),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
decodeType: "uint64",
|
|
|
|
inputValue: maxU64Plus1,
|
|
|
|
err: errBadUint64,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
decodeType: "uint64",
|
|
|
|
inputValue: maxU64,
|
|
|
|
err: nil,
|
|
|
|
expectValue: uint64(math.MaxUint64),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
decodeType: "uint256",
|
|
|
|
inputValue: maxU64Plus1,
|
|
|
|
err: nil,
|
|
|
|
expectValue: maxU64Plus1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
decodeType: "int8",
|
|
|
|
inputValue: big.NewInt(math.MaxInt8 + 1),
|
|
|
|
err: errBadInt8,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
decodeType: "int8",
|
|
|
|
inputValue: big.NewInt(math.MinInt8 - 1),
|
|
|
|
err: errBadInt8,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
decodeType: "int8",
|
|
|
|
inputValue: big.NewInt(math.MaxInt8),
|
|
|
|
err: nil,
|
|
|
|
expectValue: int8(math.MaxInt8),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
decodeType: "int16",
|
|
|
|
inputValue: big.NewInt(math.MaxInt16 + 1),
|
|
|
|
err: errBadInt16,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
decodeType: "int16",
|
|
|
|
inputValue: big.NewInt(math.MinInt16 - 1),
|
|
|
|
err: errBadInt16,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
decodeType: "int16",
|
|
|
|
inputValue: big.NewInt(math.MaxInt16),
|
|
|
|
err: nil,
|
|
|
|
expectValue: int16(math.MaxInt16),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
decodeType: "int32",
|
|
|
|
inputValue: big.NewInt(math.MaxInt32 + 1),
|
|
|
|
err: errBadInt32,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
decodeType: "int32",
|
|
|
|
inputValue: big.NewInt(math.MinInt32 - 1),
|
|
|
|
err: errBadInt32,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
decodeType: "int32",
|
|
|
|
inputValue: big.NewInt(math.MaxInt32),
|
|
|
|
err: nil,
|
|
|
|
expectValue: int32(math.MaxInt32),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
decodeType: "int64",
|
|
|
|
inputValue: new(big.Int).Add(big.NewInt(math.MaxInt64), big.NewInt(1)),
|
|
|
|
err: errBadInt64,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
decodeType: "int64",
|
|
|
|
inputValue: new(big.Int).Sub(big.NewInt(math.MinInt64), big.NewInt(1)),
|
|
|
|
err: errBadInt64,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
decodeType: "int64",
|
|
|
|
inputValue: big.NewInt(math.MaxInt64),
|
|
|
|
err: nil,
|
|
|
|
expectValue: int64(math.MaxInt64),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for i, testCase := range cases {
|
|
|
|
packed, err := encodeABI.Pack(testCase.inputValue)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
ty, err := NewType(testCase.decodeType, "", nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
decodeABI := Arguments{
|
|
|
|
{Type: ty},
|
|
|
|
}
|
|
|
|
decoded, err := decodeABI.Unpack(packed)
|
|
|
|
if err != testCase.err {
|
|
|
|
t.Fatalf("Expected error %v, actual error %v. case %d", testCase.err, err, i)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(decoded[0], testCase.expectValue) {
|
|
|
|
t.Fatalf("Expected value %v, actual value %v", testCase.expectValue, decoded[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|