github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/accounts/abi/unpack_test.go (about)

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