github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/accounts/abi/unpack_test.go (about)

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