github.com/Cleverse/go-ethereum@v0.0.0-20220927095127-45113064e7f2/accounts/abi/unpack_test.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package abi
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/hex"
    22  	"fmt"
    23  	"math/big"
    24  	"reflect"
    25  	"strconv"
    26  	"strings"
    27  	"testing"
    28  
    29  	"github.com/ethereum/go-ethereum/common"
    30  	"github.com/stretchr/testify/require"
    31  )
    32  
    33  // TestUnpack tests the general pack/unpack tests in packing_test.go
    34  func TestUnpack(t *testing.T) {
    35  	for i, test := range packUnpackTests {
    36  		t.Run(strconv.Itoa(i)+" "+test.def, func(t *testing.T) {
    37  			//Unpack
    38  			def := fmt.Sprintf(`[{ "name" : "method", "type": "function", "outputs": %s}]`, test.def)
    39  			abi, err := JSON(strings.NewReader(def))
    40  			if err != nil {
    41  				t.Fatalf("invalid ABI definition %s: %v", def, err)
    42  			}
    43  			encb, err := hex.DecodeString(test.packed)
    44  			if err != nil {
    45  				t.Fatalf("invalid hex %s: %v", test.packed, err)
    46  			}
    47  			out, err := abi.Unpack("method", encb)
    48  			if err != nil {
    49  				t.Errorf("test %d (%v) failed: %v", i, test.def, err)
    50  				return
    51  			}
    52  			if !reflect.DeepEqual(test.unpacked, ConvertType(out[0], test.unpacked)) {
    53  				t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, test.unpacked, out[0])
    54  			}
    55  		})
    56  	}
    57  }
    58  
    59  type unpackTest struct {
    60  	def  string      // ABI definition JSON
    61  	enc  string      // evm return data
    62  	want interface{} // the expected output
    63  	err  string      // empty or error if expected
    64  }
    65  
    66  func (test unpackTest) checkError(err error) error {
    67  	if err != nil {
    68  		if len(test.err) == 0 {
    69  			return fmt.Errorf("expected no err but got: %v", err)
    70  		} else if err.Error() != test.err {
    71  			return fmt.Errorf("expected err: '%v' got err: %q", test.err, err)
    72  		}
    73  	} else if len(test.err) > 0 {
    74  		return fmt.Errorf("expected err: %v but got none", test.err)
    75  	}
    76  	return nil
    77  }
    78  
    79  var unpackTests = []unpackTest{
    80  	// Bools
    81  	{
    82  		def:  `[{ "type": "bool" }]`,
    83  		enc:  "0000000000000000000000000000000000000000000000000001000000000001",
    84  		want: false,
    85  		err:  "abi: improperly encoded boolean value",
    86  	},
    87  	{
    88  		def:  `[{ "type": "bool" }]`,
    89  		enc:  "0000000000000000000000000000000000000000000000000000000000000003",
    90  		want: false,
    91  		err:  "abi: improperly encoded boolean value",
    92  	},
    93  	// Integers
    94  	{
    95  		def:  `[{"type": "uint32"}]`,
    96  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
    97  		want: uint16(0),
    98  		err:  "abi: cannot unmarshal uint32 in to uint16",
    99  	},
   100  	{
   101  		def:  `[{"type": "uint17"}]`,
   102  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
   103  		want: uint16(0),
   104  		err:  "abi: cannot unmarshal *big.Int in to uint16",
   105  	},
   106  	{
   107  		def:  `[{"type": "int32"}]`,
   108  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
   109  		want: int16(0),
   110  		err:  "abi: cannot unmarshal int32 in to int16",
   111  	},
   112  	{
   113  		def:  `[{"type": "int17"}]`,
   114  		enc:  "0000000000000000000000000000000000000000000000000000000000000001",
   115  		want: int16(0),
   116  		err:  "abi: cannot unmarshal *big.Int in to int16",
   117  	},
   118  	{
   119  		def:  `[{"type": "bytes"}]`,
   120  		enc:  "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
   121  		want: [32]byte{1},
   122  	},
   123  	{
   124  		def:  `[{"type": "bytes32"}]`,
   125  		enc:  "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
   126  		want: []byte(nil),
   127  		err:  "abi: cannot unmarshal [32]uint8 in to []uint8",
   128  	},
   129  	{
   130  		def: `[{"name":"___","type":"int256"}]`,
   131  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   132  		want: struct {
   133  			IntOne *big.Int
   134  			Intone *big.Int
   135  		}{IntOne: big.NewInt(1)},
   136  	},
   137  	{
   138  		def: `[{"name":"int_one","type":"int256"},{"name":"IntOne","type":"int256"}]`,
   139  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   140  		want: struct {
   141  			Int1 *big.Int
   142  			Int2 *big.Int
   143  		}{},
   144  		err: "abi: multiple outputs mapping to the same struct field 'IntOne'",
   145  	},
   146  	{
   147  		def: `[{"name":"int","type":"int256"},{"name":"Int","type":"int256"}]`,
   148  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   149  		want: struct {
   150  			Int1 *big.Int
   151  			Int2 *big.Int
   152  		}{},
   153  		err: "abi: multiple outputs mapping to the same struct field 'Int'",
   154  	},
   155  	{
   156  		def: `[{"name":"int","type":"int256"},{"name":"_int","type":"int256"}]`,
   157  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   158  		want: struct {
   159  			Int1 *big.Int
   160  			Int2 *big.Int
   161  		}{},
   162  		err: "abi: multiple outputs mapping to the same struct field 'Int'",
   163  	},
   164  	{
   165  		def: `[{"name":"Int","type":"int256"},{"name":"_int","type":"int256"}]`,
   166  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   167  		want: struct {
   168  			Int1 *big.Int
   169  			Int2 *big.Int
   170  		}{},
   171  		err: "abi: multiple outputs mapping to the same struct field 'Int'",
   172  	},
   173  	{
   174  		def: `[{"name":"Int","type":"int256"},{"name":"_","type":"int256"}]`,
   175  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   176  		want: struct {
   177  			Int1 *big.Int
   178  			Int2 *big.Int
   179  		}{},
   180  		err: "abi: purely underscored output cannot unpack to struct",
   181  	},
   182  	// Make sure only the first argument is consumed
   183  	{
   184  		def: `[{"name":"int_one","type":"int256"}]`,
   185  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   186  		want: struct {
   187  			IntOne *big.Int
   188  		}{big.NewInt(1)},
   189  	},
   190  	{
   191  		def: `[{"name":"int__one","type":"int256"}]`,
   192  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   193  		want: struct {
   194  			IntOne *big.Int
   195  		}{big.NewInt(1)},
   196  	},
   197  	{
   198  		def: `[{"name":"int_one_","type":"int256"}]`,
   199  		enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
   200  		want: struct {
   201  			IntOne *big.Int
   202  		}{big.NewInt(1)},
   203  	},
   204  	{
   205  		def:  `[{"type":"bool"}]`,
   206  		enc:  "",
   207  		want: false,
   208  		err:  "abi: attempting to unmarshall an empty string while arguments are expected",
   209  	},
   210  	{
   211  		def:  `[{"type":"bytes32","indexed":true},{"type":"uint256","indexed":false}]`,
   212  		enc:  "",
   213  		want: false,
   214  		err:  "abi: attempting to unmarshall an empty string while arguments are expected",
   215  	},
   216  	{
   217  		def:  `[{"type":"bool","indexed":true},{"type":"uint64","indexed":true}]`,
   218  		enc:  "",
   219  		want: false,
   220  	},
   221  }
   222  
   223  // TestLocalUnpackTests runs test specially designed only for unpacking.
   224  // All test cases that can be used to test packing and unpacking should move to packing_test.go
   225  func TestLocalUnpackTests(t *testing.T) {
   226  	for i, test := range unpackTests {
   227  		t.Run(strconv.Itoa(i), func(t *testing.T) {
   228  			//Unpack
   229  			def := fmt.Sprintf(`[{ "name" : "method", "type": "function", "outputs": %s}]`, test.def)
   230  			abi, err := JSON(strings.NewReader(def))
   231  			if err != nil {
   232  				t.Fatalf("invalid ABI definition %s: %v", def, err)
   233  			}
   234  			encb, err := hex.DecodeString(test.enc)
   235  			if err != nil {
   236  				t.Fatalf("invalid hex %s: %v", test.enc, err)
   237  			}
   238  			outptr := reflect.New(reflect.TypeOf(test.want))
   239  			err = abi.UnpackIntoInterface(outptr.Interface(), "method", encb)
   240  			if err := test.checkError(err); err != nil {
   241  				t.Errorf("test %d (%v) failed: %v", i, test.def, err)
   242  				return
   243  			}
   244  			out := outptr.Elem().Interface()
   245  			if !reflect.DeepEqual(test.want, out) {
   246  				t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, test.want, out)
   247  			}
   248  		})
   249  	}
   250  }
   251  
   252  func TestUnpackIntoInterfaceSetDynamicArrayOutput(t *testing.T) {
   253  	abi, err := JSON(strings.NewReader(`[{"constant":true,"inputs":[],"name":"testDynamicFixedBytes15","outputs":[{"name":"","type":"bytes15[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"testDynamicFixedBytes32","outputs":[{"name":"","type":"bytes32[]"}],"payable":false,"stateMutability":"view","type":"function"}]`))
   254  	if err != nil {
   255  		t.Fatal(err)
   256  	}
   257  
   258  	var (
   259  		marshalledReturn32 = common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000230783132333435363738393000000000000000000000000000000000000000003078303938373635343332310000000000000000000000000000000000000000")
   260  		marshalledReturn15 = common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000230783031323334350000000000000000000000000000000000000000000000003078393837363534000000000000000000000000000000000000000000000000")
   261  
   262  		out32 [][32]byte
   263  		out15 [][15]byte
   264  	)
   265  
   266  	// test 32
   267  	err = abi.UnpackIntoInterface(&out32, "testDynamicFixedBytes32", marshalledReturn32)
   268  	if err != nil {
   269  		t.Fatal(err)
   270  	}
   271  	if len(out32) != 2 {
   272  		t.Fatalf("expected array with 2 values, got %d", len(out32))
   273  	}
   274  	expected := common.Hex2Bytes("3078313233343536373839300000000000000000000000000000000000000000")
   275  	if !bytes.Equal(out32[0][:], expected) {
   276  		t.Errorf("expected %x, got %x\n", expected, out32[0])
   277  	}
   278  	expected = common.Hex2Bytes("3078303938373635343332310000000000000000000000000000000000000000")
   279  	if !bytes.Equal(out32[1][:], expected) {
   280  		t.Errorf("expected %x, got %x\n", expected, out32[1])
   281  	}
   282  
   283  	// test 15
   284  	err = abi.UnpackIntoInterface(&out15, "testDynamicFixedBytes32", marshalledReturn15)
   285  	if err != nil {
   286  		t.Fatal(err)
   287  	}
   288  	if len(out15) != 2 {
   289  		t.Fatalf("expected array with 2 values, got %d", len(out15))
   290  	}
   291  	expected = common.Hex2Bytes("307830313233343500000000000000")
   292  	if !bytes.Equal(out15[0][:], expected) {
   293  		t.Errorf("expected %x, got %x\n", expected, out15[0])
   294  	}
   295  	expected = common.Hex2Bytes("307839383736353400000000000000")
   296  	if !bytes.Equal(out15[1][:], expected) {
   297  		t.Errorf("expected %x, got %x\n", expected, out15[1])
   298  	}
   299  }
   300  
   301  type methodMultiOutput struct {
   302  	Int    *big.Int
   303  	String string
   304  }
   305  
   306  func methodMultiReturn(require *require.Assertions) (ABI, []byte, methodMultiOutput) {
   307  	const definition = `[
   308  	{ "name" : "multi", "type": "function", "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]`
   309  	var expected = methodMultiOutput{big.NewInt(1), "hello"}
   310  
   311  	abi, err := JSON(strings.NewReader(definition))
   312  	require.NoError(err)
   313  	// using buff to make the code readable
   314  	buff := new(bytes.Buffer)
   315  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
   316  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
   317  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
   318  	buff.Write(common.RightPadBytes([]byte(expected.String), 32))
   319  	return abi, buff.Bytes(), expected
   320  }
   321  
   322  func TestMethodMultiReturn(t *testing.T) {
   323  	type reversed struct {
   324  		String string
   325  		Int    *big.Int
   326  	}
   327  
   328  	newInterfaceSlice := func(len int) interface{} {
   329  		slice := make([]interface{}, len)
   330  		return &slice
   331  	}
   332  
   333  	abi, data, expected := methodMultiReturn(require.New(t))
   334  	bigint := new(big.Int)
   335  	var testCases = []struct {
   336  		dest     interface{}
   337  		expected interface{}
   338  		error    string
   339  		name     string
   340  	}{{
   341  		&methodMultiOutput{},
   342  		&expected,
   343  		"",
   344  		"Can unpack into structure",
   345  	}, {
   346  		&reversed{},
   347  		&reversed{expected.String, expected.Int},
   348  		"",
   349  		"Can unpack into reversed structure",
   350  	}, {
   351  		&[]interface{}{&bigint, new(string)},
   352  		&[]interface{}{&expected.Int, &expected.String},
   353  		"",
   354  		"Can unpack into a slice",
   355  	}, {
   356  		&[2]interface{}{&bigint, new(string)},
   357  		&[2]interface{}{&expected.Int, &expected.String},
   358  		"",
   359  		"Can unpack into an array",
   360  	}, {
   361  		&[2]interface{}{},
   362  		&[2]interface{}{expected.Int, expected.String},
   363  		"",
   364  		"Can unpack into interface array",
   365  	}, {
   366  		newInterfaceSlice(2),
   367  		&[]interface{}{expected.Int, expected.String},
   368  		"",
   369  		"Can unpack into interface slice",
   370  	}, {
   371  		&[]interface{}{new(int), new(int)},
   372  		&[]interface{}{&expected.Int, &expected.String},
   373  		"abi: cannot unmarshal *big.Int in to int",
   374  		"Can not unpack into a slice with wrong types",
   375  	}, {
   376  		&[]interface{}{new(int)},
   377  		&[]interface{}{},
   378  		"abi: insufficient number of arguments for unpack, want 2, got 1",
   379  		"Can not unpack into a slice with wrong types",
   380  	}}
   381  	for _, tc := range testCases {
   382  		tc := tc
   383  		t.Run(tc.name, func(t *testing.T) {
   384  			require := require.New(t)
   385  			err := abi.UnpackIntoInterface(tc.dest, "multi", data)
   386  			if tc.error == "" {
   387  				require.Nil(err, "Should be able to unpack method outputs.")
   388  				require.Equal(tc.expected, tc.dest)
   389  			} else {
   390  				require.EqualError(err, tc.error)
   391  			}
   392  		})
   393  	}
   394  }
   395  
   396  func TestMultiReturnWithArray(t *testing.T) {
   397  	const definition = `[{"name" : "multi", "type": "function", "outputs": [{"type": "uint64[3]"}, {"type": "uint64"}]}]`
   398  	abi, err := JSON(strings.NewReader(definition))
   399  	if err != nil {
   400  		t.Fatal(err)
   401  	}
   402  	buff := new(bytes.Buffer)
   403  	buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000900000000000000000000000000000000000000000000000000000000000000090000000000000000000000000000000000000000000000000000000000000009"))
   404  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000008"))
   405  
   406  	ret1, ret1Exp := new([3]uint64), [3]uint64{9, 9, 9}
   407  	ret2, ret2Exp := new(uint64), uint64(8)
   408  	if err := abi.UnpackIntoInterface(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil {
   409  		t.Fatal(err)
   410  	}
   411  	if !reflect.DeepEqual(*ret1, ret1Exp) {
   412  		t.Error("array result", *ret1, "!= Expected", ret1Exp)
   413  	}
   414  	if *ret2 != ret2Exp {
   415  		t.Error("int result", *ret2, "!= Expected", ret2Exp)
   416  	}
   417  }
   418  
   419  func TestMultiReturnWithStringArray(t *testing.T) {
   420  	const definition = `[{"name" : "multi", "type": "function", "outputs": [{"name": "","type": "uint256[3]"},{"name": "","type": "address"},{"name": "","type": "string[2]"},{"name": "","type": "bool"}]}]`
   421  	abi, err := JSON(strings.NewReader(definition))
   422  	if err != nil {
   423  		t.Fatal(err)
   424  	}
   425  	buff := new(bytes.Buffer)
   426  	buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000005c1b78ea0000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000001a055690d9db80000000000000000000000000000ab1257528b3782fb40d7ed5f72e624b744dffb2f00000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000008457468657265756d000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001048656c6c6f2c20457468657265756d2100000000000000000000000000000000"))
   427  	temp, _ := new(big.Int).SetString("30000000000000000000", 10)
   428  	ret1, ret1Exp := new([3]*big.Int), [3]*big.Int{big.NewInt(1545304298), big.NewInt(6), temp}
   429  	ret2, ret2Exp := new(common.Address), common.HexToAddress("ab1257528b3782fb40d7ed5f72e624b744dffb2f")
   430  	ret3, ret3Exp := new([2]string), [2]string{"Ethereum", "Hello, Ethereum!"}
   431  	ret4, ret4Exp := new(bool), false
   432  	if err := abi.UnpackIntoInterface(&[]interface{}{ret1, ret2, ret3, ret4}, "multi", buff.Bytes()); err != nil {
   433  		t.Fatal(err)
   434  	}
   435  	if !reflect.DeepEqual(*ret1, ret1Exp) {
   436  		t.Error("big.Int array result", *ret1, "!= Expected", ret1Exp)
   437  	}
   438  	if !reflect.DeepEqual(*ret2, ret2Exp) {
   439  		t.Error("address result", *ret2, "!= Expected", ret2Exp)
   440  	}
   441  	if !reflect.DeepEqual(*ret3, ret3Exp) {
   442  		t.Error("string array result", *ret3, "!= Expected", ret3Exp)
   443  	}
   444  	if !reflect.DeepEqual(*ret4, ret4Exp) {
   445  		t.Error("bool result", *ret4, "!= Expected", ret4Exp)
   446  	}
   447  }
   448  
   449  func TestMultiReturnWithStringSlice(t *testing.T) {
   450  	const definition = `[{"name" : "multi", "type": "function", "outputs": [{"name": "","type": "string[]"},{"name": "","type": "uint256[]"}]}]`
   451  	abi, err := JSON(strings.NewReader(definition))
   452  	if err != nil {
   453  		t.Fatal(err)
   454  	}
   455  	buff := new(bytes.Buffer)
   456  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // output[0] offset
   457  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000120")) // output[1] offset
   458  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // output[0] length
   459  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // output[0][0] offset
   460  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // output[0][1] offset
   461  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000008")) // output[0][0] length
   462  	buff.Write(common.Hex2Bytes("657468657265756d000000000000000000000000000000000000000000000000")) // output[0][0] value
   463  	buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000b")) // output[0][1] length
   464  	buff.Write(common.Hex2Bytes("676f2d657468657265756d000000000000000000000000000000000000000000")) // output[0][1] value
   465  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // output[1] length
   466  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000064")) // output[1][0] value
   467  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000065")) // output[1][1] value
   468  	ret1, ret1Exp := new([]string), []string{"ethereum", "go-ethereum"}
   469  	ret2, ret2Exp := new([]*big.Int), []*big.Int{big.NewInt(100), big.NewInt(101)}
   470  	if err := abi.UnpackIntoInterface(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil {
   471  		t.Fatal(err)
   472  	}
   473  	if !reflect.DeepEqual(*ret1, ret1Exp) {
   474  		t.Error("string slice result", *ret1, "!= Expected", ret1Exp)
   475  	}
   476  	if !reflect.DeepEqual(*ret2, ret2Exp) {
   477  		t.Error("uint256 slice result", *ret2, "!= Expected", ret2Exp)
   478  	}
   479  }
   480  
   481  func TestMultiReturnWithDeeplyNestedArray(t *testing.T) {
   482  	// Similar to TestMultiReturnWithArray, but with a special case in mind:
   483  	//  values of nested static arrays count towards the size as well, and any element following
   484  	//  after such nested array argument should be read with the correct offset,
   485  	//  so that it does not read content from the previous array argument.
   486  	const definition = `[{"name" : "multi", "type": "function", "outputs": [{"type": "uint64[3][2][4]"}, {"type": "uint64"}]}]`
   487  	abi, err := JSON(strings.NewReader(definition))
   488  	if err != nil {
   489  		t.Fatal(err)
   490  	}
   491  	buff := new(bytes.Buffer)
   492  	// construct the test array, each 3 char element is joined with 61 '0' chars,
   493  	// to from the ((3 + 61) * 0.5) = 32 byte elements in the array.
   494  	buff.Write(common.Hex2Bytes(strings.Join([]string{
   495  		"", //empty, to apply the 61-char separator to the first element as well.
   496  		"111", "112", "113", "121", "122", "123",
   497  		"211", "212", "213", "221", "222", "223",
   498  		"311", "312", "313", "321", "322", "323",
   499  		"411", "412", "413", "421", "422", "423",
   500  	}, "0000000000000000000000000000000000000000000000000000000000000")))
   501  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000009876"))
   502  
   503  	ret1, ret1Exp := new([4][2][3]uint64), [4][2][3]uint64{
   504  		{{0x111, 0x112, 0x113}, {0x121, 0x122, 0x123}},
   505  		{{0x211, 0x212, 0x213}, {0x221, 0x222, 0x223}},
   506  		{{0x311, 0x312, 0x313}, {0x321, 0x322, 0x323}},
   507  		{{0x411, 0x412, 0x413}, {0x421, 0x422, 0x423}},
   508  	}
   509  	ret2, ret2Exp := new(uint64), uint64(0x9876)
   510  	if err := abi.UnpackIntoInterface(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil {
   511  		t.Fatal(err)
   512  	}
   513  	if !reflect.DeepEqual(*ret1, ret1Exp) {
   514  		t.Error("array result", *ret1, "!= Expected", ret1Exp)
   515  	}
   516  	if *ret2 != ret2Exp {
   517  		t.Error("int result", *ret2, "!= Expected", ret2Exp)
   518  	}
   519  }
   520  
   521  func TestUnmarshal(t *testing.T) {
   522  	const definition = `[
   523  	{ "name" : "int", "type": "function", "outputs": [ { "type": "uint256" } ] },
   524  	{ "name" : "bool", "type": "function", "outputs": [ { "type": "bool" } ] },
   525  	{ "name" : "bytes", "type": "function", "outputs": [ { "type": "bytes" } ] },
   526  	{ "name" : "fixed", "type": "function", "outputs": [ { "type": "bytes32" } ] },
   527  	{ "name" : "multi", "type": "function", "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] },
   528  	{ "name" : "intArraySingle", "type": "function", "outputs": [ { "type": "uint256[3]" } ] },
   529  	{ "name" : "addressSliceSingle", "type": "function", "outputs": [ { "type": "address[]" } ] },
   530  	{ "name" : "addressSliceDouble", "type": "function", "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] },
   531  	{ "name" : "mixedBytes", "type": "function", "stateMutability" : "view", "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]`
   532  
   533  	abi, err := JSON(strings.NewReader(definition))
   534  	if err != nil {
   535  		t.Fatal(err)
   536  	}
   537  	buff := new(bytes.Buffer)
   538  
   539  	// marshall mixed bytes (mixedBytes)
   540  	p0, p0Exp := []byte{}, common.Hex2Bytes("01020000000000000000")
   541  	p1, p1Exp := [32]byte{}, common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff")
   542  	mixedBytes := []interface{}{&p0, &p1}
   543  
   544  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
   545  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff"))
   546  	buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000a"))
   547  	buff.Write(common.Hex2Bytes("0102000000000000000000000000000000000000000000000000000000000000"))
   548  
   549  	err = abi.UnpackIntoInterface(&mixedBytes, "mixedBytes", buff.Bytes())
   550  	if err != nil {
   551  		t.Error(err)
   552  	} else {
   553  		if !bytes.Equal(p0, p0Exp) {
   554  			t.Errorf("unexpected value unpacked: want %x, got %x", p0Exp, p0)
   555  		}
   556  
   557  		if !bytes.Equal(p1[:], p1Exp) {
   558  			t.Errorf("unexpected value unpacked: want %x, got %x", p1Exp, p1)
   559  		}
   560  	}
   561  
   562  	// marshal int
   563  	var Int *big.Int
   564  	err = abi.UnpackIntoInterface(&Int, "int", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
   565  	if err != nil {
   566  		t.Error(err)
   567  	}
   568  
   569  	if Int == nil || Int.Cmp(big.NewInt(1)) != 0 {
   570  		t.Error("expected Int to be 1 got", Int)
   571  	}
   572  
   573  	// marshal bool
   574  	var Bool bool
   575  	err = abi.UnpackIntoInterface(&Bool, "bool", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
   576  	if err != nil {
   577  		t.Error(err)
   578  	}
   579  
   580  	if !Bool {
   581  		t.Error("expected Bool to be true")
   582  	}
   583  
   584  	// marshal dynamic bytes max length 32
   585  	buff.Reset()
   586  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   587  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   588  	bytesOut := common.RightPadBytes([]byte("hello"), 32)
   589  	buff.Write(bytesOut)
   590  
   591  	var Bytes []byte
   592  	err = abi.UnpackIntoInterface(&Bytes, "bytes", buff.Bytes())
   593  	if err != nil {
   594  		t.Error(err)
   595  	}
   596  
   597  	if !bytes.Equal(Bytes, bytesOut) {
   598  		t.Errorf("expected %x got %x", bytesOut, Bytes)
   599  	}
   600  
   601  	// marshall dynamic bytes max length 64
   602  	buff.Reset()
   603  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   604  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
   605  	bytesOut = common.RightPadBytes([]byte("hello"), 64)
   606  	buff.Write(bytesOut)
   607  
   608  	err = abi.UnpackIntoInterface(&Bytes, "bytes", buff.Bytes())
   609  	if err != nil {
   610  		t.Error(err)
   611  	}
   612  
   613  	if !bytes.Equal(Bytes, bytesOut) {
   614  		t.Errorf("expected %x got %x", bytesOut, Bytes)
   615  	}
   616  
   617  	// marshall dynamic bytes max length 64
   618  	buff.Reset()
   619  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   620  	buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000003f"))
   621  	bytesOut = common.RightPadBytes([]byte("hello"), 64)
   622  	buff.Write(bytesOut)
   623  
   624  	err = abi.UnpackIntoInterface(&Bytes, "bytes", buff.Bytes())
   625  	if err != nil {
   626  		t.Error(err)
   627  	}
   628  
   629  	if !bytes.Equal(Bytes, bytesOut[:len(bytesOut)-1]) {
   630  		t.Errorf("expected %x got %x", bytesOut[:len(bytesOut)-1], Bytes)
   631  	}
   632  
   633  	// marshal dynamic bytes output empty
   634  	err = abi.UnpackIntoInterface(&Bytes, "bytes", nil)
   635  	if err == nil {
   636  		t.Error("expected error")
   637  	}
   638  
   639  	// marshal dynamic bytes length 5
   640  	buff.Reset()
   641  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   642  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
   643  	buff.Write(common.RightPadBytes([]byte("hello"), 32))
   644  
   645  	err = abi.UnpackIntoInterface(&Bytes, "bytes", buff.Bytes())
   646  	if err != nil {
   647  		t.Error(err)
   648  	}
   649  
   650  	if !bytes.Equal(Bytes, []byte("hello")) {
   651  		t.Errorf("expected %x got %x", bytesOut, Bytes)
   652  	}
   653  
   654  	// marshal dynamic bytes length 5
   655  	buff.Reset()
   656  	buff.Write(common.RightPadBytes([]byte("hello"), 32))
   657  
   658  	var hash common.Hash
   659  	err = abi.UnpackIntoInterface(&hash, "fixed", buff.Bytes())
   660  	if err != nil {
   661  		t.Error(err)
   662  	}
   663  
   664  	helloHash := common.BytesToHash(common.RightPadBytes([]byte("hello"), 32))
   665  	if hash != helloHash {
   666  		t.Errorf("Expected %x to equal %x", hash, helloHash)
   667  	}
   668  
   669  	// marshal error
   670  	buff.Reset()
   671  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
   672  	err = abi.UnpackIntoInterface(&Bytes, "bytes", buff.Bytes())
   673  	if err == nil {
   674  		t.Error("expected error")
   675  	}
   676  
   677  	err = abi.UnpackIntoInterface(&Bytes, "multi", make([]byte, 64))
   678  	if err == nil {
   679  		t.Error("expected error")
   680  	}
   681  
   682  	buff.Reset()
   683  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
   684  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"))
   685  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003"))
   686  	// marshal int array
   687  	var intArray [3]*big.Int
   688  	err = abi.UnpackIntoInterface(&intArray, "intArraySingle", buff.Bytes())
   689  	if err != nil {
   690  		t.Error(err)
   691  	}
   692  	var testAgainstIntArray [3]*big.Int
   693  	testAgainstIntArray[0] = big.NewInt(1)
   694  	testAgainstIntArray[1] = big.NewInt(2)
   695  	testAgainstIntArray[2] = big.NewInt(3)
   696  
   697  	for i, Int := range intArray {
   698  		if Int.Cmp(testAgainstIntArray[i]) != 0 {
   699  			t.Errorf("expected %v, got %v", testAgainstIntArray[i], Int)
   700  		}
   701  	}
   702  	// marshal address slice
   703  	buff.Reset()
   704  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) // offset
   705  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
   706  	buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
   707  
   708  	var outAddr []common.Address
   709  	err = abi.UnpackIntoInterface(&outAddr, "addressSliceSingle", buff.Bytes())
   710  	if err != nil {
   711  		t.Fatal("didn't expect error:", err)
   712  	}
   713  
   714  	if len(outAddr) != 1 {
   715  		t.Fatal("expected 1 item, got", len(outAddr))
   716  	}
   717  
   718  	if outAddr[0] != (common.Address{1}) {
   719  		t.Errorf("expected %x, got %x", common.Address{1}, outAddr[0])
   720  	}
   721  
   722  	// marshal multiple address slice
   723  	buff.Reset()
   724  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // offset
   725  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // offset
   726  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
   727  	buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
   728  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // size
   729  	buff.Write(common.Hex2Bytes("0000000000000000000000000200000000000000000000000000000000000000"))
   730  	buff.Write(common.Hex2Bytes("0000000000000000000000000300000000000000000000000000000000000000"))
   731  
   732  	var outAddrStruct struct {
   733  		A []common.Address
   734  		B []common.Address
   735  	}
   736  	err = abi.UnpackIntoInterface(&outAddrStruct, "addressSliceDouble", buff.Bytes())
   737  	if err != nil {
   738  		t.Fatal("didn't expect error:", err)
   739  	}
   740  
   741  	if len(outAddrStruct.A) != 1 {
   742  		t.Fatal("expected 1 item, got", len(outAddrStruct.A))
   743  	}
   744  
   745  	if outAddrStruct.A[0] != (common.Address{1}) {
   746  		t.Errorf("expected %x, got %x", common.Address{1}, outAddrStruct.A[0])
   747  	}
   748  
   749  	if len(outAddrStruct.B) != 2 {
   750  		t.Fatal("expected 1 item, got", len(outAddrStruct.B))
   751  	}
   752  
   753  	if outAddrStruct.B[0] != (common.Address{2}) {
   754  		t.Errorf("expected %x, got %x", common.Address{2}, outAddrStruct.B[0])
   755  	}
   756  	if outAddrStruct.B[1] != (common.Address{3}) {
   757  		t.Errorf("expected %x, got %x", common.Address{3}, outAddrStruct.B[1])
   758  	}
   759  
   760  	// marshal invalid address slice
   761  	buff.Reset()
   762  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000100"))
   763  
   764  	err = abi.UnpackIntoInterface(&outAddr, "addressSliceSingle", buff.Bytes())
   765  	if err == nil {
   766  		t.Fatal("expected error:", err)
   767  	}
   768  }
   769  
   770  func TestUnpackTuple(t *testing.T) {
   771  	const simpleTuple = `[{"name":"tuple","type":"function","outputs":[{"type":"tuple","name":"ret","components":[{"type":"int256","name":"a"},{"type":"int256","name":"b"}]}]}]`
   772  	abi, err := JSON(strings.NewReader(simpleTuple))
   773  	if err != nil {
   774  		t.Fatal(err)
   775  	}
   776  	buff := new(bytes.Buffer)
   777  
   778  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // ret[a] = 1
   779  	buff.Write(common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")) // ret[b] = -1
   780  
   781  	// If the result is single tuple, use struct as return value container directly.
   782  	type v struct {
   783  		A *big.Int
   784  		B *big.Int
   785  	}
   786  	type r struct {
   787  		Result v
   788  	}
   789  	var ret0 = new(r)
   790  	err = abi.UnpackIntoInterface(ret0, "tuple", buff.Bytes())
   791  
   792  	if err != nil {
   793  		t.Error(err)
   794  	} else {
   795  		if ret0.Result.A.Cmp(big.NewInt(1)) != 0 {
   796  			t.Errorf("unexpected value unpacked: want %x, got %x", 1, ret0.Result.A)
   797  		}
   798  		if ret0.Result.B.Cmp(big.NewInt(-1)) != 0 {
   799  			t.Errorf("unexpected value unpacked: want %x, got %x", -1, ret0.Result.B)
   800  		}
   801  	}
   802  
   803  	// Test nested tuple
   804  	const nestedTuple = `[{"name":"tuple","type":"function","outputs":[
   805  		{"type":"tuple","name":"s","components":[{"type":"uint256","name":"a"},{"type":"uint256[]","name":"b"},{"type":"tuple[]","name":"c","components":[{"name":"x", "type":"uint256"},{"name":"y","type":"uint256"}]}]},
   806  		{"type":"tuple","name":"t","components":[{"name":"x", "type":"uint256"},{"name":"y","type":"uint256"}]},
   807  		{"type":"uint256","name":"a"}
   808  	]}]`
   809  
   810  	abi, err = JSON(strings.NewReader(nestedTuple))
   811  	if err != nil {
   812  		t.Fatal(err)
   813  	}
   814  	buff.Reset()
   815  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // s offset
   816  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000000")) // t.X = 0
   817  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // t.Y = 1
   818  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // a = 1
   819  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // s.A = 1
   820  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000060")) // s.B offset
   821  	buff.Write(common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000c0")) // s.C offset
   822  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.B length
   823  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // s.B[0] = 1
   824  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.B[0] = 2
   825  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.C length
   826  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // s.C[0].X
   827  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.C[0].Y
   828  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // s.C[1].X
   829  	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // s.C[1].Y
   830  
   831  	type T struct {
   832  		X *big.Int `abi:"x"`
   833  		Z *big.Int `abi:"y"` // Test whether the abi tag works.
   834  	}
   835  
   836  	type S struct {
   837  		A *big.Int
   838  		B []*big.Int
   839  		C []T
   840  	}
   841  
   842  	type Ret struct {
   843  		FieldS S `abi:"s"`
   844  		FieldT T `abi:"t"`
   845  		A      *big.Int
   846  	}
   847  	var ret Ret
   848  	var expected = Ret{
   849  		FieldS: S{
   850  			A: big.NewInt(1),
   851  			B: []*big.Int{big.NewInt(1), big.NewInt(2)},
   852  			C: []T{
   853  				{big.NewInt(1), big.NewInt(2)},
   854  				{big.NewInt(2), big.NewInt(1)},
   855  			},
   856  		},
   857  		FieldT: T{
   858  			big.NewInt(0), big.NewInt(1),
   859  		},
   860  		A: big.NewInt(1),
   861  	}
   862  
   863  	err = abi.UnpackIntoInterface(&ret, "tuple", buff.Bytes())
   864  	if err != nil {
   865  		t.Error(err)
   866  	}
   867  	if reflect.DeepEqual(ret, expected) {
   868  		t.Error("unexpected unpack value")
   869  	}
   870  }
   871  
   872  func TestOOMMaliciousInput(t *testing.T) {
   873  	oomTests := []unpackTest{
   874  		{
   875  			def: `[{"type": "uint8[]"}]`,
   876  			enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
   877  				"0000000000000000000000000000000000000000000000000000000000000003" + // num elems
   878  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
   879  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
   880  		},
   881  		{ // Length larger than 64 bits
   882  			def: `[{"type": "uint8[]"}]`,
   883  			enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
   884  				"00ffffffffffffffffffffffffffffffffffffffffffffff0000000000000002" + // num elems
   885  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
   886  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
   887  		},
   888  		{ // Offset very large (over 64 bits)
   889  			def: `[{"type": "uint8[]"}]`,
   890  			enc: "00ffffffffffffffffffffffffffffffffffffffffffffff0000000000000020" + // offset
   891  				"0000000000000000000000000000000000000000000000000000000000000002" + // num elems
   892  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
   893  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
   894  		},
   895  		{ // Offset very large (below 64 bits)
   896  			def: `[{"type": "uint8[]"}]`,
   897  			enc: "0000000000000000000000000000000000000000000000007ffffffffff00020" + // offset
   898  				"0000000000000000000000000000000000000000000000000000000000000002" + // num elems
   899  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
   900  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
   901  		},
   902  		{ // Offset negative (as 64 bit)
   903  			def: `[{"type": "uint8[]"}]`,
   904  			enc: "000000000000000000000000000000000000000000000000f000000000000020" + // offset
   905  				"0000000000000000000000000000000000000000000000000000000000000002" + // num elems
   906  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
   907  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
   908  		},
   909  
   910  		{ // Negative length
   911  			def: `[{"type": "uint8[]"}]`,
   912  			enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
   913  				"000000000000000000000000000000000000000000000000f000000000000002" + // num elems
   914  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
   915  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
   916  		},
   917  		{ // Very large length
   918  			def: `[{"type": "uint8[]"}]`,
   919  			enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
   920  				"0000000000000000000000000000000000000000000000007fffffffff000002" + // num elems
   921  				"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
   922  				"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
   923  		},
   924  	}
   925  	for i, test := range oomTests {
   926  		def := fmt.Sprintf(`[{ "name" : "method", "type": "function", "outputs": %s}]`, test.def)
   927  		abi, err := JSON(strings.NewReader(def))
   928  		if err != nil {
   929  			t.Fatalf("invalid ABI definition %s: %v", def, err)
   930  		}
   931  		encb, err := hex.DecodeString(test.enc)
   932  		if err != nil {
   933  			t.Fatalf("invalid hex: %s" + test.enc)
   934  		}
   935  		_, err = abi.Methods["method"].Outputs.UnpackValues(encb)
   936  		if err == nil {
   937  			t.Fatalf("Expected error on malicious input, test %d", i)
   938  		}
   939  	}
   940  }