accounts/abi: reorganizing package with small fixes (#14610)
* accounts/abi: reorganizing package and some notes and a quick correction of name. Signed-off-by: RJ Catalano <rj@monax.io> get rid of some imports Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move file names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix boolean decode function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix for the array set and for creating a bool Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: be very very very correct Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix up error message and variable names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: take out unnecessary argument in pack method Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: add bool unpack test and add a panic to readBool function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix panic message Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: change from panic to basic error Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix nil to false Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fill out type regex tests and fill with the correct type for integers Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move packNumbers into pack.go. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: separation of the testing suite into appropriately named files. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: change to hex string tests. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: fix up rest of tests to hex Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: declare bool at the package level Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: use errors package in the error file. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix ugly hack and fix error type declaration. Signed-off-by: RJ Catalano <rj@monax.io>
This commit is contained in:
		
							parent
							
								
									cf611c50b9
								
							
						
					
					
						commit
						5421a08d2f
					
				| @ -17,11 +17,9 @@ | ||||
| package abi | ||||
| 
 | ||||
| import ( | ||||
| 	"encoding/binary" | ||||
| 	"encoding/json" | ||||
| 	"fmt" | ||||
| 	"io" | ||||
| 	"math/big" | ||||
| 	"reflect" | ||||
| 	"strings" | ||||
| 
 | ||||
| @ -67,7 +65,7 @@ func (abi ABI) Pack(name string, args ...interface{}) ([]byte, error) { | ||||
| 		} | ||||
| 		method = m | ||||
| 	} | ||||
| 	arguments, err := method.pack(method, args...) | ||||
| 	arguments, err := method.pack(args...) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| @ -78,199 +76,6 @@ func (abi ABI) Pack(name string, args ...interface{}) ([]byte, error) { | ||||
| 	return append(method.Id(), arguments...), nil | ||||
| } | ||||
| 
 | ||||
| // toGoSliceType parses the input and casts it to the proper slice defined by the ABI
 | ||||
| // argument in T.
 | ||||
| func toGoSlice(i int, t Argument, output []byte) (interface{}, error) { | ||||
| 	index := i * 32 | ||||
| 	// The slice must, at very least be large enough for the index+32 which is exactly the size required
 | ||||
| 	// for the [offset in output, size of offset].
 | ||||
| 	if index+32 > len(output) { | ||||
| 		return nil, fmt.Errorf("abi: cannot marshal in to go slice: insufficient size output %d require %d", len(output), index+32) | ||||
| 	} | ||||
| 	elem := t.Type.Elem | ||||
| 
 | ||||
| 	// first we need to create a slice of the type
 | ||||
| 	var refSlice reflect.Value | ||||
| 	switch elem.T { | ||||
| 	case IntTy, UintTy, BoolTy: | ||||
| 		// create a new reference slice matching the element type
 | ||||
| 		switch t.Type.Kind { | ||||
| 		case reflect.Bool: | ||||
| 			refSlice = reflect.ValueOf([]bool(nil)) | ||||
| 		case reflect.Uint8: | ||||
| 			refSlice = reflect.ValueOf([]uint8(nil)) | ||||
| 		case reflect.Uint16: | ||||
| 			refSlice = reflect.ValueOf([]uint16(nil)) | ||||
| 		case reflect.Uint32: | ||||
| 			refSlice = reflect.ValueOf([]uint32(nil)) | ||||
| 		case reflect.Uint64: | ||||
| 			refSlice = reflect.ValueOf([]uint64(nil)) | ||||
| 		case reflect.Int8: | ||||
| 			refSlice = reflect.ValueOf([]int8(nil)) | ||||
| 		case reflect.Int16: | ||||
| 			refSlice = reflect.ValueOf([]int16(nil)) | ||||
| 		case reflect.Int32: | ||||
| 			refSlice = reflect.ValueOf([]int32(nil)) | ||||
| 		case reflect.Int64: | ||||
| 			refSlice = reflect.ValueOf([]int64(nil)) | ||||
| 		default: | ||||
| 			refSlice = reflect.ValueOf([]*big.Int(nil)) | ||||
| 		} | ||||
| 	case AddressTy: // address must be of slice Address
 | ||||
| 		refSlice = reflect.ValueOf([]common.Address(nil)) | ||||
| 	case HashTy: // hash must be of slice hash
 | ||||
| 		refSlice = reflect.ValueOf([]common.Hash(nil)) | ||||
| 	case FixedBytesTy: | ||||
| 		refSlice = reflect.ValueOf([][]byte(nil)) | ||||
| 	default: // no other types are supported
 | ||||
| 		return nil, fmt.Errorf("abi: unsupported slice type %v", elem.T) | ||||
| 	} | ||||
| 
 | ||||
| 	var slice []byte | ||||
| 	var size int | ||||
| 	var offset int | ||||
| 	if t.Type.IsSlice { | ||||
| 		// get the offset which determines the start of this array ...
 | ||||
| 		offset = int(binary.BigEndian.Uint64(output[index+24 : index+32])) | ||||
| 		if offset+32 > len(output) { | ||||
| 			return nil, fmt.Errorf("abi: cannot marshal in to go slice: offset %d would go over slice boundary (len=%d)", len(output), offset+32) | ||||
| 		} | ||||
| 
 | ||||
| 		slice = output[offset:] | ||||
| 		// ... starting with the size of the array in elements ...
 | ||||
| 		size = int(binary.BigEndian.Uint64(slice[24:32])) | ||||
| 		slice = slice[32:] | ||||
| 		// ... and make sure that we've at the very least the amount of bytes
 | ||||
| 		// available in the buffer.
 | ||||
| 		if size*32 > len(slice) { | ||||
| 			return nil, fmt.Errorf("abi: cannot marshal in to go slice: insufficient size output %d require %d", len(output), offset+32+size*32) | ||||
| 		} | ||||
| 
 | ||||
| 		// reslice to match the required size
 | ||||
| 		slice = slice[:size*32] | ||||
| 	} else if t.Type.IsArray { | ||||
| 		//get the number of elements in the array
 | ||||
| 		size = t.Type.SliceSize | ||||
| 
 | ||||
| 		//check to make sure array size matches up
 | ||||
| 		if index+32*size > len(output) { | ||||
| 			return nil, fmt.Errorf("abi: cannot marshal in to go array: offset %d would go over slice boundary (len=%d)", len(output), index+32*size) | ||||
| 		} | ||||
| 		//slice is there for a fixed amount of times
 | ||||
| 		slice = output[index : index+size*32] | ||||
| 	} | ||||
| 
 | ||||
| 	for i := 0; i < size; i++ { | ||||
| 		var ( | ||||
| 			inter        interface{}             // interface type
 | ||||
| 			returnOutput = slice[i*32 : i*32+32] // the return output
 | ||||
| 		) | ||||
| 		// set inter to the correct type (cast)
 | ||||
| 		switch elem.T { | ||||
| 		case IntTy, UintTy: | ||||
| 			inter = readInteger(t.Type.Kind, returnOutput) | ||||
| 		case BoolTy: | ||||
| 			inter = !allZero(returnOutput) | ||||
| 		case AddressTy: | ||||
| 			inter = common.BytesToAddress(returnOutput) | ||||
| 		case HashTy: | ||||
| 			inter = common.BytesToHash(returnOutput) | ||||
| 		case FixedBytesTy: | ||||
| 			inter = returnOutput | ||||
| 		} | ||||
| 		// append the item to our reflect slice
 | ||||
| 		refSlice = reflect.Append(refSlice, reflect.ValueOf(inter)) | ||||
| 	} | ||||
| 
 | ||||
| 	// return the interface
 | ||||
| 	return refSlice.Interface(), nil | ||||
| } | ||||
| 
 | ||||
| func readInteger(kind reflect.Kind, b []byte) interface{} { | ||||
| 	switch kind { | ||||
| 	case reflect.Uint8: | ||||
| 		return uint8(b[len(b)-1]) | ||||
| 	case reflect.Uint16: | ||||
| 		return binary.BigEndian.Uint16(b[len(b)-2:]) | ||||
| 	case reflect.Uint32: | ||||
| 		return binary.BigEndian.Uint32(b[len(b)-4:]) | ||||
| 	case reflect.Uint64: | ||||
| 		return binary.BigEndian.Uint64(b[len(b)-8:]) | ||||
| 	case reflect.Int8: | ||||
| 		return int8(b[len(b)-1]) | ||||
| 	case reflect.Int16: | ||||
| 		return int16(binary.BigEndian.Uint16(b[len(b)-2:])) | ||||
| 	case reflect.Int32: | ||||
| 		return int32(binary.BigEndian.Uint32(b[len(b)-4:])) | ||||
| 	case reflect.Int64: | ||||
| 		return int64(binary.BigEndian.Uint64(b[len(b)-8:])) | ||||
| 	default: | ||||
| 		return new(big.Int).SetBytes(b) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func allZero(b []byte) bool { | ||||
| 	for _, byte := range b { | ||||
| 		if byte != 0 { | ||||
| 			return false | ||||
| 		} | ||||
| 	} | ||||
| 	return true | ||||
| } | ||||
| 
 | ||||
| // toGoType parses the input and casts it to the proper type defined by the ABI
 | ||||
| // argument in T.
 | ||||
| func toGoType(i int, t Argument, output []byte) (interface{}, error) { | ||||
| 	// we need to treat slices differently
 | ||||
| 	if (t.Type.IsSlice || t.Type.IsArray) && t.Type.T != BytesTy && t.Type.T != StringTy && t.Type.T != FixedBytesTy && t.Type.T != FunctionTy { | ||||
| 		return toGoSlice(i, t, output) | ||||
| 	} | ||||
| 
 | ||||
| 	index := i * 32 | ||||
| 	if index+32 > len(output) { | ||||
| 		return nil, fmt.Errorf("abi: cannot marshal in to go type: length insufficient %d require %d", len(output), index+32) | ||||
| 	} | ||||
| 
 | ||||
| 	// Parse the given index output and check whether we need to read
 | ||||
| 	// a different offset and length based on the type (i.e. string, bytes)
 | ||||
| 	var returnOutput []byte | ||||
| 	switch t.Type.T { | ||||
| 	case StringTy, BytesTy: // variable arrays are written at the end of the return bytes
 | ||||
| 		// parse offset from which we should start reading
 | ||||
| 		offset := int(binary.BigEndian.Uint64(output[index+24 : index+32])) | ||||
| 		if offset+32 > len(output) { | ||||
| 			return nil, fmt.Errorf("abi: cannot marshal in to go type: length insufficient %d require %d", len(output), offset+32) | ||||
| 		} | ||||
| 		// parse the size up until we should be reading
 | ||||
| 		size := int(binary.BigEndian.Uint64(output[offset+24 : offset+32])) | ||||
| 		if offset+32+size > len(output) { | ||||
| 			return nil, fmt.Errorf("abi: cannot marshal in to go type: length insufficient %d require %d", len(output), offset+32+size) | ||||
| 		} | ||||
| 
 | ||||
| 		// get the bytes for this return value
 | ||||
| 		returnOutput = output[offset+32 : offset+32+size] | ||||
| 	default: | ||||
| 		returnOutput = output[index : index+32] | ||||
| 	} | ||||
| 
 | ||||
| 	// convert the bytes to whatever is specified by the ABI.
 | ||||
| 	switch t.Type.T { | ||||
| 	case IntTy, UintTy: | ||||
| 		return readInteger(t.Type.Kind, returnOutput), nil | ||||
| 	case BoolTy: | ||||
| 		return !allZero(returnOutput), nil | ||||
| 	case AddressTy: | ||||
| 		return common.BytesToAddress(returnOutput), nil | ||||
| 	case HashTy: | ||||
| 		return common.BytesToHash(returnOutput), nil | ||||
| 	case BytesTy, FixedBytesTy, FunctionTy: | ||||
| 		return returnOutput, nil | ||||
| 	case StringTy: | ||||
| 		return string(returnOutput), nil | ||||
| 	} | ||||
| 	return nil, fmt.Errorf("abi: unknown type %v", t.Type.T) | ||||
| } | ||||
| 
 | ||||
| // these variable are used to determine certain types during type assertion for
 | ||||
| // assignment.
 | ||||
| var ( | ||||
|  | ||||
| @ -48,412 +48,6 @@ func pad(input []byte, size int, left bool) []byte { | ||||
| 	return common.RightPadBytes(input, size) | ||||
| } | ||||
| 
 | ||||
| func TestTypeCheck(t *testing.T) { | ||||
| 	for i, test := range []struct { | ||||
| 		typ   string | ||||
| 		input interface{} | ||||
| 		err   string | ||||
| 	}{ | ||||
| 		{"uint", big.NewInt(1), ""}, | ||||
| 		{"int", big.NewInt(1), ""}, | ||||
| 		{"uint30", big.NewInt(1), ""}, | ||||
| 		{"uint30", uint8(1), "abi: cannot use uint8 as type ptr as argument"}, | ||||
| 		{"uint16", uint16(1), ""}, | ||||
| 		{"uint16", uint8(1), "abi: cannot use uint8 as type uint16 as argument"}, | ||||
| 		{"uint16[]", []uint16{1, 2, 3}, ""}, | ||||
| 		{"uint16[]", [3]uint16{1, 2, 3}, ""}, | ||||
| 		{"uint16[]", []uint32{1, 2, 3}, "abi: cannot use []uint32 as type []uint16 as argument"}, | ||||
| 		{"uint16[3]", [3]uint32{1, 2, 3}, "abi: cannot use [3]uint32 as type [3]uint16 as argument"}, | ||||
| 		{"uint16[3]", [4]uint16{1, 2, 3}, "abi: cannot use [4]uint16 as type [3]uint16 as argument"}, | ||||
| 		{"uint16[3]", []uint16{1, 2, 3}, ""}, | ||||
| 		{"uint16[3]", []uint16{1, 2, 3, 4}, "abi: cannot use [4]uint16 as type [3]uint16 as argument"}, | ||||
| 		{"address[]", []common.Address{{1}}, ""}, | ||||
| 		{"address[1]", []common.Address{{1}}, ""}, | ||||
| 		{"address[1]", [1]common.Address{{1}}, ""}, | ||||
| 		{"address[2]", [1]common.Address{{1}}, "abi: cannot use [1]array as type [2]array as argument"}, | ||||
| 		{"bytes32", [32]byte{}, ""}, | ||||
| 		{"bytes32", [33]byte{}, "abi: cannot use [33]uint8 as type [32]uint8 as argument"}, | ||||
| 		{"bytes32", common.Hash{1}, ""}, | ||||
| 		{"bytes31", [31]byte{}, ""}, | ||||
| 		{"bytes31", [32]byte{}, "abi: cannot use [32]uint8 as type [31]uint8 as argument"}, | ||||
| 		{"bytes", []byte{0, 1}, ""}, | ||||
| 		{"bytes", [2]byte{0, 1}, ""}, | ||||
| 		{"bytes", common.Hash{1}, ""}, | ||||
| 		{"string", "hello world", ""}, | ||||
| 		{"bytes32[]", [][32]byte{{}}, ""}, | ||||
| 		{"function", [24]byte{}, ""}, | ||||
| 	} { | ||||
| 		typ, err := NewType(test.typ) | ||||
| 		if err != nil { | ||||
| 			t.Fatal("unexpected parse error:", err) | ||||
| 		} | ||||
| 
 | ||||
| 		err = typeCheck(typ, reflect.ValueOf(test.input)) | ||||
| 		if err != nil && len(test.err) == 0 { | ||||
| 			t.Errorf("%d failed. Expected no err but got: %v", i, err) | ||||
| 			continue | ||||
| 		} | ||||
| 		if err == nil && len(test.err) != 0 { | ||||
| 			t.Errorf("%d failed. Expected err: %v but got none", i, test.err) | ||||
| 			continue | ||||
| 		} | ||||
| 
 | ||||
| 		if err != nil && len(test.err) != 0 && err.Error() != test.err { | ||||
| 			t.Errorf("%d failed. Expected err: '%v' got err: '%v'", i, test.err, err) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestSimpleMethodUnpack(t *testing.T) { | ||||
| 	for i, test := range []struct { | ||||
| 		def              string      // definition of the **output** ABI params
 | ||||
| 		marshalledOutput []byte      // evm return data
 | ||||
| 		expectedOut      interface{} // the expected output
 | ||||
| 		outVar           string      // the output variable (e.g. uint32, *big.Int, etc)
 | ||||
| 		err              string      // empty or error if expected
 | ||||
| 	}{ | ||||
| 		{ | ||||
| 			`[ { "type": "uint32" } ]`, | ||||
| 			pad([]byte{1}, 32, true), | ||||
| 			uint32(1), | ||||
| 			"uint32", | ||||
| 			"", | ||||
| 		}, | ||||
| 		{ | ||||
| 			`[ { "type": "uint32" } ]`, | ||||
| 			pad([]byte{1}, 32, true), | ||||
| 			nil, | ||||
| 			"uint16", | ||||
| 			"abi: cannot unmarshal uint32 in to uint16", | ||||
| 		}, | ||||
| 		{ | ||||
| 			`[ { "type": "uint17" } ]`, | ||||
| 			pad([]byte{1}, 32, true), | ||||
| 			nil, | ||||
| 			"uint16", | ||||
| 			"abi: cannot unmarshal *big.Int in to uint16", | ||||
| 		}, | ||||
| 		{ | ||||
| 			`[ { "type": "uint17" } ]`, | ||||
| 			pad([]byte{1}, 32, true), | ||||
| 			big.NewInt(1), | ||||
| 			"*big.Int", | ||||
| 			"", | ||||
| 		}, | ||||
| 
 | ||||
| 		{ | ||||
| 			`[ { "type": "int32" } ]`, | ||||
| 			pad([]byte{1}, 32, true), | ||||
| 			int32(1), | ||||
| 			"int32", | ||||
| 			"", | ||||
| 		}, | ||||
| 		{ | ||||
| 			`[ { "type": "int32" } ]`, | ||||
| 			pad([]byte{1}, 32, true), | ||||
| 			nil, | ||||
| 			"int16", | ||||
| 			"abi: cannot unmarshal int32 in to int16", | ||||
| 		}, | ||||
| 		{ | ||||
| 			`[ { "type": "int17" } ]`, | ||||
| 			pad([]byte{1}, 32, true), | ||||
| 			nil, | ||||
| 			"int16", | ||||
| 			"abi: cannot unmarshal *big.Int in to int16", | ||||
| 		}, | ||||
| 		{ | ||||
| 			`[ { "type": "int17" } ]`, | ||||
| 			pad([]byte{1}, 32, true), | ||||
| 			big.NewInt(1), | ||||
| 			"*big.Int", | ||||
| 			"", | ||||
| 		}, | ||||
| 
 | ||||
| 		{ | ||||
| 			`[ { "type": "address" } ]`, | ||||
| 			pad(pad([]byte{1}, 20, false), 32, true), | ||||
| 			common.Address{1}, | ||||
| 			"address", | ||||
| 			"", | ||||
| 		}, | ||||
| 		{ | ||||
| 			`[ { "type": "bytes32" } ]`, | ||||
| 			pad([]byte{1}, 32, false), | ||||
| 			pad([]byte{1}, 32, false), | ||||
| 			"bytes", | ||||
| 			"", | ||||
| 		}, | ||||
| 		{ | ||||
| 			`[ { "type": "bytes32" } ]`, | ||||
| 			pad([]byte{1}, 32, false), | ||||
| 			pad([]byte{1}, 32, false), | ||||
| 			"hash", | ||||
| 			"", | ||||
| 		}, | ||||
| 		{ | ||||
| 			`[ { "type": "bytes32" } ]`, | ||||
| 			pad([]byte{1}, 32, false), | ||||
| 			pad([]byte{1}, 32, false), | ||||
| 			"interface", | ||||
| 			"", | ||||
| 		}, | ||||
| 		{ | ||||
| 			`[ { "type": "function" } ]`, | ||||
| 			pad([]byte{1}, 32, false), | ||||
| 			[24]byte{1}, | ||||
| 			"function", | ||||
| 			"", | ||||
| 		}, | ||||
| 	} { | ||||
| 		abiDefinition := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def) | ||||
| 		abi, err := JSON(strings.NewReader(abiDefinition)) | ||||
| 		if err != nil { | ||||
| 			t.Errorf("%d failed. %v", i, err) | ||||
| 			continue | ||||
| 		} | ||||
| 
 | ||||
| 		var outvar interface{} | ||||
| 		switch test.outVar { | ||||
| 		case "uint8": | ||||
| 			var v uint8 | ||||
| 			err = abi.Unpack(&v, "method", test.marshalledOutput) | ||||
| 			outvar = v | ||||
| 		case "uint16": | ||||
| 			var v uint16 | ||||
| 			err = abi.Unpack(&v, "method", test.marshalledOutput) | ||||
| 			outvar = v | ||||
| 		case "uint32": | ||||
| 			var v uint32 | ||||
| 			err = abi.Unpack(&v, "method", test.marshalledOutput) | ||||
| 			outvar = v | ||||
| 		case "uint64": | ||||
| 			var v uint64 | ||||
| 			err = abi.Unpack(&v, "method", test.marshalledOutput) | ||||
| 			outvar = v | ||||
| 		case "int8": | ||||
| 			var v int8 | ||||
| 			err = abi.Unpack(&v, "method", test.marshalledOutput) | ||||
| 			outvar = v | ||||
| 		case "int16": | ||||
| 			var v int16 | ||||
| 			err = abi.Unpack(&v, "method", test.marshalledOutput) | ||||
| 			outvar = v | ||||
| 		case "int32": | ||||
| 			var v int32 | ||||
| 			err = abi.Unpack(&v, "method", test.marshalledOutput) | ||||
| 			outvar = v | ||||
| 		case "int64": | ||||
| 			var v int64 | ||||
| 			err = abi.Unpack(&v, "method", test.marshalledOutput) | ||||
| 			outvar = v | ||||
| 		case "*big.Int": | ||||
| 			var v *big.Int | ||||
| 			err = abi.Unpack(&v, "method", test.marshalledOutput) | ||||
| 			outvar = v | ||||
| 		case "address": | ||||
| 			var v common.Address | ||||
| 			err = abi.Unpack(&v, "method", test.marshalledOutput) | ||||
| 			outvar = v | ||||
| 		case "bytes": | ||||
| 			var v []byte | ||||
| 			err = abi.Unpack(&v, "method", test.marshalledOutput) | ||||
| 			outvar = v | ||||
| 		case "hash": | ||||
| 			var v common.Hash | ||||
| 			err = abi.Unpack(&v, "method", test.marshalledOutput) | ||||
| 			outvar = v | ||||
| 		case "function": | ||||
| 			var v [24]byte | ||||
| 			err = abi.Unpack(&v, "method", test.marshalledOutput) | ||||
| 			outvar = v | ||||
| 		case "interface": | ||||
| 			err = abi.Unpack(&outvar, "method", test.marshalledOutput) | ||||
| 		default: | ||||
| 			t.Errorf("unsupported type '%v' please add it to the switch statement in this test", test.outVar) | ||||
| 			continue | ||||
| 		} | ||||
| 
 | ||||
| 		if err != nil && len(test.err) == 0 { | ||||
| 			t.Errorf("%d failed. Expected no err but got: %v", i, err) | ||||
| 			continue | ||||
| 		} | ||||
| 		if err == nil && len(test.err) != 0 { | ||||
| 			t.Errorf("%d failed. Expected err: %v but got none", i, test.err) | ||||
| 			continue | ||||
| 		} | ||||
| 		if err != nil && len(test.err) != 0 && err.Error() != test.err { | ||||
| 			t.Errorf("%d failed. Expected err: '%v' got err: '%v'", i, test.err, err) | ||||
| 			continue | ||||
| 		} | ||||
| 
 | ||||
| 		if err == nil { | ||||
| 			// bit of an ugly hack for hash type but I don't feel like finding a proper solution
 | ||||
| 			if test.outVar == "hash" { | ||||
| 				tmp := outvar.(common.Hash) // without assignment it's unaddressable
 | ||||
| 				outvar = tmp[:] | ||||
| 			} | ||||
| 
 | ||||
| 			if !reflect.DeepEqual(test.expectedOut, outvar) { | ||||
| 				t.Errorf("%d failed. Output error: expected %v, got %v", i, test.expectedOut, outvar) | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestUnpackSetInterfaceSlice(t *testing.T) { | ||||
| 	var ( | ||||
| 		var1 = new(uint8) | ||||
| 		var2 = new(uint8) | ||||
| 	) | ||||
| 	out := []interface{}{var1, var2} | ||||
| 	abi, err := JSON(strings.NewReader(`[{"type":"function", "name":"ints", "outputs":[{"type":"uint8"}, {"type":"uint8"}]}]`)) | ||||
| 	if err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
| 	marshalledReturn := append(pad([]byte{1}, 32, true), pad([]byte{2}, 32, true)...) | ||||
| 	err = abi.Unpack(&out, "ints", marshalledReturn) | ||||
| 	if err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
| 	if *var1 != 1 { | ||||
| 		t.Error("expected var1 to be 1, got", *var1) | ||||
| 	} | ||||
| 	if *var2 != 2 { | ||||
| 		t.Error("expected var2 to be 2, got", *var2) | ||||
| 	} | ||||
| 
 | ||||
| 	out = []interface{}{var1} | ||||
| 	err = abi.Unpack(&out, "ints", marshalledReturn) | ||||
| 
 | ||||
| 	expErr := "abi: cannot marshal in to slices of unequal size (require: 2, got: 1)" | ||||
| 	if err == nil || err.Error() != expErr { | ||||
| 		t.Error("expected err:", expErr, "Got:", err) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestUnpackSetInterfaceArrayOutput(t *testing.T) { | ||||
| 	var ( | ||||
| 		var1 = new([1]uint32) | ||||
| 		var2 = new([1]uint32) | ||||
| 	) | ||||
| 	out := []interface{}{var1, var2} | ||||
| 	abi, err := JSON(strings.NewReader(`[{"type":"function", "name":"ints", "outputs":[{"type":"uint32[1]"}, {"type":"uint32[1]"}]}]`)) | ||||
| 	if err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
| 	marshalledReturn := append(pad([]byte{1}, 32, true), pad([]byte{2}, 32, true)...) | ||||
| 	err = abi.Unpack(&out, "ints", marshalledReturn) | ||||
| 	if err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
| 
 | ||||
| 	if *var1 != [1]uint32{1} { | ||||
| 		t.Error("expected var1 to be [1], got", *var1) | ||||
| 	} | ||||
| 	if *var2 != [1]uint32{2} { | ||||
| 		t.Error("expected var2 to be [2], got", *var2) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestPack(t *testing.T) { | ||||
| 	for i, test := range []struct { | ||||
| 		typ string | ||||
| 
 | ||||
| 		input  interface{} | ||||
| 		output []byte | ||||
| 	}{ | ||||
| 		{"uint16", uint16(2), pad([]byte{2}, 32, true)}, | ||||
| 		{"uint16[]", []uint16{1, 2}, formatSliceOutput([]byte{1}, []byte{2})}, | ||||
| 		{"bytes20", [20]byte{1}, pad([]byte{1}, 32, false)}, | ||||
| 		{"uint256[]", []*big.Int{big.NewInt(1), big.NewInt(2)}, formatSliceOutput([]byte{1}, []byte{2})}, | ||||
| 		{"address[]", []common.Address{{1}, {2}}, formatSliceOutput(pad([]byte{1}, 20, false), pad([]byte{2}, 20, false))}, | ||||
| 		{"bytes32[]", []common.Hash{{1}, {2}}, formatSliceOutput(pad([]byte{1}, 32, false), pad([]byte{2}, 32, false))}, | ||||
| 		{"function", [24]byte{1}, pad([]byte{1}, 32, false)}, | ||||
| 	} { | ||||
| 		typ, err := NewType(test.typ) | ||||
| 		if err != nil { | ||||
| 			t.Fatal("unexpected parse error:", err) | ||||
| 		} | ||||
| 
 | ||||
| 		output, err := typ.pack(reflect.ValueOf(test.input)) | ||||
| 		if err != nil { | ||||
| 			t.Fatal("unexpected pack error:", err) | ||||
| 		} | ||||
| 
 | ||||
| 		if !bytes.Equal(output, test.output) { | ||||
| 			t.Errorf("%d failed. Expected bytes: '%x' Got: '%x'", i, test.output, output) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestMethodPack(t *testing.T) { | ||||
| 	abi, err := JSON(strings.NewReader(jsondata2)) | ||||
| 	if err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
| 
 | ||||
| 	sig := abi.Methods["slice"].Id() | ||||
| 	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) | ||||
| 	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) | ||||
| 
 | ||||
| 	packed, err := abi.Pack("slice", []uint32{1, 2}) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
| 
 | ||||
| 	if !bytes.Equal(packed, sig) { | ||||
| 		t.Errorf("expected %x got %x", sig, packed) | ||||
| 	} | ||||
| 
 | ||||
| 	var addrA, addrB = common.Address{1}, common.Address{2} | ||||
| 	sig = abi.Methods["sliceAddress"].Id() | ||||
| 	sig = append(sig, common.LeftPadBytes([]byte{32}, 32)...) | ||||
| 	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) | ||||
| 	sig = append(sig, common.LeftPadBytes(addrA[:], 32)...) | ||||
| 	sig = append(sig, common.LeftPadBytes(addrB[:], 32)...) | ||||
| 
 | ||||
| 	packed, err = abi.Pack("sliceAddress", []common.Address{addrA, addrB}) | ||||
| 	if err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
| 	if !bytes.Equal(packed, sig) { | ||||
| 		t.Errorf("expected %x got %x", sig, packed) | ||||
| 	} | ||||
| 
 | ||||
| 	var addrC, addrD = common.Address{3}, common.Address{4} | ||||
| 	sig = abi.Methods["sliceMultiAddress"].Id() | ||||
| 	sig = append(sig, common.LeftPadBytes([]byte{64}, 32)...) | ||||
| 	sig = append(sig, common.LeftPadBytes([]byte{160}, 32)...) | ||||
| 	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) | ||||
| 	sig = append(sig, common.LeftPadBytes(addrA[:], 32)...) | ||||
| 	sig = append(sig, common.LeftPadBytes(addrB[:], 32)...) | ||||
| 	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) | ||||
| 	sig = append(sig, common.LeftPadBytes(addrC[:], 32)...) | ||||
| 	sig = append(sig, common.LeftPadBytes(addrD[:], 32)...) | ||||
| 
 | ||||
| 	packed, err = abi.Pack("sliceMultiAddress", []common.Address{addrA, addrB}, []common.Address{addrC, addrD}) | ||||
| 	if err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
| 	if !bytes.Equal(packed, sig) { | ||||
| 		t.Errorf("expected %x got %x", sig, packed) | ||||
| 	} | ||||
| 
 | ||||
| 	sig = abi.Methods["slice256"].Id() | ||||
| 	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) | ||||
| 	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) | ||||
| 
 | ||||
| 	packed, err = abi.Pack("slice256", []*big.Int{big.NewInt(1), big.NewInt(2)}) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
| 
 | ||||
| 	if !bytes.Equal(packed, sig) { | ||||
| 		t.Errorf("expected %x got %x", sig, packed) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| const jsondata = ` | ||||
| [ | ||||
| 	{ "type" : "function", "name" : "balance", "constant" : true }, | ||||
| @ -843,399 +437,3 @@ func TestBareEvents(t *testing.T) { | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestMultiReturnWithStruct(t *testing.T) { | ||||
| 	const definition = `[ | ||||
| 	{ "name" : "multi", "constant" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]` | ||||
| 
 | ||||
| 	abi, err := JSON(strings.NewReader(definition)) | ||||
| 	if err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
| 
 | ||||
| 	// using buff to make the code readable
 | ||||
| 	buff := new(bytes.Buffer) | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005")) | ||||
| 	stringOut := "hello" | ||||
| 	buff.Write(common.RightPadBytes([]byte(stringOut), 32)) | ||||
| 
 | ||||
| 	var inter struct { | ||||
| 		Int    *big.Int | ||||
| 		String string | ||||
| 	} | ||||
| 	err = abi.Unpack(&inter, "multi", buff.Bytes()) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
| 
 | ||||
| 	if inter.Int == nil || inter.Int.Cmp(big.NewInt(1)) != 0 { | ||||
| 		t.Error("expected Int to be 1 got", inter.Int) | ||||
| 	} | ||||
| 
 | ||||
| 	if inter.String != stringOut { | ||||
| 		t.Error("expected String to be", stringOut, "got", inter.String) | ||||
| 	} | ||||
| 
 | ||||
| 	var reversed struct { | ||||
| 		String string | ||||
| 		Int    *big.Int | ||||
| 	} | ||||
| 
 | ||||
| 	err = abi.Unpack(&reversed, "multi", buff.Bytes()) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
| 
 | ||||
| 	if reversed.Int == nil || reversed.Int.Cmp(big.NewInt(1)) != 0 { | ||||
| 		t.Error("expected Int to be 1 got", reversed.Int) | ||||
| 	} | ||||
| 
 | ||||
| 	if reversed.String != stringOut { | ||||
| 		t.Error("expected String to be", stringOut, "got", reversed.String) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestMultiReturnWithSlice(t *testing.T) { | ||||
| 	const definition = `[ | ||||
| 	{ "name" : "multi", "constant" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]` | ||||
| 
 | ||||
| 	abi, err := JSON(strings.NewReader(definition)) | ||||
| 	if err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
| 
 | ||||
| 	// using buff to make the code readable
 | ||||
| 	buff := new(bytes.Buffer) | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005")) | ||||
| 	stringOut := "hello" | ||||
| 	buff.Write(common.RightPadBytes([]byte(stringOut), 32)) | ||||
| 
 | ||||
| 	var inter []interface{} | ||||
| 	err = abi.Unpack(&inter, "multi", buff.Bytes()) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
| 
 | ||||
| 	if len(inter) != 2 { | ||||
| 		t.Fatal("expected 2 results got", len(inter)) | ||||
| 	} | ||||
| 
 | ||||
| 	if num, ok := inter[0].(*big.Int); !ok || num.Cmp(big.NewInt(1)) != 0 { | ||||
| 		t.Error("expected index 0 to be 1 got", num) | ||||
| 	} | ||||
| 
 | ||||
| 	if str, ok := inter[1].(string); !ok || str != stringOut { | ||||
| 		t.Error("expected index 1 to be", stringOut, "got", str) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestMarshalArrays(t *testing.T) { | ||||
| 	const definition = `[ | ||||
| 	{ "name" : "bytes32", "constant" : false, "outputs": [ { "type": "bytes32" } ] }, | ||||
| 	{ "name" : "bytes10", "constant" : false, "outputs": [ { "type": "bytes10" } ] } | ||||
| 	]` | ||||
| 
 | ||||
| 	abi, err := JSON(strings.NewReader(definition)) | ||||
| 	if err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
| 
 | ||||
| 	output := common.LeftPadBytes([]byte{1}, 32) | ||||
| 
 | ||||
| 	var bytes10 [10]byte | ||||
| 	err = abi.Unpack(&bytes10, "bytes32", output) | ||||
| 	if err == nil || err.Error() != "abi: cannot unmarshal src (len=32) in to dst (len=10)" { | ||||
| 		t.Error("expected error or bytes32 not be assignable to bytes10:", err) | ||||
| 	} | ||||
| 
 | ||||
| 	var bytes32 [32]byte | ||||
| 	err = abi.Unpack(&bytes32, "bytes32", output) | ||||
| 	if err != nil { | ||||
| 		t.Error("didn't expect error:", err) | ||||
| 	} | ||||
| 	if !bytes.Equal(bytes32[:], output) { | ||||
| 		t.Error("expected bytes32[31] to be 1 got", bytes32[31]) | ||||
| 	} | ||||
| 
 | ||||
| 	type ( | ||||
| 		B10 [10]byte | ||||
| 		B32 [32]byte | ||||
| 	) | ||||
| 
 | ||||
| 	var b10 B10 | ||||
| 	err = abi.Unpack(&b10, "bytes32", output) | ||||
| 	if err == nil || err.Error() != "abi: cannot unmarshal src (len=32) in to dst (len=10)" { | ||||
| 		t.Error("expected error or bytes32 not be assignable to bytes10:", err) | ||||
| 	} | ||||
| 
 | ||||
| 	var b32 B32 | ||||
| 	err = abi.Unpack(&b32, "bytes32", output) | ||||
| 	if err != nil { | ||||
| 		t.Error("didn't expect error:", err) | ||||
| 	} | ||||
| 	if !bytes.Equal(b32[:], output) { | ||||
| 		t.Error("expected bytes32[31] to be 1 got", bytes32[31]) | ||||
| 	} | ||||
| 
 | ||||
| 	output[10] = 1 | ||||
| 	var shortAssignLong [32]byte | ||||
| 	err = abi.Unpack(&shortAssignLong, "bytes10", output) | ||||
| 	if err != nil { | ||||
| 		t.Error("didn't expect error:", err) | ||||
| 	} | ||||
| 	if !bytes.Equal(output, shortAssignLong[:]) { | ||||
| 		t.Errorf("expected %x to be %x", shortAssignLong, output) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestUnmarshal(t *testing.T) { | ||||
| 	const definition = `[ | ||||
| 	{ "name" : "int", "constant" : false, "outputs": [ { "type": "uint256" } ] }, | ||||
| 	{ "name" : "bool", "constant" : false, "outputs": [ { "type": "bool" } ] }, | ||||
| 	{ "name" : "bytes", "constant" : false, "outputs": [ { "type": "bytes" } ] }, | ||||
| 	{ "name" : "fixed", "constant" : false, "outputs": [ { "type": "bytes32" } ] }, | ||||
| 	{ "name" : "multi", "constant" : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] }, | ||||
| 	{ "name" : "intArraySingle", "constant" : false, "outputs": [ { "type": "uint256[3]" } ] }, | ||||
| 	{ "name" : "addressSliceSingle", "constant" : false, "outputs": [ { "type": "address[]" } ] }, | ||||
| 	{ "name" : "addressSliceDouble", "constant" : false, "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] }, | ||||
| 	{ "name" : "mixedBytes", "constant" : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]` | ||||
| 
 | ||||
| 	abi, err := JSON(strings.NewReader(definition)) | ||||
| 	if err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
| 	buff := new(bytes.Buffer) | ||||
| 
 | ||||
| 	// marshal int
 | ||||
| 	var Int *big.Int | ||||
| 	err = abi.Unpack(&Int, "int", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
| 
 | ||||
| 	if Int == nil || Int.Cmp(big.NewInt(1)) != 0 { | ||||
| 		t.Error("expected Int to be 1 got", Int) | ||||
| 	} | ||||
| 
 | ||||
| 	// marshal bool
 | ||||
| 	var Bool bool | ||||
| 	err = abi.Unpack(&Bool, "bool", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
| 
 | ||||
| 	if !Bool { | ||||
| 		t.Error("expected Bool to be true") | ||||
| 	} | ||||
| 
 | ||||
| 	// marshal dynamic bytes max length 32
 | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) | ||||
| 	bytesOut := common.RightPadBytes([]byte("hello"), 32) | ||||
| 	buff.Write(bytesOut) | ||||
| 
 | ||||
| 	var Bytes []byte | ||||
| 	err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
| 
 | ||||
| 	if !bytes.Equal(Bytes, bytesOut) { | ||||
| 		t.Errorf("expected %x got %x", bytesOut, Bytes) | ||||
| 	} | ||||
| 
 | ||||
| 	// marshall dynamic bytes max length 64
 | ||||
| 	buff.Reset() | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) | ||||
| 	bytesOut = common.RightPadBytes([]byte("hello"), 64) | ||||
| 	buff.Write(bytesOut) | ||||
| 
 | ||||
| 	err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
| 
 | ||||
| 	if !bytes.Equal(Bytes, bytesOut) { | ||||
| 		t.Errorf("expected %x got %x", bytesOut, Bytes) | ||||
| 	} | ||||
| 
 | ||||
| 	// marshall dynamic bytes max length 63
 | ||||
| 	buff.Reset() | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) | ||||
| 	buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000003f")) | ||||
| 	bytesOut = common.RightPadBytes([]byte("hello"), 63) | ||||
| 	buff.Write(bytesOut) | ||||
| 
 | ||||
| 	err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
| 
 | ||||
| 	if !bytes.Equal(Bytes, bytesOut) { | ||||
| 		t.Errorf("expected %x got %x", bytesOut, Bytes) | ||||
| 	} | ||||
| 
 | ||||
| 	// marshal dynamic bytes output empty
 | ||||
| 	err = abi.Unpack(&Bytes, "bytes", nil) | ||||
| 	if err == nil { | ||||
| 		t.Error("expected error") | ||||
| 	} | ||||
| 
 | ||||
| 	// marshal dynamic bytes length 5
 | ||||
| 	buff.Reset() | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005")) | ||||
| 	buff.Write(common.RightPadBytes([]byte("hello"), 32)) | ||||
| 
 | ||||
| 	err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
| 
 | ||||
| 	if !bytes.Equal(Bytes, []byte("hello")) { | ||||
| 		t.Errorf("expected %x got %x", bytesOut, Bytes) | ||||
| 	} | ||||
| 
 | ||||
| 	// marshal dynamic bytes length 5
 | ||||
| 	buff.Reset() | ||||
| 	buff.Write(common.RightPadBytes([]byte("hello"), 32)) | ||||
| 
 | ||||
| 	var hash common.Hash | ||||
| 	err = abi.Unpack(&hash, "fixed", buff.Bytes()) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
| 
 | ||||
| 	helloHash := common.BytesToHash(common.RightPadBytes([]byte("hello"), 32)) | ||||
| 	if hash != helloHash { | ||||
| 		t.Errorf("Expected %x to equal %x", hash, helloHash) | ||||
| 	} | ||||
| 
 | ||||
| 	// marshal error
 | ||||
| 	buff.Reset() | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) | ||||
| 	err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) | ||||
| 	if err == nil { | ||||
| 		t.Error("expected error") | ||||
| 	} | ||||
| 
 | ||||
| 	err = abi.Unpack(&Bytes, "multi", make([]byte, 64)) | ||||
| 	if err == nil { | ||||
| 		t.Error("expected error") | ||||
| 	} | ||||
| 
 | ||||
| 	// marshal mixed bytes
 | ||||
| 	buff.Reset() | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) | ||||
| 	fixed := common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001") | ||||
| 	buff.Write(fixed) | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) | ||||
| 	bytesOut = common.RightPadBytes([]byte("hello"), 32) | ||||
| 	buff.Write(bytesOut) | ||||
| 
 | ||||
| 	var out []interface{} | ||||
| 	err = abi.Unpack(&out, "mixedBytes", buff.Bytes()) | ||||
| 	if err != nil { | ||||
| 		t.Fatal("didn't expect error:", err) | ||||
| 	} | ||||
| 
 | ||||
| 	if !bytes.Equal(bytesOut, out[0].([]byte)) { | ||||
| 		t.Errorf("expected %x, got %x", bytesOut, out[0]) | ||||
| 	} | ||||
| 
 | ||||
| 	if !bytes.Equal(fixed, out[1].([]byte)) { | ||||
| 		t.Errorf("expected %x, got %x", fixed, out[1]) | ||||
| 	} | ||||
| 
 | ||||
| 	buff.Reset() | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003")) | ||||
| 	// marshal int array
 | ||||
| 	var intArray [3]*big.Int | ||||
| 	err = abi.Unpack(&intArray, "intArraySingle", buff.Bytes()) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
| 	var testAgainstIntArray [3]*big.Int | ||||
| 	testAgainstIntArray[0] = big.NewInt(1) | ||||
| 	testAgainstIntArray[1] = big.NewInt(2) | ||||
| 	testAgainstIntArray[2] = big.NewInt(3) | ||||
| 
 | ||||
| 	for i, Int := range intArray { | ||||
| 		if Int.Cmp(testAgainstIntArray[i]) != 0 { | ||||
| 			t.Errorf("expected %v, got %v", testAgainstIntArray[i], Int) | ||||
| 		} | ||||
| 	} | ||||
| 	// marshal address slice
 | ||||
| 	buff.Reset() | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) // offset
 | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
 | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000")) | ||||
| 
 | ||||
| 	var outAddr []common.Address | ||||
| 	err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes()) | ||||
| 	if err != nil { | ||||
| 		t.Fatal("didn't expect error:", err) | ||||
| 	} | ||||
| 
 | ||||
| 	if len(outAddr) != 1 { | ||||
| 		t.Fatal("expected 1 item, got", len(outAddr)) | ||||
| 	} | ||||
| 
 | ||||
| 	if outAddr[0] != (common.Address{1}) { | ||||
| 		t.Errorf("expected %x, got %x", common.Address{1}, outAddr[0]) | ||||
| 	} | ||||
| 
 | ||||
| 	// marshal multiple address slice
 | ||||
| 	buff.Reset() | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // offset
 | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // offset
 | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
 | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000")) | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // size
 | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000200000000000000000000000000000000000000")) | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000300000000000000000000000000000000000000")) | ||||
| 
 | ||||
| 	var outAddrStruct struct { | ||||
| 		A []common.Address | ||||
| 		B []common.Address | ||||
| 	} | ||||
| 	err = abi.Unpack(&outAddrStruct, "addressSliceDouble", buff.Bytes()) | ||||
| 	if err != nil { | ||||
| 		t.Fatal("didn't expect error:", err) | ||||
| 	} | ||||
| 
 | ||||
| 	if len(outAddrStruct.A) != 1 { | ||||
| 		t.Fatal("expected 1 item, got", len(outAddrStruct.A)) | ||||
| 	} | ||||
| 
 | ||||
| 	if outAddrStruct.A[0] != (common.Address{1}) { | ||||
| 		t.Errorf("expected %x, got %x", common.Address{1}, outAddrStruct.A[0]) | ||||
| 	} | ||||
| 
 | ||||
| 	if len(outAddrStruct.B) != 2 { | ||||
| 		t.Fatal("expected 1 item, got", len(outAddrStruct.B)) | ||||
| 	} | ||||
| 
 | ||||
| 	if outAddrStruct.B[0] != (common.Address{2}) { | ||||
| 		t.Errorf("expected %x, got %x", common.Address{2}, outAddrStruct.B[0]) | ||||
| 	} | ||||
| 	if outAddrStruct.B[1] != (common.Address{3}) { | ||||
| 		t.Errorf("expected %x, got %x", common.Address{3}, outAddrStruct.B[1]) | ||||
| 	} | ||||
| 
 | ||||
| 	// marshal invalid address slice
 | ||||
| 	buff.Reset() | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000100")) | ||||
| 
 | ||||
| 	err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes()) | ||||
| 	if err == nil { | ||||
| 		t.Fatal("expected error:", err) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @ -17,10 +17,15 @@ | ||||
| package abi | ||||
| 
 | ||||
| import ( | ||||
| 	"errors" | ||||
| 	"fmt" | ||||
| 	"reflect" | ||||
| ) | ||||
| 
 | ||||
| var ( | ||||
| 	errBadBool = errors.New("abi: improperly encoded boolean value") | ||||
| ) | ||||
| 
 | ||||
| // formatSliceString formats the reflection kind with the given slice size
 | ||||
| // and returns a formatted string representation.
 | ||||
| func formatSliceString(kind reflect.Kind, sliceSize int) string { | ||||
|  | ||||
| @ -39,7 +39,7 @@ type Method struct { | ||||
| 	Outputs []Argument | ||||
| } | ||||
| 
 | ||||
| func (m Method) pack(method Method, args ...interface{}) ([]byte, error) { | ||||
| func (method Method) pack(args ...interface{}) ([]byte, error) { | ||||
| 	// Make sure arguments match up and pack them
 | ||||
| 	if len(args) != len(method.Inputs) { | ||||
| 		return nil, fmt.Errorf("argument count mismatch: %d for %d", len(args), len(method.Inputs)) | ||||
|  | ||||
| @ -62,19 +62,6 @@ func U256(n *big.Int) []byte { | ||||
| 	return math.PaddedBigBytes(math.U256(n), 32) | ||||
| } | ||||
| 
 | ||||
| // packNum packs the given number (using the reflect value) and will cast it to appropriate number representation
 | ||||
| func packNum(value reflect.Value) []byte { | ||||
| 	switch kind := value.Kind(); kind { | ||||
| 	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: | ||||
| 		return U256(new(big.Int).SetUint64(value.Uint())) | ||||
| 	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | ||||
| 		return U256(big.NewInt(value.Int())) | ||||
| 	case reflect.Ptr: | ||||
| 		return U256(value.Interface().(*big.Int)) | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
| 
 | ||||
| // checks whether the given reflect value is signed. This also works for slices with a number type
 | ||||
| func isSigned(v reflect.Value) bool { | ||||
| 	switch v.Type() { | ||||
|  | ||||
| @ -18,7 +18,6 @@ package abi | ||||
| 
 | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"math" | ||||
| 	"math/big" | ||||
| 	"reflect" | ||||
| 	"testing" | ||||
| @ -34,43 +33,6 @@ func TestNumberTypes(t *testing.T) { | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestPackNumber(t *testing.T) { | ||||
| 	tests := []struct { | ||||
| 		value  reflect.Value | ||||
| 		packed []byte | ||||
| 	}{ | ||||
| 		// Protocol limits
 | ||||
| 		{reflect.ValueOf(0), []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, | ||||
| 		{reflect.ValueOf(1), []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}}, | ||||
| 		{reflect.ValueOf(-1), []byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}}, | ||||
| 
 | ||||
| 		// Type corner cases
 | ||||
| 		{reflect.ValueOf(uint8(math.MaxUint8)), []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255}}, | ||||
| 		{reflect.ValueOf(uint16(math.MaxUint16)), []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255}}, | ||||
| 		{reflect.ValueOf(uint32(math.MaxUint32)), []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255}}, | ||||
| 		{reflect.ValueOf(uint64(math.MaxUint64)), []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255}}, | ||||
| 
 | ||||
| 		{reflect.ValueOf(int8(math.MaxInt8)), []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 127}}, | ||||
| 		{reflect.ValueOf(int16(math.MaxInt16)), []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 127, 255}}, | ||||
| 		{reflect.ValueOf(int32(math.MaxInt32)), []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 127, 255, 255, 255}}, | ||||
| 		{reflect.ValueOf(int64(math.MaxInt64)), []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 127, 255, 255, 255, 255, 255, 255, 255}}, | ||||
| 
 | ||||
| 		{reflect.ValueOf(int8(math.MinInt8)), []byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 128}}, | ||||
| 		{reflect.ValueOf(int16(math.MinInt16)), []byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 128, 0}}, | ||||
| 		{reflect.ValueOf(int32(math.MinInt32)), []byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 128, 0, 0, 0}}, | ||||
| 		{reflect.ValueOf(int64(math.MinInt64)), []byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 128, 0, 0, 0, 0, 0, 0, 0}}, | ||||
| 	} | ||||
| 	for i, tt := range tests { | ||||
| 		packed := packNum(tt.value) | ||||
| 		if !bytes.Equal(packed, tt.packed) { | ||||
| 			t.Errorf("test %d: pack mismatch: have %x, want %x", i, packed, tt.packed) | ||||
| 		} | ||||
| 	} | ||||
| 	if packed := packNum(reflect.ValueOf("string")); packed != nil { | ||||
| 		t.Errorf("expected 'string' to pack to nil. got %x instead", packed) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestSigned(t *testing.T) { | ||||
| 	if isSigned(reflect.ValueOf(uint(10))) { | ||||
| 		t.Error("signed") | ||||
|  | ||||
| @ -17,6 +17,7 @@ | ||||
| package abi | ||||
| 
 | ||||
| import ( | ||||
| 	"math/big" | ||||
| 	"reflect" | ||||
| 
 | ||||
| 	"github.com/ethereum/go-ethereum/common" | ||||
| @ -59,8 +60,20 @@ func packElement(t Type, reflectValue reflect.Value) []byte { | ||||
| 		if reflectValue.Kind() == reflect.Array { | ||||
| 			reflectValue = mustArrayToByteSlice(reflectValue) | ||||
| 		} | ||||
| 
 | ||||
| 		return common.RightPadBytes(reflectValue.Bytes(), 32) | ||||
| 	} | ||||
| 	panic("abi: fatal error") | ||||
| } | ||||
| 
 | ||||
| // packNum packs the given number (using the reflect value) and will cast it to appropriate number representation
 | ||||
| func packNum(value reflect.Value) []byte { | ||||
| 	switch kind := value.Kind(); kind { | ||||
| 	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: | ||||
| 		return U256(new(big.Int).SetUint64(value.Uint())) | ||||
| 	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: | ||||
| 		return U256(big.NewInt(value.Int())) | ||||
| 	case reflect.Ptr: | ||||
| 		return U256(value.Interface().(*big.Int)) | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
							
								
								
									
										441
									
								
								accounts/abi/pack_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										441
									
								
								accounts/abi/pack_test.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,441 @@ | ||||
| // Copyright 2015 The go-ethereum Authors
 | ||||
| // This file is part of the go-ethereum library.
 | ||||
| //
 | ||||
| // The go-ethereum library is free software: you can redistribute it and/or modify
 | ||||
| // it under the terms of the GNU Lesser General Public License as published by
 | ||||
| // the Free Software Foundation, either version 3 of the License, or
 | ||||
| // (at your option) any later version.
 | ||||
| //
 | ||||
| // The go-ethereum library is distributed in the hope that it will be useful,
 | ||||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | ||||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 | ||||
| // GNU Lesser General Public License for more details.
 | ||||
| //
 | ||||
| // You should have received a copy of the GNU Lesser General Public License
 | ||||
| // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
 | ||||
| 
 | ||||
| package abi | ||||
| 
 | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"math" | ||||
| 	"math/big" | ||||
| 	"reflect" | ||||
| 	"strings" | ||||
| 	"testing" | ||||
| 
 | ||||
| 	"github.com/ethereum/go-ethereum/common" | ||||
| ) | ||||
| 
 | ||||
| func TestPack(t *testing.T) { | ||||
| 	for i, test := range []struct { | ||||
| 		typ string | ||||
| 
 | ||||
| 		input  interface{} | ||||
| 		output []byte | ||||
| 	}{ | ||||
| 		{ | ||||
| 			"uint8", | ||||
| 			uint8(2), | ||||
| 			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"uint8[]", | ||||
| 			[]uint8{1, 2}, | ||||
| 			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"uint16", | ||||
| 			uint16(2), | ||||
| 			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"uint16[]", | ||||
| 			[]uint16{1, 2}, | ||||
| 			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"uint32", | ||||
| 			uint32(2), | ||||
| 			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"uint32[]", | ||||
| 			[]uint32{1, 2}, | ||||
| 			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"uint64", | ||||
| 			uint64(2), | ||||
| 			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"uint64[]", | ||||
| 			[]uint64{1, 2}, | ||||
| 			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"uint256", | ||||
| 			big.NewInt(2), | ||||
| 			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"uint256[]", | ||||
| 			[]*big.Int{big.NewInt(1), big.NewInt(2)}, | ||||
| 			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"int8", | ||||
| 			int8(2), | ||||
| 			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"int8[]", | ||||
| 			[]int8{1, 2}, | ||||
| 			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"int16", | ||||
| 			int16(2), | ||||
| 			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"int16[]", | ||||
| 			[]int16{1, 2}, | ||||
| 			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"int32", | ||||
| 			int32(2), | ||||
| 			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"int32[]", | ||||
| 			[]int32{1, 2}, | ||||
| 			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"int64", | ||||
| 			int64(2), | ||||
| 			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"int64[]", | ||||
| 			[]int64{1, 2}, | ||||
| 			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"int256", | ||||
| 			big.NewInt(2), | ||||
| 			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"int256[]", | ||||
| 			[]*big.Int{big.NewInt(1), big.NewInt(2)}, | ||||
| 			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"bytes1", | ||||
| 			[1]byte{1}, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"bytes2", | ||||
| 			[2]byte{1}, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"bytes3", | ||||
| 			[3]byte{1}, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"bytes4", | ||||
| 			[4]byte{1}, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"bytes5", | ||||
| 			[5]byte{1}, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"bytes6", | ||||
| 			[6]byte{1}, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"bytes7", | ||||
| 			[7]byte{1}, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"bytes8", | ||||
| 			[8]byte{1}, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"bytes9", | ||||
| 			[9]byte{1}, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"bytes10", | ||||
| 			[10]byte{1}, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"bytes11", | ||||
| 			[11]byte{1}, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"bytes12", | ||||
| 			[12]byte{1}, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"bytes13", | ||||
| 			[13]byte{1}, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"bytes14", | ||||
| 			[14]byte{1}, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"bytes15", | ||||
| 			[15]byte{1}, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"bytes16", | ||||
| 			[16]byte{1}, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"bytes17", | ||||
| 			[17]byte{1}, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"bytes18", | ||||
| 			[18]byte{1}, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"bytes19", | ||||
| 			[19]byte{1}, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"bytes20", | ||||
| 			[20]byte{1}, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"bytes21", | ||||
| 			[21]byte{1}, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"bytes22", | ||||
| 			[22]byte{1}, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"bytes23", | ||||
| 			[23]byte{1}, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"bytes24", | ||||
| 			[24]byte{1}, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"bytes24", | ||||
| 			[24]byte{1}, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"bytes25", | ||||
| 			[25]byte{1}, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"bytes26", | ||||
| 			[26]byte{1}, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"bytes27", | ||||
| 			[27]byte{1}, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"bytes28", | ||||
| 			[28]byte{1}, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"bytes29", | ||||
| 			[29]byte{1}, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"bytes30", | ||||
| 			[30]byte{1}, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"bytes31", | ||||
| 			[31]byte{1}, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"bytes32", | ||||
| 			[32]byte{1}, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"address[]", | ||||
| 			[]common.Address{{1}, {2}}, | ||||
| 			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000200000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"bytes32[]", | ||||
| 			[]common.Hash{{1}, {2}}, | ||||
| 			common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000201000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"function", | ||||
| 			[24]byte{1}, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 		{ | ||||
| 			"string", | ||||
| 			"foobar", | ||||
| 			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000006666f6f6261720000000000000000000000000000000000000000000000000000"), | ||||
| 		}, | ||||
| 	} { | ||||
| 		typ, err := NewType(test.typ) | ||||
| 		if err != nil { | ||||
| 			t.Fatal("unexpected parse error:", err) | ||||
| 		} | ||||
| 
 | ||||
| 		output, err := typ.pack(reflect.ValueOf(test.input)) | ||||
| 		if err != nil { | ||||
| 			t.Fatal("unexpected pack error:", err) | ||||
| 		} | ||||
| 
 | ||||
| 		if !bytes.Equal(output, test.output) { | ||||
| 			t.Errorf("%d failed. Expected bytes: '%x' Got: '%x'", i, test.output, output) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestMethodPack(t *testing.T) { | ||||
| 	abi, err := JSON(strings.NewReader(jsondata2)) | ||||
| 	if err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
| 
 | ||||
| 	sig := abi.Methods["slice"].Id() | ||||
| 	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) | ||||
| 	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) | ||||
| 
 | ||||
| 	packed, err := abi.Pack("slice", []uint32{1, 2}) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
| 
 | ||||
| 	if !bytes.Equal(packed, sig) { | ||||
| 		t.Errorf("expected %x got %x", sig, packed) | ||||
| 	} | ||||
| 
 | ||||
| 	var addrA, addrB = common.Address{1}, common.Address{2} | ||||
| 	sig = abi.Methods["sliceAddress"].Id() | ||||
| 	sig = append(sig, common.LeftPadBytes([]byte{32}, 32)...) | ||||
| 	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) | ||||
| 	sig = append(sig, common.LeftPadBytes(addrA[:], 32)...) | ||||
| 	sig = append(sig, common.LeftPadBytes(addrB[:], 32)...) | ||||
| 
 | ||||
| 	packed, err = abi.Pack("sliceAddress", []common.Address{addrA, addrB}) | ||||
| 	if err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
| 	if !bytes.Equal(packed, sig) { | ||||
| 		t.Errorf("expected %x got %x", sig, packed) | ||||
| 	} | ||||
| 
 | ||||
| 	var addrC, addrD = common.Address{3}, common.Address{4} | ||||
| 	sig = abi.Methods["sliceMultiAddress"].Id() | ||||
| 	sig = append(sig, common.LeftPadBytes([]byte{64}, 32)...) | ||||
| 	sig = append(sig, common.LeftPadBytes([]byte{160}, 32)...) | ||||
| 	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) | ||||
| 	sig = append(sig, common.LeftPadBytes(addrA[:], 32)...) | ||||
| 	sig = append(sig, common.LeftPadBytes(addrB[:], 32)...) | ||||
| 	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) | ||||
| 	sig = append(sig, common.LeftPadBytes(addrC[:], 32)...) | ||||
| 	sig = append(sig, common.LeftPadBytes(addrD[:], 32)...) | ||||
| 
 | ||||
| 	packed, err = abi.Pack("sliceMultiAddress", []common.Address{addrA, addrB}, []common.Address{addrC, addrD}) | ||||
| 	if err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
| 	if !bytes.Equal(packed, sig) { | ||||
| 		t.Errorf("expected %x got %x", sig, packed) | ||||
| 	} | ||||
| 
 | ||||
| 	sig = abi.Methods["slice256"].Id() | ||||
| 	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) | ||||
| 	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) | ||||
| 
 | ||||
| 	packed, err = abi.Pack("slice256", []*big.Int{big.NewInt(1), big.NewInt(2)}) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
| 
 | ||||
| 	if !bytes.Equal(packed, sig) { | ||||
| 		t.Errorf("expected %x got %x", sig, packed) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestPackNumber(t *testing.T) { | ||||
| 	tests := []struct { | ||||
| 		value  reflect.Value | ||||
| 		packed []byte | ||||
| 	}{ | ||||
| 		// Protocol limits
 | ||||
| 		{reflect.ValueOf(0), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000000")}, | ||||
| 		{reflect.ValueOf(1), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")}, | ||||
| 		{reflect.ValueOf(-1), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")}, | ||||
| 
 | ||||
| 		// Type corner cases
 | ||||
| 		{reflect.ValueOf(uint8(math.MaxUint8)), common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000ff")}, | ||||
| 		{reflect.ValueOf(uint16(math.MaxUint16)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000ffff")}, | ||||
| 		{reflect.ValueOf(uint32(math.MaxUint32)), common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000ffffffff")}, | ||||
| 		{reflect.ValueOf(uint64(math.MaxUint64)), common.Hex2Bytes("000000000000000000000000000000000000000000000000ffffffffffffffff")}, | ||||
| 
 | ||||
| 		{reflect.ValueOf(int8(math.MaxInt8)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000007f")}, | ||||
| 		{reflect.ValueOf(int16(math.MaxInt16)), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000007fff")}, | ||||
| 		{reflect.ValueOf(int32(math.MaxInt32)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000007fffffff")}, | ||||
| 		{reflect.ValueOf(int64(math.MaxInt64)), common.Hex2Bytes("0000000000000000000000000000000000000000000000007fffffffffffffff")}, | ||||
| 
 | ||||
| 		{reflect.ValueOf(int8(math.MinInt8)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80")}, | ||||
| 		{reflect.ValueOf(int16(math.MinInt16)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8000")}, | ||||
| 		{reflect.ValueOf(int32(math.MinInt32)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000")}, | ||||
| 		{reflect.ValueOf(int64(math.MinInt64)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffff8000000000000000")}, | ||||
| 	} | ||||
| 	for i, tt := range tests { | ||||
| 		packed := packNum(tt.value) | ||||
| 		if !bytes.Equal(packed, tt.packed) { | ||||
| 			t.Errorf("test %d: pack mismatch: have %x, want %x", i, packed, tt.packed) | ||||
| 		} | ||||
| 	} | ||||
| 	if packed := packNum(reflect.ValueOf("string")); packed != nil { | ||||
| 		t.Errorf("expected 'string' to pack to nil. got %x instead", packed) | ||||
| 	} | ||||
| } | ||||
| @ -32,30 +32,30 @@ func indirect(v reflect.Value) reflect.Value { | ||||
| 
 | ||||
| // reflectIntKind returns the reflect using the given size and
 | ||||
| // unsignedness.
 | ||||
| func reflectIntKind(unsigned bool, size int) reflect.Kind { | ||||
| func reflectIntKindAndType(unsigned bool, size int) (reflect.Kind, reflect.Type) { | ||||
| 	switch size { | ||||
| 	case 8: | ||||
| 		if unsigned { | ||||
| 			return reflect.Uint8 | ||||
| 			return reflect.Uint8, uint8_t | ||||
| 		} | ||||
| 		return reflect.Int8 | ||||
| 		return reflect.Int8, int8_t | ||||
| 	case 16: | ||||
| 		if unsigned { | ||||
| 			return reflect.Uint16 | ||||
| 			return reflect.Uint16, uint16_t | ||||
| 		} | ||||
| 		return reflect.Int16 | ||||
| 		return reflect.Int16, int16_t | ||||
| 	case 32: | ||||
| 		if unsigned { | ||||
| 			return reflect.Uint32 | ||||
| 			return reflect.Uint32, uint32_t | ||||
| 		} | ||||
| 		return reflect.Int32 | ||||
| 		return reflect.Int32, int32_t | ||||
| 	case 64: | ||||
| 		if unsigned { | ||||
| 			return reflect.Uint64 | ||||
| 			return reflect.Uint64, uint64_t | ||||
| 		} | ||||
| 		return reflect.Int64 | ||||
| 		return reflect.Int64, int64_t | ||||
| 	} | ||||
| 	return reflect.Ptr | ||||
| 	return reflect.Ptr, big_t | ||||
| } | ||||
| 
 | ||||
| // mustArrayToBytesSlice creates a new byte slice with the exact same size as value
 | ||||
|  | ||||
| @ -33,7 +33,7 @@ const ( | ||||
| 	FixedBytesTy | ||||
| 	BytesTy | ||||
| 	HashTy | ||||
| 	FixedpointTy | ||||
| 	FixedPointTy | ||||
| 	FunctionTy | ||||
| ) | ||||
| 
 | ||||
| @ -126,13 +126,11 @@ func NewType(t string) (typ Type, err error) { | ||||
| 
 | ||||
| 	switch varType { | ||||
| 	case "int": | ||||
| 		typ.Kind = reflectIntKind(false, varSize) | ||||
| 		typ.Type = big_t | ||||
| 		typ.Kind, typ.Type = reflectIntKindAndType(false, varSize) | ||||
| 		typ.Size = varSize | ||||
| 		typ.T = IntTy | ||||
| 	case "uint": | ||||
| 		typ.Kind = reflectIntKind(true, varSize) | ||||
| 		typ.Type = ubig_t | ||||
| 		typ.Kind, typ.Type = reflectIntKindAndType(true, varSize) | ||||
| 		typ.Size = varSize | ||||
| 		typ.T = UintTy | ||||
| 	case "bool": | ||||
|  | ||||
| @ -17,8 +17,11 @@ | ||||
| package abi | ||||
| 
 | ||||
| import ( | ||||
| 	"math/big" | ||||
| 	"reflect" | ||||
| 	"testing" | ||||
| 
 | ||||
| 	"github.com/ethereum/go-ethereum/common" | ||||
| ) | ||||
| 
 | ||||
| // typeWithoutStringer is a alias for the Type type which simply doesn't implement
 | ||||
| @ -31,26 +34,44 @@ func TestTypeRegexp(t *testing.T) { | ||||
| 		blob string | ||||
| 		kind Type | ||||
| 	}{ | ||||
| 		{"int", Type{Kind: reflect.Ptr, Type: big_t, Size: 256, T: IntTy, stringKind: "int256"}}, | ||||
| 		{"int8", Type{Kind: reflect.Int8, Type: big_t, Size: 8, T: IntTy, stringKind: "int8"}}, | ||||
| 		{"bool", Type{Kind: reflect.Bool, T: BoolTy, stringKind: "bool"}}, | ||||
| 		{"bool[]", Type{IsSlice: true, SliceSize: -1, Kind: reflect.Bool, T: BoolTy, Elem: &Type{Kind: reflect.Bool, T: BoolTy, stringKind: "bool"}, stringKind: "bool[]"}}, | ||||
| 		{"bool[2]", Type{IsArray: true, SliceSize: 2, Kind: reflect.Bool, T: BoolTy, Elem: &Type{Kind: reflect.Bool, T: BoolTy, stringKind: "bool"}, stringKind: "bool[2]"}}, | ||||
| 		{"int8", Type{Kind: reflect.Int8, Type: int8_t, Size: 8, T: IntTy, stringKind: "int8"}}, | ||||
| 		{"int16", Type{Kind: reflect.Int16, Type: int16_t, Size: 16, T: IntTy, stringKind: "int16"}}, | ||||
| 		{"int32", Type{Kind: reflect.Int32, Type: int32_t, Size: 32, T: IntTy, stringKind: "int32"}}, | ||||
| 		{"int64", Type{Kind: reflect.Int64, Type: int64_t, Size: 64, T: IntTy, stringKind: "int64"}}, | ||||
| 		{"int256", Type{Kind: reflect.Ptr, Type: big_t, Size: 256, T: IntTy, stringKind: "int256"}}, | ||||
| 		{"int[]", Type{IsSlice: true, SliceSize: -1, Kind: reflect.Ptr, Type: big_t, Size: 256, T: IntTy, Elem: &Type{Kind: reflect.Ptr, Type: big_t, Size: 256, T: IntTy, stringKind: "int256"}, stringKind: "int256[]"}}, | ||||
| 		{"int[2]", Type{IsArray: true, SliceSize: 2, Kind: reflect.Ptr, Type: big_t, Size: 256, T: IntTy, Elem: &Type{Kind: reflect.Ptr, Type: big_t, Size: 256, T: IntTy, stringKind: "int256"}, stringKind: "int256[2]"}}, | ||||
| 		{"int32[]", Type{IsSlice: true, SliceSize: -1, Kind: reflect.Int32, Type: big_t, Size: 32, T: IntTy, Elem: &Type{Kind: reflect.Int32, Type: big_t, Size: 32, T: IntTy, stringKind: "int32"}, stringKind: "int32[]"}}, | ||||
| 		{"int32[2]", Type{IsArray: true, SliceSize: 2, Kind: reflect.Int32, Type: big_t, Size: 32, T: IntTy, Elem: &Type{Kind: reflect.Int32, Type: big_t, Size: 32, T: IntTy, stringKind: "int32"}, stringKind: "int32[2]"}}, | ||||
| 		{"uint", Type{Kind: reflect.Ptr, Type: ubig_t, Size: 256, T: UintTy, stringKind: "uint256"}}, | ||||
| 		{"uint8", Type{Kind: reflect.Uint8, Type: ubig_t, Size: 8, T: UintTy, stringKind: "uint8"}}, | ||||
| 		{"uint256", Type{Kind: reflect.Ptr, Type: ubig_t, Size: 256, T: UintTy, stringKind: "uint256"}}, | ||||
| 		{"uint[]", Type{IsSlice: true, SliceSize: -1, Kind: reflect.Ptr, Type: ubig_t, Size: 256, T: UintTy, Elem: &Type{Kind: reflect.Ptr, Type: ubig_t, Size: 256, T: UintTy, stringKind: "uint256"}, stringKind: "uint256[]"}}, | ||||
| 		{"uint[2]", Type{IsArray: true, SliceSize: 2, Kind: reflect.Ptr, Type: ubig_t, Size: 256, T: UintTy, Elem: &Type{Kind: reflect.Ptr, Type: ubig_t, Size: 256, T: UintTy, stringKind: "uint256"}, stringKind: "uint256[2]"}}, | ||||
| 		{"uint32[]", Type{IsSlice: true, SliceSize: -1, Kind: reflect.Uint32, Type: ubig_t, Size: 32, T: UintTy, Elem: &Type{Kind: reflect.Uint32, Type: big_t, Size: 32, T: UintTy, stringKind: "uint32"}, stringKind: "uint32[]"}}, | ||||
| 		{"uint32[2]", Type{IsArray: true, SliceSize: 2, Kind: reflect.Uint32, Type: ubig_t, Size: 32, T: UintTy, Elem: &Type{Kind: reflect.Uint32, Type: big_t, Size: 32, T: UintTy, stringKind: "uint32"}, stringKind: "uint32[2]"}}, | ||||
| 		{"bytes", Type{IsSlice: true, SliceSize: -1, Elem: &Type{Kind: reflect.Uint8, Type: ubig_t, Size: 8, T: UintTy, stringKind: "uint8"}, T: BytesTy, stringKind: "bytes"}}, | ||||
| 		{"bytes32", Type{IsArray: true, SliceSize: 32, Elem: &Type{Kind: reflect.Uint8, Type: ubig_t, Size: 8, T: UintTy, stringKind: "uint8"}, T: FixedBytesTy, stringKind: "bytes32"}}, | ||||
| 		{"bytes[]", Type{IsSlice: true, SliceSize: -1, Elem: &Type{IsSlice: true, SliceSize: -1, Elem: &Type{Kind: reflect.Uint8, Type: ubig_t, Size: 8, T: UintTy, stringKind: "uint8"}, T: BytesTy, stringKind: "bytes"}, stringKind: "bytes[]"}}, | ||||
| 		{"bytes[2]", Type{IsArray: true, SliceSize: 2, Elem: &Type{IsSlice: true, SliceSize: -1, Elem: &Type{Kind: reflect.Uint8, Type: ubig_t, Size: 8, T: UintTy, stringKind: "uint8"}, T: BytesTy, stringKind: "bytes"}, stringKind: "bytes[2]"}}, | ||||
| 		{"bytes32[]", Type{IsSlice: true, SliceSize: -1, Elem: &Type{IsArray: true, SliceSize: 32, Elem: &Type{Kind: reflect.Uint8, Type: ubig_t, Size: 8, T: UintTy, stringKind: "uint8"}, T: FixedBytesTy, stringKind: "bytes32"}, stringKind: "bytes32[]"}}, | ||||
| 		{"bytes32[2]", Type{IsArray: true, SliceSize: 2, Elem: &Type{IsArray: true, SliceSize: 32, Elem: &Type{Kind: reflect.Uint8, Type: ubig_t, Size: 8, T: UintTy, stringKind: "uint8"}, T: FixedBytesTy, stringKind: "bytes32"}, stringKind: "bytes32[2]"}}, | ||||
| 		{"int8[]", Type{IsSlice: true, SliceSize: -1, Kind: reflect.Int8, Type: int8_t, Size: 8, T: IntTy, Elem: &Type{Kind: reflect.Int8, Type: int8_t, Size: 8, T: IntTy, stringKind: "int8"}, stringKind: "int8[]"}}, | ||||
| 		{"int8[2]", Type{IsArray: true, SliceSize: 2, Kind: reflect.Int8, Type: int8_t, Size: 8, T: IntTy, Elem: &Type{Kind: reflect.Int8, Type: int8_t, Size: 8, T: IntTy, stringKind: "int8"}, stringKind: "int8[2]"}}, | ||||
| 		{"int16[]", Type{IsSlice: true, SliceSize: -1, Kind: reflect.Int16, Type: int16_t, Size: 16, T: IntTy, Elem: &Type{Kind: reflect.Int16, Type: int16_t, Size: 16, T: IntTy, stringKind: "int16"}, stringKind: "int16[]"}}, | ||||
| 		{"int16[2]", Type{IsArray: true, SliceSize: 2, Kind: reflect.Int16, Type: int16_t, Size: 16, T: IntTy, Elem: &Type{Kind: reflect.Int16, Type: int16_t, Size: 16, T: IntTy, stringKind: "int16"}, stringKind: "int16[2]"}}, | ||||
| 		{"int32[]", Type{IsSlice: true, SliceSize: -1, Kind: reflect.Int32, Type: int32_t, Size: 32, T: IntTy, Elem: &Type{Kind: reflect.Int32, Type: int32_t, Size: 32, T: IntTy, stringKind: "int32"}, stringKind: "int32[]"}}, | ||||
| 		{"int32[2]", Type{IsArray: true, SliceSize: 2, Kind: reflect.Int32, Type: int32_t, Size: 32, T: IntTy, Elem: &Type{Kind: reflect.Int32, Type: int32_t, Size: 32, T: IntTy, stringKind: "int32"}, stringKind: "int32[2]"}}, | ||||
| 		{"int64[]", Type{IsSlice: true, SliceSize: -1, Kind: reflect.Int64, Type: int64_t, Size: 64, T: IntTy, Elem: &Type{Kind: reflect.Int64, Type: int64_t, Size: 64, T: IntTy, stringKind: "int64"}, stringKind: "int64[]"}}, | ||||
| 		{"int64[2]", Type{IsArray: true, SliceSize: 2, Kind: reflect.Int64, Type: int64_t, Size: 64, T: IntTy, Elem: &Type{Kind: reflect.Int64, Type: int64_t, Size: 64, T: IntTy, stringKind: "int64"}, stringKind: "int64[2]"}}, | ||||
| 		{"int256[]", Type{IsSlice: true, SliceSize: -1, Kind: reflect.Ptr, Type: big_t, Size: 256, T: IntTy, Elem: &Type{Kind: reflect.Ptr, Type: big_t, Size: 256, T: IntTy, stringKind: "int256"}, stringKind: "int256[]"}}, | ||||
| 		{"int256[2]", Type{IsArray: true, SliceSize: 2, Kind: reflect.Ptr, Type: big_t, Size: 256, T: IntTy, Elem: &Type{Kind: reflect.Ptr, Type: big_t, Size: 256, T: IntTy, stringKind: "int256"}, stringKind: "int256[2]"}}, | ||||
| 		{"uint8", Type{Kind: reflect.Uint8, Type: uint8_t, Size: 8, T: UintTy, stringKind: "uint8"}}, | ||||
| 		{"uint16", Type{Kind: reflect.Uint16, Type: uint16_t, Size: 16, T: UintTy, stringKind: "uint16"}}, | ||||
| 		{"uint32", Type{Kind: reflect.Uint32, Type: uint32_t, Size: 32, T: UintTy, stringKind: "uint32"}}, | ||||
| 		{"uint64", Type{Kind: reflect.Uint64, Type: uint64_t, Size: 64, T: UintTy, stringKind: "uint64"}}, | ||||
| 		{"uint256", Type{Kind: reflect.Ptr, Type: big_t, Size: 256, T: UintTy, stringKind: "uint256"}}, | ||||
| 		{"uint8[]", Type{IsSlice: true, SliceSize: -1, Kind: reflect.Uint8, Type: uint8_t, Size: 8, T: UintTy, Elem: &Type{Kind: reflect.Uint8, Type: uint8_t, Size: 8, T: UintTy, stringKind: "uint8"}, stringKind: "uint8[]"}}, | ||||
| 		{"uint8[2]", Type{IsArray: true, SliceSize: 2, Kind: reflect.Uint8, Type: uint8_t, Size: 8, T: UintTy, Elem: &Type{Kind: reflect.Uint8, Type: uint8_t, Size: 8, T: UintTy, stringKind: "uint8"}, stringKind: "uint8[2]"}}, | ||||
| 		{"uint16[]", Type{IsSlice: true, SliceSize: -1, Kind: reflect.Uint16, Type: uint16_t, Size: 16, T: UintTy, Elem: &Type{Kind: reflect.Uint16, Type: uint16_t, Size: 16, T: UintTy, stringKind: "uint16"}, stringKind: "uint16[]"}}, | ||||
| 		{"uint16[2]", Type{IsArray: true, SliceSize: 2, Kind: reflect.Uint16, Type: uint16_t, Size: 16, T: UintTy, Elem: &Type{Kind: reflect.Uint16, Type: uint16_t, Size: 16, T: UintTy, stringKind: "uint16"}, stringKind: "uint16[2]"}}, | ||||
| 		{"uint32[]", Type{IsSlice: true, SliceSize: -1, Kind: reflect.Uint32, Type: uint32_t, Size: 32, T: UintTy, Elem: &Type{Kind: reflect.Uint32, Type: uint32_t, Size: 32, T: UintTy, stringKind: "uint32"}, stringKind: "uint32[]"}}, | ||||
| 		{"uint32[2]", Type{IsArray: true, SliceSize: 2, Kind: reflect.Uint32, Type: uint32_t, Size: 32, T: UintTy, Elem: &Type{Kind: reflect.Uint32, Type: uint32_t, Size: 32, T: UintTy, stringKind: "uint32"}, stringKind: "uint32[2]"}}, | ||||
| 		{"uint64[]", Type{IsSlice: true, SliceSize: -1, Kind: reflect.Uint64, Type: uint64_t, Size: 64, T: UintTy, Elem: &Type{Kind: reflect.Uint64, Type: uint64_t, Size: 64, T: UintTy, stringKind: "uint64"}, stringKind: "uint64[]"}}, | ||||
| 		{"uint64[2]", Type{IsArray: true, SliceSize: 2, Kind: reflect.Uint64, Type: uint64_t, Size: 64, T: UintTy, Elem: &Type{Kind: reflect.Uint64, Type: uint64_t, Size: 64, T: UintTy, stringKind: "uint64"}, stringKind: "uint64[2]"}}, | ||||
| 		{"uint256[]", Type{IsSlice: true, SliceSize: -1, Kind: reflect.Ptr, Type: big_t, Size: 256, T: UintTy, Elem: &Type{Kind: reflect.Ptr, Type: big_t, Size: 256, T: UintTy, stringKind: "uint256"}, stringKind: "uint256[]"}}, | ||||
| 		{"uint256[2]", Type{IsArray: true, SliceSize: 2, Kind: reflect.Ptr, Type: big_t, Size: 256, T: UintTy, Elem: &Type{Kind: reflect.Ptr, Type: big_t, Size: 256, T: UintTy, stringKind: "uint256"}, stringKind: "uint256[2]"}}, | ||||
| 		{"bytes32", Type{IsArray: true, SliceSize: 32, Elem: &Type{Kind: reflect.Uint8, Type: uint8_t, Size: 8, T: UintTy, stringKind: "uint8"}, T: FixedBytesTy, stringKind: "bytes32"}}, | ||||
| 		{"bytes[]", Type{IsSlice: true, SliceSize: -1, Elem: &Type{IsSlice: true, SliceSize: -1, Elem: &Type{Kind: reflect.Uint8, Type: uint8_t, Size: 8, T: UintTy, stringKind: "uint8"}, T: BytesTy, stringKind: "bytes"}, stringKind: "bytes[]"}}, | ||||
| 		{"bytes[2]", Type{IsArray: true, SliceSize: 2, Elem: &Type{IsSlice: true, SliceSize: -1, Elem: &Type{Kind: reflect.Uint8, Type: uint8_t, Size: 8, T: UintTy, stringKind: "uint8"}, T: BytesTy, stringKind: "bytes"}, stringKind: "bytes[2]"}}, | ||||
| 		{"bytes32[]", Type{IsSlice: true, SliceSize: -1, Elem: &Type{IsArray: true, SliceSize: 32, Elem: &Type{Kind: reflect.Uint8, Type: uint8_t, Size: 8, T: UintTy, stringKind: "uint8"}, T: FixedBytesTy, stringKind: "bytes32"}, stringKind: "bytes32[]"}}, | ||||
| 		{"bytes32[2]", Type{IsArray: true, SliceSize: 2, Elem: &Type{IsArray: true, SliceSize: 32, Elem: &Type{Kind: reflect.Uint8, Type: uint8_t, Size: 8, T: UintTy, stringKind: "uint8"}, T: FixedBytesTy, stringKind: "bytes32"}, stringKind: "bytes32[2]"}}, | ||||
| 		{"string", Type{Kind: reflect.String, Size: -1, T: StringTy, stringKind: "string"}}, | ||||
| 		{"string[]", Type{IsSlice: true, SliceSize: -1, Kind: reflect.String, T: StringTy, Size: -1, Elem: &Type{Kind: reflect.String, T: StringTy, Size: -1, stringKind: "string"}, stringKind: "string[]"}}, | ||||
| 		{"string[2]", Type{IsArray: true, SliceSize: 2, Kind: reflect.String, T: StringTy, Size: -1, Elem: &Type{Kind: reflect.String, T: StringTy, Size: -1, stringKind: "string"}, stringKind: "string[2]"}}, | ||||
| @ -76,3 +97,59 @@ func TestTypeRegexp(t *testing.T) { | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestTypeCheck(t *testing.T) { | ||||
| 	for i, test := range []struct { | ||||
| 		typ   string | ||||
| 		input interface{} | ||||
| 		err   string | ||||
| 	}{ | ||||
| 		{"uint", big.NewInt(1), ""}, | ||||
| 		{"int", big.NewInt(1), ""}, | ||||
| 		{"uint30", big.NewInt(1), ""}, | ||||
| 		{"uint30", uint8(1), "abi: cannot use uint8 as type ptr as argument"}, | ||||
| 		{"uint16", uint16(1), ""}, | ||||
| 		{"uint16", uint8(1), "abi: cannot use uint8 as type uint16 as argument"}, | ||||
| 		{"uint16[]", []uint16{1, 2, 3}, ""}, | ||||
| 		{"uint16[]", [3]uint16{1, 2, 3}, ""}, | ||||
| 		{"uint16[]", []uint32{1, 2, 3}, "abi: cannot use []uint32 as type []uint16 as argument"}, | ||||
| 		{"uint16[3]", [3]uint32{1, 2, 3}, "abi: cannot use [3]uint32 as type [3]uint16 as argument"}, | ||||
| 		{"uint16[3]", [4]uint16{1, 2, 3}, "abi: cannot use [4]uint16 as type [3]uint16 as argument"}, | ||||
| 		{"uint16[3]", []uint16{1, 2, 3}, ""}, | ||||
| 		{"uint16[3]", []uint16{1, 2, 3, 4}, "abi: cannot use [4]uint16 as type [3]uint16 as argument"}, | ||||
| 		{"address[]", []common.Address{{1}}, ""}, | ||||
| 		{"address[1]", []common.Address{{1}}, ""}, | ||||
| 		{"address[1]", [1]common.Address{{1}}, ""}, | ||||
| 		{"address[2]", [1]common.Address{{1}}, "abi: cannot use [1]array as type [2]array as argument"}, | ||||
| 		{"bytes32", [32]byte{}, ""}, | ||||
| 		{"bytes32", [33]byte{}, "abi: cannot use [33]uint8 as type [32]uint8 as argument"}, | ||||
| 		{"bytes32", common.Hash{1}, ""}, | ||||
| 		{"bytes31", [31]byte{}, ""}, | ||||
| 		{"bytes31", [32]byte{}, "abi: cannot use [32]uint8 as type [31]uint8 as argument"}, | ||||
| 		{"bytes", []byte{0, 1}, ""}, | ||||
| 		{"bytes", [2]byte{0, 1}, ""}, | ||||
| 		{"bytes", common.Hash{1}, ""}, | ||||
| 		{"string", "hello world", ""}, | ||||
| 		{"bytes32[]", [][32]byte{{}}, ""}, | ||||
| 		{"function", [24]byte{}, ""}, | ||||
| 	} { | ||||
| 		typ, err := NewType(test.typ) | ||||
| 		if err != nil { | ||||
| 			t.Fatal("unexpected parse error:", err) | ||||
| 		} | ||||
| 
 | ||||
| 		err = typeCheck(typ, reflect.ValueOf(test.input)) | ||||
| 		if err != nil && len(test.err) == 0 { | ||||
| 			t.Errorf("%d failed. Expected no err but got: %v", i, err) | ||||
| 			continue | ||||
| 		} | ||||
| 		if err == nil && len(test.err) != 0 { | ||||
| 			t.Errorf("%d failed. Expected err: %v but got none", i, test.err) | ||||
| 			continue | ||||
| 		} | ||||
| 
 | ||||
| 		if err != nil && len(test.err) != 0 && err.Error() != test.err { | ||||
| 			t.Errorf("%d failed. Expected err: '%v' got err: '%v'", i, test.err, err) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
							
								
								
									
										235
									
								
								accounts/abi/unpack.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										235
									
								
								accounts/abi/unpack.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,235 @@ | ||||
| // Copyright 2015 The go-ethereum Authors
 | ||||
| // This file is part of the go-ethereum library.
 | ||||
| //
 | ||||
| // The go-ethereum library is free software: you can redistribute it and/or modify
 | ||||
| // it under the terms of the GNU Lesser General Public License as published by
 | ||||
| // the Free Software Foundation, either version 3 of the License, or
 | ||||
| // (at your option) any later version.
 | ||||
| //
 | ||||
| // The go-ethereum library is distributed in the hope that it will be useful,
 | ||||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | ||||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 | ||||
| // GNU Lesser General Public License for more details.
 | ||||
| //
 | ||||
| // You should have received a copy of the GNU Lesser General Public License
 | ||||
| // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
 | ||||
| 
 | ||||
| package abi | ||||
| 
 | ||||
| import ( | ||||
| 	"encoding/binary" | ||||
| 	"fmt" | ||||
| 	"math/big" | ||||
| 	"reflect" | ||||
| 
 | ||||
| 	"github.com/ethereum/go-ethereum/common" | ||||
| ) | ||||
| 
 | ||||
| // toGoSliceType parses the input and casts it to the proper slice defined by the ABI
 | ||||
| // argument in T.
 | ||||
| func toGoSlice(i int, t Argument, output []byte) (interface{}, error) { | ||||
| 	index := i * 32 | ||||
| 	// The slice must, at very least be large enough for the index+32 which is exactly the size required
 | ||||
| 	// for the [offset in output, size of offset].
 | ||||
| 	if index+32 > len(output) { | ||||
| 		return nil, fmt.Errorf("abi: cannot marshal in to go slice: insufficient size output %d require %d", len(output), index+32) | ||||
| 	} | ||||
| 	elem := t.Type.Elem | ||||
| 
 | ||||
| 	// first we need to create a slice of the type
 | ||||
| 	var refSlice reflect.Value | ||||
| 	switch elem.T { | ||||
| 	case IntTy, UintTy, BoolTy: | ||||
| 		// create a new reference slice matching the element type
 | ||||
| 		switch t.Type.Kind { | ||||
| 		case reflect.Bool: | ||||
| 			refSlice = reflect.ValueOf([]bool(nil)) | ||||
| 		case reflect.Uint8: | ||||
| 			refSlice = reflect.ValueOf([]uint8(nil)) | ||||
| 		case reflect.Uint16: | ||||
| 			refSlice = reflect.ValueOf([]uint16(nil)) | ||||
| 		case reflect.Uint32: | ||||
| 			refSlice = reflect.ValueOf([]uint32(nil)) | ||||
| 		case reflect.Uint64: | ||||
| 			refSlice = reflect.ValueOf([]uint64(nil)) | ||||
| 		case reflect.Int8: | ||||
| 			refSlice = reflect.ValueOf([]int8(nil)) | ||||
| 		case reflect.Int16: | ||||
| 			refSlice = reflect.ValueOf([]int16(nil)) | ||||
| 		case reflect.Int32: | ||||
| 			refSlice = reflect.ValueOf([]int32(nil)) | ||||
| 		case reflect.Int64: | ||||
| 			refSlice = reflect.ValueOf([]int64(nil)) | ||||
| 		default: | ||||
| 			refSlice = reflect.ValueOf([]*big.Int(nil)) | ||||
| 		} | ||||
| 	case AddressTy: // address must be of slice Address
 | ||||
| 		refSlice = reflect.ValueOf([]common.Address(nil)) | ||||
| 	case HashTy: // hash must be of slice hash
 | ||||
| 		refSlice = reflect.ValueOf([]common.Hash(nil)) | ||||
| 	case FixedBytesTy: | ||||
| 		refSlice = reflect.ValueOf([][]byte(nil)) | ||||
| 	default: // no other types are supported
 | ||||
| 		return nil, fmt.Errorf("abi: unsupported slice type %v", elem.T) | ||||
| 	} | ||||
| 
 | ||||
| 	var slice []byte | ||||
| 	var size int | ||||
| 	var offset int | ||||
| 	if t.Type.IsSlice { | ||||
| 		// get the offset which determines the start of this array ...
 | ||||
| 		offset = int(binary.BigEndian.Uint64(output[index+24 : index+32])) | ||||
| 		if offset+32 > len(output) { | ||||
| 			return nil, fmt.Errorf("abi: cannot marshal in to go slice: offset %d would go over slice boundary (len=%d)", len(output), offset+32) | ||||
| 		} | ||||
| 
 | ||||
| 		slice = output[offset:] | ||||
| 		// ... starting with the size of the array in elements ...
 | ||||
| 		size = int(binary.BigEndian.Uint64(slice[24:32])) | ||||
| 		slice = slice[32:] | ||||
| 		// ... and make sure that we've at the very least the amount of bytes
 | ||||
| 		// available in the buffer.
 | ||||
| 		if size*32 > len(slice) { | ||||
| 			return nil, fmt.Errorf("abi: cannot marshal in to go slice: insufficient size output %d require %d", len(output), offset+32+size*32) | ||||
| 		} | ||||
| 
 | ||||
| 		// reslice to match the required size
 | ||||
| 		slice = slice[:size*32] | ||||
| 	} else if t.Type.IsArray { | ||||
| 		//get the number of elements in the array
 | ||||
| 		size = t.Type.SliceSize | ||||
| 
 | ||||
| 		//check to make sure array size matches up
 | ||||
| 		if index+32*size > len(output) { | ||||
| 			return nil, fmt.Errorf("abi: cannot marshal in to go array: offset %d would go over slice boundary (len=%d)", len(output), index+32*size) | ||||
| 		} | ||||
| 		//slice is there for a fixed amount of times
 | ||||
| 		slice = output[index : index+size*32] | ||||
| 	} | ||||
| 
 | ||||
| 	for i := 0; i < size; i++ { | ||||
| 		var ( | ||||
| 			inter        interface{}             // interface type
 | ||||
| 			returnOutput = slice[i*32 : i*32+32] // the return output
 | ||||
| 			err          error | ||||
| 		) | ||||
| 		// set inter to the correct type (cast)
 | ||||
| 		switch elem.T { | ||||
| 		case IntTy, UintTy: | ||||
| 			inter = readInteger(t.Type.Kind, returnOutput) | ||||
| 		case BoolTy: | ||||
| 			inter, err = readBool(returnOutput) | ||||
| 			if err != nil { | ||||
| 				return nil, err | ||||
| 			} | ||||
| 		case AddressTy: | ||||
| 			inter = common.BytesToAddress(returnOutput) | ||||
| 		case HashTy: | ||||
| 			inter = common.BytesToHash(returnOutput) | ||||
| 		case FixedBytesTy: | ||||
| 			inter = returnOutput | ||||
| 		} | ||||
| 		// append the item to our reflect slice
 | ||||
| 		refSlice = reflect.Append(refSlice, reflect.ValueOf(inter)) | ||||
| 	} | ||||
| 
 | ||||
| 	// return the interface
 | ||||
| 	return refSlice.Interface(), nil | ||||
| } | ||||
| 
 | ||||
| func readInteger(kind reflect.Kind, b []byte) interface{} { | ||||
| 	switch kind { | ||||
| 	case reflect.Uint8: | ||||
| 		return uint8(b[len(b)-1]) | ||||
| 	case reflect.Uint16: | ||||
| 		return binary.BigEndian.Uint16(b[len(b)-2:]) | ||||
| 	case reflect.Uint32: | ||||
| 		return binary.BigEndian.Uint32(b[len(b)-4:]) | ||||
| 	case reflect.Uint64: | ||||
| 		return binary.BigEndian.Uint64(b[len(b)-8:]) | ||||
| 	case reflect.Int8: | ||||
| 		return int8(b[len(b)-1]) | ||||
| 	case reflect.Int16: | ||||
| 		return int16(binary.BigEndian.Uint16(b[len(b)-2:])) | ||||
| 	case reflect.Int32: | ||||
| 		return int32(binary.BigEndian.Uint32(b[len(b)-4:])) | ||||
| 	case reflect.Int64: | ||||
| 		return int64(binary.BigEndian.Uint64(b[len(b)-8:])) | ||||
| 	default: | ||||
| 		return new(big.Int).SetBytes(b) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func readBool(word []byte) (bool, error) { | ||||
| 	if len(word) != 32 { | ||||
| 		return false, fmt.Errorf("abi: fatal error: incorrect word length") | ||||
| 	} | ||||
| 
 | ||||
| 	for i, b := range word { | ||||
| 		if b != 0 && i != 31 { | ||||
| 			return false, errBadBool | ||||
| 		} | ||||
| 	} | ||||
| 	switch word[31] { | ||||
| 	case 0: | ||||
| 		return false, nil | ||||
| 	case 1: | ||||
| 		return true, nil | ||||
| 	default: | ||||
| 		return false, errBadBool | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| // toGoType parses the input and casts it to the proper type defined by the ABI
 | ||||
| // argument in T.
 | ||||
| func toGoType(i int, t Argument, output []byte) (interface{}, error) { | ||||
| 	// we need to treat slices differently
 | ||||
| 	if (t.Type.IsSlice || t.Type.IsArray) && t.Type.T != BytesTy && t.Type.T != StringTy && t.Type.T != FixedBytesTy && t.Type.T != FunctionTy { | ||||
| 		return toGoSlice(i, t, output) | ||||
| 	} | ||||
| 
 | ||||
| 	index := i * 32 | ||||
| 	if index+32 > len(output) { | ||||
| 		return nil, fmt.Errorf("abi: cannot marshal in to go type: length insufficient %d require %d", len(output), index+32) | ||||
| 	} | ||||
| 
 | ||||
| 	// Parse the given index output and check whether we need to read
 | ||||
| 	// a different offset and length based on the type (i.e. string, bytes)
 | ||||
| 	var returnOutput []byte | ||||
| 	switch t.Type.T { | ||||
| 	case StringTy, BytesTy: // variable arrays are written at the end of the return bytes
 | ||||
| 		// parse offset from which we should start reading
 | ||||
| 		offset := int(binary.BigEndian.Uint64(output[index+24 : index+32])) | ||||
| 		if offset+32 > len(output) { | ||||
| 			return nil, fmt.Errorf("abi: cannot marshal in to go type: length insufficient %d require %d", len(output), offset+32) | ||||
| 		} | ||||
| 		// parse the size up until we should be reading
 | ||||
| 		size := int(binary.BigEndian.Uint64(output[offset+24 : offset+32])) | ||||
| 		if offset+32+size > len(output) { | ||||
| 			return nil, fmt.Errorf("abi: cannot marshal in to go type: length insufficient %d require %d", len(output), offset+32+size) | ||||
| 		} | ||||
| 
 | ||||
| 		// get the bytes for this return value
 | ||||
| 		returnOutput = output[offset+32 : offset+32+size] | ||||
| 	default: | ||||
| 		returnOutput = output[index : index+32] | ||||
| 	} | ||||
| 
 | ||||
| 	// convert the bytes to whatever is specified by the ABI.
 | ||||
| 	switch t.Type.T { | ||||
| 	case IntTy, UintTy: | ||||
| 		return readInteger(t.Type.Kind, returnOutput), nil | ||||
| 	case BoolTy: | ||||
| 		return readBool(returnOutput) | ||||
| 	case AddressTy: | ||||
| 		return common.BytesToAddress(returnOutput), nil | ||||
| 	case HashTy: | ||||
| 		return common.BytesToHash(returnOutput), nil | ||||
| 	case BytesTy, FixedBytesTy, FunctionTy: | ||||
| 		return returnOutput, nil | ||||
| 	case StringTy: | ||||
| 		return string(returnOutput), nil | ||||
| 	} | ||||
| 	return nil, fmt.Errorf("abi: unknown type %v", t.Type.T) | ||||
| } | ||||
							
								
								
									
										681
									
								
								accounts/abi/unpack_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										681
									
								
								accounts/abi/unpack_test.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,681 @@ | ||||
| // Copyright 2015 The go-ethereum Authors
 | ||||
| // This file is part of the go-ethereum library.
 | ||||
| //
 | ||||
| // The go-ethereum library is free software: you can redistribute it and/or modify
 | ||||
| // it under the terms of the GNU Lesser General Public License as published by
 | ||||
| // the Free Software Foundation, either version 3 of the License, or
 | ||||
| // (at your option) any later version.
 | ||||
| //
 | ||||
| // The go-ethereum library is distributed in the hope that it will be useful,
 | ||||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | ||||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 | ||||
| // GNU Lesser General Public License for more details.
 | ||||
| //
 | ||||
| // You should have received a copy of the GNU Lesser General Public License
 | ||||
| // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
 | ||||
| 
 | ||||
| package abi | ||||
| 
 | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"fmt" | ||||
| 	"math/big" | ||||
| 	"reflect" | ||||
| 	"strings" | ||||
| 	"testing" | ||||
| 
 | ||||
| 	"github.com/ethereum/go-ethereum/common" | ||||
| ) | ||||
| 
 | ||||
| func TestSimpleMethodUnpack(t *testing.T) { | ||||
| 	for i, test := range []struct { | ||||
| 		def              string      // definition of the **output** ABI params
 | ||||
| 		marshalledOutput []byte      // evm return data
 | ||||
| 		expectedOut      interface{} // the expected output
 | ||||
| 		outVar           string      // the output variable (e.g. uint32, *big.Int, etc)
 | ||||
| 		err              string      // empty or error if expected
 | ||||
| 	}{ | ||||
| 		{ | ||||
| 			`[ { "type": "bool" } ]`, | ||||
| 			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"), | ||||
| 			bool(true), | ||||
| 			"bool", | ||||
| 			"", | ||||
| 		}, | ||||
| 		{ | ||||
| 			`[ { "type": "uint32" } ]`, | ||||
| 			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"), | ||||
| 			uint32(1), | ||||
| 			"uint32", | ||||
| 			"", | ||||
| 		}, | ||||
| 		{ | ||||
| 			`[ { "type": "uint32" } ]`, | ||||
| 			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"), | ||||
| 			nil, | ||||
| 			"uint16", | ||||
| 			"abi: cannot unmarshal uint32 in to uint16", | ||||
| 		}, | ||||
| 		{ | ||||
| 			`[ { "type": "uint17" } ]`, | ||||
| 			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"), | ||||
| 			nil, | ||||
| 			"uint16", | ||||
| 			"abi: cannot unmarshal *big.Int in to uint16", | ||||
| 		}, | ||||
| 		{ | ||||
| 			`[ { "type": "uint17" } ]`, | ||||
| 			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"), | ||||
| 			big.NewInt(1), | ||||
| 			"*big.Int", | ||||
| 			"", | ||||
| 		}, | ||||
| 
 | ||||
| 		{ | ||||
| 			`[ { "type": "int32" } ]`, | ||||
| 			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"), | ||||
| 			int32(1), | ||||
| 			"int32", | ||||
| 			"", | ||||
| 		}, | ||||
| 		{ | ||||
| 			`[ { "type": "int32" } ]`, | ||||
| 			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"), | ||||
| 			nil, | ||||
| 			"int16", | ||||
| 			"abi: cannot unmarshal int32 in to int16", | ||||
| 		}, | ||||
| 		{ | ||||
| 			`[ { "type": "int17" } ]`, | ||||
| 			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"), | ||||
| 			nil, | ||||
| 			"int16", | ||||
| 			"abi: cannot unmarshal *big.Int in to int16", | ||||
| 		}, | ||||
| 		{ | ||||
| 			`[ { "type": "int17" } ]`, | ||||
| 			common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"), | ||||
| 			big.NewInt(1), | ||||
| 			"*big.Int", | ||||
| 			"", | ||||
| 		}, | ||||
| 
 | ||||
| 		{ | ||||
| 			`[ { "type": "address" } ]`, | ||||
| 			common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"), | ||||
| 			common.Address{1}, | ||||
| 			"address", | ||||
| 			"", | ||||
| 		}, | ||||
| 		{ | ||||
| 			`[ { "type": "bytes32" } ]`, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 			"bytes", | ||||
| 			"", | ||||
| 		}, | ||||
| 		{ | ||||
| 			`[ { "type": "bytes32" } ]`, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 			"hash", | ||||
| 			"", | ||||
| 		}, | ||||
| 		{ | ||||
| 			`[ { "type": "bytes32" } ]`, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 			"interface", | ||||
| 			"", | ||||
| 		}, | ||||
| 		{ | ||||
| 			`[ { "type": "function" } ]`, | ||||
| 			common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"), | ||||
| 			[24]byte{1}, | ||||
| 			"function", | ||||
| 			"", | ||||
| 		}, | ||||
| 	} { | ||||
| 		abiDefinition := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def) | ||||
| 		abi, err := JSON(strings.NewReader(abiDefinition)) | ||||
| 		if err != nil { | ||||
| 			t.Errorf("%d failed. %v", i, err) | ||||
| 			continue | ||||
| 		} | ||||
| 
 | ||||
| 		var outvar interface{} | ||||
| 		switch test.outVar { | ||||
| 		case "bool": | ||||
| 			var v bool | ||||
| 			err = abi.Unpack(&v, "method", test.marshalledOutput) | ||||
| 			outvar = v | ||||
| 		case "uint8": | ||||
| 			var v uint8 | ||||
| 			err = abi.Unpack(&v, "method", test.marshalledOutput) | ||||
| 			outvar = v | ||||
| 		case "uint16": | ||||
| 			var v uint16 | ||||
| 			err = abi.Unpack(&v, "method", test.marshalledOutput) | ||||
| 			outvar = v | ||||
| 		case "uint32": | ||||
| 			var v uint32 | ||||
| 			err = abi.Unpack(&v, "method", test.marshalledOutput) | ||||
| 			outvar = v | ||||
| 		case "uint64": | ||||
| 			var v uint64 | ||||
| 			err = abi.Unpack(&v, "method", test.marshalledOutput) | ||||
| 			outvar = v | ||||
| 		case "int8": | ||||
| 			var v int8 | ||||
| 			err = abi.Unpack(&v, "method", test.marshalledOutput) | ||||
| 			outvar = v | ||||
| 		case "int16": | ||||
| 			var v int16 | ||||
| 			err = abi.Unpack(&v, "method", test.marshalledOutput) | ||||
| 			outvar = v | ||||
| 		case "int32": | ||||
| 			var v int32 | ||||
| 			err = abi.Unpack(&v, "method", test.marshalledOutput) | ||||
| 			outvar = v | ||||
| 		case "int64": | ||||
| 			var v int64 | ||||
| 			err = abi.Unpack(&v, "method", test.marshalledOutput) | ||||
| 			outvar = v | ||||
| 		case "*big.Int": | ||||
| 			var v *big.Int | ||||
| 			err = abi.Unpack(&v, "method", test.marshalledOutput) | ||||
| 			outvar = v | ||||
| 		case "address": | ||||
| 			var v common.Address | ||||
| 			err = abi.Unpack(&v, "method", test.marshalledOutput) | ||||
| 			outvar = v | ||||
| 		case "bytes": | ||||
| 			var v []byte | ||||
| 			err = abi.Unpack(&v, "method", test.marshalledOutput) | ||||
| 			outvar = v | ||||
| 		case "hash": | ||||
| 			var v common.Hash | ||||
| 			err = abi.Unpack(&v, "method", test.marshalledOutput) | ||||
| 			outvar = v.Bytes()[:] | ||||
| 		case "function": | ||||
| 			var v [24]byte | ||||
| 			err = abi.Unpack(&v, "method", test.marshalledOutput) | ||||
| 			outvar = v | ||||
| 		case "interface": | ||||
| 			err = abi.Unpack(&outvar, "method", test.marshalledOutput) | ||||
| 		default: | ||||
| 			t.Errorf("unsupported type '%v' please add it to the switch statement in this test", test.outVar) | ||||
| 			continue | ||||
| 		} | ||||
| 
 | ||||
| 		if err != nil && len(test.err) == 0 { | ||||
| 			t.Errorf("%d failed. Expected no err but got: %v", i, err) | ||||
| 			continue | ||||
| 		} | ||||
| 		if err == nil && len(test.err) != 0 { | ||||
| 			t.Errorf("%d failed. Expected err: %v but got none", i, test.err) | ||||
| 			continue | ||||
| 		} | ||||
| 		if err != nil && len(test.err) != 0 && err.Error() != test.err { | ||||
| 			t.Errorf("%d failed. Expected err: '%v' got err: '%v'", i, test.err, err) | ||||
| 			continue | ||||
| 		} | ||||
| 
 | ||||
| 		if err == nil { | ||||
| 			if !reflect.DeepEqual(test.expectedOut, outvar) { | ||||
| 				t.Errorf("%d failed. Output error: expected %v, got %v", i, test.expectedOut, outvar) | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestUnpackSetInterfaceSlice(t *testing.T) { | ||||
| 	var ( | ||||
| 		var1 = new(uint8) | ||||
| 		var2 = new(uint8) | ||||
| 	) | ||||
| 	out := []interface{}{var1, var2} | ||||
| 	abi, err := JSON(strings.NewReader(`[{"type":"function", "name":"ints", "outputs":[{"type":"uint8"}, {"type":"uint8"}]}]`)) | ||||
| 	if err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
| 	marshalledReturn := append(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")...) | ||||
| 	err = abi.Unpack(&out, "ints", marshalledReturn) | ||||
| 	if err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
| 	if *var1 != 1 { | ||||
| 		t.Error("expected var1 to be 1, got", *var1) | ||||
| 	} | ||||
| 	if *var2 != 2 { | ||||
| 		t.Error("expected var2 to be 2, got", *var2) | ||||
| 	} | ||||
| 
 | ||||
| 	out = []interface{}{var1} | ||||
| 	err = abi.Unpack(&out, "ints", marshalledReturn) | ||||
| 
 | ||||
| 	expErr := "abi: cannot marshal in to slices of unequal size (require: 2, got: 1)" | ||||
| 	if err == nil || err.Error() != expErr { | ||||
| 		t.Error("expected err:", expErr, "Got:", err) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestUnpackSetInterfaceArrayOutput(t *testing.T) { | ||||
| 	var ( | ||||
| 		var1 = new([1]uint32) | ||||
| 		var2 = new([1]uint32) | ||||
| 	) | ||||
| 	out := []interface{}{var1, var2} | ||||
| 	abi, err := JSON(strings.NewReader(`[{"type":"function", "name":"ints", "outputs":[{"type":"uint32[1]"}, {"type":"uint32[1]"}]}]`)) | ||||
| 	if err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
| 	marshalledReturn := append(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")...) | ||||
| 	err = abi.Unpack(&out, "ints", marshalledReturn) | ||||
| 	if err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
| 
 | ||||
| 	if *var1 != [1]uint32{1} { | ||||
| 		t.Error("expected var1 to be [1], got", *var1) | ||||
| 	} | ||||
| 	if *var2 != [1]uint32{2} { | ||||
| 		t.Error("expected var2 to be [2], got", *var2) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestMultiReturnWithStruct(t *testing.T) { | ||||
| 	const definition = `[ | ||||
| 	{ "name" : "multi", "constant" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]` | ||||
| 
 | ||||
| 	abi, err := JSON(strings.NewReader(definition)) | ||||
| 	if err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
| 
 | ||||
| 	// using buff to make the code readable
 | ||||
| 	buff := new(bytes.Buffer) | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005")) | ||||
| 	stringOut := "hello" | ||||
| 	buff.Write(common.RightPadBytes([]byte(stringOut), 32)) | ||||
| 
 | ||||
| 	var inter struct { | ||||
| 		Int    *big.Int | ||||
| 		String string | ||||
| 	} | ||||
| 	err = abi.Unpack(&inter, "multi", buff.Bytes()) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
| 
 | ||||
| 	if inter.Int == nil || inter.Int.Cmp(big.NewInt(1)) != 0 { | ||||
| 		t.Error("expected Int to be 1 got", inter.Int) | ||||
| 	} | ||||
| 
 | ||||
| 	if inter.String != stringOut { | ||||
| 		t.Error("expected String to be", stringOut, "got", inter.String) | ||||
| 	} | ||||
| 
 | ||||
| 	var reversed struct { | ||||
| 		String string | ||||
| 		Int    *big.Int | ||||
| 	} | ||||
| 
 | ||||
| 	err = abi.Unpack(&reversed, "multi", buff.Bytes()) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
| 
 | ||||
| 	if reversed.Int == nil || reversed.Int.Cmp(big.NewInt(1)) != 0 { | ||||
| 		t.Error("expected Int to be 1 got", reversed.Int) | ||||
| 	} | ||||
| 
 | ||||
| 	if reversed.String != stringOut { | ||||
| 		t.Error("expected String to be", stringOut, "got", reversed.String) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestMultiReturnWithSlice(t *testing.T) { | ||||
| 	const definition = `[ | ||||
| 	{ "name" : "multi", "constant" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]` | ||||
| 
 | ||||
| 	abi, err := JSON(strings.NewReader(definition)) | ||||
| 	if err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
| 
 | ||||
| 	// using buff to make the code readable
 | ||||
| 	buff := new(bytes.Buffer) | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005")) | ||||
| 	stringOut := "hello" | ||||
| 	buff.Write(common.RightPadBytes([]byte(stringOut), 32)) | ||||
| 
 | ||||
| 	var inter []interface{} | ||||
| 	err = abi.Unpack(&inter, "multi", buff.Bytes()) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
| 
 | ||||
| 	if len(inter) != 2 { | ||||
| 		t.Fatal("expected 2 results got", len(inter)) | ||||
| 	} | ||||
| 
 | ||||
| 	if num, ok := inter[0].(*big.Int); !ok || num.Cmp(big.NewInt(1)) != 0 { | ||||
| 		t.Error("expected index 0 to be 1 got", num) | ||||
| 	} | ||||
| 
 | ||||
| 	if str, ok := inter[1].(string); !ok || str != stringOut { | ||||
| 		t.Error("expected index 1 to be", stringOut, "got", str) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestMarshalArrays(t *testing.T) { | ||||
| 	const definition = `[ | ||||
| 	{ "name" : "bytes32", "constant" : false, "outputs": [ { "type": "bytes32" } ] }, | ||||
| 	{ "name" : "bytes10", "constant" : false, "outputs": [ { "type": "bytes10" } ] } | ||||
| 	]` | ||||
| 
 | ||||
| 	abi, err := JSON(strings.NewReader(definition)) | ||||
| 	if err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
| 
 | ||||
| 	output := common.LeftPadBytes([]byte{1}, 32) | ||||
| 
 | ||||
| 	var bytes10 [10]byte | ||||
| 	err = abi.Unpack(&bytes10, "bytes32", output) | ||||
| 	if err == nil || err.Error() != "abi: cannot unmarshal src (len=32) in to dst (len=10)" { | ||||
| 		t.Error("expected error or bytes32 not be assignable to bytes10:", err) | ||||
| 	} | ||||
| 
 | ||||
| 	var bytes32 [32]byte | ||||
| 	err = abi.Unpack(&bytes32, "bytes32", output) | ||||
| 	if err != nil { | ||||
| 		t.Error("didn't expect error:", err) | ||||
| 	} | ||||
| 	if !bytes.Equal(bytes32[:], output) { | ||||
| 		t.Error("expected bytes32[31] to be 1 got", bytes32[31]) | ||||
| 	} | ||||
| 
 | ||||
| 	type ( | ||||
| 		B10 [10]byte | ||||
| 		B32 [32]byte | ||||
| 	) | ||||
| 
 | ||||
| 	var b10 B10 | ||||
| 	err = abi.Unpack(&b10, "bytes32", output) | ||||
| 	if err == nil || err.Error() != "abi: cannot unmarshal src (len=32) in to dst (len=10)" { | ||||
| 		t.Error("expected error or bytes32 not be assignable to bytes10:", err) | ||||
| 	} | ||||
| 
 | ||||
| 	var b32 B32 | ||||
| 	err = abi.Unpack(&b32, "bytes32", output) | ||||
| 	if err != nil { | ||||
| 		t.Error("didn't expect error:", err) | ||||
| 	} | ||||
| 	if !bytes.Equal(b32[:], output) { | ||||
| 		t.Error("expected bytes32[31] to be 1 got", bytes32[31]) | ||||
| 	} | ||||
| 
 | ||||
| 	output[10] = 1 | ||||
| 	var shortAssignLong [32]byte | ||||
| 	err = abi.Unpack(&shortAssignLong, "bytes10", output) | ||||
| 	if err != nil { | ||||
| 		t.Error("didn't expect error:", err) | ||||
| 	} | ||||
| 	if !bytes.Equal(output, shortAssignLong[:]) { | ||||
| 		t.Errorf("expected %x to be %x", shortAssignLong, output) | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| func TestUnmarshal(t *testing.T) { | ||||
| 	const definition = `[ | ||||
| 	{ "name" : "int", "constant" : false, "outputs": [ { "type": "uint256" } ] }, | ||||
| 	{ "name" : "bool", "constant" : false, "outputs": [ { "type": "bool" } ] }, | ||||
| 	{ "name" : "bytes", "constant" : false, "outputs": [ { "type": "bytes" } ] }, | ||||
| 	{ "name" : "fixed", "constant" : false, "outputs": [ { "type": "bytes32" } ] }, | ||||
| 	{ "name" : "multi", "constant" : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] }, | ||||
| 	{ "name" : "intArraySingle", "constant" : false, "outputs": [ { "type": "uint256[3]" } ] }, | ||||
| 	{ "name" : "addressSliceSingle", "constant" : false, "outputs": [ { "type": "address[]" } ] }, | ||||
| 	{ "name" : "addressSliceDouble", "constant" : false, "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] }, | ||||
| 	{ "name" : "mixedBytes", "constant" : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]` | ||||
| 
 | ||||
| 	abi, err := JSON(strings.NewReader(definition)) | ||||
| 	if err != nil { | ||||
| 		t.Fatal(err) | ||||
| 	} | ||||
| 	buff := new(bytes.Buffer) | ||||
| 
 | ||||
| 	// marshal int
 | ||||
| 	var Int *big.Int | ||||
| 	err = abi.Unpack(&Int, "int", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
| 
 | ||||
| 	if Int == nil || Int.Cmp(big.NewInt(1)) != 0 { | ||||
| 		t.Error("expected Int to be 1 got", Int) | ||||
| 	} | ||||
| 
 | ||||
| 	// marshal bool
 | ||||
| 	var Bool bool | ||||
| 	err = abi.Unpack(&Bool, "bool", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
| 
 | ||||
| 	if !Bool { | ||||
| 		t.Error("expected Bool to be true") | ||||
| 	} | ||||
| 
 | ||||
| 	// marshal dynamic bytes max length 32
 | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) | ||||
| 	bytesOut := common.RightPadBytes([]byte("hello"), 32) | ||||
| 	buff.Write(bytesOut) | ||||
| 
 | ||||
| 	var Bytes []byte | ||||
| 	err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
| 
 | ||||
| 	if !bytes.Equal(Bytes, bytesOut) { | ||||
| 		t.Errorf("expected %x got %x", bytesOut, Bytes) | ||||
| 	} | ||||
| 
 | ||||
| 	// marshall dynamic bytes max length 64
 | ||||
| 	buff.Reset() | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) | ||||
| 	bytesOut = common.RightPadBytes([]byte("hello"), 64) | ||||
| 	buff.Write(bytesOut) | ||||
| 
 | ||||
| 	err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
| 
 | ||||
| 	if !bytes.Equal(Bytes, bytesOut) { | ||||
| 		t.Errorf("expected %x got %x", bytesOut, Bytes) | ||||
| 	} | ||||
| 
 | ||||
| 	// marshall dynamic bytes max length 63
 | ||||
| 	buff.Reset() | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) | ||||
| 	buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000003f")) | ||||
| 	bytesOut = common.RightPadBytes([]byte("hello"), 63) | ||||
| 	buff.Write(bytesOut) | ||||
| 
 | ||||
| 	err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
| 
 | ||||
| 	if !bytes.Equal(Bytes, bytesOut) { | ||||
| 		t.Errorf("expected %x got %x", bytesOut, Bytes) | ||||
| 	} | ||||
| 
 | ||||
| 	// marshal dynamic bytes output empty
 | ||||
| 	err = abi.Unpack(&Bytes, "bytes", nil) | ||||
| 	if err == nil { | ||||
| 		t.Error("expected error") | ||||
| 	} | ||||
| 
 | ||||
| 	// marshal dynamic bytes length 5
 | ||||
| 	buff.Reset() | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005")) | ||||
| 	buff.Write(common.RightPadBytes([]byte("hello"), 32)) | ||||
| 
 | ||||
| 	err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
| 
 | ||||
| 	if !bytes.Equal(Bytes, []byte("hello")) { | ||||
| 		t.Errorf("expected %x got %x", bytesOut, Bytes) | ||||
| 	} | ||||
| 
 | ||||
| 	// marshal dynamic bytes length 5
 | ||||
| 	buff.Reset() | ||||
| 	buff.Write(common.RightPadBytes([]byte("hello"), 32)) | ||||
| 
 | ||||
| 	var hash common.Hash | ||||
| 	err = abi.Unpack(&hash, "fixed", buff.Bytes()) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
| 
 | ||||
| 	helloHash := common.BytesToHash(common.RightPadBytes([]byte("hello"), 32)) | ||||
| 	if hash != helloHash { | ||||
| 		t.Errorf("Expected %x to equal %x", hash, helloHash) | ||||
| 	} | ||||
| 
 | ||||
| 	// marshal error
 | ||||
| 	buff.Reset() | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) | ||||
| 	err = abi.Unpack(&Bytes, "bytes", buff.Bytes()) | ||||
| 	if err == nil { | ||||
| 		t.Error("expected error") | ||||
| 	} | ||||
| 
 | ||||
| 	err = abi.Unpack(&Bytes, "multi", make([]byte, 64)) | ||||
| 	if err == nil { | ||||
| 		t.Error("expected error") | ||||
| 	} | ||||
| 
 | ||||
| 	// marshal mixed bytes
 | ||||
| 	buff.Reset() | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) | ||||
| 	fixed := common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001") | ||||
| 	buff.Write(fixed) | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) | ||||
| 	bytesOut = common.RightPadBytes([]byte("hello"), 32) | ||||
| 	buff.Write(bytesOut) | ||||
| 
 | ||||
| 	var out []interface{} | ||||
| 	err = abi.Unpack(&out, "mixedBytes", buff.Bytes()) | ||||
| 	if err != nil { | ||||
| 		t.Fatal("didn't expect error:", err) | ||||
| 	} | ||||
| 
 | ||||
| 	if !bytes.Equal(bytesOut, out[0].([]byte)) { | ||||
| 		t.Errorf("expected %x, got %x", bytesOut, out[0]) | ||||
| 	} | ||||
| 
 | ||||
| 	if !bytes.Equal(fixed, out[1].([]byte)) { | ||||
| 		t.Errorf("expected %x, got %x", fixed, out[1]) | ||||
| 	} | ||||
| 
 | ||||
| 	buff.Reset() | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003")) | ||||
| 	// marshal int array
 | ||||
| 	var intArray [3]*big.Int | ||||
| 	err = abi.Unpack(&intArray, "intArraySingle", buff.Bytes()) | ||||
| 	if err != nil { | ||||
| 		t.Error(err) | ||||
| 	} | ||||
| 	var testAgainstIntArray [3]*big.Int | ||||
| 	testAgainstIntArray[0] = big.NewInt(1) | ||||
| 	testAgainstIntArray[1] = big.NewInt(2) | ||||
| 	testAgainstIntArray[2] = big.NewInt(3) | ||||
| 
 | ||||
| 	for i, Int := range intArray { | ||||
| 		if Int.Cmp(testAgainstIntArray[i]) != 0 { | ||||
| 			t.Errorf("expected %v, got %v", testAgainstIntArray[i], Int) | ||||
| 		} | ||||
| 	} | ||||
| 	// marshal address slice
 | ||||
| 	buff.Reset() | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) // offset
 | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
 | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000")) | ||||
| 
 | ||||
| 	var outAddr []common.Address | ||||
| 	err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes()) | ||||
| 	if err != nil { | ||||
| 		t.Fatal("didn't expect error:", err) | ||||
| 	} | ||||
| 
 | ||||
| 	if len(outAddr) != 1 { | ||||
| 		t.Fatal("expected 1 item, got", len(outAddr)) | ||||
| 	} | ||||
| 
 | ||||
| 	if outAddr[0] != (common.Address{1}) { | ||||
| 		t.Errorf("expected %x, got %x", common.Address{1}, outAddr[0]) | ||||
| 	} | ||||
| 
 | ||||
| 	// marshal multiple address slice
 | ||||
| 	buff.Reset() | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // offset
 | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // offset
 | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
 | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000")) | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // size
 | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000200000000000000000000000000000000000000")) | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000300000000000000000000000000000000000000")) | ||||
| 
 | ||||
| 	var outAddrStruct struct { | ||||
| 		A []common.Address | ||||
| 		B []common.Address | ||||
| 	} | ||||
| 	err = abi.Unpack(&outAddrStruct, "addressSliceDouble", buff.Bytes()) | ||||
| 	if err != nil { | ||||
| 		t.Fatal("didn't expect error:", err) | ||||
| 	} | ||||
| 
 | ||||
| 	if len(outAddrStruct.A) != 1 { | ||||
| 		t.Fatal("expected 1 item, got", len(outAddrStruct.A)) | ||||
| 	} | ||||
| 
 | ||||
| 	if outAddrStruct.A[0] != (common.Address{1}) { | ||||
| 		t.Errorf("expected %x, got %x", common.Address{1}, outAddrStruct.A[0]) | ||||
| 	} | ||||
| 
 | ||||
| 	if len(outAddrStruct.B) != 2 { | ||||
| 		t.Fatal("expected 1 item, got", len(outAddrStruct.B)) | ||||
| 	} | ||||
| 
 | ||||
| 	if outAddrStruct.B[0] != (common.Address{2}) { | ||||
| 		t.Errorf("expected %x, got %x", common.Address{2}, outAddrStruct.B[0]) | ||||
| 	} | ||||
| 	if outAddrStruct.B[1] != (common.Address{3}) { | ||||
| 		t.Errorf("expected %x, got %x", common.Address{3}, outAddrStruct.B[1]) | ||||
| 	} | ||||
| 
 | ||||
| 	// marshal invalid address slice
 | ||||
| 	buff.Reset() | ||||
| 	buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000100")) | ||||
| 
 | ||||
| 	err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes()) | ||||
| 	if err == nil { | ||||
| 		t.Fatal("expected error:", err) | ||||
| 	} | ||||
| } | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user