github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/accounts/abi/unpack_test.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //版权所有2017 Go Ethereum作者
    10  //此文件是Go以太坊库的一部分。
    11  //
    12  //Go-Ethereum库是免费软件:您可以重新分发它和/或修改
    13  //根据GNU发布的较低通用公共许可证的条款
    14  //自由软件基金会,或者许可证的第3版,或者
    15  //(由您选择)任何更高版本。
    16  //
    17  //Go以太坊图书馆的发行目的是希望它会有用,
    18  //但没有任何保证;甚至没有
    19  //适销性或特定用途的适用性。见
    20  //GNU较低的通用公共许可证,了解更多详细信息。
    21  //
    22  //你应该收到一份GNU较低级别的公共许可证副本
    23  //以及Go以太坊图书馆。如果没有,请参见<http://www.gnu.org/licenses/>。
    24  
    25  package abi
    26  
    27  import (
    28  	"bytes"
    29  	"encoding/hex"
    30  	"fmt"
    31  	"math/big"
    32  	"reflect"
    33  	"strconv"
    34  	"strings"
    35  	"testing"
    36  
    37  	"github.com/ethereum/go-ethereum/common"
    38  	"github.com/stretchr/testify/require"
    39  )
    40  
    41  type unpackTest struct {
    42  def  string      //ABI定义JSON
    43  enc  string      //返回数据
    44  want interface{} //预期产量
    45  err  string      //如果需要,则为空或错误
    46  }
    47  
    48  func (test unpackTest) checkError(err error) error {
    49  	if err != nil {
    50  		if len(test.err) == 0 {
    51  			return fmt.Errorf("expected no err but got: %v", err)
    52  		} else if err.Error() != test.err {
    53  			return fmt.Errorf("expected err: '%v' got err: %q", test.err, err)
    54  		}
    55  	} else if len(test.err) > 0 {
    56  		return fmt.Errorf("expected err: %v but got none", test.err)
    57  	}
    58  	return nil
    59  }
    60  
    61  var unpackTests = []unpackTest{
    62  	{
    63  		def:  `[{ "type": "bool" }]`,
    64  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
    65  		want: true,
    66  	},
    67  	{
    68  		def:  `[{ "type": "bool" }]`,
    69  		enc:  "0000000000000000000000000000000000000000000000000000000000000000",
    70  		want: false,
    71  	},
    72  	{
    73  		def:  `[{ "type": "bool" }]`,
    74  		enc:  "0000000000000000000000000000000000000000000000000001000000000001",
    75  		want: false,
    76  		err:  "abi: improperly encoded boolean value",
    77  	},
    78  	{
    79  		def:  `[{ "type": "bool" }]`,
    80  		enc:  "0000000000000000000000000000000000000000000000000000000000000003",
    81  		want: false,
    82  		err:  "abi: improperly encoded boolean value",
    83  	},
    84  	{
    85  		def:  `[{"type": "uint32"}]`,
    86  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
    87  		want: uint32(1),
    88  	},
    89  	{
    90  		def:  `[{"type": "uint32"}]`,
    91  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
    92  		want: uint16(0),
    93  		err:  "abi: cannot unmarshal uint32 in to uint16",
    94  	},
    95  	{
    96  		def:  `[{"type": "uint17"}]`,
    97  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
    98  		want: uint16(0),
    99  		err:  "abi: cannot unmarshal *big.Int in to uint16",
   100  	},
   101  	{
   102  		def:  `[{"type": "uint17"}]`,
   103  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
   104  		want: big.NewInt(1),
   105  	},
   106  	{
   107  		def:  `[{"type": "int32"}]`,
   108  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
   109  		want: int32(1),
   110  	},
   111  	{
   112  		def:  `[{"type": "int32"}]`,
   113  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
   114  		want: int16(0),
   115  		err:  "abi: cannot unmarshal int32 in to int16",
   116  	},
   117  	{
   118  		def:  `[{"type": "int17"}]`,
   119  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
   120  		want: int16(0),
   121  		err:  "abi: cannot unmarshal *big.Int in to int16",
   122  	},
   123  	{
   124  		def:  `[{"type": "int17"}]`,
   125  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
   126  		want: big.NewInt(1),
   127  	},
   128  	{
   129  		def:  `[{"type": "address"}]`,
   130  		enc:  "0000000000000000000000000100000000000000000000000000000000000000",
   131  		want: common.Address{1},
   132  	},
   133  	{
   134  		def:  `[{"type": "bytes32"}]`,
   135  		enc:  "0100000000000000000000000000000000000000000000000000000000000000",
   136  		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},
   137  	},
   138  	{
   139  		def:  `[{"type": "bytes"}]`,
   140  		enc:  "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
   141  		want: common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   142  	},
   143  	{
   144  		def:  `[{"type": "bytes"}]`,
   145  		enc:  "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
   146  		want: [32]byte{},
   147  		err:  "abi: cannot unmarshal []uint8 in to [32]uint8",
   148  	},
   149  	{
   150  		def:  `[{"type": "bytes32"}]`,
   151  		enc:  "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
   152  		want: []byte(nil),
   153  		err:  "abi: cannot unmarshal [32]uint8 in to []uint8",
   154  	},
   155  	{
   156  		def:  `[{"type": "bytes32"}]`,
   157  		enc:  "0100000000000000000000000000000000000000000000000000000000000000",
   158  		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},
   159  	},
   160  	{
   161  		def:  `[{"type": "function"}]`,
   162  		enc:  "0100000000000000000000000000000000000000000000000000000000000000",
   163  		want: [24]byte{1},
   164  	},
   165  //片
   166  	{
   167  		def:  `[{"type": "uint8[]"}]`,
   168  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   169  		want: []uint8{1, 2},
   170  	},
   171  	{
   172  		def:  `[{"type": "uint8[2]"}]`,
   173  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   174  		want: [2]uint8{1, 2},
   175  	},
   176  //多维,如果这些通过,则不需要长度前缀的所有类型都应通过
   177  	{
   178  		def:  `[{"type": "uint8[][]"}]`,
   179  		enc:  "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000E0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   180  		want: [][]uint8{{1, 2}, {1, 2}},
   181  	},
   182  	{
   183  		def:  `[{"type": "uint8[2][2]"}]`,
   184  		enc:  "0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   185  		want: [2][2]uint8{{1, 2}, {1, 2}},
   186  	},
   187  	{
   188  		def:  `[{"type": "uint8[][2]"}]`,
   189  		enc:  "000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001",
   190  		want: [2][]uint8{{1}, {1}},
   191  	},
   192  	{
   193  		def:  `[{"type": "uint8[2][]"}]`,
   194  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   195  		want: [][2]uint8{{1, 2}},
   196  	},
   197  	{
   198  		def:  `[{"type": "uint16[]"}]`,
   199  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   200  		want: []uint16{1, 2},
   201  	},
   202  	{
   203  		def:  `[{"type": "uint16[2]"}]`,
   204  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   205  		want: [2]uint16{1, 2},
   206  	},
   207  	{
   208  		def:  `[{"type": "uint32[]"}]`,
   209  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   210  		want: []uint32{1, 2},
   211  	},
   212  	{
   213  		def:  `[{"type": "uint32[2]"}]`,
   214  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   215  		want: [2]uint32{1, 2},
   216  	},
   217  	{
   218  		def:  `[{"type": "uint32[2][3][4]"}]`,
   219  		enc:  "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000f000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000001300000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000015000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000170000000000000000000000000000000000000000000000000000000000000018",
   220  		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}}},
   221  	},
   222  	{
   223  		def:  `[{"type": "uint64[]"}]`,
   224  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   225  		want: []uint64{1, 2},
   226  	},
   227  	{
   228  		def:  `[{"type": "uint64[2]"}]`,
   229  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   230  		want: [2]uint64{1, 2},
   231  	},
   232  	{
   233  		def:  `[{"type": "uint256[]"}]`,
   234  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   235  		want: []*big.Int{big.NewInt(1), big.NewInt(2)},
   236  	},
   237  	{
   238  		def:  `[{"type": "uint256[3]"}]`,
   239  		enc:  "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
   240  		want: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)},
   241  	},
   242  	{
   243  		def:  `[{"type": "int8[]"}]`,
   244  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   245  		want: []int8{1, 2},
   246  	},
   247  	{
   248  		def:  `[{"type": "int8[2]"}]`,
   249  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   250  		want: [2]int8{1, 2},
   251  	},
   252  	{
   253  		def:  `[{"type": "int16[]"}]`,
   254  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   255  		want: []int16{1, 2},
   256  	},
   257  	{
   258  		def:  `[{"type": "int16[2]"}]`,
   259  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   260  		want: [2]int16{1, 2},
   261  	},
   262  	{
   263  		def:  `[{"type": "int32[]"}]`,
   264  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   265  		want: []int32{1, 2},
   266  	},
   267  	{
   268  		def:  `[{"type": "int32[2]"}]`,
   269  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   270  		want: [2]int32{1, 2},
   271  	},
   272  	{
   273  		def:  `[{"type": "int64[]"}]`,
   274  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   275  		want: []int64{1, 2},
   276  	},
   277  	{
   278  		def:  `[{"type": "int64[2]"}]`,
   279  		enc:  "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   280  		want: [2]int64{1, 2},
   281  	},
   282  	{
   283  		def:  `[{"type": "int256[]"}]`,
   284  		enc:  "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   285  		want: []*big.Int{big.NewInt(1), big.NewInt(2)},
   286  	},
   287  	{
   288  		def:  `[{"type": "int256[3]"}]`,
   289  		enc:  "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
   290  		want: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)},
   291  	},
   292  //结构输出
   293  	{
   294  		def: `[{"name":"int1","type":"int256"},{"name":"int2","type":"int256"}]`,
   295  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   296  		want: struct {
   297  			Int1 *big.Int
   298  			Int2 *big.Int
   299  		}{big.NewInt(1), big.NewInt(2)},
   300  	},
   301  	{
   302  		def: `[{"name":"int","type":"int256"},{"name":"Int","type":"int256"}]`,
   303  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   304  		want: struct {
   305  			Int1 *big.Int
   306  			Int2 *big.Int
   307  		}{},
   308  		err: "abi: multiple outputs mapping to the same struct field 'Int'",
   309  	},
   310  	{
   311  		def: `[{"name":"int","type":"int256"},{"name":"_int","type":"int256"}]`,
   312  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   313  		want: struct {
   314  			Int1 *big.Int
   315  			Int2 *big.Int
   316  		}{},
   317  		err: "abi: multiple outputs mapping to the same struct field 'Int'",
   318  	},
   319  	{
   320  		def: `[{"name":"Int","type":"int256"},{"name":"_int","type":"int256"}]`,
   321  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   322  		want: struct {
   323  			Int1 *big.Int
   324  			Int2 *big.Int
   325  		}{},
   326  		err: "abi: multiple outputs mapping to the same struct field 'Int'",
   327  	},
   328  	{
   329  		def: `[{"name":"Int","type":"int256"},{"name":"_","type":"int256"}]`,
   330  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   331  		want: struct {
   332  			Int1 *big.Int
   333  			Int2 *big.Int
   334  		}{},
   335  		err: "abi: purely underscored output cannot unpack to struct",
   336  	},
   337  }
   338  
   339  func TestUnpack(t *testing.T) {
   340  	for i, test := range unpackTests {
   341  		t.Run(strconv.Itoa(i), func(t *testing.T) {
   342  			def := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def)
   343  			abi, err := JSON(strings.NewReader(def))
   344  			if err != nil {
   345  				t.Fatalf("invalid ABI definition %s: %v", def, err)
   346  			}
   347  			encb, err := hex.DecodeString(test.enc)
   348  			if err != nil {
   349  				t.Fatalf("invalid hex: %s" + test.enc)
   350  			}
   351  			outptr := reflect.New(reflect.TypeOf(test.want))
   352  			err = abi.Unpack(outptr.Interface(), "method", encb)
   353  			if err := test.checkError(err); err != nil {
   354  				t.Errorf("test %d (%v) failed: %v", i, test.def, err)
   355  				return
   356  			}
   357  			out := outptr.Elem().Interface()
   358  			if !reflect.DeepEqual(test.want, out) {
   359  				t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, test.want, out)
   360  			}
   361  		})
   362  	}
   363  }
   364  
   365  type methodMultiOutput struct {
   366  	Int    *big.Int
   367  	String string
   368  }
   369  
   370  func methodMultiReturn(require *require.Assertions) (ABI, []byte, methodMultiOutput) {
   371  	const definition = `[
   372  	{ "name" : "multi", "constant" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]`
   373  	var expected = methodMultiOutput{big.NewInt(1), "hello"}
   374  
   375  	abi, err := JSON(strings.NewReader(definition))
   376  	require.NoError(err)
   377  //使用buff使代码可读
   378  	buff := new(bytes.Buffer)
   379  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
   380  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
   381  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
   382  	buff.Write(common.RightPadBytes([]byte(expected.String), 32))
   383  	return abi, buff.Bytes(), expected
   384  }
   385  
   386  func TestMethodMultiReturn(t *testing.T) {
   387  	type reversed struct {
   388  		String string
   389  		Int    *big.Int
   390  	}
   391  
   392  	abi, data, expected := methodMultiReturn(require.New(t))
   393  	bigint := new(big.Int)
   394  	var testCases = []struct {
   395  		dest     interface{}
   396  		expected interface{}
   397  		error    string
   398  		name     string
   399  	}{{
   400  		&methodMultiOutput{},
   401  		&expected,
   402  		"",
   403  		"Can unpack into structure",
   404  	}, {
   405  		&reversed{},
   406  		&reversed{expected.String, expected.Int},
   407  		"",
   408  		"Can unpack into reversed structure",
   409  	}, {
   410  		&[]interface{}{&bigint, new(string)},
   411  		&[]interface{}{&expected.Int, &expected.String},
   412  		"",
   413  		"Can unpack into a slice",
   414  	}, {
   415  		&[2]interface{}{&bigint, new(string)},
   416  		&[2]interface{}{&expected.Int, &expected.String},
   417  		"",
   418  		"Can unpack into an array",
   419  	}, {
   420  		&[]interface{}{new(int), new(int)},
   421  		&[]interface{}{&expected.Int, &expected.String},
   422  		"abi: cannot unmarshal *big.Int in to int",
   423  		"Can not unpack into a slice with wrong types",
   424  	}, {
   425  		&[]interface{}{new(int)},
   426  		&[]interface{}{},
   427  		"abi: insufficient number of elements in the list/array for unpack, want 2, got 1",
   428  		"Can not unpack into a slice with wrong types",
   429  	}}
   430  	for _, tc := range testCases {
   431  		tc := tc
   432  		t.Run(tc.name, func(t *testing.T) {
   433  			require := require.New(t)
   434  			err := abi.Unpack(tc.dest, "multi", data)
   435  			if tc.error == "" {
   436  				require.Nil(err, "Should be able to unpack method outputs.")
   437  				require.Equal(tc.expected, tc.dest)
   438  			} else {
   439  				require.EqualError(err, tc.error)
   440  			}
   441  		})
   442  	}
   443  }
   444  
   445  func TestMultiReturnWithArray(t *testing.T) {
   446  	const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3]"}, {"type": "uint64"}]}]`
   447  	abi, err := JSON(strings.NewReader(definition))
   448  	if err != nil {
   449  		t.Fatal(err)
   450  	}
   451  	buff := new(bytes.Buffer)
   452  	buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000900000000000000000000000000000000000000000000000000000000000000090000000000000000000000000000000000000000000000000000000000000009"))
   453  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000008"))
   454  
   455  	ret1, ret1Exp := new([3]uint64), [3]uint64{9, 9, 9}
   456  	ret2, ret2Exp := new(uint64), uint64(8)
   457  	if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil {
   458  		t.Fatal(err)
   459  	}
   460  	if !reflect.DeepEqual(*ret1, ret1Exp) {
   461  		t.Error("array result", *ret1, "!= Expected", ret1Exp)
   462  	}
   463  	if *ret2 != ret2Exp {
   464  		t.Error("int result", *ret2, "!= Expected", ret2Exp)
   465  	}
   466  }
   467  
   468  func TestMultiReturnWithDeeplyNestedArray(t *testing.T) {
   469  //类似于TestMultiReturnWithArray,但考虑到特殊情况:
   470  //嵌套静态数组的值也将计入大小,以及后面的任何元素
   471  //在这种嵌套数组参数之后,应该用正确的偏移量读取,
   472  //这样它就不会从前面的数组参数中读取内容。
   473  	const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3][2][4]"}, {"type": "uint64"}]}]`
   474  	abi, err := JSON(strings.NewReader(definition))
   475  	if err != nil {
   476  		t.Fatal(err)
   477  	}
   478  	buff := new(bytes.Buffer)
   479  //构造测试数组,每个3个char元素与61个“0”chars连接,
   480  //从((3+61)*0.5)=32字节的数组元素。
   481  	buff.Write(common.Hex2Bytes(strings.Join([]string{
   482  "", //空,以便将61个字符分隔符也应用于第一个元素。
   483  		"111", "112", "113", "121", "122", "123",
   484  		"211", "212", "213", "221", "222", "223",
   485  		"311", "312", "313", "321", "322", "323",
   486  		"411", "412", "413", "421", "422", "423",
   487  	}, "0000000000000000000000000000000000000000000000000000000000000")))
   488  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000009876"))
   489  
   490  	ret1, ret1Exp := new([4][2][3]uint64), [4][2][3]uint64{
   491  		{{0x111, 0x112, 0x113}, {0x121, 0x122, 0x123}},
   492  		{{0x211, 0x212, 0x213}, {0x221, 0x222, 0x223}},
   493  		{{0x311, 0x312, 0x313}, {0x321, 0x322, 0x323}},
   494  		{{0x411, 0x412, 0x413}, {0x421, 0x422, 0x423}},
   495  	}
   496  	ret2, ret2Exp := new(uint64), uint64(0x9876)
   497  	if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil {
   498  		t.Fatal(err)
   499  	}
   500  	if !reflect.DeepEqual(*ret1, ret1Exp) {
   501  		t.Error("array result", *ret1, "!= Expected", ret1Exp)
   502  	}
   503  	if *ret2 != ret2Exp {
   504  		t.Error("int result", *ret2, "!= Expected", ret2Exp)
   505  	}
   506  }
   507  
   508  func TestUnmarshal(t *testing.T) {
   509  	const definition = `[
   510  	{ "name" : "int", "constant" : false, "outputs": [ { "type": "uint256" } ] },
   511  	{ "name" : "bool", "constant" : false, "outputs": [ { "type": "bool" } ] },
   512  	{ "name" : "bytes", "constant" : false, "outputs": [ { "type": "bytes" } ] },
   513  	{ "name" : "fixed", "constant" : false, "outputs": [ { "type": "bytes32" } ] },
   514  	{ "name" : "multi", "constant" : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] },
   515  	{ "name" : "intArraySingle", "constant" : false, "outputs": [ { "type": "uint256[3]" } ] },
   516  	{ "name" : "addressSliceSingle", "constant" : false, "outputs": [ { "type": "address[]" } ] },
   517  	{ "name" : "addressSliceDouble", "constant" : false, "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] },
   518  	{ "name" : "mixedBytes", "constant" : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]`
   519  
   520  	abi, err := JSON(strings.NewReader(definition))
   521  	if err != nil {
   522  		t.Fatal(err)
   523  	}
   524  	buff := new(bytes.Buffer)
   525  
   526  //Marshall混合字节(MixedBytes)
   527  	p0, p0Exp := []byte{}, common.Hex2Bytes("01020000000000000000")
   528  	p1, p1Exp := [32]byte{}, common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff")
   529  	mixedBytes := []interface{}{&p0, &p1}
   530  
   531  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
   532  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff"))
   533  	buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000a"))
   534  	buff.Write(common.Hex2Bytes("0102000000000000000000000000000000000000000000000000000000000000"))
   535  
   536  	err = abi.Unpack(&mixedBytes, "mixedBytes", buff.Bytes())
   537  	if err != nil {
   538  		t.Error(err)
   539  	} else {
   540  		if !bytes.Equal(p0, p0Exp) {
   541  			t.Errorf("unexpected value unpacked: want %x, got %x", p0Exp, p0)
   542  		}
   543  
   544  		if !bytes.Equal(p1[:], p1Exp) {
   545  			t.Errorf("unexpected value unpacked: want %x, got %x", p1Exp, p1)
   546  		}
   547  	}
   548  
   549  //元帅INT
   550  	var Int *big.Int
   551  	err = abi.Unpack(&Int, "int", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
   552  	if err != nil {
   553  		t.Error(err)
   554  	}
   555  
   556  	if Int == nil || Int.Cmp(big.NewInt(1)) != 0 {
   557  		t.Error("expected Int to be 1 got", Int)
   558  	}
   559  
   560  //元帅
   561  	var Bool bool
   562  	err = abi.Unpack(&Bool, "bool", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
   563  	if err != nil {
   564  		t.Error(err)
   565  	}
   566  
   567  	if !Bool {
   568  		t.Error("expected Bool to be true")
   569  	}
   570  
   571  //封送动态字节最大长度32
   572  	buff.Reset()
   573  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   574  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   575  	bytesOut := common.RightPadBytes([]byte("hello"), 32)
   576  	buff.Write(bytesOut)
   577  
   578  	var Bytes []byte
   579  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
   580  	if err != nil {
   581  		t.Error(err)
   582  	}
   583  
   584  	if !bytes.Equal(Bytes, bytesOut) {
   585  		t.Errorf("expected %x got %x", bytesOut, Bytes)
   586  	}
   587  
   588  //马歇尔动态字节最大长度64
   589  	buff.Reset()
   590  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   591  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
   592  	bytesOut = common.RightPadBytes([]byte("hello"), 64)
   593  	buff.Write(bytesOut)
   594  
   595  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
   596  	if err != nil {
   597  		t.Error(err)
   598  	}
   599  
   600  	if !bytes.Equal(Bytes, bytesOut) {
   601  		t.Errorf("expected %x got %x", bytesOut, Bytes)
   602  	}
   603  
   604  //马歇尔动态字节最大长度64
   605  	buff.Reset()
   606  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   607  	buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000003f"))
   608  	bytesOut = common.RightPadBytes([]byte("hello"), 64)
   609  	buff.Write(bytesOut)
   610  
   611  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
   612  	if err != nil {
   613  		t.Error(err)
   614  	}
   615  
   616  	if !bytes.Equal(Bytes, bytesOut[:len(bytesOut)-1]) {
   617  		t.Errorf("expected %x got %x", bytesOut[:len(bytesOut)-1], Bytes)
   618  	}
   619  
   620  //封送动态字节输出为空
   621  	err = abi.Unpack(&Bytes, "bytes", nil)
   622  	if err == nil {
   623  		t.Error("expected error")
   624  	}
   625  
   626  //封送动态字节长度5
   627  	buff.Reset()
   628  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   629  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
   630  	buff.Write(common.RightPadBytes([]byte("hello"), 32))
   631  
   632  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
   633  	if err != nil {
   634  		t.Error(err)
   635  	}
   636  
   637  	if !bytes.Equal(Bytes, []byte("hello")) {
   638  		t.Errorf("expected %x got %x", bytesOut, Bytes)
   639  	}
   640  
   641  //封送动态字节长度5
   642  	buff.Reset()
   643  	buff.Write(common.RightPadBytes([]byte("hello"), 32))
   644  
   645  	var hash common.Hash
   646  	err = abi.Unpack(&hash, "fixed", buff.Bytes())
   647  	if err != nil {
   648  		t.Error(err)
   649  	}
   650  
   651  	helloHash := common.BytesToHash(common.RightPadBytes([]byte("hello"), 32))
   652  	if hash != helloHash {
   653  		t.Errorf("Expected %x to equal %x", hash, helloHash)
   654  	}
   655  
   656  //元帅错误
   657  	buff.Reset()
   658  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   659  	err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
   660  	if err == nil {
   661  		t.Error("expected error")
   662  	}
   663  
   664  	err = abi.Unpack(&Bytes, "multi", make([]byte, 64))
   665  	if err == nil {
   666  		t.Error("expected error")
   667  	}
   668  
   669  	buff.Reset()
   670  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
   671  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"))
   672  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003"))
   673  //marshal int数组
   674  	var intArray [3]*big.Int
   675  	err = abi.Unpack(&intArray, "intArraySingle", buff.Bytes())
   676  	if err != nil {
   677  		t.Error(err)
   678  	}
   679  	var testAgainstIntArray [3]*big.Int
   680  	testAgainstIntArray[0] = big.NewInt(1)
   681  	testAgainstIntArray[1] = big.NewInt(2)
   682  	testAgainstIntArray[2] = big.NewInt(3)
   683  
   684  	for i, Int := range intArray {
   685  		if Int.Cmp(testAgainstIntArray[i]) != 0 {
   686  			t.Errorf("expected %v, got %v", testAgainstIntArray[i], Int)
   687  		}
   688  	}
   689  //封送地址切片
   690  	buff.Reset()
   691  buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) //抵消
   692  buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) //大小
   693  	buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
   694  
   695  	var outAddr []common.Address
   696  	err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
   697  	if err != nil {
   698  		t.Fatal("didn't expect error:", err)
   699  	}
   700  
   701  	if len(outAddr) != 1 {
   702  		t.Fatal("expected 1 item, got", len(outAddr))
   703  	}
   704  
   705  	if outAddr[0] != (common.Address{1}) {
   706  		t.Errorf("expected %x, got %x", common.Address{1}, outAddr[0])
   707  	}
   708  
   709  //封送多个地址片
   710  	buff.Reset()
   711  buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) //抵消
   712  buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) //抵消
   713  buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) //大小
   714  	buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
   715  buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) //大小
   716  	buff.Write(common.Hex2Bytes("0000000000000000000000000200000000000000000000000000000000000000"))
   717  	buff.Write(common.Hex2Bytes("0000000000000000000000000300000000000000000000000000000000000000"))
   718  
   719  	var outAddrStruct struct {
   720  		A []common.Address
   721  		B []common.Address
   722  	}
   723  	err = abi.Unpack(&outAddrStruct, "addressSliceDouble", buff.Bytes())
   724  	if err != nil {
   725  		t.Fatal("didn't expect error:", err)
   726  	}
   727  
   728  	if len(outAddrStruct.A) != 1 {
   729  		t.Fatal("expected 1 item, got", len(outAddrStruct.A))
   730  	}
   731  
   732  	if outAddrStruct.A[0] != (common.Address{1}) {
   733  		t.Errorf("expected %x, got %x", common.Address{1}, outAddrStruct.A[0])
   734  	}
   735  
   736  	if len(outAddrStruct.B) != 2 {
   737  		t.Fatal("expected 1 item, got", len(outAddrStruct.B))
   738  	}
   739  
   740  	if outAddrStruct.B[0] != (common.Address{2}) {
   741  		t.Errorf("expected %x, got %x", common.Address{2}, outAddrStruct.B[0])
   742  	}
   743  	if outAddrStruct.B[1] != (common.Address{3}) {
   744  		t.Errorf("expected %x, got %x", common.Address{3}, outAddrStruct.B[1])
   745  	}
   746  
   747  //封送无效的地址切片
   748  	buff.Reset()
   749  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000100"))
   750  
   751  	err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
   752  	if err == nil {
   753  		t.Fatal("expected error:", err)
   754  	}
   755  }
   756  
   757  func TestOOMMaliciousInput(t *testing.T) {
   758  	oomTests := []unpackTest{
   759  		{
   760  			def: `[{"type": "uint8[]"}]`,
   761  enc: "0000000000000000000000000000000000000000000000000000000000000020" + //抵消
   762  "0000000000000000000000000000000000000000000000000000000000000003" + //努姆埃勒姆
   763  "0000000000000000000000000000000000000000000000000000000000000001" + //ELEM 1
   764  "0000000000000000000000000000000000000000000000000000000000000002", //ELEM 2
   765  		},
   766  { //长度大于64位
   767  			def: `[{"type": "uint8[]"}]`,
   768  enc: "0000000000000000000000000000000000000000000000000000000000000020" + //抵消
   769  "00ffffffffffffffffffffffffffffffffffffffffffffff0000000000000002" + //努姆埃勒姆
   770  "0000000000000000000000000000000000000000000000000000000000000001" + //ELEM 1
   771  "0000000000000000000000000000000000000000000000000000000000000002", //ELEM 2
   772  		},
   773  { //偏移量非常大(超过64位)
   774  			def: `[{"type": "uint8[]"}]`,
   775  enc: "00ffffffffffffffffffffffffffffffffffffffffffffff0000000000000020" + //抵消
   776  "0000000000000000000000000000000000000000000000000000000000000002" + //努姆埃勒姆
   777  "0000000000000000000000000000000000000000000000000000000000000001" + //ELEM 1
   778  "0000000000000000000000000000000000000000000000000000000000000002", //ELEM 2
   779  		},
   780  { //偏移量非常大(低于64位)
   781  			def: `[{"type": "uint8[]"}]`,
   782  enc: "0000000000000000000000000000000000000000000000007ffffffffff00020" + //抵消
   783  "0000000000000000000000000000000000000000000000000000000000000002" + //努姆埃勒姆
   784  "0000000000000000000000000000000000000000000000000000000000000001" + //ELEM 1
   785  "0000000000000000000000000000000000000000000000000000000000000002", //ELEM 2
   786  		},
   787  { //负偏移量(64位)
   788  			def: `[{"type": "uint8[]"}]`,
   789  enc: "000000000000000000000000000000000000000000000000f000000000000020" + //抵消
   790  "0000000000000000000000000000000000000000000000000000000000000002" + //努姆埃勒姆
   791  "0000000000000000000000000000000000000000000000000000000000000001" + //ELEM 1
   792  "0000000000000000000000000000000000000000000000000000000000000002", //ELEM 2
   793  		},
   794  
   795  { //负长度
   796  			def: `[{"type": "uint8[]"}]`,
   797  enc: "0000000000000000000000000000000000000000000000000000000000000020" + //抵消
   798  "000000000000000000000000000000000000000000000000f000000000000002" + //努姆埃勒姆
   799  "0000000000000000000000000000000000000000000000000000000000000001" + //ELEM 1
   800  "0000000000000000000000000000000000000000000000000000000000000002", //ELEM 2
   801  		},
   802  { //非常大的长度
   803  			def: `[{"type": "uint8[]"}]`,
   804  enc: "0000000000000000000000000000000000000000000000000000000000000020" + //抵消
   805  "0000000000000000000000000000000000000000000000007fffffffff000002" + //努姆埃勒姆
   806  "0000000000000000000000000000000000000000000000000000000000000001" + //ELEM 1
   807  "0000000000000000000000000000000000000000000000000000000000000002", //ELEM 2
   808  		},
   809  	}
   810  	for i, test := range oomTests {
   811  		def := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def)
   812  		abi, err := JSON(strings.NewReader(def))
   813  		if err != nil {
   814  			t.Fatalf("invalid ABI definition %s: %v", def, err)
   815  		}
   816  		encb, err := hex.DecodeString(test.enc)
   817  		if err != nil {
   818  			t.Fatalf("invalid hex: %s" + test.enc)
   819  		}
   820  		_, err = abi.Methods["method"].Outputs.UnpackValues(encb)
   821  		if err == nil {
   822  			t.Fatalf("Expected error on malicious input, test %d", i)
   823  		}
   824  	}
   825  }