2018-02-14 06:49:11 -06:00
|
|
|
// Copyright 2017 The go-ethereum Authors
|
2017-06-27 03:05:33 -05: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"
|
|
|
|
"math"
|
|
|
|
"math/big"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPack(t *testing.T) {
|
|
|
|
for i, test := range []struct {
|
2019-01-10 02:59:37 -06:00
|
|
|
typ string
|
|
|
|
components []ArgumentMarshaling
|
|
|
|
input interface{}
|
|
|
|
output []byte
|
2017-06-27 03:05:33 -05:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"uint8",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
uint8(2),
|
|
|
|
common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"uint8[]",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[]uint8{1, 2},
|
|
|
|
common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"uint16",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
uint16(2),
|
|
|
|
common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"uint16[]",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[]uint16{1, 2},
|
|
|
|
common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"uint32",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
uint32(2),
|
|
|
|
common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"uint32[]",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[]uint32{1, 2},
|
|
|
|
common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"uint64",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
uint64(2),
|
|
|
|
common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"uint64[]",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[]uint64{1, 2},
|
|
|
|
common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"uint256",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
big.NewInt(2),
|
|
|
|
common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"uint256[]",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[]*big.Int{big.NewInt(1), big.NewInt(2)},
|
|
|
|
common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"int8",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
int8(2),
|
|
|
|
common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"int8[]",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[]int8{1, 2},
|
|
|
|
common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"int16",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
int16(2),
|
|
|
|
common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"int16[]",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[]int16{1, 2},
|
|
|
|
common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"int32",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
int32(2),
|
|
|
|
common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"int32[]",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[]int32{1, 2},
|
|
|
|
common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"int64",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
int64(2),
|
|
|
|
common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"int64[]",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[]int64{1, 2},
|
|
|
|
common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"int256",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
big.NewInt(2),
|
|
|
|
common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"int256[]",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[]*big.Int{big.NewInt(1), big.NewInt(2)},
|
|
|
|
common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bytes1",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[1]byte{1},
|
|
|
|
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bytes2",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[2]byte{1},
|
|
|
|
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bytes3",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[3]byte{1},
|
|
|
|
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bytes4",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[4]byte{1},
|
|
|
|
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bytes5",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[5]byte{1},
|
|
|
|
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bytes6",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[6]byte{1},
|
|
|
|
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bytes7",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[7]byte{1},
|
|
|
|
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bytes8",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[8]byte{1},
|
|
|
|
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bytes9",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[9]byte{1},
|
|
|
|
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bytes10",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[10]byte{1},
|
|
|
|
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bytes11",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[11]byte{1},
|
|
|
|
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bytes12",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[12]byte{1},
|
|
|
|
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bytes13",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[13]byte{1},
|
|
|
|
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bytes14",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[14]byte{1},
|
|
|
|
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bytes15",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[15]byte{1},
|
|
|
|
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bytes16",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[16]byte{1},
|
|
|
|
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bytes17",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[17]byte{1},
|
|
|
|
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bytes18",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[18]byte{1},
|
|
|
|
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bytes19",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[19]byte{1},
|
|
|
|
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bytes20",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[20]byte{1},
|
|
|
|
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bytes21",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[21]byte{1},
|
|
|
|
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bytes22",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[22]byte{1},
|
|
|
|
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bytes23",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[23]byte{1},
|
|
|
|
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bytes24",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[24]byte{1},
|
|
|
|
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bytes25",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[25]byte{1},
|
|
|
|
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bytes26",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[26]byte{1},
|
|
|
|
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bytes27",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[27]byte{1},
|
|
|
|
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bytes28",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[28]byte{1},
|
|
|
|
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bytes29",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[29]byte{1},
|
|
|
|
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bytes30",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[30]byte{1},
|
|
|
|
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bytes31",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[31]byte{1},
|
|
|
|
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bytes32",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[32]byte{1},
|
|
|
|
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
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 16:24:17 -06:00
|
|
|
{
|
|
|
|
"uint32[2][3][4]",
|
2019-01-10 02:59:37 -06:00
|
|
|
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 16:24:17 -06:00
|
|
|
[4][3][2]uint32{{{1, 2}, {3, 4}, {5, 6}}, {{7, 8}, {9, 10}, {11, 12}}, {{13, 14}, {15, 16}, {17, 18}}, {{19, 20}, {21, 22}, {23, 24}}},
|
|
|
|
common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000f000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000001300000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000015000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000170000000000000000000000000000000000000000000000000000000000000018"),
|
|
|
|
},
|
2017-06-27 03:05:33 -05:00
|
|
|
{
|
|
|
|
"address[]",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[]common.Address{{1}, {2}},
|
|
|
|
common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bytes32[]",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[]common.Hash{{1}, {2}},
|
|
|
|
common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000201000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"function",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
[24]byte{1},
|
|
|
|
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"string",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2017-06-27 03:05:33 -05:00
|
|
|
"foobar",
|
|
|
|
common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000006666f6f6261720000000000000000000000000000000000000000000000000000"),
|
|
|
|
},
|
2018-12-04 08:27:55 -06:00
|
|
|
{
|
|
|
|
"string[]",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2018-12-04 08:27:55 -06:00
|
|
|
[]string{"hello", "foobar"},
|
|
|
|
common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002" + // len(array) = 2
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i = 0
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000080" + // offset 128 to i = 1
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000005" + // len(str[0]) = 5
|
|
|
|
"68656c6c6f000000000000000000000000000000000000000000000000000000" + // str[0]
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000006" + // len(str[1]) = 6
|
|
|
|
"666f6f6261720000000000000000000000000000000000000000000000000000"), // str[1]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"string[2]",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2018-12-04 08:27:55 -06:00
|
|
|
[]string{"hello", "foobar"},
|
|
|
|
common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040" + // offset to i = 0
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000080" + // offset to i = 1
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000005" + // len(str[0]) = 5
|
|
|
|
"68656c6c6f000000000000000000000000000000000000000000000000000000" + // str[0]
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000006" + // len(str[1]) = 6
|
|
|
|
"666f6f6261720000000000000000000000000000000000000000000000000000"), // str[1]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bytes32[][]",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2018-12-04 08:27:55 -06:00
|
|
|
[][]common.Hash{{{1}, {2}}, {{3}, {4}, {5}}},
|
|
|
|
common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002" + // len(array) = 2
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i = 0
|
|
|
|
"00000000000000000000000000000000000000000000000000000000000000a0" + // offset 160 to i = 1
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000002" + // len(array[0]) = 2
|
|
|
|
"0100000000000000000000000000000000000000000000000000000000000000" + // array[0][0]
|
|
|
|
"0200000000000000000000000000000000000000000000000000000000000000" + // array[0][1]
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000003" + // len(array[1]) = 3
|
|
|
|
"0300000000000000000000000000000000000000000000000000000000000000" + // array[1][0]
|
|
|
|
"0400000000000000000000000000000000000000000000000000000000000000" + // array[1][1]
|
|
|
|
"0500000000000000000000000000000000000000000000000000000000000000"), // array[1][2]
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"bytes32[][2]",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2018-12-04 08:27:55 -06:00
|
|
|
[][]common.Hash{{{1}, {2}}, {{3}, {4}, {5}}},
|
|
|
|
common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i = 0
|
|
|
|
"00000000000000000000000000000000000000000000000000000000000000a0" + // offset 160 to i = 1
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000002" + // len(array[0]) = 2
|
|
|
|
"0100000000000000000000000000000000000000000000000000000000000000" + // array[0][0]
|
|
|
|
"0200000000000000000000000000000000000000000000000000000000000000" + // array[0][1]
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000003" + // len(array[1]) = 3
|
|
|
|
"0300000000000000000000000000000000000000000000000000000000000000" + // array[1][0]
|
|
|
|
"0400000000000000000000000000000000000000000000000000000000000000" + // array[1][1]
|
|
|
|
"0500000000000000000000000000000000000000000000000000000000000000"), // array[1][2]
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"bytes32[3][2]",
|
2019-01-10 02:59:37 -06:00
|
|
|
nil,
|
2018-12-04 08:27:55 -06:00
|
|
|
[][]common.Hash{{{1}, {2}, {3}}, {{3}, {4}, {5}}},
|
|
|
|
common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000" + // array[0][0]
|
|
|
|
"0200000000000000000000000000000000000000000000000000000000000000" + // array[0][1]
|
|
|
|
"0300000000000000000000000000000000000000000000000000000000000000" + // array[0][2]
|
|
|
|
"0300000000000000000000000000000000000000000000000000000000000000" + // array[1][0]
|
|
|
|
"0400000000000000000000000000000000000000000000000000000000000000" + // array[1][1]
|
|
|
|
"0500000000000000000000000000000000000000000000000000000000000000"), // array[1][2]
|
|
|
|
},
|
2019-01-10 02:59:37 -06:00
|
|
|
{
|
|
|
|
// static tuple
|
|
|
|
"tuple",
|
|
|
|
[]ArgumentMarshaling{
|
|
|
|
{Name: "a", Type: "int64"},
|
|
|
|
{Name: "b", Type: "int256"},
|
|
|
|
{Name: "c", Type: "int256"},
|
|
|
|
{Name: "d", Type: "bool"},
|
|
|
|
{Name: "e", Type: "bytes32[3][2]"},
|
|
|
|
},
|
|
|
|
struct {
|
|
|
|
A int64
|
|
|
|
B *big.Int
|
|
|
|
C *big.Int
|
|
|
|
D bool
|
|
|
|
E [][]common.Hash
|
|
|
|
}{1, big.NewInt(1), big.NewInt(-1), true, [][]common.Hash{{{1}, {2}, {3}}, {{3}, {4}, {5}}}},
|
|
|
|
common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001" + // struct[a]
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000001" + // struct[b]
|
|
|
|
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + // struct[c]
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000001" + // struct[d]
|
|
|
|
"0100000000000000000000000000000000000000000000000000000000000000" + // struct[e] array[0][0]
|
|
|
|
"0200000000000000000000000000000000000000000000000000000000000000" + // struct[e] array[0][1]
|
|
|
|
"0300000000000000000000000000000000000000000000000000000000000000" + // struct[e] array[0][2]
|
|
|
|
"0300000000000000000000000000000000000000000000000000000000000000" + // struct[e] array[1][0]
|
|
|
|
"0400000000000000000000000000000000000000000000000000000000000000" + // struct[e] array[1][1]
|
|
|
|
"0500000000000000000000000000000000000000000000000000000000000000"), // struct[e] array[1][2]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// dynamic tuple
|
|
|
|
"tuple",
|
|
|
|
[]ArgumentMarshaling{
|
|
|
|
{Name: "a", Type: "string"},
|
|
|
|
{Name: "b", Type: "int64"},
|
|
|
|
{Name: "c", Type: "bytes"},
|
|
|
|
{Name: "d", Type: "string[]"},
|
|
|
|
{Name: "e", Type: "int256[]"},
|
|
|
|
{Name: "f", Type: "address[]"},
|
|
|
|
},
|
|
|
|
struct {
|
|
|
|
FieldA string `abi:"a"` // Test whether abi tag works
|
|
|
|
FieldB int64 `abi:"b"`
|
|
|
|
C []byte
|
|
|
|
D []string
|
|
|
|
E []*big.Int
|
|
|
|
F []common.Address
|
|
|
|
}{"foobar", 1, []byte{1}, []string{"foo", "bar"}, []*big.Int{big.NewInt(1), big.NewInt(-1)}, []common.Address{{1}, {2}}},
|
|
|
|
common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000c0" + // struct[a] offset
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000001" + // struct[b]
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000100" + // struct[c] offset
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000140" + // struct[d] offset
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000220" + // struct[e] offset
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000280" + // struct[f] offset
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000006" + // struct[a] length
|
|
|
|
"666f6f6261720000000000000000000000000000000000000000000000000000" + // struct[a] "foobar"
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000001" + // struct[c] length
|
|
|
|
"0100000000000000000000000000000000000000000000000000000000000000" + // []byte{1}
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000002" + // struct[d] length
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000040" + // foo offset
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000080" + // bar offset
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000003" + // foo length
|
|
|
|
"666f6f0000000000000000000000000000000000000000000000000000000000" + // foo
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000003" + // bar offset
|
|
|
|
"6261720000000000000000000000000000000000000000000000000000000000" + // bar
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000002" + // struct[e] length
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000001" + // 1
|
|
|
|
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + // -1
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000002" + // struct[f] length
|
|
|
|
"0000000000000000000000000100000000000000000000000000000000000000" + // common.Address{1}
|
|
|
|
"0000000000000000000000000200000000000000000000000000000000000000"), // common.Address{2}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// nested tuple
|
|
|
|
"tuple",
|
|
|
|
[]ArgumentMarshaling{
|
|
|
|
{Name: "a", Type: "tuple", Components: []ArgumentMarshaling{{Name: "a", Type: "uint256"}, {Name: "b", Type: "uint256[]"}}},
|
|
|
|
{Name: "b", Type: "int256[]"},
|
|
|
|
},
|
|
|
|
struct {
|
|
|
|
A struct {
|
|
|
|
FieldA *big.Int `abi:"a"`
|
|
|
|
B []*big.Int
|
|
|
|
}
|
|
|
|
B []*big.Int
|
|
|
|
}{
|
|
|
|
A: struct {
|
|
|
|
FieldA *big.Int `abi:"a"` // Test whether abi tag works for nested tuple
|
|
|
|
B []*big.Int
|
|
|
|
}{big.NewInt(1), []*big.Int{big.NewInt(1), big.NewInt(0)}},
|
|
|
|
B: []*big.Int{big.NewInt(1), big.NewInt(0)}},
|
|
|
|
common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040" + // a offset
|
|
|
|
"00000000000000000000000000000000000000000000000000000000000000e0" + // b offset
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000001" + // a.a value
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000040" + // a.b offset
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000002" + // a.b length
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000001" + // a.b[0] value
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000000" + // a.b[1] value
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000002" + // b length
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000001" + // b[0] value
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000000"), // b[1] value
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// tuple slice
|
|
|
|
"tuple[]",
|
|
|
|
[]ArgumentMarshaling{
|
|
|
|
{Name: "a", Type: "int256"},
|
|
|
|
{Name: "b", Type: "int256[]"},
|
|
|
|
},
|
|
|
|
[]struct {
|
|
|
|
A *big.Int
|
|
|
|
B []*big.Int
|
|
|
|
}{
|
|
|
|
{big.NewInt(-1), []*big.Int{big.NewInt(1), big.NewInt(0)}},
|
|
|
|
{big.NewInt(1), []*big.Int{big.NewInt(2), big.NewInt(-1)}},
|
|
|
|
},
|
|
|
|
common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002" + // tuple length
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000040" + // tuple[0] offset
|
|
|
|
"00000000000000000000000000000000000000000000000000000000000000e0" + // tuple[1] offset
|
|
|
|
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + // tuple[0].A
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000040" + // tuple[0].B offset
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000002" + // tuple[0].B length
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000001" + // tuple[0].B[0] value
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000000" + // tuple[0].B[1] value
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000001" + // tuple[1].A
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000040" + // tuple[1].B offset
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000002" + // tuple[1].B length
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000002" + // tuple[1].B[0] value
|
|
|
|
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), // tuple[1].B[1] value
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// static tuple array
|
|
|
|
"tuple[2]",
|
|
|
|
[]ArgumentMarshaling{
|
|
|
|
{Name: "a", Type: "int256"},
|
|
|
|
{Name: "b", Type: "int256"},
|
|
|
|
},
|
|
|
|
[2]struct {
|
|
|
|
A *big.Int
|
|
|
|
B *big.Int
|
|
|
|
}{
|
|
|
|
{big.NewInt(-1), big.NewInt(1)},
|
|
|
|
{big.NewInt(1), big.NewInt(-1)},
|
|
|
|
},
|
|
|
|
common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + // tuple[0].a
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000001" + // tuple[0].b
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000001" + // tuple[1].a
|
|
|
|
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), // tuple[1].b
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// dynamic tuple array
|
|
|
|
"tuple[2]",
|
|
|
|
[]ArgumentMarshaling{
|
|
|
|
{Name: "a", Type: "int256[]"},
|
|
|
|
},
|
|
|
|
[2]struct {
|
|
|
|
A []*big.Int
|
|
|
|
}{
|
|
|
|
{[]*big.Int{big.NewInt(-1), big.NewInt(1)}},
|
|
|
|
{[]*big.Int{big.NewInt(1), big.NewInt(-1)}},
|
|
|
|
},
|
|
|
|
common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040" + // tuple[0] offset
|
|
|
|
"00000000000000000000000000000000000000000000000000000000000000c0" + // tuple[1] offset
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000020" + // tuple[0].A offset
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000002" + // tuple[0].A length
|
|
|
|
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + // tuple[0].A[0]
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000001" + // tuple[0].A[1]
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000020" + // tuple[1].A offset
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000002" + // tuple[1].A length
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000001" + // tuple[1].A[0]
|
|
|
|
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), // tuple[1].A[1]
|
|
|
|
},
|
2017-06-27 03:05:33 -05:00
|
|
|
} {
|
2019-01-10 02:59:37 -06:00
|
|
|
typ, err := NewType(test.typ, test.components)
|
2017-06-27 03:05:33 -05:00
|
|
|
if err != nil {
|
2017-10-17 06:07:08 -05:00
|
|
|
t.Fatalf("%v failed. Unexpected parse error: %v", i, err)
|
2017-06-27 03:05:33 -05:00
|
|
|
}
|
|
|
|
output, err := typ.pack(reflect.ValueOf(test.input))
|
|
|
|
if err != nil {
|
2017-10-17 06:07:08 -05:00
|
|
|
t.Fatalf("%v failed. Unexpected pack error: %v", i, err)
|
2017-06-27 03:05:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(output, test.output) {
|
2018-12-04 08:27:55 -06:00
|
|
|
t.Errorf("input %d for typ: %v failed. Expected bytes: '%x' Got: '%x'", i, typ.String(), test.output, output)
|
2017-06-27 03:05:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMethodPack(t *testing.T) {
|
|
|
|
abi, err := JSON(strings.NewReader(jsondata2))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-08-02 02:20:46 -05:00
|
|
|
sig := abi.Methods["slice"].ID()
|
2017-06-27 03:05:33 -05:00
|
|
|
sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
|
|
|
|
sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
|
|
|
|
|
|
|
|
packed, err := abi.Pack("slice", []uint32{1, 2})
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(packed, sig) {
|
|
|
|
t.Errorf("expected %x got %x", sig, packed)
|
|
|
|
}
|
|
|
|
|
|
|
|
var addrA, addrB = common.Address{1}, common.Address{2}
|
2019-08-02 02:20:46 -05:00
|
|
|
sig = abi.Methods["sliceAddress"].ID()
|
2017-06-27 03:05:33 -05:00
|
|
|
sig = append(sig, common.LeftPadBytes([]byte{32}, 32)...)
|
|
|
|
sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
|
|
|
|
sig = append(sig, common.LeftPadBytes(addrA[:], 32)...)
|
|
|
|
sig = append(sig, common.LeftPadBytes(addrB[:], 32)...)
|
|
|
|
|
|
|
|
packed, err = abi.Pack("sliceAddress", []common.Address{addrA, addrB})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(packed, sig) {
|
|
|
|
t.Errorf("expected %x got %x", sig, packed)
|
|
|
|
}
|
|
|
|
|
|
|
|
var addrC, addrD = common.Address{3}, common.Address{4}
|
2019-08-02 02:20:46 -05:00
|
|
|
sig = abi.Methods["sliceMultiAddress"].ID()
|
2017-06-27 03:05:33 -05:00
|
|
|
sig = append(sig, common.LeftPadBytes([]byte{64}, 32)...)
|
|
|
|
sig = append(sig, common.LeftPadBytes([]byte{160}, 32)...)
|
|
|
|
sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
|
|
|
|
sig = append(sig, common.LeftPadBytes(addrA[:], 32)...)
|
|
|
|
sig = append(sig, common.LeftPadBytes(addrB[:], 32)...)
|
|
|
|
sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
|
|
|
|
sig = append(sig, common.LeftPadBytes(addrC[:], 32)...)
|
|
|
|
sig = append(sig, common.LeftPadBytes(addrD[:], 32)...)
|
|
|
|
|
|
|
|
packed, err = abi.Pack("sliceMultiAddress", []common.Address{addrA, addrB}, []common.Address{addrC, addrD})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(packed, sig) {
|
|
|
|
t.Errorf("expected %x got %x", sig, packed)
|
|
|
|
}
|
|
|
|
|
2019-08-02 02:20:46 -05:00
|
|
|
sig = abi.Methods["slice256"].ID()
|
2017-06-27 03:05:33 -05:00
|
|
|
sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
|
|
|
|
sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
|
|
|
|
|
|
|
|
packed, err = abi.Pack("slice256", []*big.Int{big.NewInt(1), big.NewInt(2)})
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(packed, sig) {
|
|
|
|
t.Errorf("expected %x got %x", sig, packed)
|
|
|
|
}
|
2019-01-10 02:59:37 -06:00
|
|
|
|
|
|
|
a := [2][2]*big.Int{{big.NewInt(1), big.NewInt(1)}, {big.NewInt(2), big.NewInt(0)}}
|
2019-08-02 02:20:46 -05:00
|
|
|
sig = abi.Methods["nestedArray"].ID()
|
2019-01-10 02:59:37 -06:00
|
|
|
sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
|
|
|
|
sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
|
|
|
|
sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
|
|
|
|
sig = append(sig, common.LeftPadBytes([]byte{0}, 32)...)
|
|
|
|
sig = append(sig, common.LeftPadBytes([]byte{0xa0}, 32)...)
|
|
|
|
sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
|
|
|
|
sig = append(sig, common.LeftPadBytes(addrC[:], 32)...)
|
|
|
|
sig = append(sig, common.LeftPadBytes(addrD[:], 32)...)
|
|
|
|
packed, err = abi.Pack("nestedArray", a, []common.Address{addrC, addrD})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(packed, sig) {
|
|
|
|
t.Errorf("expected %x got %x", sig, packed)
|
|
|
|
}
|
|
|
|
|
2019-08-02 02:20:46 -05:00
|
|
|
sig = abi.Methods["nestedArray2"].ID()
|
2019-01-10 02:59:37 -06:00
|
|
|
sig = append(sig, common.LeftPadBytes([]byte{0x20}, 32)...)
|
|
|
|
sig = append(sig, common.LeftPadBytes([]byte{0x40}, 32)...)
|
|
|
|
sig = append(sig, common.LeftPadBytes([]byte{0x80}, 32)...)
|
|
|
|
sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
|
|
|
|
sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
|
|
|
|
sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
|
|
|
|
sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
|
|
|
|
packed, err = abi.Pack("nestedArray2", [2][]uint8{{1}, {1}})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(packed, sig) {
|
|
|
|
t.Errorf("expected %x got %x", sig, packed)
|
|
|
|
}
|
|
|
|
|
2019-08-02 02:20:46 -05:00
|
|
|
sig = abi.Methods["nestedSlice"].ID()
|
2019-01-10 02:59:37 -06:00
|
|
|
sig = append(sig, common.LeftPadBytes([]byte{0x20}, 32)...)
|
|
|
|
sig = append(sig, common.LeftPadBytes([]byte{0x02}, 32)...)
|
|
|
|
sig = append(sig, common.LeftPadBytes([]byte{0x40}, 32)...)
|
|
|
|
sig = append(sig, common.LeftPadBytes([]byte{0xa0}, 32)...)
|
|
|
|
sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
|
|
|
|
sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
|
|
|
|
sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
|
|
|
|
sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
|
|
|
|
sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
|
|
|
|
sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
|
|
|
|
packed, err = abi.Pack("nestedSlice", [][]uint8{{1, 2}, {1, 2}})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(packed, sig) {
|
|
|
|
t.Errorf("expected %x got %x", sig, packed)
|
|
|
|
}
|
2017-06-27 03:05:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPackNumber(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
value reflect.Value
|
|
|
|
packed []byte
|
|
|
|
}{
|
|
|
|
// Protocol limits
|
|
|
|
{reflect.ValueOf(0), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000000")},
|
|
|
|
{reflect.ValueOf(1), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")},
|
|
|
|
{reflect.ValueOf(-1), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")},
|
|
|
|
|
|
|
|
// Type corner cases
|
|
|
|
{reflect.ValueOf(uint8(math.MaxUint8)), common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000ff")},
|
|
|
|
{reflect.ValueOf(uint16(math.MaxUint16)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000ffff")},
|
|
|
|
{reflect.ValueOf(uint32(math.MaxUint32)), common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000ffffffff")},
|
|
|
|
{reflect.ValueOf(uint64(math.MaxUint64)), common.Hex2Bytes("000000000000000000000000000000000000000000000000ffffffffffffffff")},
|
|
|
|
|
|
|
|
{reflect.ValueOf(int8(math.MaxInt8)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000007f")},
|
|
|
|
{reflect.ValueOf(int16(math.MaxInt16)), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000007fff")},
|
|
|
|
{reflect.ValueOf(int32(math.MaxInt32)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000007fffffff")},
|
|
|
|
{reflect.ValueOf(int64(math.MaxInt64)), common.Hex2Bytes("0000000000000000000000000000000000000000000000007fffffffffffffff")},
|
|
|
|
|
|
|
|
{reflect.ValueOf(int8(math.MinInt8)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80")},
|
|
|
|
{reflect.ValueOf(int16(math.MinInt16)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8000")},
|
|
|
|
{reflect.ValueOf(int32(math.MinInt32)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000")},
|
|
|
|
{reflect.ValueOf(int64(math.MinInt64)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffff8000000000000000")},
|
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
|
|
|
packed := packNum(tt.value)
|
|
|
|
if !bytes.Equal(packed, tt.packed) {
|
|
|
|
t.Errorf("test %d: pack mismatch: have %x, want %x", i, packed, tt.packed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|