github.com/yihuang/erigon@v1.9.7/accounts/abi/unpack_test.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package abi
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/hex"
    22  	"fmt"
    23  	"math/big"
    24  	"reflect"
    25  	"strconv"
    26  	"strings"
    27  	"testing"
    28  
    29  	"github.com/ethereum/go-ethereum/common"
    30  	"github.com/stretchr/testify/require"
    31  )
    32  
    33  type unpackTest struct {
    34  	def  string      // ABI definition JSON
    35  	enc  string      // evm return data
    36  	want interface{} // the expected output
    37  	err  string      // empty or error if expected
    38  }
    39  
    40  func (test unpackTest) checkError(err error) error {
    41  	if err != nil {
    42  		if len(test.err) == 0 {
    43  			return fmt.Errorf("expected no err but got: %v", err)
    44  		} else if err.Error() != test.err {
    45  			return fmt.Errorf("expected err: '%v' got err: %q", test.err, err)
    46  		}
    47  	} else if len(test.err) > 0 {
    48  		return fmt.Errorf("expected err: %v but got none", test.err)
    49  	}
    50  	return nil
    51  }
    52  
    53  var unpackTests = []unpackTest{
    54  	// Bools
    55  	{
    56  		def:  `[{ "type": "bool" }]`,
    57  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
    58  		want: true,
    59  	},
    60  	{
    61  		def:  `[{ "type": "bool" }]`,
    62  		enc:  "0000000000000000000000000000000000000000000000000000000000000000",
    63  		want: false,
    64  	},
    65  	{
    66  		def:  `[{ "type": "bool" }]`,
    67  		enc:  "0000000000000000000000000000000000000000000000000001000000000001",
    68  		want: false,
    69  		err:  "abi: improperly encoded boolean value",
    70  	},
    71  	{
    72  		def:  `[{ "type": "bool" }]`,
    73  		enc:  "0000000000000000000000000000000000000000000000000000000000000003",
    74  		want: false,
    75  		err:  "abi: improperly encoded boolean value",
    76  	},
    77  	// Integers
    78  	{
    79  		def:  `[{"type": "uint32"}]`,
    80  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
    81  		want: uint32(1),
    82  	},
    83  	{
    84  		def:  `[{"type": "uint32"}]`,
    85  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
    86  		want: uint16(0),
    87  		err:  "abi: cannot unmarshal uint32 in to uint16",
    88  	},
    89  	{
    90  		def:  `[{"type": "uint17"}]`,
    91  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
    92  		want: uint16(0),
    93  		err:  "abi: cannot unmarshal *big.Int in to uint16",
    94  	},
    95  	{
    96  		def:  `[{"type": "uint17"}]`,
    97  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
    98  		want: big.NewInt(1),
    99  	},
   100  	{
   101  		def:  `[{"type": "int32"}]`,
   102  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
   103  		want: int32(1),
   104  	},
   105  	{
   106  		def:  `[{"type": "int32"}]`,
   107  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
   108  		want: int16(0),
   109  		err:  "abi: cannot unmarshal int32 in to int16",
   110  	},
   111  	{
   112  		def:  `[{"type": "int17"}]`,
   113  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
   114  		want: int16(0),
   115  		err:  "abi: cannot unmarshal *big.Int in to int16",
   116  	},
   117  	{
   118  		def:  `[{"type": "int17"}]`,
   119  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
   120  		want: big.NewInt(1),
   121  	},
   122  	{
   123  		def:  `[{"type": "int256"}]`,
   124  		enc:  "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
   125  		want: big.NewInt(-1),
   126  	},
   127  	// Address
   128  	{
   129  		def:  `[{"type": "address"}]`,
   130  		enc:  "0000000000000000000000000100000000000000000000000000000000000000",
   131  		want: common.Address{1},
   132  	},
   133  	// Bytes
   134  	{
   135  		def:  `[{"type": "bytes32"}]`,
   136  		enc:  "0100000000000000000000000000000000000000000000000000000000000000",
   137  		want: [32]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   138  	},
   139  	{
   140  		def:  `[{"type": "bytes"}]`,
   141  		enc:  "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
   142  		want: common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   143  	},
   144  	{
   145  		def:  `[{"type": "bytes"}]`,
   146  		enc:  "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
   147  		want: [32]byte{},
   148  		err:  "abi: cannot unmarshal []uint8 in to [32]uint8",
   149  	},
   150  	{
   151  		def:  `[{"type": "bytes32"}]`,
   152  		enc:  "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
   153  		want: []byte(nil),
   154  		err:  "abi: cannot unmarshal [32]uint8 in to []uint8",
   155  	},
   156  	{
   157  		def:  `[{"type": "bytes32"}]`,
   158  		enc:  "0100000000000000000000000000000000000000000000000000000000000000",
   159  		want: [32]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
   160  	},
   161  	// Functions
   162  	{
   163  		def:  `[{"type": "function"}]`,
   164  		enc:  "0100000000000000000000000000000000000000000000000000000000000000",
   165  		want: [24]byte{1},
   166  	},
   167  	// Slice and Array
   168  	{
   169  		def:  `[{"type": "uint8[]"}]`,
   170  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   171  		want: []uint8{1, 2},
   172  	},
   173  	{
   174  		def:  `[{"type": "uint8[]"}]`,
   175  		enc:  "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000",
   176  		want: []uint8{},
   177  	},
   178  	{
   179  		def:  `[{"type": "uint256[]"}]`,
   180  		enc:  "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000",
   181  		want: []*big.Int{},
   182  	},
   183  	{
   184  		def:  `[{"type": "uint8[2]"}]`,
   185  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   186  		want: [2]uint8{1, 2},
   187  	},
   188  	// multi dimensional, if these pass, all types that don't require length prefix should pass
   189  	{
   190  		def:  `[{"type": "uint8[][]"}]`,
   191  		enc:  "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000",
   192  		want: [][]uint8{},
   193  	},
   194  	{
   195  		def:  `[{"type": "uint8[][]"}]`,
   196  		enc:  "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   197  		want: [][]uint8{{1, 2}, {1, 2}},
   198  	},
   199  	{
   200  		def:  `[{"type": "uint8[][]"}]`,
   201  		enc:  "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
   202  		want: [][]uint8{{1, 2}, {1, 2, 3}},
   203  	},
   204  	{
   205  		def:  `[{"type": "uint8[2][2]"}]`,
   206  		enc:  "0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   207  		want: [2][2]uint8{{1, 2}, {1, 2}},
   208  	},
   209  	{
   210  		def:  `[{"type": "uint8[][2]"}]`,
   211  		enc:  "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
   212  		want: [2][]uint8{{}, {}},
   213  	},
   214  	{
   215  		def:  `[{"type": "uint8[][2]"}]`,
   216  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001",
   217  		want: [2][]uint8{{1}, {1}},
   218  	},
   219  	{
   220  		def:  `[{"type": "uint8[2][]"}]`,
   221  		enc:  "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000",
   222  		want: [][2]uint8{},
   223  	},
   224  	{
   225  		def:  `[{"type": "uint8[2][]"}]`,
   226  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   227  		want: [][2]uint8{{1, 2}},
   228  	},
   229  	{
   230  		def:  `[{"type": "uint8[2][]"}]`,
   231  		enc:  "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   232  		want: [][2]uint8{{1, 2}, {1, 2}},
   233  	},
   234  	{
   235  		def:  `[{"type": "uint16[]"}]`,
   236  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   237  		want: []uint16{1, 2},
   238  	},
   239  	{
   240  		def:  `[{"type": "uint16[2]"}]`,
   241  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   242  		want: [2]uint16{1, 2},
   243  	},
   244  	{
   245  		def:  `[{"type": "uint32[]"}]`,
   246  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   247  		want: []uint32{1, 2},
   248  	},
   249  	{
   250  		def:  `[{"type": "uint32[2]"}]`,
   251  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   252  		want: [2]uint32{1, 2},
   253  	},
   254  	{
   255  		def:  `[{"type": "uint32[2][3][4]"}]`,
   256  		enc:  "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000f000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000001300000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000015000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000170000000000000000000000000000000000000000000000000000000000000018",
   257  		want: [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}}},
   258  	},
   259  	{
   260  		def:  `[{"type": "uint64[]"}]`,
   261  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   262  		want: []uint64{1, 2},
   263  	},
   264  	{
   265  		def:  `[{"type": "uint64[2]"}]`,
   266  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   267  		want: [2]uint64{1, 2},
   268  	},
   269  	{
   270  		def:  `[{"type": "uint256[]"}]`,
   271  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   272  		want: []*big.Int{big.NewInt(1), big.NewInt(2)},
   273  	},
   274  	{
   275  		def:  `[{"type": "uint256[3]"}]`,
   276  		enc:  "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
   277  		want: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)},
   278  	},
   279  	{
   280  		def:  `[{"type": "string[4]"}]`,
   281  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000548656c6c6f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005576f726c64000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b476f2d657468657265756d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008457468657265756d000000000000000000000000000000000000000000000000",
   282  		want: [4]string{"Hello", "World", "Go-ethereum", "Ethereum"},
   283  	},
   284  	{
   285  		def:  `[{"type": "string[]"}]`,
   286  		enc:  "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000008457468657265756d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b676f2d657468657265756d000000000000000000000000000000000000000000",
   287  		want: []string{"Ethereum", "go-ethereum"},
   288  	},
   289  	{
   290  		def:  `[{"type": "bytes[]"}]`,
   291  		enc:  "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000003f0f0f000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003f0f0f00000000000000000000000000000000000000000000000000000000000",
   292  		want: [][]byte{{0xf0, 0xf0, 0xf0}, {0xf0, 0xf0, 0xf0}},
   293  	},
   294  	{
   295  		def:  `[{"type": "uint256[2][][]"}]`,
   296  		enc:  "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000c8000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000003e80000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000c8000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000003e8",
   297  		want: [][][2]*big.Int{{{big.NewInt(1), big.NewInt(200)}, {big.NewInt(1), big.NewInt(1000)}}, {{big.NewInt(1), big.NewInt(200)}, {big.NewInt(1), big.NewInt(1000)}}},
   298  	},
   299  	{
   300  		def:  `[{"type": "int8[]"}]`,
   301  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   302  		want: []int8{1, 2},
   303  	},
   304  	{
   305  		def:  `[{"type": "int8[2]"}]`,
   306  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   307  		want: [2]int8{1, 2},
   308  	},
   309  	{
   310  		def:  `[{"type": "int16[]"}]`,
   311  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   312  		want: []int16{1, 2},
   313  	},
   314  	{
   315  		def:  `[{"type": "int16[2]"}]`,
   316  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   317  		want: [2]int16{1, 2},
   318  	},
   319  	{
   320  		def:  `[{"type": "int32[]"}]`,
   321  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   322  		want: []int32{1, 2},
   323  	},
   324  	{
   325  		def:  `[{"type": "int32[2]"}]`,
   326  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   327  		want: [2]int32{1, 2},
   328  	},
   329  	{
   330  		def:  `[{"type": "int64[]"}]`,
   331  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   332  		want: []int64{1, 2},
   333  	},
   334  	{
   335  		def:  `[{"type": "int64[2]"}]`,
   336  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   337  		want: [2]int64{1, 2},
   338  	},
   339  	{
   340  		def:  `[{"type": "int256[]"}]`,
   341  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   342  		want: []*big.Int{big.NewInt(1), big.NewInt(2)},
   343  	},
   344  	{
   345  		def:  `[{"type": "int256[3]"}]`,
   346  		enc:  "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
   347  		want: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)},
   348  	},
   349  	// struct outputs
   350  	{
   351  		def: `[{"name":"int1","type":"int256"},{"name":"int2","type":"int256"}]`,
   352  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   353  		want: struct {
   354  			Int1 *big.Int
   355  			Int2 *big.Int
   356  		}{big.NewInt(1), big.NewInt(2)},
   357  	},
   358  	{
   359  		def: `[{"name":"int_one","type":"int256"}]`,
   360  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   361  		want: struct {
   362  			IntOne *big.Int
   363  		}{big.NewInt(1)},
   364  	},
   365  	{
   366  		def: `[{"name":"int__one","type":"int256"}]`,
   367  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   368  		want: struct {
   369  			IntOne *big.Int
   370  		}{big.NewInt(1)},
   371  	},
   372  	{
   373  		def: `[{"name":"int_one_","type":"int256"}]`,
   374  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   375  		want: struct {
   376  			IntOne *big.Int
   377  		}{big.NewInt(1)},
   378  	},
   379  	{
   380  		def: `[{"name":"int_one","type":"int256"}, {"name":"intone","type":"int256"}]`,
   381  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   382  		want: struct {
   383  			IntOne *big.Int
   384  			Intone *big.Int
   385  		}{big.NewInt(1), big.NewInt(2)},
   386  	},
   387  	{
   388  		def: `[{"name":"___","type":"int256"}]`,
   389  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   390  		want: struct {
   391  			IntOne *big.Int
   392  			Intone *big.Int
   393  		}{},
   394  		err: "abi: purely underscored output cannot unpack to struct",
   395  	},
   396  	{
   397  		def: `[{"name":"int_one","type":"int256"},{"name":"IntOne","type":"int256"}]`,
   398  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   399  		want: struct {
   400  			Int1 *big.Int
   401  			Int2 *big.Int
   402  		}{},
   403  		err: "abi: multiple outputs mapping to the same struct field 'IntOne'",
   404  	},
   405  	{
   406  		def: `[{"name":"int","type":"int256"},{"name":"Int","type":"int256"}]`,
   407  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   408  		want: struct {
   409  			Int1 *big.Int
   410  			Int2 *big.Int
   411  		}{},
   412  		err: "abi: multiple outputs mapping to the same struct field 'Int'",
   413  	},
   414  	{
   415  		def: `[{"name":"int","type":"int256"},{"name":"_int","type":"int256"}]`,
   416  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   417  		want: struct {
   418  			Int1 *big.Int
   419  			Int2 *big.Int
   420  		}{},
   421  		err: "abi: multiple outputs mapping to the same struct field 'Int'",
   422  	},
   423  	{
   424  		def: `[{"name":"Int","type":"int256"},{"name":"_int","type":"int256"}]`,
   425  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   426  		want: struct {
   427  			Int1 *big.Int
   428  			Int2 *big.Int
   429  		}{},
   430  		err: "abi: multiple outputs mapping to the same struct field 'Int'",
   431  	},
   432  	{
   433  		def: `[{"name":"Int","type":"int256"},{"name":"_","type":"int256"}]`,
   434  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   435  		want: struct {
   436  			Int1 *big.Int
   437  			Int2 *big.Int
   438  		}{},
   439  		err: "abi: purely underscored output cannot unpack to struct",
   440  	},
   441  }
   442  
   443  func TestUnpack(t *testing.T) {
   444  	for i, test := range unpackTests {
   445  		t.Run(strconv.Itoa(i), func(t *testing.T) {
   446  			def := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def)
   447  			abi, err := JSON(strings.NewReader(def))
   448  			if err != nil {
   449  				t.Fatalf("invalid ABI definition %s: %v", def, err)
   450  			}
   451  			encb, err := hex.DecodeString(test.enc)
   452  			if err != nil {
   453  				t.Fatalf("invalid hex %s: %v", test.enc, err)
   454  			}
   455  			outptr := reflect.New(reflect.TypeOf(test.want))
   456  			err = abi.Unpack(outptr.Interface(), "method", encb)
   457  			if err := test.checkError(err); err != nil {
   458  				t.Errorf("test %d (%v) failed: %v", i, test.def, err)
   459  				return
   460  			}
   461  			out := outptr.Elem().Interface()
   462  			if !reflect.DeepEqual(test.want, out) {
   463  				t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, test.want, out)
   464  			}
   465  		})
   466  	}
   467  }
   468  
   469  func TestUnpackSetDynamicArrayOutput(t *testing.T) {
   470  	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"}]`))
   471  	if err != nil {
   472  		t.Fatal(err)
   473  	}
   474  
   475  	var (
   476  		marshalledReturn32 = common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000230783132333435363738393000000000000000000000000000000000000000003078303938373635343332310000000000000000000000000000000000000000")
   477  		marshalledReturn15 = common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000230783031323334350000000000000000000000000000000000000000000000003078393837363534000000000000000000000000000000000000000000000000")
   478  
   479  		out32 [][32]byte
   480  		out15 [][15]byte
   481  	)
   482  
   483  	// test 32
   484  	err = abi.Unpack(&out32, "testDynamicFixedBytes32", marshalledReturn32)
   485  	if err != nil {
   486  		t.Fatal(err)
   487  	}
   488  	if len(out32) != 2 {
   489  		t.Fatalf("expected array with 2 values, got %d", len(out32))
   490  	}
   491  	expected := common.Hex2Bytes("3078313233343536373839300000000000000000000000000000000000000000")
   492  	if !bytes.Equal(out32[0][:], expected) {
   493  		t.Errorf("expected %x, got %x\n", expected, out32[0])
   494  	}
   495  	expected = common.Hex2Bytes("3078303938373635343332310000000000000000000000000000000000000000")
   496  	if !bytes.Equal(out32[1][:], expected) {
   497  		t.Errorf("expected %x, got %x\n", expected, out32[1])
   498  	}
   499  
   500  	// test 15
   501  	err = abi.Unpack(&out15, "testDynamicFixedBytes32", marshalledReturn15)
   502  	if err != nil {
   503  		t.Fatal(err)
   504  	}
   505  	if len(out15) != 2 {
   506  		t.Fatalf("expected array with 2 values, got %d", len(out15))
   507  	}
   508  	expected = common.Hex2Bytes("307830313233343500000000000000")
   509  	if !bytes.Equal(out15[0][:], expected) {
   510  		t.Errorf("expected %x, got %x\n", expected, out15[0])
   511  	}
   512  	expected = common.Hex2Bytes("307839383736353400000000000000")
   513  	if !bytes.Equal(out15[1][:], expected) {
   514  		t.Errorf("expected %x, got %x\n", expected, out15[1])
   515  	}
   516  }
   517  
   518  type methodMultiOutput struct {
   519  	Int    *big.Int
   520  	String string
   521  }
   522  
   523  func methodMultiReturn(require *require.Assertions) (ABI, []byte, methodMultiOutput) {
   524  	const definition = `[
   525  	{ "name" : "multi", "constant" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]`
   526  	var expected = methodMultiOutput{big.NewInt(1), "hello"}
   527  
   528  	abi, err := JSON(strings.NewReader(definition))
   529  	require.NoError(err)
   530  	// using buff to make the code readable
   531  	buff := new(bytes.Buffer)
   532  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
   533  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
   534  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
   535  	buff.Write(common.RightPadBytes([]byte(expected.String), 32))
   536  	return abi, buff.Bytes(), expected
   537  }
   538  
   539  func TestMethodMultiReturn(t *testing.T) {
   540  	type reversed struct {
   541  		String string
   542  		Int    *big.Int
   543  	}
   544  
   545  	newInterfaceSlice := func(len int) interface{} {
   546  		slice := make([]interface{}, len)
   547  		return &slice
   548  	}
   549  
   550  	abi, data, expected := methodMultiReturn(require.New(t))
   551  	bigint := new(big.Int)
   552  	var testCases = []struct {
   553  		dest     interface{}
   554  		expected interface{}
   555  		error    string
   556  		name     string
   557  	}{{
   558  		&methodMultiOutput{},
   559  		&expected,
   560  		"",
   561  		"Can unpack into structure",
   562  	}, {
   563  		&reversed{},
   564  		&reversed{expected.String, expected.Int},
   565  		"",
   566  		"Can unpack into reversed structure",
   567  	}, {
   568  		&[]interface{}{&bigint, new(string)},
   569  		&[]interface{}{&expected.Int, &expected.String},
   570  		"",
   571  		"Can unpack into a slice",
   572  	}, {
   573  		&[2]interface{}{&bigint, new(string)},
   574  		&[2]interface{}{&expected.Int, &expected.String},
   575  		"",
   576  		"Can unpack into an array",
   577  	}, {
   578  		&[2]interface{}{},
   579  		&[2]interface{}{expected.Int, expected.String},
   580  		"",
   581  		"Can unpack into interface array",
   582  	}, {
   583  		newInterfaceSlice(2),
   584  		&[]interface{}{expected.Int, expected.String},
   585  		"",
   586  		"Can unpack into interface slice",
   587  	}, {
   588  		&[]interface{}{new(int), new(int)},
   589  		&[]interface{}{&expected.Int, &expected.String},
   590  		"abi: cannot unmarshal *big.Int in to int",
   591  		"Can not unpack into a slice with wrong types",
   592  	}, {
   593  		&[]interface{}{new(int)},
   594  		&[]interface{}{},
   595  		"abi: insufficient number of elements in the list/array for unpack, want 2, got 1",
   596  		"Can not unpack into a slice with wrong types",
   597  	}}
   598  	for _, tc := range testCases {
   599  		tc := tc
   600  		t.Run(tc.name, func(t *testing.T) {
   601  			require := require.New(t)
   602  			err := abi.Unpack(tc.dest, "multi", data)
   603  			if tc.error == "" {
   604  				require.Nil(err, "Should be able to unpack method outputs.")
   605  				require.Equal(tc.expected, tc.dest)
   606  			} else {
   607  				require.EqualError(err, tc.error)
   608  			}
   609  		})
   610  	}
   611  }
   612  
   613  func TestMultiReturnWithArray(t *testing.T) {
   614  	const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3]"}, {"type": "uint64"}]}]`
   615  	abi, err := JSON(strings.NewReader(definition))
   616  	if err != nil {
   617  		t.Fatal(err)
   618  	}
   619  	buff := new(bytes.Buffer)
   620  	buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000900000000000000000000000000000000000000000000000000000000000000090000000000000000000000000000000000000000000000000000000000000009"))
   621  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000008"))
   622  
   623  	ret1, ret1Exp := new([3]uint64), [3]uint64{9, 9, 9}
   624  	ret2, ret2Exp := new(uint64), uint64(8)
   625  	if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil {
   626  		t.Fatal(err)
   627  	}
   628  	if !reflect.DeepEqual(*ret1, ret1Exp) {
   629  		t.Error("array result", *ret1, "!= Expected", ret1Exp)
   630  	}
   631  	if *ret2 != ret2Exp {
   632  		t.Error("int result", *ret2, "!= Expected", ret2Exp)
   633  	}
   634  }
   635  
   636  func TestMultiReturnWithStringArray(t *testing.T) {
   637  	const definition = `[{"name" : "multi", "outputs": [{"name": "","type": "uint256[3]"},{"name": "","type": "address"},{"name": "","type": "string[2]"},{"name": "","type": "bool"}]}]`
   638  	abi, err := JSON(strings.NewReader(definition))
   639  	if err != nil {
   640  		t.Fatal(err)
   641  	}
   642  	buff := new(bytes.Buffer)
   643  	buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000005c1b78ea0000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000001a055690d9db80000000000000000000000000000ab1257528b3782fb40d7ed5f72e624b744dffb2f00000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000008457468657265756d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001048656c6c6f2c20457468657265756d2100000000000000000000000000000000"))
   644  	temp, _ := big.NewInt(0).SetString("30000000000000000000", 10)
   645  	ret1, ret1Exp := new([3]*big.Int), [3]*big.Int{big.NewInt(1545304298), big.NewInt(6), temp}
   646  	ret2, ret2Exp := new(common.Address), common.HexToAddress("ab1257528b3782fb40d7ed5f72e624b744dffb2f")
   647  	ret3, ret3Exp := new([2]string), [2]string{"Ethereum", "Hello, Ethereum!"}
   648  	ret4, ret4Exp := new(bool), false
   649  	if err := abi.Unpack(&[]interface{}{ret1, ret2, ret3, ret4}, "multi", buff.Bytes()); err != nil {
   650  		t.Fatal(err)
   651  	}
   652  	if !reflect.DeepEqual(*ret1, ret1Exp) {
   653  		t.Error("big.Int array result", *ret1, "!= Expected", ret1Exp)
   654  	}
   655  	if !reflect.DeepEqual(*ret2, ret2Exp) {
   656  		t.Error("address result", *ret2, "!= Expected", ret2Exp)
   657  	}
   658  	if !reflect.DeepEqual(*ret3, ret3Exp) {
   659  		t.Error("string array result", *ret3, "!= Expected", ret3Exp)
   660  	}
   661  	if !reflect.DeepEqual(*ret4, ret4Exp) {
   662  		t.Error("bool result", *ret4, "!= Expected", ret4Exp)
   663  	}
   664  }
   665  
   666  func TestMultiReturnWithStringSlice(t *testing.T) {
   667  	const definition = `[{"name" : "multi", "outputs": [{"name": "","type": "string[]"},{"name": "","type": "uint256[]"}]}]`
   668  	abi, err := JSON(strings.NewReader(definition))
   669  	if err != nil {
   670  		t.Fatal(err)
   671  	}
   672  	buff := new(bytes.Buffer)
   673  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // output[0] offset
   674  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000120")) // output[1] offset
   675  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // output[0] length
   676  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // output[0][0] offset
   677  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // output[0][1] offset
   678  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000008")) // output[0][0] length
   679  	buff.Write(common.Hex2Bytes("657468657265756d000000000000000000000000000000000000000000000000")) // output[0][0] value
   680  	buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000b")) // output[0][1] length
   681  	buff.Write(common.Hex2Bytes("676f2d657468657265756d000000000000000000000000000000000000000000")) // output[0][1] value
   682  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // output[1] length
   683  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000064")) // output[1][0] value
   684  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000065")) // output[1][1] value
   685  	ret1, ret1Exp := new([]string), []string{"ethereum", "go-ethereum"}
   686  	ret2, ret2Exp := new([]*big.Int), []*big.Int{big.NewInt(100), big.NewInt(101)}
   687  	if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil {
   688  		t.Fatal(err)
   689  	}
   690  	if !reflect.DeepEqual(*ret1, ret1Exp) {
   691  		t.Error("string slice result", *ret1, "!= Expected", ret1Exp)
   692  	}
   693  	if !reflect.DeepEqual(*ret2, ret2Exp) {
   694  		t.Error("uint256 slice result", *ret2, "!= Expected", ret2Exp)
   695  	}
   696  }
   697  
   698  func TestMultiReturnWithDeeplyNestedArray(t *testing.T) {
   699  	// Similar to TestMultiReturnWithArray, but with a special case in mind:
   700  	//  values of nested static arrays count towards the size as well, and any element following
   701  	//  after such nested array argument should be read with the correct offset,
   702  	//  so that it does not read content from the previous array argument.
   703  	const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3][2][4]"}, {"type": "uint64"}]}]`
   704  	abi, err := JSON(strings.NewReader(definition))
   705  	if err != nil {
   706  		t.Fatal(err)
   707  	}
   708  	buff := new(bytes.Buffer)
   709  	// construct the test array, each 3 char element is joined with 61 '0' chars,
   710  	// to from the ((3 + 61) * 0.5) = 32 byte elements in the array.
   711  	buff.Write(common.Hex2Bytes(strings.Join([]string{
   712  		"", //empty, to apply the 61-char separator to the first element as well.
   713  		"111", "112", "113", "121", "122", "123",
   714  		"211", "212", "213", "221", "222", "223",
   715  		"311", "312", "313", "321", "322", "323",
   716  		"411", "412", "413", "421", "422", "423",
   717  	}, "0000000000000000000000000000000000000000000000000000000000000")))
   718  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000009876"))
   719  
   720  	ret1, ret1Exp := new([4][2][3]uint64), [4][2][3]uint64{
   721  		{{0x111, 0x112, 0x113}, {0x121, 0x122, 0x123}},
   722  		{{0x211, 0x212, 0x213}, {0x221, 0x222, 0x223}},
   723  		{{0x311, 0x312, 0x313}, {0x321, 0x322, 0x323}},
   724  		{{0x411, 0x412, 0x413}, {0x421, 0x422, 0x423}},
   725  	}
   726  	ret2, ret2Exp := new(uint64), uint64(0x9876)
   727  	if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil {
   728  		t.Fatal(err)
   729  	}
   730  	if !reflect.DeepEqual(*ret1, ret1Exp) {
   731  		t.Error("array result", *ret1, "!= Expected", ret1Exp)
   732  	}
   733  	if *ret2 != ret2Exp {
   734  		t.Error("int result", *ret2, "!= Expected", ret2Exp)
   735  	}
   736  }
   737  
   738  func TestUnmarshal(t *testing.T) {
   739  	const definition = `[
   740  	{ "name" : "int", "constant" : false, "outputs": [ { "type": "uint256" } ] },
   741  	{ "name" : "bool", "constant" : false, "outputs": [ { "type": "bool" } ] },
   742  	{ "name" : "bytes", "constant" : false, "outputs": [ { "type": "bytes" } ] },
   743  	{ "name" : "fixed", "constant" : false, "outputs": [ { "type": "bytes32" } ] },
   744  	{ "name" : "multi", "constant" : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] },
   745  	{ "name" : "intArraySingle", "constant" : false, "outputs": [ { "type": "uint256[3]" } ] },
   746  	{ "name" : "addressSliceSingle", "constant" : false, "outputs": [ { "type": "address[]" } ] },
   747  	{ "name" : "addressSliceDouble", "constant" : false, "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] },
   748  	{ "name" : "mixedBytes", "constant" : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]`
   749  
   750  	abi, err := JSON(strings.NewReader(definition))
   751  	if err != nil {
   752  		t.Fatal(err)
   753  	}
   754  	buff := new(bytes.Buffer)
   755  
   756  	// marshall mixed bytes (mixedBytes)
   757  	p0, p0Exp := []byte{}, common.Hex2Bytes("01020000000000000000")
   758  	p1, p1Exp := [32]byte{}, common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff")
   759  	mixedBytes := []interface{}{&p0, &p1}
   760  
   761  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
   762  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff"))
   763  	buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000a"))
   764  	buff.Write(common.Hex2Bytes("0102000000000000000000000000000000000000000000000000000000000000"))
   765  
   766  	err = abi.Unpack(&mixedBytes, "mixedBytes", buff.Bytes())
   767  	if err != nil {
   768  		t.Error(err)
   769  	} else {
   770  		if !bytes.Equal(p0, p0Exp) {
   771  			t.Errorf("unexpected value unpacked: want %x, got %x", p0Exp, p0)
   772  		}
   773  
   774  		if !bytes.Equal(p1[:], p1Exp) {
   775  			t.Errorf("unexpected value unpacked: want %x, got %x", p1Exp, p1)
   776  		}
   777  	}
   778  
   779  	// marshal int
   780  	var Int *big.Int
   781  	err = abi.Unpack(&Int, "int", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
   782  	if err != nil {
   783  		t.Error(err)
   784  	}
   785  
   786  	if Int == nil || Int.Cmp(big.NewInt(1)) != 0 {
   787  		t.Error("expected Int to be 1 got", Int)
   788  	}
   789  
   790  	// marshal bool
   791  	var Bool bool
   792  	err = abi.Unpack(&Bool, "bool", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
   793  	if err != nil {
   794  		t.Error(err)
   795  	}
   796  
   797  	if !Bool {
   798  		t.Error("expected Bool to be true")
   799  	}
   800  
   801  	// marshal dynamic bytes max length 32
   802  	buff.Reset()
   803  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   804  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   805  	bytesOut := common.RightPadBytes([]byte("hello"), 32)
   806  	buff.Write(bytesOut)
   807  
   808  	var Bytes []byte
   809  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
   810  	if err != nil {
   811  		t.Error(err)
   812  	}
   813  
   814  	if !bytes.Equal(Bytes, bytesOut) {
   815  		t.Errorf("expected %x got %x", bytesOut, Bytes)
   816  	}
   817  
   818  	// marshall dynamic bytes max length 64
   819  	buff.Reset()
   820  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   821  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
   822  	bytesOut = common.RightPadBytes([]byte("hello"), 64)
   823  	buff.Write(bytesOut)
   824  
   825  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
   826  	if err != nil {
   827  		t.Error(err)
   828  	}
   829  
   830  	if !bytes.Equal(Bytes, bytesOut) {
   831  		t.Errorf("expected %x got %x", bytesOut, Bytes)
   832  	}
   833  
   834  	// marshall dynamic bytes max length 64
   835  	buff.Reset()
   836  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   837  	buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000003f"))
   838  	bytesOut = common.RightPadBytes([]byte("hello"), 64)
   839  	buff.Write(bytesOut)
   840  
   841  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
   842  	if err != nil {
   843  		t.Error(err)
   844  	}
   845  
   846  	if !bytes.Equal(Bytes, bytesOut[:len(bytesOut)-1]) {
   847  		t.Errorf("expected %x got %x", bytesOut[:len(bytesOut)-1], Bytes)
   848  	}
   849  
   850  	// marshal dynamic bytes output empty
   851  	err = abi.Unpack(&Bytes, "bytes", nil)
   852  	if err == nil {
   853  		t.Error("expected error")
   854  	}
   855  
   856  	// marshal dynamic bytes length 5
   857  	buff.Reset()
   858  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   859  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
   860  	buff.Write(common.RightPadBytes([]byte("hello"), 32))
   861  
   862  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
   863  	if err != nil {
   864  		t.Error(err)
   865  	}
   866  
   867  	if !bytes.Equal(Bytes, []byte("hello")) {
   868  		t.Errorf("expected %x got %x", bytesOut, Bytes)
   869  	}
   870  
   871  	// marshal dynamic bytes length 5
   872  	buff.Reset()
   873  	buff.Write(common.RightPadBytes([]byte("hello"), 32))
   874  
   875  	var hash common.Hash
   876  	err = abi.Unpack(&hash, "fixed", buff.Bytes())
   877  	if err != nil {
   878  		t.Error(err)
   879  	}
   880  
   881  	helloHash := common.BytesToHash(common.RightPadBytes([]byte("hello"), 32))
   882  	if hash != helloHash {
   883  		t.Errorf("Expected %x to equal %x", hash, helloHash)
   884  	}
   885  
   886  	// marshal error
   887  	buff.Reset()
   888  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   889  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
   890  	if err == nil {
   891  		t.Error("expected error")
   892  	}
   893  
   894  	err = abi.Unpack(&Bytes, "multi", make([]byte, 64))
   895  	if err == nil {
   896  		t.Error("expected error")
   897  	}
   898  
   899  	buff.Reset()
   900  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
   901  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"))
   902  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003"))
   903  	// marshal int array
   904  	var intArray [3]*big.Int
   905  	err = abi.Unpack(&intArray, "intArraySingle", buff.Bytes())
   906  	if err != nil {
   907  		t.Error(err)
   908  	}
   909  	var testAgainstIntArray [3]*big.Int
   910  	testAgainstIntArray[0] = big.NewInt(1)
   911  	testAgainstIntArray[1] = big.NewInt(2)
   912  	testAgainstIntArray[2] = big.NewInt(3)
   913  
   914  	for i, Int := range intArray {
   915  		if Int.Cmp(testAgainstIntArray[i]) != 0 {
   916  			t.Errorf("expected %v, got %v", testAgainstIntArray[i], Int)
   917  		}
   918  	}
   919  	// marshal address slice
   920  	buff.Reset()
   921  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) // offset
   922  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
   923  	buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
   924  
   925  	var outAddr []common.Address
   926  	err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
   927  	if err != nil {
   928  		t.Fatal("didn't expect error:", err)
   929  	}
   930  
   931  	if len(outAddr) != 1 {
   932  		t.Fatal("expected 1 item, got", len(outAddr))
   933  	}
   934  
   935  	if outAddr[0] != (common.Address{1}) {
   936  		t.Errorf("expected %x, got %x", common.Address{1}, outAddr[0])
   937  	}
   938  
   939  	// marshal multiple address slice
   940  	buff.Reset()
   941  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // offset
   942  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // offset
   943  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
   944  	buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
   945  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // size
   946  	buff.Write(common.Hex2Bytes("0000000000000000000000000200000000000000000000000000000000000000"))
   947  	buff.Write(common.Hex2Bytes("0000000000000000000000000300000000000000000000000000000000000000"))
   948  
   949  	var outAddrStruct struct {
   950  		A []common.Address
   951  		B []common.Address
   952  	}
   953  	err = abi.Unpack(&outAddrStruct, "addressSliceDouble", buff.Bytes())
   954  	if err != nil {
   955  		t.Fatal("didn't expect error:", err)
   956  	}
   957  
   958  	if len(outAddrStruct.A) != 1 {
   959  		t.Fatal("expected 1 item, got", len(outAddrStruct.A))
   960  	}
   961  
   962  	if outAddrStruct.A[0] != (common.Address{1}) {
   963  		t.Errorf("expected %x, got %x", common.Address{1}, outAddrStruct.A[0])
   964  	}
   965  
   966  	if len(outAddrStruct.B) != 2 {
   967  		t.Fatal("expected 1 item, got", len(outAddrStruct.B))
   968  	}
   969  
   970  	if outAddrStruct.B[0] != (common.Address{2}) {
   971  		t.Errorf("expected %x, got %x", common.Address{2}, outAddrStruct.B[0])
   972  	}
   973  	if outAddrStruct.B[1] != (common.Address{3}) {
   974  		t.Errorf("expected %x, got %x", common.Address{3}, outAddrStruct.B[1])
   975  	}
   976  
   977  	// marshal invalid address slice
   978  	buff.Reset()
   979  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000100"))
   980  
   981  	err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
   982  	if err == nil {
   983  		t.Fatal("expected error:", err)
   984  	}
   985  }
   986  
   987  func TestUnpackTuple(t *testing.T) {
   988  	const simpleTuple = `[{"name":"tuple","constant":false,"outputs":[{"type":"tuple","name":"ret","components":[{"type":"int256","name":"a"},{"type":"int256","name":"b"}]}]}]`
   989  	abi, err := JSON(strings.NewReader(simpleTuple))
   990  	if err != nil {
   991  		t.Fatal(err)
   992  	}
   993  	buff := new(bytes.Buffer)
   994  
   995  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // ret[a] = 1
   996  	buff.Write(common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")) // ret[b] = -1
   997  
   998  	// If the result is single tuple, use struct as return value container directly.
   999  	v := struct {
  1000  		A *big.Int
  1001  		B *big.Int
  1002  	}{new(big.Int), new(big.Int)}
  1003  
  1004  	err = abi.Unpack(&v, "tuple", buff.Bytes())
  1005  	if err != nil {
  1006  		t.Error(err)
  1007  	} else {
  1008  		if v.A.Cmp(big.NewInt(1)) != 0 {
  1009  			t.Errorf("unexpected value unpacked: want %x, got %x", 1, v.A)
  1010  		}
  1011  		if v.B.Cmp(big.NewInt(-1)) != 0 {
  1012  			t.Errorf("unexpected value unpacked: want %x, got %x", v.B, -1)
  1013  		}
  1014  	}
  1015  
  1016  	// Test nested tuple
  1017  	const nestedTuple = `[{"name":"tuple","constant":false,"outputs":[
  1018  		{"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"}]}]},
  1019  		{"type":"tuple","name":"t","components":[{"name":"x", "type":"uint256"},{"name":"y","type":"uint256"}]},
  1020  		{"type":"uint256","name":"a"}
  1021  	]}]`
  1022  
  1023  	abi, err = JSON(strings.NewReader(nestedTuple))
  1024  	if err != nil {
  1025  		t.Fatal(err)
  1026  	}
  1027  	buff.Reset()
  1028  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // s offset
  1029  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000000")) // t.X = 0
  1030  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // t.Y = 1
  1031  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // a = 1
  1032  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // s.A = 1
  1033  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000060")) // s.B offset
  1034  	buff.Write(common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000c0")) // s.C offset
  1035  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.B length
  1036  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // s.B[0] = 1
  1037  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.B[0] = 2
  1038  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.C length
  1039  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // s.C[0].X
  1040  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.C[0].Y
  1041  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.C[1].X
  1042  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // s.C[1].Y
  1043  
  1044  	type T struct {
  1045  		X *big.Int `abi:"x"`
  1046  		Z *big.Int `abi:"y"` // Test whether the abi tag works.
  1047  	}
  1048  
  1049  	type S struct {
  1050  		A *big.Int
  1051  		B []*big.Int
  1052  		C []T
  1053  	}
  1054  
  1055  	type Ret struct {
  1056  		FieldS S `abi:"s"`
  1057  		FieldT T `abi:"t"`
  1058  		A      *big.Int
  1059  	}
  1060  	var ret Ret
  1061  	var expected = Ret{
  1062  		FieldS: S{
  1063  			A: big.NewInt(1),
  1064  			B: []*big.Int{big.NewInt(1), big.NewInt(2)},
  1065  			C: []T{
  1066  				{big.NewInt(1), big.NewInt(2)},
  1067  				{big.NewInt(2), big.NewInt(1)},
  1068  			},
  1069  		},
  1070  		FieldT: T{
  1071  			big.NewInt(0), big.NewInt(1),
  1072  		},
  1073  		A: big.NewInt(1),
  1074  	}
  1075  
  1076  	err = abi.Unpack(&ret, "tuple", buff.Bytes())
  1077  	if err != nil {
  1078  		t.Error(err)
  1079  	}
  1080  	if reflect.DeepEqual(ret, expected) {
  1081  		t.Error("unexpected unpack value")
  1082  	}
  1083  }
  1084  
  1085  func TestOOMMaliciousInput(t *testing.T) {
  1086  	oomTests := []unpackTest{
  1087  		{
  1088  			def: `[{"type": "uint8[]"}]`,
  1089  			enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
  1090  				"0000000000000000000000000000000000000000000000000000000000000003" + // num elems
  1091  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
  1092  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
  1093  		},
  1094  		{ // Length larger than 64 bits
  1095  			def: `[{"type": "uint8[]"}]`,
  1096  			enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
  1097  				"00ffffffffffffffffffffffffffffffffffffffffffffff0000000000000002" + // num elems
  1098  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
  1099  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
  1100  		},
  1101  		{ // Offset very large (over 64 bits)
  1102  			def: `[{"type": "uint8[]"}]`,
  1103  			enc: "00ffffffffffffffffffffffffffffffffffffffffffffff0000000000000020" + // offset
  1104  				"0000000000000000000000000000000000000000000000000000000000000002" + // num elems
  1105  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
  1106  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
  1107  		},
  1108  		{ // Offset very large (below 64 bits)
  1109  			def: `[{"type": "uint8[]"}]`,
  1110  			enc: "0000000000000000000000000000000000000000000000007ffffffffff00020" + // offset
  1111  				"0000000000000000000000000000000000000000000000000000000000000002" + // num elems
  1112  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
  1113  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
  1114  		},
  1115  		{ // Offset negative (as 64 bit)
  1116  			def: `[{"type": "uint8[]"}]`,
  1117  			enc: "000000000000000000000000000000000000000000000000f000000000000020" + // offset
  1118  				"0000000000000000000000000000000000000000000000000000000000000002" + // num elems
  1119  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
  1120  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
  1121  		},
  1122  
  1123  		{ // Negative length
  1124  			def: `[{"type": "uint8[]"}]`,
  1125  			enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
  1126  				"000000000000000000000000000000000000000000000000f000000000000002" + // num elems
  1127  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
  1128  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
  1129  		},
  1130  		{ // Very large length
  1131  			def: `[{"type": "uint8[]"}]`,
  1132  			enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
  1133  				"0000000000000000000000000000000000000000000000007fffffffff000002" + // num elems
  1134  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
  1135  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
  1136  		},
  1137  	}
  1138  	for i, test := range oomTests {
  1139  		def := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def)
  1140  		abi, err := JSON(strings.NewReader(def))
  1141  		if err != nil {
  1142  			t.Fatalf("invalid ABI definition %s: %v", def, err)
  1143  		}
  1144  		encb, err := hex.DecodeString(test.enc)
  1145  		if err != nil {
  1146  			t.Fatalf("invalid hex: %s" + test.enc)
  1147  		}
  1148  		_, err = abi.Methods["method"].Outputs.UnpackValues(encb)
  1149  		if err == nil {
  1150  			t.Fatalf("Expected error on malicious input, test %d", i)
  1151  		}
  1152  	}
  1153  }