github.com/alexdevranger/node-1.8.27@v0.0.0-20221128213301-aa5841e41d2d/accounts/abi/unpack_test.go (about)

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