github.com/nonsense/go-ethereum@v1.9.7/accounts/abi/pack_test.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package abi
    18  
    19  import (
    20  	"bytes"
    21  	"math"
    22  	"math/big"
    23  	"reflect"
    24  	"strings"
    25  	"testing"
    26  
    27  	"github.com/ethereum/go-ethereum/common"
    28  )
    29  
    30  func TestPack(t *testing.T) {
    31  	for i, test := range []struct {
    32  		typ        string
    33  		components []ArgumentMarshaling
    34  		input      interface{}
    35  		output     []byte
    36  	}{
    37  		{
    38  			"uint8",
    39  			nil,
    40  			uint8(2),
    41  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
    42  		},
    43  		{
    44  			"uint8[]",
    45  			nil,
    46  			[]uint8{1, 2},
    47  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
    48  		},
    49  		{
    50  			"uint16",
    51  			nil,
    52  			uint16(2),
    53  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
    54  		},
    55  		{
    56  			"uint16[]",
    57  			nil,
    58  			[]uint16{1, 2},
    59  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
    60  		},
    61  		{
    62  			"uint32",
    63  			nil,
    64  			uint32(2),
    65  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
    66  		},
    67  		{
    68  			"uint32[]",
    69  			nil,
    70  			[]uint32{1, 2},
    71  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
    72  		},
    73  		{
    74  			"uint64",
    75  			nil,
    76  			uint64(2),
    77  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
    78  		},
    79  		{
    80  			"uint64[]",
    81  			nil,
    82  			[]uint64{1, 2},
    83  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
    84  		},
    85  		{
    86  			"uint256",
    87  			nil,
    88  			big.NewInt(2),
    89  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
    90  		},
    91  		{
    92  			"uint256[]",
    93  			nil,
    94  			[]*big.Int{big.NewInt(1), big.NewInt(2)},
    95  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
    96  		},
    97  		{
    98  			"int8",
    99  			nil,
   100  			int8(2),
   101  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
   102  		},
   103  		{
   104  			"int8[]",
   105  			nil,
   106  			[]int8{1, 2},
   107  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
   108  		},
   109  		{
   110  			"int16",
   111  			nil,
   112  			int16(2),
   113  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
   114  		},
   115  		{
   116  			"int16[]",
   117  			nil,
   118  			[]int16{1, 2},
   119  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
   120  		},
   121  		{
   122  			"int32",
   123  			nil,
   124  			int32(2),
   125  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
   126  		},
   127  		{
   128  			"int32[]",
   129  			nil,
   130  			[]int32{1, 2},
   131  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
   132  		},
   133  		{
   134  			"int64",
   135  			nil,
   136  			int64(2),
   137  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
   138  		},
   139  		{
   140  			"int64[]",
   141  			nil,
   142  			[]int64{1, 2},
   143  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
   144  		},
   145  		{
   146  			"int256",
   147  			nil,
   148  			big.NewInt(2),
   149  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
   150  		},
   151  		{
   152  			"int256[]",
   153  			nil,
   154  			[]*big.Int{big.NewInt(1), big.NewInt(2)},
   155  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
   156  		},
   157  		{
   158  			"bytes1",
   159  			nil,
   160  			[1]byte{1},
   161  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   162  		},
   163  		{
   164  			"bytes2",
   165  			nil,
   166  			[2]byte{1},
   167  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   168  		},
   169  		{
   170  			"bytes3",
   171  			nil,
   172  			[3]byte{1},
   173  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   174  		},
   175  		{
   176  			"bytes4",
   177  			nil,
   178  			[4]byte{1},
   179  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   180  		},
   181  		{
   182  			"bytes5",
   183  			nil,
   184  			[5]byte{1},
   185  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   186  		},
   187  		{
   188  			"bytes6",
   189  			nil,
   190  			[6]byte{1},
   191  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   192  		},
   193  		{
   194  			"bytes7",
   195  			nil,
   196  			[7]byte{1},
   197  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   198  		},
   199  		{
   200  			"bytes8",
   201  			nil,
   202  			[8]byte{1},
   203  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   204  		},
   205  		{
   206  			"bytes9",
   207  			nil,
   208  			[9]byte{1},
   209  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   210  		},
   211  		{
   212  			"bytes10",
   213  			nil,
   214  			[10]byte{1},
   215  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   216  		},
   217  		{
   218  			"bytes11",
   219  			nil,
   220  			[11]byte{1},
   221  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   222  		},
   223  		{
   224  			"bytes12",
   225  			nil,
   226  			[12]byte{1},
   227  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   228  		},
   229  		{
   230  			"bytes13",
   231  			nil,
   232  			[13]byte{1},
   233  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   234  		},
   235  		{
   236  			"bytes14",
   237  			nil,
   238  			[14]byte{1},
   239  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   240  		},
   241  		{
   242  			"bytes15",
   243  			nil,
   244  			[15]byte{1},
   245  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   246  		},
   247  		{
   248  			"bytes16",
   249  			nil,
   250  			[16]byte{1},
   251  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   252  		},
   253  		{
   254  			"bytes17",
   255  			nil,
   256  			[17]byte{1},
   257  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   258  		},
   259  		{
   260  			"bytes18",
   261  			nil,
   262  			[18]byte{1},
   263  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   264  		},
   265  		{
   266  			"bytes19",
   267  			nil,
   268  			[19]byte{1},
   269  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   270  		},
   271  		{
   272  			"bytes20",
   273  			nil,
   274  			[20]byte{1},
   275  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   276  		},
   277  		{
   278  			"bytes21",
   279  			nil,
   280  			[21]byte{1},
   281  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   282  		},
   283  		{
   284  			"bytes22",
   285  			nil,
   286  			[22]byte{1},
   287  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   288  		},
   289  		{
   290  			"bytes23",
   291  			nil,
   292  			[23]byte{1},
   293  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   294  		},
   295  		{
   296  			"bytes24",
   297  			nil,
   298  			[24]byte{1},
   299  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   300  		},
   301  		{
   302  			"bytes25",
   303  			nil,
   304  			[25]byte{1},
   305  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   306  		},
   307  		{
   308  			"bytes26",
   309  			nil,
   310  			[26]byte{1},
   311  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   312  		},
   313  		{
   314  			"bytes27",
   315  			nil,
   316  			[27]byte{1},
   317  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   318  		},
   319  		{
   320  			"bytes28",
   321  			nil,
   322  			[28]byte{1},
   323  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   324  		},
   325  		{
   326  			"bytes29",
   327  			nil,
   328  			[29]byte{1},
   329  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   330  		},
   331  		{
   332  			"bytes30",
   333  			nil,
   334  			[30]byte{1},
   335  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   336  		},
   337  		{
   338  			"bytes31",
   339  			nil,
   340  			[31]byte{1},
   341  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   342  		},
   343  		{
   344  			"bytes32",
   345  			nil,
   346  			[32]byte{1},
   347  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   348  		},
   349  		{
   350  			"uint32[2][3][4]",
   351  			nil,
   352  			[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}}},
   353  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000f000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000001300000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000015000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000170000000000000000000000000000000000000000000000000000000000000018"),
   354  		},
   355  		{
   356  			"address[]",
   357  			nil,
   358  			[]common.Address{{1}, {2}},
   359  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000"),
   360  		},
   361  		{
   362  			"bytes32[]",
   363  			nil,
   364  			[]common.Hash{{1}, {2}},
   365  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000201000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000"),
   366  		},
   367  		{
   368  			"function",
   369  			nil,
   370  			[24]byte{1},
   371  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   372  		},
   373  		{
   374  			"string",
   375  			nil,
   376  			"foobar",
   377  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000006666f6f6261720000000000000000000000000000000000000000000000000000"),
   378  		},
   379  		{
   380  			"string[]",
   381  			nil,
   382  			[]string{"hello", "foobar"},
   383  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002" + // len(array) = 2
   384  				"0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i = 0
   385  				"0000000000000000000000000000000000000000000000000000000000000080" + // offset 128 to i = 1
   386  				"0000000000000000000000000000000000000000000000000000000000000005" + // len(str[0]) = 5
   387  				"68656c6c6f000000000000000000000000000000000000000000000000000000" + // str[0]
   388  				"0000000000000000000000000000000000000000000000000000000000000006" + // len(str[1]) = 6
   389  				"666f6f6261720000000000000000000000000000000000000000000000000000"), // str[1]
   390  		},
   391  		{
   392  			"string[2]",
   393  			nil,
   394  			[]string{"hello", "foobar"},
   395  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040" + // offset to i = 0
   396  				"0000000000000000000000000000000000000000000000000000000000000080" + // offset to i = 1
   397  				"0000000000000000000000000000000000000000000000000000000000000005" + // len(str[0]) = 5
   398  				"68656c6c6f000000000000000000000000000000000000000000000000000000" + // str[0]
   399  				"0000000000000000000000000000000000000000000000000000000000000006" + // len(str[1]) = 6
   400  				"666f6f6261720000000000000000000000000000000000000000000000000000"), // str[1]
   401  		},
   402  		{
   403  			"bytes32[][]",
   404  			nil,
   405  			[][]common.Hash{{{1}, {2}}, {{3}, {4}, {5}}},
   406  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002" + // len(array) = 2
   407  				"0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i = 0
   408  				"00000000000000000000000000000000000000000000000000000000000000a0" + // offset 160 to i = 1
   409  				"0000000000000000000000000000000000000000000000000000000000000002" + // len(array[0]) = 2
   410  				"0100000000000000000000000000000000000000000000000000000000000000" + // array[0][0]
   411  				"0200000000000000000000000000000000000000000000000000000000000000" + // array[0][1]
   412  				"0000000000000000000000000000000000000000000000000000000000000003" + // len(array[1]) = 3
   413  				"0300000000000000000000000000000000000000000000000000000000000000" + // array[1][0]
   414  				"0400000000000000000000000000000000000000000000000000000000000000" + // array[1][1]
   415  				"0500000000000000000000000000000000000000000000000000000000000000"), // array[1][2]
   416  		},
   417  
   418  		{
   419  			"bytes32[][2]",
   420  			nil,
   421  			[][]common.Hash{{{1}, {2}}, {{3}, {4}, {5}}},
   422  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040" + // offset 64 to i = 0
   423  				"00000000000000000000000000000000000000000000000000000000000000a0" + // offset 160 to i = 1
   424  				"0000000000000000000000000000000000000000000000000000000000000002" + // len(array[0]) = 2
   425  				"0100000000000000000000000000000000000000000000000000000000000000" + // array[0][0]
   426  				"0200000000000000000000000000000000000000000000000000000000000000" + // array[0][1]
   427  				"0000000000000000000000000000000000000000000000000000000000000003" + // len(array[1]) = 3
   428  				"0300000000000000000000000000000000000000000000000000000000000000" + // array[1][0]
   429  				"0400000000000000000000000000000000000000000000000000000000000000" + // array[1][1]
   430  				"0500000000000000000000000000000000000000000000000000000000000000"), // array[1][2]
   431  		},
   432  
   433  		{
   434  			"bytes32[3][2]",
   435  			nil,
   436  			[][]common.Hash{{{1}, {2}, {3}}, {{3}, {4}, {5}}},
   437  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000" + // array[0][0]
   438  				"0200000000000000000000000000000000000000000000000000000000000000" + // array[0][1]
   439  				"0300000000000000000000000000000000000000000000000000000000000000" + // array[0][2]
   440  				"0300000000000000000000000000000000000000000000000000000000000000" + // array[1][0]
   441  				"0400000000000000000000000000000000000000000000000000000000000000" + // array[1][1]
   442  				"0500000000000000000000000000000000000000000000000000000000000000"), // array[1][2]
   443  		},
   444  		{
   445  			// static tuple
   446  			"tuple",
   447  			[]ArgumentMarshaling{
   448  				{Name: "a", Type: "int64"},
   449  				{Name: "b", Type: "int256"},
   450  				{Name: "c", Type: "int256"},
   451  				{Name: "d", Type: "bool"},
   452  				{Name: "e", Type: "bytes32[3][2]"},
   453  			},
   454  			struct {
   455  				A int64
   456  				B *big.Int
   457  				C *big.Int
   458  				D bool
   459  				E [][]common.Hash
   460  			}{1, big.NewInt(1), big.NewInt(-1), true, [][]common.Hash{{{1}, {2}, {3}}, {{3}, {4}, {5}}}},
   461  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001" + // struct[a]
   462  				"0000000000000000000000000000000000000000000000000000000000000001" + // struct[b]
   463  				"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + // struct[c]
   464  				"0000000000000000000000000000000000000000000000000000000000000001" + // struct[d]
   465  				"0100000000000000000000000000000000000000000000000000000000000000" + // struct[e] array[0][0]
   466  				"0200000000000000000000000000000000000000000000000000000000000000" + // struct[e] array[0][1]
   467  				"0300000000000000000000000000000000000000000000000000000000000000" + // struct[e] array[0][2]
   468  				"0300000000000000000000000000000000000000000000000000000000000000" + // struct[e] array[1][0]
   469  				"0400000000000000000000000000000000000000000000000000000000000000" + // struct[e] array[1][1]
   470  				"0500000000000000000000000000000000000000000000000000000000000000"), // struct[e] array[1][2]
   471  		},
   472  		{
   473  			// dynamic tuple
   474  			"tuple",
   475  			[]ArgumentMarshaling{
   476  				{Name: "a", Type: "string"},
   477  				{Name: "b", Type: "int64"},
   478  				{Name: "c", Type: "bytes"},
   479  				{Name: "d", Type: "string[]"},
   480  				{Name: "e", Type: "int256[]"},
   481  				{Name: "f", Type: "address[]"},
   482  			},
   483  			struct {
   484  				FieldA string `abi:"a"` // Test whether abi tag works
   485  				FieldB int64  `abi:"b"`
   486  				C      []byte
   487  				D      []string
   488  				E      []*big.Int
   489  				F      []common.Address
   490  			}{"foobar", 1, []byte{1}, []string{"foo", "bar"}, []*big.Int{big.NewInt(1), big.NewInt(-1)}, []common.Address{{1}, {2}}},
   491  			common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000c0" + // struct[a] offset
   492  				"0000000000000000000000000000000000000000000000000000000000000001" + // struct[b]
   493  				"0000000000000000000000000000000000000000000000000000000000000100" + // struct[c] offset
   494  				"0000000000000000000000000000000000000000000000000000000000000140" + // struct[d] offset
   495  				"0000000000000000000000000000000000000000000000000000000000000220" + // struct[e] offset
   496  				"0000000000000000000000000000000000000000000000000000000000000280" + // struct[f] offset
   497  				"0000000000000000000000000000000000000000000000000000000000000006" + // struct[a] length
   498  				"666f6f6261720000000000000000000000000000000000000000000000000000" + // struct[a] "foobar"
   499  				"0000000000000000000000000000000000000000000000000000000000000001" + // struct[c] length
   500  				"0100000000000000000000000000000000000000000000000000000000000000" + // []byte{1}
   501  				"0000000000000000000000000000000000000000000000000000000000000002" + // struct[d] length
   502  				"0000000000000000000000000000000000000000000000000000000000000040" + // foo offset
   503  				"0000000000000000000000000000000000000000000000000000000000000080" + // bar offset
   504  				"0000000000000000000000000000000000000000000000000000000000000003" + // foo length
   505  				"666f6f0000000000000000000000000000000000000000000000000000000000" + // foo
   506  				"0000000000000000000000000000000000000000000000000000000000000003" + // bar offset
   507  				"6261720000000000000000000000000000000000000000000000000000000000" + // bar
   508  				"0000000000000000000000000000000000000000000000000000000000000002" + // struct[e] length
   509  				"0000000000000000000000000000000000000000000000000000000000000001" + // 1
   510  				"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + // -1
   511  				"0000000000000000000000000000000000000000000000000000000000000002" + // struct[f] length
   512  				"0000000000000000000000000100000000000000000000000000000000000000" + // common.Address{1}
   513  				"0000000000000000000000000200000000000000000000000000000000000000"), // common.Address{2}
   514  		},
   515  		{
   516  			// nested tuple
   517  			"tuple",
   518  			[]ArgumentMarshaling{
   519  				{Name: "a", Type: "tuple", Components: []ArgumentMarshaling{{Name: "a", Type: "uint256"}, {Name: "b", Type: "uint256[]"}}},
   520  				{Name: "b", Type: "int256[]"},
   521  			},
   522  			struct {
   523  				A struct {
   524  					FieldA *big.Int `abi:"a"`
   525  					B      []*big.Int
   526  				}
   527  				B []*big.Int
   528  			}{
   529  				A: struct {
   530  					FieldA *big.Int `abi:"a"` // Test whether abi tag works for nested tuple
   531  					B      []*big.Int
   532  				}{big.NewInt(1), []*big.Int{big.NewInt(1), big.NewInt(0)}},
   533  				B: []*big.Int{big.NewInt(1), big.NewInt(0)}},
   534  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040" + // a offset
   535  				"00000000000000000000000000000000000000000000000000000000000000e0" + // b offset
   536  				"0000000000000000000000000000000000000000000000000000000000000001" + // a.a value
   537  				"0000000000000000000000000000000000000000000000000000000000000040" + // a.b offset
   538  				"0000000000000000000000000000000000000000000000000000000000000002" + // a.b length
   539  				"0000000000000000000000000000000000000000000000000000000000000001" + // a.b[0] value
   540  				"0000000000000000000000000000000000000000000000000000000000000000" + // a.b[1] value
   541  				"0000000000000000000000000000000000000000000000000000000000000002" + // b length
   542  				"0000000000000000000000000000000000000000000000000000000000000001" + // b[0] value
   543  				"0000000000000000000000000000000000000000000000000000000000000000"), // b[1] value
   544  		},
   545  		{
   546  			// tuple slice
   547  			"tuple[]",
   548  			[]ArgumentMarshaling{
   549  				{Name: "a", Type: "int256"},
   550  				{Name: "b", Type: "int256[]"},
   551  			},
   552  			[]struct {
   553  				A *big.Int
   554  				B []*big.Int
   555  			}{
   556  				{big.NewInt(-1), []*big.Int{big.NewInt(1), big.NewInt(0)}},
   557  				{big.NewInt(1), []*big.Int{big.NewInt(2), big.NewInt(-1)}},
   558  			},
   559  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002" + // tuple length
   560  				"0000000000000000000000000000000000000000000000000000000000000040" + // tuple[0] offset
   561  				"00000000000000000000000000000000000000000000000000000000000000e0" + // tuple[1] offset
   562  				"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + // tuple[0].A
   563  				"0000000000000000000000000000000000000000000000000000000000000040" + // tuple[0].B offset
   564  				"0000000000000000000000000000000000000000000000000000000000000002" + // tuple[0].B length
   565  				"0000000000000000000000000000000000000000000000000000000000000001" + // tuple[0].B[0] value
   566  				"0000000000000000000000000000000000000000000000000000000000000000" + // tuple[0].B[1] value
   567  				"0000000000000000000000000000000000000000000000000000000000000001" + // tuple[1].A
   568  				"0000000000000000000000000000000000000000000000000000000000000040" + // tuple[1].B offset
   569  				"0000000000000000000000000000000000000000000000000000000000000002" + // tuple[1].B length
   570  				"0000000000000000000000000000000000000000000000000000000000000002" + // tuple[1].B[0] value
   571  				"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), // tuple[1].B[1] value
   572  		},
   573  		{
   574  			// static tuple array
   575  			"tuple[2]",
   576  			[]ArgumentMarshaling{
   577  				{Name: "a", Type: "int256"},
   578  				{Name: "b", Type: "int256"},
   579  			},
   580  			[2]struct {
   581  				A *big.Int
   582  				B *big.Int
   583  			}{
   584  				{big.NewInt(-1), big.NewInt(1)},
   585  				{big.NewInt(1), big.NewInt(-1)},
   586  			},
   587  			common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + // tuple[0].a
   588  				"0000000000000000000000000000000000000000000000000000000000000001" + // tuple[0].b
   589  				"0000000000000000000000000000000000000000000000000000000000000001" + // tuple[1].a
   590  				"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), // tuple[1].b
   591  		},
   592  		{
   593  			// dynamic tuple array
   594  			"tuple[2]",
   595  			[]ArgumentMarshaling{
   596  				{Name: "a", Type: "int256[]"},
   597  			},
   598  			[2]struct {
   599  				A []*big.Int
   600  			}{
   601  				{[]*big.Int{big.NewInt(-1), big.NewInt(1)}},
   602  				{[]*big.Int{big.NewInt(1), big.NewInt(-1)}},
   603  			},
   604  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040" + // tuple[0] offset
   605  				"00000000000000000000000000000000000000000000000000000000000000c0" + // tuple[1] offset
   606  				"0000000000000000000000000000000000000000000000000000000000000020" + // tuple[0].A offset
   607  				"0000000000000000000000000000000000000000000000000000000000000002" + // tuple[0].A length
   608  				"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + // tuple[0].A[0]
   609  				"0000000000000000000000000000000000000000000000000000000000000001" + // tuple[0].A[1]
   610  				"0000000000000000000000000000000000000000000000000000000000000020" + // tuple[1].A offset
   611  				"0000000000000000000000000000000000000000000000000000000000000002" + // tuple[1].A length
   612  				"0000000000000000000000000000000000000000000000000000000000000001" + // tuple[1].A[0]
   613  				"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), // tuple[1].A[1]
   614  		},
   615  	} {
   616  		typ, err := NewType(test.typ, "", test.components)
   617  		if err != nil {
   618  			t.Fatalf("%v failed. Unexpected parse error: %v", i, err)
   619  		}
   620  		output, err := typ.pack(reflect.ValueOf(test.input))
   621  		if err != nil {
   622  			t.Fatalf("%v failed. Unexpected pack error: %v", i, err)
   623  		}
   624  
   625  		if !bytes.Equal(output, test.output) {
   626  			t.Errorf("input %d for typ: %v failed. Expected bytes: '%x' Got: '%x'", i, typ.String(), test.output, output)
   627  		}
   628  	}
   629  }
   630  
   631  func TestMethodPack(t *testing.T) {
   632  	abi, err := JSON(strings.NewReader(jsondata2))
   633  	if err != nil {
   634  		t.Fatal(err)
   635  	}
   636  
   637  	sig := abi.Methods["slice"].ID()
   638  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   639  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   640  
   641  	packed, err := abi.Pack("slice", []uint32{1, 2})
   642  	if err != nil {
   643  		t.Error(err)
   644  	}
   645  
   646  	if !bytes.Equal(packed, sig) {
   647  		t.Errorf("expected %x got %x", sig, packed)
   648  	}
   649  
   650  	var addrA, addrB = common.Address{1}, common.Address{2}
   651  	sig = abi.Methods["sliceAddress"].ID()
   652  	sig = append(sig, common.LeftPadBytes([]byte{32}, 32)...)
   653  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   654  	sig = append(sig, common.LeftPadBytes(addrA[:], 32)...)
   655  	sig = append(sig, common.LeftPadBytes(addrB[:], 32)...)
   656  
   657  	packed, err = abi.Pack("sliceAddress", []common.Address{addrA, addrB})
   658  	if err != nil {
   659  		t.Fatal(err)
   660  	}
   661  	if !bytes.Equal(packed, sig) {
   662  		t.Errorf("expected %x got %x", sig, packed)
   663  	}
   664  
   665  	var addrC, addrD = common.Address{3}, common.Address{4}
   666  	sig = abi.Methods["sliceMultiAddress"].ID()
   667  	sig = append(sig, common.LeftPadBytes([]byte{64}, 32)...)
   668  	sig = append(sig, common.LeftPadBytes([]byte{160}, 32)...)
   669  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   670  	sig = append(sig, common.LeftPadBytes(addrA[:], 32)...)
   671  	sig = append(sig, common.LeftPadBytes(addrB[:], 32)...)
   672  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   673  	sig = append(sig, common.LeftPadBytes(addrC[:], 32)...)
   674  	sig = append(sig, common.LeftPadBytes(addrD[:], 32)...)
   675  
   676  	packed, err = abi.Pack("sliceMultiAddress", []common.Address{addrA, addrB}, []common.Address{addrC, addrD})
   677  	if err != nil {
   678  		t.Fatal(err)
   679  	}
   680  	if !bytes.Equal(packed, sig) {
   681  		t.Errorf("expected %x got %x", sig, packed)
   682  	}
   683  
   684  	sig = abi.Methods["slice256"].ID()
   685  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   686  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   687  
   688  	packed, err = abi.Pack("slice256", []*big.Int{big.NewInt(1), big.NewInt(2)})
   689  	if err != nil {
   690  		t.Error(err)
   691  	}
   692  
   693  	if !bytes.Equal(packed, sig) {
   694  		t.Errorf("expected %x got %x", sig, packed)
   695  	}
   696  
   697  	a := [2][2]*big.Int{{big.NewInt(1), big.NewInt(1)}, {big.NewInt(2), big.NewInt(0)}}
   698  	sig = abi.Methods["nestedArray"].ID()
   699  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   700  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   701  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   702  	sig = append(sig, common.LeftPadBytes([]byte{0}, 32)...)
   703  	sig = append(sig, common.LeftPadBytes([]byte{0xa0}, 32)...)
   704  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   705  	sig = append(sig, common.LeftPadBytes(addrC[:], 32)...)
   706  	sig = append(sig, common.LeftPadBytes(addrD[:], 32)...)
   707  	packed, err = abi.Pack("nestedArray", a, []common.Address{addrC, addrD})
   708  	if err != nil {
   709  		t.Fatal(err)
   710  	}
   711  	if !bytes.Equal(packed, sig) {
   712  		t.Errorf("expected %x got %x", sig, packed)
   713  	}
   714  
   715  	sig = abi.Methods["nestedArray2"].ID()
   716  	sig = append(sig, common.LeftPadBytes([]byte{0x20}, 32)...)
   717  	sig = append(sig, common.LeftPadBytes([]byte{0x40}, 32)...)
   718  	sig = append(sig, common.LeftPadBytes([]byte{0x80}, 32)...)
   719  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   720  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   721  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   722  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   723  	packed, err = abi.Pack("nestedArray2", [2][]uint8{{1}, {1}})
   724  	if err != nil {
   725  		t.Fatal(err)
   726  	}
   727  	if !bytes.Equal(packed, sig) {
   728  		t.Errorf("expected %x got %x", sig, packed)
   729  	}
   730  
   731  	sig = abi.Methods["nestedSlice"].ID()
   732  	sig = append(sig, common.LeftPadBytes([]byte{0x20}, 32)...)
   733  	sig = append(sig, common.LeftPadBytes([]byte{0x02}, 32)...)
   734  	sig = append(sig, common.LeftPadBytes([]byte{0x40}, 32)...)
   735  	sig = append(sig, common.LeftPadBytes([]byte{0xa0}, 32)...)
   736  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   737  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   738  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   739  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   740  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   741  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   742  	packed, err = abi.Pack("nestedSlice", [][]uint8{{1, 2}, {1, 2}})
   743  	if err != nil {
   744  		t.Fatal(err)
   745  	}
   746  	if !bytes.Equal(packed, sig) {
   747  		t.Errorf("expected %x got %x", sig, packed)
   748  	}
   749  }
   750  
   751  func TestPackNumber(t *testing.T) {
   752  	tests := []struct {
   753  		value  reflect.Value
   754  		packed []byte
   755  	}{
   756  		// Protocol limits
   757  		{reflect.ValueOf(0), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000000")},
   758  		{reflect.ValueOf(1), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")},
   759  		{reflect.ValueOf(-1), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")},
   760  
   761  		// Type corner cases
   762  		{reflect.ValueOf(uint8(math.MaxUint8)), common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000ff")},
   763  		{reflect.ValueOf(uint16(math.MaxUint16)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000ffff")},
   764  		{reflect.ValueOf(uint32(math.MaxUint32)), common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000ffffffff")},
   765  		{reflect.ValueOf(uint64(math.MaxUint64)), common.Hex2Bytes("000000000000000000000000000000000000000000000000ffffffffffffffff")},
   766  
   767  		{reflect.ValueOf(int8(math.MaxInt8)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000007f")},
   768  		{reflect.ValueOf(int16(math.MaxInt16)), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000007fff")},
   769  		{reflect.ValueOf(int32(math.MaxInt32)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000007fffffff")},
   770  		{reflect.ValueOf(int64(math.MaxInt64)), common.Hex2Bytes("0000000000000000000000000000000000000000000000007fffffffffffffff")},
   771  
   772  		{reflect.ValueOf(int8(math.MinInt8)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80")},
   773  		{reflect.ValueOf(int16(math.MinInt16)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8000")},
   774  		{reflect.ValueOf(int32(math.MinInt32)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000")},
   775  		{reflect.ValueOf(int64(math.MinInt64)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffff8000000000000000")},
   776  	}
   777  	for i, tt := range tests {
   778  		packed := packNum(tt.value)
   779  		if !bytes.Equal(packed, tt.packed) {
   780  			t.Errorf("test %d: pack mismatch: have %x, want %x", i, packed, tt.packed)
   781  		}
   782  	}
   783  }