github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/accounts/abi/pack_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 12:09:26</date>
    10  //</624342583828549632>
    11  
    12  
    13  package abi
    14  
    15  import (
    16  	"bytes"
    17  	"math"
    18  	"math/big"
    19  	"reflect"
    20  	"strings"
    21  	"testing"
    22  
    23  	"github.com/ethereum/go-ethereum/common"
    24  )
    25  
    26  func TestPack(t *testing.T) {
    27  	for i, test := range []struct {
    28  		typ string
    29  
    30  		input  interface{}
    31  		output []byte
    32  	}{
    33  		{
    34  			"uint8",
    35  			uint8(2),
    36  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
    37  		},
    38  		{
    39  			"uint8[]",
    40  			[]uint8{1, 2},
    41  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
    42  		},
    43  		{
    44  			"uint16",
    45  			uint16(2),
    46  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
    47  		},
    48  		{
    49  			"uint16[]",
    50  			[]uint16{1, 2},
    51  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
    52  		},
    53  		{
    54  			"uint32",
    55  			uint32(2),
    56  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
    57  		},
    58  		{
    59  			"uint32[]",
    60  			[]uint32{1, 2},
    61  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
    62  		},
    63  		{
    64  			"uint64",
    65  			uint64(2),
    66  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
    67  		},
    68  		{
    69  			"uint64[]",
    70  			[]uint64{1, 2},
    71  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
    72  		},
    73  		{
    74  			"uint256",
    75  			big.NewInt(2),
    76  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
    77  		},
    78  		{
    79  			"uint256[]",
    80  			[]*big.Int{big.NewInt(1), big.NewInt(2)},
    81  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
    82  		},
    83  		{
    84  			"int8",
    85  			int8(2),
    86  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
    87  		},
    88  		{
    89  			"int8[]",
    90  			[]int8{1, 2},
    91  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
    92  		},
    93  		{
    94  			"int16",
    95  			int16(2),
    96  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
    97  		},
    98  		{
    99  			"int16[]",
   100  			[]int16{1, 2},
   101  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
   102  		},
   103  		{
   104  			"int32",
   105  			int32(2),
   106  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
   107  		},
   108  		{
   109  			"int32[]",
   110  			[]int32{1, 2},
   111  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
   112  		},
   113  		{
   114  			"int64",
   115  			int64(2),
   116  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
   117  		},
   118  		{
   119  			"int64[]",
   120  			[]int64{1, 2},
   121  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
   122  		},
   123  		{
   124  			"int256",
   125  			big.NewInt(2),
   126  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"),
   127  		},
   128  		{
   129  			"int256[]",
   130  			[]*big.Int{big.NewInt(1), big.NewInt(2)},
   131  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"),
   132  		},
   133  		{
   134  			"bytes1",
   135  			[1]byte{1},
   136  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   137  		},
   138  		{
   139  			"bytes2",
   140  			[2]byte{1},
   141  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   142  		},
   143  		{
   144  			"bytes3",
   145  			[3]byte{1},
   146  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   147  		},
   148  		{
   149  			"bytes4",
   150  			[4]byte{1},
   151  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   152  		},
   153  		{
   154  			"bytes5",
   155  			[5]byte{1},
   156  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   157  		},
   158  		{
   159  			"bytes6",
   160  			[6]byte{1},
   161  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   162  		},
   163  		{
   164  			"bytes7",
   165  			[7]byte{1},
   166  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   167  		},
   168  		{
   169  			"bytes8",
   170  			[8]byte{1},
   171  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   172  		},
   173  		{
   174  			"bytes9",
   175  			[9]byte{1},
   176  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   177  		},
   178  		{
   179  			"bytes10",
   180  			[10]byte{1},
   181  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   182  		},
   183  		{
   184  			"bytes11",
   185  			[11]byte{1},
   186  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   187  		},
   188  		{
   189  			"bytes12",
   190  			[12]byte{1},
   191  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   192  		},
   193  		{
   194  			"bytes13",
   195  			[13]byte{1},
   196  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   197  		},
   198  		{
   199  			"bytes14",
   200  			[14]byte{1},
   201  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   202  		},
   203  		{
   204  			"bytes15",
   205  			[15]byte{1},
   206  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   207  		},
   208  		{
   209  			"bytes16",
   210  			[16]byte{1},
   211  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   212  		},
   213  		{
   214  			"bytes17",
   215  			[17]byte{1},
   216  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   217  		},
   218  		{
   219  			"bytes18",
   220  			[18]byte{1},
   221  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   222  		},
   223  		{
   224  			"bytes19",
   225  			[19]byte{1},
   226  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   227  		},
   228  		{
   229  			"bytes20",
   230  			[20]byte{1},
   231  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   232  		},
   233  		{
   234  			"bytes21",
   235  			[21]byte{1},
   236  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   237  		},
   238  		{
   239  			"bytes22",
   240  			[22]byte{1},
   241  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   242  		},
   243  		{
   244  			"bytes23",
   245  			[23]byte{1},
   246  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   247  		},
   248  		{
   249  			"bytes24",
   250  			[24]byte{1},
   251  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   252  		},
   253  		{
   254  			"bytes24",
   255  			[24]byte{1},
   256  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   257  		},
   258  		{
   259  			"bytes25",
   260  			[25]byte{1},
   261  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   262  		},
   263  		{
   264  			"bytes26",
   265  			[26]byte{1},
   266  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   267  		},
   268  		{
   269  			"bytes27",
   270  			[27]byte{1},
   271  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   272  		},
   273  		{
   274  			"bytes28",
   275  			[28]byte{1},
   276  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   277  		},
   278  		{
   279  			"bytes29",
   280  			[29]byte{1},
   281  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   282  		},
   283  		{
   284  			"bytes30",
   285  			[30]byte{1},
   286  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   287  		},
   288  		{
   289  			"bytes31",
   290  			[31]byte{1},
   291  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   292  		},
   293  		{
   294  			"bytes32",
   295  			[32]byte{1},
   296  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   297  		},
   298  		{
   299  			"uint32[2][3][4]",
   300  			[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}}},
   301  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000f000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000001300000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000015000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000170000000000000000000000000000000000000000000000000000000000000018"),
   302  		},
   303  		{
   304  			"address[]",
   305  			[]common.Address{{1}, {2}},
   306  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000"),
   307  		},
   308  		{
   309  			"bytes32[]",
   310  			[]common.Hash{{1}, {2}},
   311  			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000201000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000"),
   312  		},
   313  		{
   314  			"function",
   315  			[24]byte{1},
   316  			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
   317  		},
   318  		{
   319  			"string",
   320  			"foobar",
   321  			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000006666f6f6261720000000000000000000000000000000000000000000000000000"),
   322  		},
   323  	} {
   324  		typ, err := NewType(test.typ)
   325  		if err != nil {
   326  			t.Fatalf("%v failed. Unexpected parse error: %v", i, err)
   327  		}
   328  
   329  		output, err := typ.pack(reflect.ValueOf(test.input))
   330  		if err != nil {
   331  			t.Fatalf("%v failed. Unexpected pack error: %v", i, err)
   332  		}
   333  
   334  		if !bytes.Equal(output, test.output) {
   335  			t.Errorf("%d failed. Expected bytes: '%x' Got: '%x'", i, test.output, output)
   336  		}
   337  	}
   338  }
   339  
   340  func TestMethodPack(t *testing.T) {
   341  	abi, err := JSON(strings.NewReader(jsondata2))
   342  	if err != nil {
   343  		t.Fatal(err)
   344  	}
   345  
   346  	sig := abi.Methods["slice"].Id()
   347  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   348  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   349  
   350  	packed, err := abi.Pack("slice", []uint32{1, 2})
   351  	if err != nil {
   352  		t.Error(err)
   353  	}
   354  
   355  	if !bytes.Equal(packed, sig) {
   356  		t.Errorf("expected %x got %x", sig, packed)
   357  	}
   358  
   359  	var addrA, addrB = common.Address{1}, common.Address{2}
   360  	sig = abi.Methods["sliceAddress"].Id()
   361  	sig = append(sig, common.LeftPadBytes([]byte{32}, 32)...)
   362  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   363  	sig = append(sig, common.LeftPadBytes(addrA[:], 32)...)
   364  	sig = append(sig, common.LeftPadBytes(addrB[:], 32)...)
   365  
   366  	packed, err = abi.Pack("sliceAddress", []common.Address{addrA, addrB})
   367  	if err != nil {
   368  		t.Fatal(err)
   369  	}
   370  	if !bytes.Equal(packed, sig) {
   371  		t.Errorf("expected %x got %x", sig, packed)
   372  	}
   373  
   374  	var addrC, addrD = common.Address{3}, common.Address{4}
   375  	sig = abi.Methods["sliceMultiAddress"].Id()
   376  	sig = append(sig, common.LeftPadBytes([]byte{64}, 32)...)
   377  	sig = append(sig, common.LeftPadBytes([]byte{160}, 32)...)
   378  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   379  	sig = append(sig, common.LeftPadBytes(addrA[:], 32)...)
   380  	sig = append(sig, common.LeftPadBytes(addrB[:], 32)...)
   381  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   382  	sig = append(sig, common.LeftPadBytes(addrC[:], 32)...)
   383  	sig = append(sig, common.LeftPadBytes(addrD[:], 32)...)
   384  
   385  	packed, err = abi.Pack("sliceMultiAddress", []common.Address{addrA, addrB}, []common.Address{addrC, addrD})
   386  	if err != nil {
   387  		t.Fatal(err)
   388  	}
   389  	if !bytes.Equal(packed, sig) {
   390  		t.Errorf("expected %x got %x", sig, packed)
   391  	}
   392  
   393  	sig = abi.Methods["slice256"].Id()
   394  	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   395  	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   396  
   397  	packed, err = abi.Pack("slice256", []*big.Int{big.NewInt(1), big.NewInt(2)})
   398  	if err != nil {
   399  		t.Error(err)
   400  	}
   401  
   402  	if !bytes.Equal(packed, sig) {
   403  		t.Errorf("expected %x got %x", sig, packed)
   404  	}
   405  }
   406  
   407  func TestPackNumber(t *testing.T) {
   408  	tests := []struct {
   409  		value  reflect.Value
   410  		packed []byte
   411  	}{
   412  //协议限制
   413  		{reflect.ValueOf(0), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000000")},
   414  		{reflect.ValueOf(1), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")},
   415  		{reflect.ValueOf(-1), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")},
   416  
   417  //键入角大小写
   418  		{reflect.ValueOf(uint8(math.MaxUint8)), common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000ff")},
   419  		{reflect.ValueOf(uint16(math.MaxUint16)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000ffff")},
   420  		{reflect.ValueOf(uint32(math.MaxUint32)), common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000ffffffff")},
   421  		{reflect.ValueOf(uint64(math.MaxUint64)), common.Hex2Bytes("000000000000000000000000000000000000000000000000ffffffffffffffff")},
   422  
   423  		{reflect.ValueOf(int8(math.MaxInt8)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000007f")},
   424  		{reflect.ValueOf(int16(math.MaxInt16)), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000007fff")},
   425  		{reflect.ValueOf(int32(math.MaxInt32)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000007fffffff")},
   426  		{reflect.ValueOf(int64(math.MaxInt64)), common.Hex2Bytes("0000000000000000000000000000000000000000000000007fffffffffffffff")},
   427  
   428  		{reflect.ValueOf(int8(math.MinInt8)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80")},
   429  		{reflect.ValueOf(int16(math.MinInt16)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8000")},
   430  		{reflect.ValueOf(int32(math.MinInt32)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000")},
   431  		{reflect.ValueOf(int64(math.MinInt64)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffff8000000000000000")},
   432  	}
   433  	for i, tt := range tests {
   434  		packed := packNum(tt.value)
   435  		if !bytes.Equal(packed, tt.packed) {
   436  			t.Errorf("test %d: pack mismatch: have %x, want %x", i, packed, tt.packed)
   437  		}
   438  	}
   439  }
   440