github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/accounts/abi/abi_test.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package abi
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/hex"
    22  	"fmt"
    23  	"log"
    24  	"math/big"
    25  	"strings"
    26  	"testing"
    27  
    28  	"reflect"
    29  
    30  	"github.com/vntchain/go-vnt/common"
    31  	"github.com/vntchain/go-vnt/crypto"
    32  )
    33  
    34  const jsondata = `
    35  [
    36  	{ "type" : "function", "name" : "balance", "constant" : true },
    37  	{ "type" : "function", "name" : "send", "constant" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] }
    38  ]`
    39  
    40  const jsondata2 = `
    41  [
    42  	{ "type" : "function", "name" : "balance", "constant" : true },
    43  	{ "type" : "function", "name" : "send", "constant" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" }] },
    44  	{ "type" : "function", "name" : "test", "constant" : false, "inputs" : [ { "name" : "number", "type" : "uint32" } ] },
    45  	{ "type" : "function", "name" : "string", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "string" } ] },
    46  	{ "type" : "function", "name" : "bool", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "bool" } ] },
    47  	{ "type" : "function", "name" : "address", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "address" } ] },
    48  	{ "type" : "function", "name" : "uint64[2]", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[2]" } ] },
    49  	{ "type" : "function", "name" : "uint64[]", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[]" } ] },
    50  	{ "type" : "function", "name" : "foo", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" } ] },
    51  	{ "type" : "function", "name" : "bar", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" }, { "name" : "string", "type" : "uint16" } ] },
    52  	{ "type" : "function", "name" : "slice", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32[2]" } ] },
    53  	{ "type" : "function", "name" : "slice256", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "uint256[2]" } ] },
    54  	{ "type" : "function", "name" : "sliceAddress", "constant" : false, "inputs" : [ { "name" : "inputs", "type" : "address[]" } ] },
    55  	{ "type" : "function", "name" : "sliceMultiAddress", "constant" : false, "inputs" : [ { "name" : "a", "type" : "address[]" }, { "name" : "b", "type" : "address[]" } ] }
    56  ]`
    57  
    58  func TestReader(t *testing.T) {
    59  	Uint256, _ := NewType("uint256")
    60  	exp := ABI{
    61  		Methods: map[string]Method{
    62  			"balance": {
    63  				"balance", true, nil, nil,
    64  			},
    65  			"send": {
    66  				"send", false, []Argument{
    67  					{"amount", Uint256, false},
    68  				}, nil,
    69  			},
    70  		},
    71  	}
    72  
    73  	abi, err := JSON(strings.NewReader(jsondata))
    74  	if err != nil {
    75  		t.Error(err)
    76  	}
    77  
    78  	// deep equal fails for some reason
    79  	for name, expM := range exp.Methods {
    80  		gotM, exist := abi.Methods[name]
    81  		if !exist {
    82  			t.Errorf("Missing expected method %v", name)
    83  		}
    84  		if !reflect.DeepEqual(gotM, expM) {
    85  			t.Errorf("\nGot abi method: \n%v\ndoes not match expected method\n%v", gotM, expM)
    86  		}
    87  	}
    88  
    89  	for name, gotM := range abi.Methods {
    90  		expM, exist := exp.Methods[name]
    91  		if !exist {
    92  			t.Errorf("Found extra method %v", name)
    93  		}
    94  		if !reflect.DeepEqual(gotM, expM) {
    95  			t.Errorf("\nGot abi method: \n%v\ndoes not match expected method\n%v", gotM, expM)
    96  		}
    97  	}
    98  }
    99  
   100  func TestTestNumbers(t *testing.T) {
   101  	abi, err := JSON(strings.NewReader(jsondata2))
   102  	if err != nil {
   103  		t.Error(err)
   104  		t.FailNow()
   105  	}
   106  
   107  	if _, err := abi.Pack("balance"); err != nil {
   108  		t.Error(err)
   109  	}
   110  
   111  	if _, err := abi.Pack("balance", 1); err == nil {
   112  		t.Error("expected error for balance(1)")
   113  	}
   114  
   115  	if _, err := abi.Pack("doesntexist", nil); err == nil {
   116  		t.Errorf("doesntexist shouldn't exist")
   117  	}
   118  
   119  	if _, err := abi.Pack("doesntexist", 1); err == nil {
   120  		t.Errorf("doesntexist(1) shouldn't exist")
   121  	}
   122  
   123  	if _, err := abi.Pack("send", big.NewInt(1000)); err != nil {
   124  		t.Error(err)
   125  	}
   126  
   127  	i := new(int)
   128  	*i = 1000
   129  	if _, err := abi.Pack("send", i); err == nil {
   130  		t.Errorf("expected send( ptr ) to throw, requires *big.Int instead of *int")
   131  	}
   132  
   133  	if _, err := abi.Pack("test", uint32(1000)); err != nil {
   134  		t.Error(err)
   135  	}
   136  }
   137  
   138  func TestTestString(t *testing.T) {
   139  	abi, err := JSON(strings.NewReader(jsondata2))
   140  	if err != nil {
   141  		t.Error(err)
   142  		t.FailNow()
   143  	}
   144  
   145  	if _, err := abi.Pack("string", "hello world"); err != nil {
   146  		t.Error(err)
   147  	}
   148  }
   149  
   150  func TestTestBool(t *testing.T) {
   151  	abi, err := JSON(strings.NewReader(jsondata2))
   152  	if err != nil {
   153  		t.Error(err)
   154  		t.FailNow()
   155  	}
   156  
   157  	if _, err := abi.Pack("bool", true); err != nil {
   158  		t.Error(err)
   159  	}
   160  }
   161  
   162  func TestTestSlice(t *testing.T) {
   163  	abi, err := JSON(strings.NewReader(jsondata2))
   164  	if err != nil {
   165  		t.Error(err)
   166  		t.FailNow()
   167  	}
   168  
   169  	slice := make([]uint64, 2)
   170  	if _, err := abi.Pack("uint64[2]", slice); err != nil {
   171  		t.Error(err)
   172  	}
   173  
   174  	if _, err := abi.Pack("uint64[]", slice); err != nil {
   175  		t.Error(err)
   176  	}
   177  }
   178  
   179  func TestMethodSignature(t *testing.T) {
   180  	String, _ := NewType("string")
   181  	m := Method{"foo", false, []Argument{{"bar", String, false}, {"baz", String, false}}, nil}
   182  	exp := "foo(string,string)"
   183  	if m.Sig() != exp {
   184  		t.Error("signature mismatch", exp, "!=", m.Sig())
   185  	}
   186  
   187  	idexp := crypto.Keccak256([]byte(exp))[:4]
   188  	if !bytes.Equal(m.Id(), idexp) {
   189  		t.Errorf("expected ids to match %x != %x", m.Id(), idexp)
   190  	}
   191  
   192  	uintt, _ := NewType("uint256")
   193  	m = Method{"foo", false, []Argument{{"bar", uintt, false}}, nil}
   194  	exp = "foo(uint256)"
   195  	if m.Sig() != exp {
   196  		t.Error("signature mismatch", exp, "!=", m.Sig())
   197  	}
   198  }
   199  
   200  func TestMultiPack(t *testing.T) {
   201  	abi, err := JSON(strings.NewReader(jsondata2))
   202  	if err != nil {
   203  		t.Error(err)
   204  		t.FailNow()
   205  	}
   206  
   207  	sig := crypto.Keccak256([]byte("bar(uint32,uint16)"))[:4]
   208  	sig = append(sig, make([]byte, 64)...)
   209  	sig[35] = 10
   210  	sig[67] = 11
   211  
   212  	packed, err := abi.Pack("bar", uint32(10), uint16(11))
   213  	if err != nil {
   214  		t.Error(err)
   215  		t.FailNow()
   216  	}
   217  
   218  	if !bytes.Equal(packed, sig) {
   219  		t.Errorf("expected %x got %x", sig, packed)
   220  	}
   221  }
   222  
   223  func ExampleJSON() {
   224  	const definition = `[{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"isBar","outputs":[{"name":"","type":"bool"}],"type":"function"}]`
   225  
   226  	abi, err := JSON(strings.NewReader(definition))
   227  	if err != nil {
   228  		log.Fatalln(err)
   229  	}
   230  	_, err = abi.Pack("isBar", common.HexToAddress("01"))
   231  
   232  	if err != nil {
   233  		log.Fatalln(err)
   234  	}
   235  
   236  	//fmt.Printf("out: %x\n", out)
   237  	// Output:
   238  	// 1f2c40920000000000000000000000000000000000000000000000000000000000000001
   239  }
   240  
   241  func TestInputVariableInputLength(t *testing.T) {
   242  	const definition = `[
   243  	{ "type" : "function", "name" : "strOne", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" } ] },
   244  	{ "type" : "function", "name" : "bytesOne", "constant" : true, "inputs" : [ { "name" : "str", "type" : "bytes" } ] },
   245  	{ "type" : "function", "name" : "strTwo", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "str1", "type" : "string" } ] }
   246  	]`
   247  
   248  	abi, err := JSON(strings.NewReader(definition))
   249  	if err != nil {
   250  		t.Fatal(err)
   251  	}
   252  
   253  	// test one string
   254  	strin := "hello world"
   255  	strpack, err := abi.Pack("strOne", strin)
   256  	if err != nil {
   257  		t.Error(err)
   258  	}
   259  
   260  	offset := make([]byte, 32)
   261  	offset[31] = 32
   262  	length := make([]byte, 32)
   263  	length[31] = byte(len(strin))
   264  	value := common.RightPadBytes([]byte(strin), 32)
   265  	exp := append(offset, append(length, value...)...)
   266  
   267  	// ignore first 4 bytes of the output. This is the function identifier
   268  	strpack = strpack[4:]
   269  	if !bytes.Equal(strpack, exp) {
   270  		t.Errorf("expected %x, got %x\n", exp, strpack)
   271  	}
   272  
   273  	// test one bytes
   274  	btspack, err := abi.Pack("bytesOne", []byte(strin))
   275  	if err != nil {
   276  		t.Error(err)
   277  	}
   278  	// ignore first 4 bytes of the output. This is the function identifier
   279  	btspack = btspack[4:]
   280  	if !bytes.Equal(btspack, exp) {
   281  		t.Errorf("expected %x, got %x\n", exp, btspack)
   282  	}
   283  
   284  	//  test two strings
   285  	str1 := "hello"
   286  	str2 := "world"
   287  	str2pack, err := abi.Pack("strTwo", str1, str2)
   288  	if err != nil {
   289  		t.Error(err)
   290  	}
   291  
   292  	offset1 := make([]byte, 32)
   293  	offset1[31] = 64
   294  	length1 := make([]byte, 32)
   295  	length1[31] = byte(len(str1))
   296  	value1 := common.RightPadBytes([]byte(str1), 32)
   297  
   298  	offset2 := make([]byte, 32)
   299  	offset2[31] = 128
   300  	length2 := make([]byte, 32)
   301  	length2[31] = byte(len(str2))
   302  	value2 := common.RightPadBytes([]byte(str2), 32)
   303  
   304  	exp2 := append(offset1, offset2...)
   305  	exp2 = append(exp2, append(length1, value1...)...)
   306  	exp2 = append(exp2, append(length2, value2...)...)
   307  
   308  	// ignore first 4 bytes of the output. This is the function identifier
   309  	str2pack = str2pack[4:]
   310  	if !bytes.Equal(str2pack, exp2) {
   311  		t.Errorf("expected %x, got %x\n", exp, str2pack)
   312  	}
   313  
   314  	// test two strings, first > 32, second < 32
   315  	str1 = strings.Repeat("a", 33)
   316  	str2pack, err = abi.Pack("strTwo", str1, str2)
   317  	if err != nil {
   318  		t.Error(err)
   319  	}
   320  
   321  	offset1 = make([]byte, 32)
   322  	offset1[31] = 64
   323  	length1 = make([]byte, 32)
   324  	length1[31] = byte(len(str1))
   325  	value1 = common.RightPadBytes([]byte(str1), 64)
   326  	offset2[31] = 160
   327  
   328  	exp2 = append(offset1, offset2...)
   329  	exp2 = append(exp2, append(length1, value1...)...)
   330  	exp2 = append(exp2, append(length2, value2...)...)
   331  
   332  	// ignore first 4 bytes of the output. This is the function identifier
   333  	str2pack = str2pack[4:]
   334  	if !bytes.Equal(str2pack, exp2) {
   335  		t.Errorf("expected %x, got %x\n", exp, str2pack)
   336  	}
   337  
   338  	// test two strings, first > 32, second >32
   339  	str1 = strings.Repeat("a", 33)
   340  	str2 = strings.Repeat("a", 33)
   341  	str2pack, err = abi.Pack("strTwo", str1, str2)
   342  	if err != nil {
   343  		t.Error(err)
   344  	}
   345  
   346  	offset1 = make([]byte, 32)
   347  	offset1[31] = 64
   348  	length1 = make([]byte, 32)
   349  	length1[31] = byte(len(str1))
   350  	value1 = common.RightPadBytes([]byte(str1), 64)
   351  
   352  	offset2 = make([]byte, 32)
   353  	offset2[31] = 160
   354  	length2 = make([]byte, 32)
   355  	length2[31] = byte(len(str2))
   356  	value2 = common.RightPadBytes([]byte(str2), 64)
   357  
   358  	exp2 = append(offset1, offset2...)
   359  	exp2 = append(exp2, append(length1, value1...)...)
   360  	exp2 = append(exp2, append(length2, value2...)...)
   361  
   362  	// ignore first 4 bytes of the output. This is the function identifier
   363  	str2pack = str2pack[4:]
   364  	if !bytes.Equal(str2pack, exp2) {
   365  		t.Errorf("expected %x, got %x\n", exp, str2pack)
   366  	}
   367  }
   368  
   369  func TestInputFixedArrayAndVariableInputLength(t *testing.T) {
   370  	const definition = `[
   371  	{ "type" : "function", "name" : "fixedArrStr", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "fixedArr", "type" : "uint256[2]" } ] },
   372  	{ "type" : "function", "name" : "fixedArrBytes", "constant" : true, "inputs" : [ { "name" : "str", "type" : "bytes" }, { "name" : "fixedArr", "type" : "uint256[2]" } ] },
   373      { "type" : "function", "name" : "mixedArrStr", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "fixedArr", "type": "uint256[2]" }, { "name" : "dynArr", "type": "uint256[]" } ] },
   374      { "type" : "function", "name" : "doubleFixedArrStr", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "fixedArr1", "type": "uint256[2]" }, { "name" : "fixedArr2", "type": "uint256[3]" } ] },
   375      { "type" : "function", "name" : "multipleMixedArrStr", "constant" : true, "inputs" : [ { "name" : "str", "type" : "string" }, { "name" : "fixedArr1", "type": "uint256[2]" }, { "name" : "dynArr", "type" : "uint256[]" }, { "name" : "fixedArr2", "type" : "uint256[3]" } ] }
   376  	]`
   377  
   378  	abi, err := JSON(strings.NewReader(definition))
   379  	if err != nil {
   380  		t.Error(err)
   381  	}
   382  
   383  	// test string, fixed array uint256[2]
   384  	strin := "hello world"
   385  	arrin := [2]*big.Int{big.NewInt(1), big.NewInt(2)}
   386  	fixedArrStrPack, err := abi.Pack("fixedArrStr", strin, arrin)
   387  	if err != nil {
   388  		t.Error(err)
   389  	}
   390  
   391  	// generate expected output
   392  	offset := make([]byte, 32)
   393  	offset[31] = 96
   394  	length := make([]byte, 32)
   395  	length[31] = byte(len(strin))
   396  	strvalue := common.RightPadBytes([]byte(strin), 32)
   397  	arrinvalue1 := common.LeftPadBytes(arrin[0].Bytes(), 32)
   398  	arrinvalue2 := common.LeftPadBytes(arrin[1].Bytes(), 32)
   399  	exp := append(offset, arrinvalue1...)
   400  	exp = append(exp, arrinvalue2...)
   401  	exp = append(exp, append(length, strvalue...)...)
   402  
   403  	// ignore first 4 bytes of the output. This is the function identifier
   404  	fixedArrStrPack = fixedArrStrPack[4:]
   405  	if !bytes.Equal(fixedArrStrPack, exp) {
   406  		t.Errorf("expected %x, got %x\n", exp, fixedArrStrPack)
   407  	}
   408  
   409  	// test byte array, fixed array uint256[2]
   410  	bytesin := []byte(strin)
   411  	arrin = [2]*big.Int{big.NewInt(1), big.NewInt(2)}
   412  	fixedArrBytesPack, err := abi.Pack("fixedArrBytes", bytesin, arrin)
   413  	if err != nil {
   414  		t.Error(err)
   415  	}
   416  
   417  	// generate expected output
   418  	offset = make([]byte, 32)
   419  	offset[31] = 96
   420  	length = make([]byte, 32)
   421  	length[31] = byte(len(strin))
   422  	strvalue = common.RightPadBytes([]byte(strin), 32)
   423  	arrinvalue1 = common.LeftPadBytes(arrin[0].Bytes(), 32)
   424  	arrinvalue2 = common.LeftPadBytes(arrin[1].Bytes(), 32)
   425  	exp = append(offset, arrinvalue1...)
   426  	exp = append(exp, arrinvalue2...)
   427  	exp = append(exp, append(length, strvalue...)...)
   428  
   429  	// ignore first 4 bytes of the output. This is the function identifier
   430  	fixedArrBytesPack = fixedArrBytesPack[4:]
   431  	if !bytes.Equal(fixedArrBytesPack, exp) {
   432  		t.Errorf("expected %x, got %x\n", exp, fixedArrBytesPack)
   433  	}
   434  
   435  	// test string, fixed array uint256[2], dynamic array uint256[]
   436  	strin = "hello world"
   437  	fixedarrin := [2]*big.Int{big.NewInt(1), big.NewInt(2)}
   438  	dynarrin := []*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)}
   439  	mixedArrStrPack, err := abi.Pack("mixedArrStr", strin, fixedarrin, dynarrin)
   440  	if err != nil {
   441  		t.Error(err)
   442  	}
   443  
   444  	// generate expected output
   445  	stroffset := make([]byte, 32)
   446  	stroffset[31] = 128
   447  	strlength := make([]byte, 32)
   448  	strlength[31] = byte(len(strin))
   449  	strvalue = common.RightPadBytes([]byte(strin), 32)
   450  	fixedarrinvalue1 := common.LeftPadBytes(fixedarrin[0].Bytes(), 32)
   451  	fixedarrinvalue2 := common.LeftPadBytes(fixedarrin[1].Bytes(), 32)
   452  	dynarroffset := make([]byte, 32)
   453  	dynarroffset[31] = byte(160 + ((len(strin)/32)+1)*32)
   454  	dynarrlength := make([]byte, 32)
   455  	dynarrlength[31] = byte(len(dynarrin))
   456  	dynarrinvalue1 := common.LeftPadBytes(dynarrin[0].Bytes(), 32)
   457  	dynarrinvalue2 := common.LeftPadBytes(dynarrin[1].Bytes(), 32)
   458  	dynarrinvalue3 := common.LeftPadBytes(dynarrin[2].Bytes(), 32)
   459  	exp = append(stroffset, fixedarrinvalue1...)
   460  	exp = append(exp, fixedarrinvalue2...)
   461  	exp = append(exp, dynarroffset...)
   462  	exp = append(exp, append(strlength, strvalue...)...)
   463  	dynarrarg := append(dynarrlength, dynarrinvalue1...)
   464  	dynarrarg = append(dynarrarg, dynarrinvalue2...)
   465  	dynarrarg = append(dynarrarg, dynarrinvalue3...)
   466  	exp = append(exp, dynarrarg...)
   467  
   468  	// ignore first 4 bytes of the output. This is the function identifier
   469  	mixedArrStrPack = mixedArrStrPack[4:]
   470  	if !bytes.Equal(mixedArrStrPack, exp) {
   471  		t.Errorf("expected %x, got %x\n", exp, mixedArrStrPack)
   472  	}
   473  
   474  	// test string, fixed array uint256[2], fixed array uint256[3]
   475  	strin = "hello world"
   476  	fixedarrin1 := [2]*big.Int{big.NewInt(1), big.NewInt(2)}
   477  	fixedarrin2 := [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)}
   478  	doubleFixedArrStrPack, err := abi.Pack("doubleFixedArrStr", strin, fixedarrin1, fixedarrin2)
   479  	if err != nil {
   480  		t.Error(err)
   481  	}
   482  
   483  	// generate expected output
   484  	stroffset = make([]byte, 32)
   485  	stroffset[31] = 192
   486  	strlength = make([]byte, 32)
   487  	strlength[31] = byte(len(strin))
   488  	strvalue = common.RightPadBytes([]byte(strin), 32)
   489  	fixedarrin1value1 := common.LeftPadBytes(fixedarrin1[0].Bytes(), 32)
   490  	fixedarrin1value2 := common.LeftPadBytes(fixedarrin1[1].Bytes(), 32)
   491  	fixedarrin2value1 := common.LeftPadBytes(fixedarrin2[0].Bytes(), 32)
   492  	fixedarrin2value2 := common.LeftPadBytes(fixedarrin2[1].Bytes(), 32)
   493  	fixedarrin2value3 := common.LeftPadBytes(fixedarrin2[2].Bytes(), 32)
   494  	exp = append(stroffset, fixedarrin1value1...)
   495  	exp = append(exp, fixedarrin1value2...)
   496  	exp = append(exp, fixedarrin2value1...)
   497  	exp = append(exp, fixedarrin2value2...)
   498  	exp = append(exp, fixedarrin2value3...)
   499  	exp = append(exp, append(strlength, strvalue...)...)
   500  
   501  	// ignore first 4 bytes of the output. This is the function identifier
   502  	doubleFixedArrStrPack = doubleFixedArrStrPack[4:]
   503  	if !bytes.Equal(doubleFixedArrStrPack, exp) {
   504  		t.Errorf("expected %x, got %x\n", exp, doubleFixedArrStrPack)
   505  	}
   506  
   507  	// test string, fixed array uint256[2], dynamic array uint256[], fixed array uint256[3]
   508  	strin = "hello world"
   509  	fixedarrin1 = [2]*big.Int{big.NewInt(1), big.NewInt(2)}
   510  	dynarrin = []*big.Int{big.NewInt(1), big.NewInt(2)}
   511  	fixedarrin2 = [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)}
   512  	multipleMixedArrStrPack, err := abi.Pack("multipleMixedArrStr", strin, fixedarrin1, dynarrin, fixedarrin2)
   513  	if err != nil {
   514  		t.Error(err)
   515  	}
   516  
   517  	// generate expected output
   518  	stroffset = make([]byte, 32)
   519  	stroffset[31] = 224
   520  	strlength = make([]byte, 32)
   521  	strlength[31] = byte(len(strin))
   522  	strvalue = common.RightPadBytes([]byte(strin), 32)
   523  	fixedarrin1value1 = common.LeftPadBytes(fixedarrin1[0].Bytes(), 32)
   524  	fixedarrin1value2 = common.LeftPadBytes(fixedarrin1[1].Bytes(), 32)
   525  	dynarroffset = U256(big.NewInt(int64(256 + ((len(strin)/32)+1)*32)))
   526  	dynarrlength = make([]byte, 32)
   527  	dynarrlength[31] = byte(len(dynarrin))
   528  	dynarrinvalue1 = common.LeftPadBytes(dynarrin[0].Bytes(), 32)
   529  	dynarrinvalue2 = common.LeftPadBytes(dynarrin[1].Bytes(), 32)
   530  	fixedarrin2value1 = common.LeftPadBytes(fixedarrin2[0].Bytes(), 32)
   531  	fixedarrin2value2 = common.LeftPadBytes(fixedarrin2[1].Bytes(), 32)
   532  	fixedarrin2value3 = common.LeftPadBytes(fixedarrin2[2].Bytes(), 32)
   533  	exp = append(stroffset, fixedarrin1value1...)
   534  	exp = append(exp, fixedarrin1value2...)
   535  	exp = append(exp, dynarroffset...)
   536  	exp = append(exp, fixedarrin2value1...)
   537  	exp = append(exp, fixedarrin2value2...)
   538  	exp = append(exp, fixedarrin2value3...)
   539  	exp = append(exp, append(strlength, strvalue...)...)
   540  	dynarrarg = append(dynarrlength, dynarrinvalue1...)
   541  	dynarrarg = append(dynarrarg, dynarrinvalue2...)
   542  	exp = append(exp, dynarrarg...)
   543  
   544  	// ignore first 4 bytes of the output. This is the function identifier
   545  	multipleMixedArrStrPack = multipleMixedArrStrPack[4:]
   546  	if !bytes.Equal(multipleMixedArrStrPack, exp) {
   547  		t.Errorf("expected %x, got %x\n", exp, multipleMixedArrStrPack)
   548  	}
   549  }
   550  
   551  func TestDefaultFunctionParsing(t *testing.T) {
   552  	const definition = `[{ "name" : "balance" }]`
   553  
   554  	abi, err := JSON(strings.NewReader(definition))
   555  	if err != nil {
   556  		t.Fatal(err)
   557  	}
   558  
   559  	if _, ok := abi.Methods["balance"]; !ok {
   560  		t.Error("expected 'balance' to be present")
   561  	}
   562  }
   563  
   564  func TestBareEvents(t *testing.T) {
   565  	const definition = `[
   566  	{ "type" : "event", "name" : "balance" },
   567  	{ "type" : "event", "name" : "anon", "anonymous" : true},
   568  	{ "type" : "event", "name" : "args", "inputs" : [{ "indexed":false, "name":"arg0", "type":"uint256" }, { "indexed":true, "name":"arg1", "type":"address" }] }
   569  	]`
   570  
   571  	arg0, _ := NewType("uint256")
   572  	arg1, _ := NewType("address")
   573  
   574  	expectedEvents := map[string]struct {
   575  		Anonymous bool
   576  		Args      []Argument
   577  	}{
   578  		"balance": {false, nil},
   579  		"anon":    {true, nil},
   580  		"args": {false, []Argument{
   581  			{Name: "arg0", Type: arg0, Indexed: false},
   582  			{Name: "arg1", Type: arg1, Indexed: true},
   583  		}},
   584  	}
   585  
   586  	abi, err := JSON(strings.NewReader(definition))
   587  	if err != nil {
   588  		t.Fatal(err)
   589  	}
   590  
   591  	if len(abi.Events) != len(expectedEvents) {
   592  		t.Fatalf("invalid number of events after parsing, want %d, got %d", len(expectedEvents), len(abi.Events))
   593  	}
   594  
   595  	for name, exp := range expectedEvents {
   596  		got, ok := abi.Events[name]
   597  		if !ok {
   598  			t.Errorf("could not found event %s", name)
   599  			continue
   600  		}
   601  		if got.Anonymous != exp.Anonymous {
   602  			t.Errorf("invalid anonymous indication for event %s, want %v, got %v", name, exp.Anonymous, got.Anonymous)
   603  		}
   604  		if len(got.Inputs) != len(exp.Args) {
   605  			t.Errorf("invalid number of args, want %d, got %d", len(exp.Args), len(got.Inputs))
   606  			continue
   607  		}
   608  		for i, arg := range exp.Args {
   609  			if arg.Name != got.Inputs[i].Name {
   610  				t.Errorf("events[%s].Input[%d] has an invalid name, want %s, got %s", name, i, arg.Name, got.Inputs[i].Name)
   611  			}
   612  			if arg.Indexed != got.Inputs[i].Indexed {
   613  				t.Errorf("events[%s].Input[%d] has an invalid indexed indication, want %v, got %v", name, i, arg.Indexed, got.Inputs[i].Indexed)
   614  			}
   615  			if arg.Type.T != got.Inputs[i].Type.T {
   616  				t.Errorf("events[%s].Input[%d] has an invalid type, want %x, got %x", name, i, arg.Type.T, got.Inputs[i].Type.T)
   617  			}
   618  		}
   619  	}
   620  }
   621  
   622  // TestUnpackEvent is based on this contract:
   623  //    contract T {
   624  //      event received(address sender, uint amount, bytes memo);
   625  //      event receivedAddr(address sender);
   626  //      function receive(bytes memo) external payable {
   627  //        received(msg.sender, msg.value, memo);
   628  //        receivedAddr(msg.sender);
   629  //      }
   630  //    }
   631  // When receive("X") is called with sender 0x00... and value 1, it produces this tx receipt:
   632  //   receipt{status=1 cgas=23949 bloom=00000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000040200000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 logs=[log: b6818c8064f645cd82d99b59a1a267d6d61117ef [75fd880d39c1daf53b6547ab6cb59451fc6452d27caa90e5b6649dd8293b9eed] 000000000000000000000000376c47978271565f56deb45495afa69e59c16ab200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000158 9ae378b6d4409eada347a5dc0c180f186cb62dc68fcc0f043425eb917335aa28 0 95d429d309bb9d753954195fe2d69bd140b4ae731b9b5b605c34323de162cf00 0]}
   633  func TestUnpackEvent(t *testing.T) {
   634  	const abiJSON = `[{"constant":false,"inputs":[{"name":"memo","type":"bytes"}],"name":"receive","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"memo","type":"bytes"}],"name":"received","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"}],"name":"receivedAddr","type":"event"}]`
   635  	abi, err := JSON(strings.NewReader(abiJSON))
   636  	if err != nil {
   637  		t.Fatal(err)
   638  	}
   639  
   640  	const hexdata = `000000000000000000000000376c47978271565f56deb45495afa69e59c16ab200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000158`
   641  	data, err := hex.DecodeString(hexdata)
   642  	if err != nil {
   643  		t.Fatal(err)
   644  	}
   645  	if len(data)%32 == 0 {
   646  		t.Errorf("len(data) is %d, want a non-multiple of 32", len(data))
   647  	}
   648  
   649  	type ReceivedEvent struct {
   650  		Address common.Address
   651  		Amount  *big.Int
   652  		Memo    []byte
   653  	}
   654  	var ev ReceivedEvent
   655  
   656  	err = abi.Unpack(&ev, "received", data)
   657  	if err != nil {
   658  		t.Error(err)
   659  	} else {
   660  		t.Logf("len(data): %d; received event: %+v", len(data), ev)
   661  	}
   662  
   663  	type ReceivedAddrEvent struct {
   664  		Address common.Address
   665  	}
   666  	var receivedAddrEv ReceivedAddrEvent
   667  	err = abi.Unpack(&receivedAddrEv, "receivedAddr", data)
   668  	if err != nil {
   669  		t.Error(err)
   670  	} else {
   671  		t.Logf("len(data): %d; received event: %+v", len(data), receivedAddrEv)
   672  	}
   673  }
   674  
   675  func TestABI_MethodById(t *testing.T) {
   676  	const abiJSON = `[
   677  		{"type":"function","name":"receive","constant":false,"inputs":[{"name":"memo","type":"bytes"}],"outputs":[],"payable":true,"stateMutability":"payable"},
   678  		{"type":"event","name":"received","anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"memo","type":"bytes"}]},
   679  		{"type":"function","name":"fixedArrStr","constant":true,"inputs":[{"name":"str","type":"string"},{"name":"fixedArr","type":"uint256[2]"}]},
   680  		{"type":"function","name":"fixedArrBytes","constant":true,"inputs":[{"name":"str","type":"bytes"},{"name":"fixedArr","type":"uint256[2]"}]},
   681  		{"type":"function","name":"mixedArrStr","constant":true,"inputs":[{"name":"str","type":"string"},{"name":"fixedArr","type":"uint256[2]"},{"name":"dynArr","type":"uint256[]"}]},
   682  		{"type":"function","name":"doubleFixedArrStr","constant":true,"inputs":[{"name":"str","type":"string"},{"name":"fixedArr1","type":"uint256[2]"},{"name":"fixedArr2","type":"uint256[3]"}]},
   683  		{"type":"function","name":"multipleMixedArrStr","constant":true,"inputs":[{"name":"str","type":"string"},{"name":"fixedArr1","type":"uint256[2]"},{"name":"dynArr","type":"uint256[]"},{"name":"fixedArr2","type":"uint256[3]"}]},
   684  		{"type":"function","name":"balance","constant":true},
   685  		{"type":"function","name":"send","constant":false,"inputs":[{"name":"amount","type":"uint256"}]},
   686  		{"type":"function","name":"test","constant":false,"inputs":[{"name":"number","type":"uint32"}]},
   687  		{"type":"function","name":"string","constant":false,"inputs":[{"name":"inputs","type":"string"}]},
   688  		{"type":"function","name":"bool","constant":false,"inputs":[{"name":"inputs","type":"bool"}]},
   689  		{"type":"function","name":"address","constant":false,"inputs":[{"name":"inputs","type":"address"}]},
   690  		{"type":"function","name":"uint64[2]","constant":false,"inputs":[{"name":"inputs","type":"uint64[2]"}]},
   691  		{"type":"function","name":"uint64[]","constant":false,"inputs":[{"name":"inputs","type":"uint64[]"}]},
   692  		{"type":"function","name":"foo","constant":false,"inputs":[{"name":"inputs","type":"uint32"}]},
   693  		{"type":"function","name":"bar","constant":false,"inputs":[{"name":"inputs","type":"uint32"},{"name":"string","type":"uint16"}]},
   694  		{"type":"function","name":"_slice","constant":false,"inputs":[{"name":"inputs","type":"uint32[2]"}]},
   695  		{"type":"function","name":"__slice256","constant":false,"inputs":[{"name":"inputs","type":"uint256[2]"}]},
   696  		{"type":"function","name":"sliceAddress","constant":false,"inputs":[{"name":"inputs","type":"address[]"}]},
   697  		{"type":"function","name":"sliceMultiAddress","constant":false,"inputs":[{"name":"a","type":"address[]"},{"name":"b","type":"address[]"}]}
   698  	]
   699  `
   700  	abi, err := JSON(strings.NewReader(abiJSON))
   701  	if err != nil {
   702  		t.Fatal(err)
   703  	}
   704  	for name, m := range abi.Methods {
   705  		a := fmt.Sprintf("%v", m)
   706  		m2, err := abi.MethodById(m.Id())
   707  		if err != nil {
   708  			t.Fatalf("Failed to look up ABI method: %v", err)
   709  		}
   710  		b := fmt.Sprintf("%v", m2)
   711  		if a != b {
   712  			t.Errorf("Method %v (id %v) not 'findable' by id in ABI", name, common.ToHex(m.Id()))
   713  		}
   714  	}
   715  
   716  }
   717  
   718  func TestABI_UnpackInput(t *testing.T) {
   719  	const abiJSON = `[{"constant":false,"inputs":[{"name":"memo","type":"bytes"}],"name":"receive","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"memo","type":"bytes"}],"name":"received","type":"function"}]`
   720  	abi, err := JSON(strings.NewReader(abiJSON))
   721  	if err != nil {
   722  		t.Fatal(err)
   723  	}
   724  
   725  	type Received struct {
   726  		Sender common.Address
   727  		Amount *big.Int
   728  		Memo   []byte
   729  	}
   730  
   731  	ev := Received{
   732  		Sender: common.BytesToAddress([]byte{10}),
   733  		Amount: big.NewInt(20),
   734  		Memo:   []byte("abc"),
   735  	}
   736  
   737  	data, err := abi.Pack("received", ev.Sender, ev.Amount, ev.Memo)
   738  	if err != nil {
   739  		t.Error(err)
   740  	}
   741  
   742  	var unpackEv Received
   743  	err = abi.UnpackInput(&unpackEv, "received", data[4:])
   744  	if err != nil {
   745  		t.Error(err)
   746  	} else {
   747  		t.Logf("len(data): %d; received : %+v", len(data), unpackEv)
   748  	}
   749  	if !bytes.Equal(unpackEv.Sender[:], ev.Sender[:]) {
   750  		t.Errorf("the sender before and after pack is different. Before: 0x%x, after: 0x%x", ev.Sender, unpackEv.Sender)
   751  	}
   752  	if !bytes.Equal(unpackEv.Memo, ev.Memo) {
   753  		t.Errorf("the memo before and after pack is different. Before: 0x%x, after: 0x%x", ev.Memo, unpackEv.Memo)
   754  	}
   755  	if !bytes.Equal(unpackEv.Amount.Bytes(), ev.Amount.Bytes()) {
   756  		t.Errorf("the amount before and after pack is different.Before: %d, after: %d", ev.Amount, unpackEv.Amount)
   757  	}
   758  }