From 93f3e15c344aa836afde334d2207575dbf257422 Mon Sep 17 00:00:00 2001 From: RJ Catalano Date: Fri, 30 Jun 2017 11:53:23 -0500 Subject: [PATCH 1/3] accounts/abi: make a better slicing scheme Signed-off-by: RJ Catalano --- accounts/abi/unpack.go | 60 ++++-------- accounts/abi/unpack_test.go | 181 ++++++++++++++++++++++++++++++++++++ 2 files changed, 198 insertions(+), 43 deletions(-) diff --git a/accounts/abi/unpack.go b/accounts/abi/unpack.go index fc41c88ac7e7..a76a57cd86ae 100644 --- a/accounts/abi/unpack.go +++ b/accounts/abi/unpack.go @@ -34,49 +34,15 @@ func toGoSlice(i int, t Argument, output []byte) (interface{}, error) { 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 + // this value will become our slice or our array, depending on 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 { + if t.Type.T == SliceTy { // 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) { @@ -95,10 +61,13 @@ func toGoSlice(i int, t Argument, output []byte) (interface{}, error) { // reslice to match the required size slice = slice[:size*32] - } else if t.Type.IsArray { + // declare our slice + refSlice = reflect.MakeSlice(reflect.SliceOf(elem.Type), size, size) + } else if t.Type.T == ArrayTy { //get the number of elements in the array - size = t.Type.SliceSize - + size = t.Type.Size + // declare our slice + refSlice = reflect.New(reflect.ArrayOf(size, elem.Type)).Elem() //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) @@ -116,7 +85,7 @@ func toGoSlice(i int, t Argument, output []byte) (interface{}, error) { // set inter to the correct type (cast) switch elem.T { case IntTy, UintTy: - inter = readInteger(t.Type.Kind, returnOutput) + inter = readInteger(elem.Kind, returnOutput) case BoolTy: inter, err = readBool(returnOutput) if err != nil { @@ -128,9 +97,14 @@ func toGoSlice(i int, t Argument, output []byte) (interface{}, error) { inter = common.BytesToHash(returnOutput) case FixedBytesTy: inter = returnOutput + default: + return nil, fmt.Errorf("abi: unsupported slice type passed in") } + + //fmt.Printf("type: %T, value: %v\n", inter, inter) + //fmt.Printf("%v\n", elem.stringKind) // append the item to our reflect slice - refSlice = reflect.Append(refSlice, reflect.ValueOf(inter)) + refSlice.Index(i).Set(reflect.ValueOf(inter)) } // return the interface @@ -185,7 +159,7 @@ func readBool(word []byte) (bool, error) { // 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 { + if t.Type.T == SliceTy || t.Type.T == ArrayTy { return toGoSlice(i, t, output) } diff --git a/accounts/abi/unpack_test.go b/accounts/abi/unpack_test.go index 8e3afee4e678..6f25284e2461 100644 --- a/accounts/abi/unpack_test.go +++ b/accounts/abi/unpack_test.go @@ -229,6 +229,187 @@ func TestSimpleMethodUnpack(t *testing.T) { } } +func TestArraysAndSlicesUnpack(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 interface{} // the output variable (e.g. uint32, *big.Int, etc) + err string // empty or error if expected + }{ + { + `[ { "type": "uint8[]" } ]`, + common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), + []uint8{1, 2}, + []uint8{}, + "", + }, + { + `[ { "type": "uint8[2]" } ]`, + common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), + [2]uint8{1, 2}, + [2]uint8{}, + "", + }, + { + `[ { "type": "uint16[]" } ]`, + common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), + []uint16{1, 2}, + []uint16{}, + "", + }, + { + `[ { "type": "uint16[2]" } ]`, + common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), + [2]uint16{1, 2}, + [2]uint16{}, + "", + }, + { + `[ { "type": "uint32[]" } ]`, + common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), + []uint32{1, 2}, + []uint32{}, + "", + }, + { + `[ { "type": "uint32[2]" } ]`, + common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), + [2]uint32{1, 2}, + [2]uint32{}, + "", + }, + { + `[ { "type": "uint64[]" } ]`, + common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), + []uint64{1, 2}, + []uint64{}, + "", + }, + { + `[ { "type": "uint64[2]" } ]`, + common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), + [2]uint64{1, 2}, + [2]uint64{}, + "", + }, + { + `[ { "type": "uint256[]" } ]`, + common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), + []*big.Int{big.NewInt(1), big.NewInt(2)}, + []*big.Int{}, + "", + }, + { + `[ { "type": "uint256[3]" } ]`, + append(pad([]byte{1}, 32, true), append(pad([]byte{2}, 32, true), pad([]byte{3}, 32, true)...)...), + [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)}, + [3]*big.Int{}, + "", + }, + { + `[ { "type": "int8[]" } ]`, + common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), + []int8{1, 2}, + []int8{}, + "", + }, + { + `[ { "type": "int8[2]" } ]`, + common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), + [2]int8{1, 2}, + [2]int8{}, + "", + }, + { + `[ { "type": "int16[]" } ]`, + common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), + []int16{1, 2}, + []int16{}, + "", + }, + { + `[ { "type": "int16[2]" } ]`, + common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), + [2]int16{1, 2}, + [2]int16{}, + "", + }, + { + `[ { "type": "int32[]" } ]`, + common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), + []int32{1, 2}, + []int32{}, + "", + }, + { + `[ { "type": "int32[2]" } ]`, + common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), + [2]int32{1, 2}, + [2]int32{}, + "", + }, + { + `[ { "type": "int64[]" } ]`, + common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), + []int64{1, 2}, + []int64{}, + "", + }, + { + `[ { "type": "int64[2]" } ]`, + common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), + [2]int64{1, 2}, + [2]int64{}, + "", + }, + { + `[ { "type": "int256[]" } ]`, + common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), + []*big.Int{big.NewInt(1), big.NewInt(2)}, + []*big.Int{}, + "", + }, + { + `[ { "type": "int256[3]" } ]`, + common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003"), + [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)}, + [3]*big.Int{}, + "", + }, + } { + + 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 + } + + err = abi.Unpack(&test.outVar, "method", test.marshalledOutput) + + 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, test.outVar) { + t.Errorf("%d failed. Output error: expected %v, got %v", i, test.expectedOut, test.outVar) + } + } + + } +} + func TestUnpackSetInterfaceSlice(t *testing.T) { var ( var1 = new(uint8) From e75ba6857d60aed58f5e4a872114704d73c941ed Mon Sep 17 00:00:00 2001 From: RJ Catalano Date: Fri, 30 Jun 2017 11:54:04 -0500 Subject: [PATCH 2/3] accounts/abi: redo types so that they get the actual type Signed-off-by: RJ Catalano --- accounts/abi/type.go | 210 +++++++++++++++++++------------------- accounts/abi/type_test.go | 208 +++++++++++++++++++++++++++++-------- 2 files changed, 269 insertions(+), 149 deletions(-) diff --git a/accounts/abi/type.go b/accounts/abi/type.go index 5f20babb3fc6..fba10b96d2fb 100644 --- a/accounts/abi/type.go +++ b/accounts/abi/type.go @@ -21,6 +21,7 @@ import ( "reflect" "regexp" "strconv" + "strings" ) const ( @@ -29,6 +30,7 @@ const ( BoolTy StringTy SliceTy + ArrayTy AddressTy FixedBytesTy BytesTy @@ -39,9 +41,6 @@ const ( // Type is the reflection of the supported argument type type Type struct { - IsSlice, IsArray bool - SliceSize int - Elem *Type Kind reflect.Kind @@ -53,118 +52,116 @@ type Type struct { } var ( - // fullTypeRegex parses the abi types - // - // Types can be in the format of: - // - // Input = Type [ "[" [ Number ] "]" ] Name . - // Type = [ "u" ] "int" [ Number ] [ x ] [ Number ]. - // - // Examples: - // - // string int uint fixed - // string32 int8 uint8 uint[] - // address int256 uint256 fixed128x128[2] - fullTypeRegex = regexp.MustCompile(`([a-zA-Z0-9]+)(\[([0-9]*)\])?`) // typeRegex parses the abi sub types typeRegex = regexp.MustCompile("([a-zA-Z]+)(([0-9]+)(x([0-9]+))?)?") ) // NewType creates a new reflection type of abi type given in t. func NewType(t string) (typ Type, err error) { - res := fullTypeRegex.FindAllStringSubmatch(t, -1)[0] - // check if type is slice and parse type. - switch { - case res[3] != "": - // err is ignored. Already checked for number through the regexp - typ.SliceSize, _ = strconv.Atoi(res[3]) - typ.IsArray = true - case res[2] != "": - typ.IsSlice, typ.SliceSize = true, -1 - case res[0] == "": - return Type{}, fmt.Errorf("abi: type parse error: %s", t) + // check that array brackets are equal if they exist + if strings.Count(t, "[") != strings.Count(t, "]") { + return Type{}, fmt.Errorf("invalid arg type in abi") } - if typ.IsArray || typ.IsSlice { - sliceType, err := NewType(res[1]) + + typ.stringKind = t + + // if there are brackets, get ready to go into slice/array mode and + // recursively create the type + if strings.Count(t, "[") != 0 { + i := strings.LastIndex(t, "[") + // recursively embed the type + embeddedType, err := NewType(t[:i]) if err != nil { return Type{}, err } - typ.Elem = &sliceType - typ.stringKind = sliceType.stringKind + t[len(res[1]):] - // Although we know that this is an array, we cannot return - // as we don't know the type of the element, however, if it - // is still an array, then don't determine the type. - if typ.Elem.IsArray || typ.Elem.IsSlice { - return typ, nil - } - } - - // parse the type and size of the abi-type. - parsedType := typeRegex.FindAllStringSubmatch(res[1], -1)[0] - // varSize is the size of the variable - var varSize int - if len(parsedType[3]) > 0 { - var err error - varSize, err = strconv.Atoi(parsedType[2]) - if err != nil { - return Type{}, fmt.Errorf("abi: error parsing variable size: %v", err) + // grab the last cell and create a type from there + sliced := t[i:] + // grab the slice size with regexp + re := regexp.MustCompile("[0-9]+") + intz := re.FindAllString(sliced, -1) + + if len(intz) == 0 { + // is a slice + typ.T = SliceTy + typ.Kind = reflect.Slice + typ.Elem = &embeddedType + typ.Type = reflect.SliceOf(embeddedType.Type) + } else if len(intz) == 1 { + // is a array + typ.T = ArrayTy + typ.Kind = reflect.Array + typ.Elem = &embeddedType + typ.Size, err = strconv.Atoi(intz[0]) + if err != nil { + return Type{}, fmt.Errorf("abi: error parsing variable size: %v", err) + } + typ.Type = reflect.ArrayOf(typ.Size, embeddedType.Type) + } else { + return Type{}, fmt.Errorf("invalid formatting of array type") } - } - // varType is the parsed abi type - varType := parsedType[1] - // substitute canonical integer - if varSize == 0 && (varType == "int" || varType == "uint") { - varSize = 256 - t += "256" - } - - // only set stringKind if not array or slice, as for those, - // the correct string type has been set - if !(typ.IsArray || typ.IsSlice) { - typ.stringKind = t - } - - switch varType { - case "int": - typ.Kind, typ.Type = reflectIntKindAndType(false, varSize) - typ.Size = varSize - typ.T = IntTy - case "uint": - typ.Kind, typ.Type = reflectIntKindAndType(true, varSize) - typ.Size = varSize - typ.T = UintTy - case "bool": - typ.Kind = reflect.Bool - typ.T = BoolTy - case "address": - typ.Kind = reflect.Array - typ.Type = address_t - typ.Size = 20 - typ.T = AddressTy - case "string": - typ.Kind = reflect.String - typ.Size = -1 - typ.T = StringTy - case "bytes": - sliceType, _ := NewType("uint8") - typ.Elem = &sliceType - if varSize == 0 { - typ.IsSlice = true - typ.T = BytesTy - typ.SliceSize = -1 + return typ, err + } else { + // parse the type and size of the abi-type. + parsedType := typeRegex.FindAllStringSubmatch(t, -1)[0] + // varSize is the size of the variable + var varSize int + if len(parsedType[3]) > 0 { + var err error + varSize, err = strconv.Atoi(parsedType[2]) + if err != nil { + return Type{}, fmt.Errorf("abi: error parsing variable size: %v", err) + } } else { - typ.IsArray = true - typ.T = FixedBytesTy - typ.SliceSize = varSize + if parsedType[0] == "uint" || parsedType[0] == "int" { + // this should fail because it means that there's something wrong with + // the abi type (the compiler should always format it to the size...always) + return Type{}, fmt.Errorf("unsupported arg type: %s", t) + } + } + // varType is the parsed abi type + varType := parsedType[1] + + switch varType { + case "int": + typ.Kind, typ.Type = reflectIntKindAndType(false, varSize) + typ.Size = varSize + typ.T = IntTy + case "uint": + typ.Kind, typ.Type = reflectIntKindAndType(true, varSize) + typ.Size = varSize + typ.T = UintTy + case "bool": + typ.Kind = reflect.Bool + typ.T = BoolTy + typ.Type = reflect.TypeOf(bool(false)) + case "address": + typ.Kind = reflect.Array + typ.Type = address_t + typ.Size = 20 + typ.T = AddressTy + case "string": + typ.Kind = reflect.String + typ.Type = reflect.TypeOf("") + typ.T = StringTy + case "bytes": + if varSize == 0 { + typ.T = BytesTy + typ.Kind = reflect.Slice + typ.Type = reflect.SliceOf(reflect.TypeOf(byte(0))) + } else { + typ.T = FixedBytesTy + typ.Kind = reflect.Array + typ.Size = varSize + typ.Type = reflect.ArrayOf(varSize, reflect.TypeOf(byte(0))) + } + case "function": + typ.Kind = reflect.Array + typ.T = FunctionTy + typ.Size = 24 + typ.Type = reflect.ArrayOf(24, reflect.TypeOf(byte(0))) + default: + return Type{}, fmt.Errorf("unsupported arg type: %s", t) } - case "function": - sliceType, _ := NewType("uint8") - typ.Elem = &sliceType - typ.IsArray = true - typ.T = FunctionTy - typ.SliceSize = 24 - default: - return Type{}, fmt.Errorf("unsupported arg type: %s", t) } return @@ -183,7 +180,7 @@ func (t Type) pack(v reflect.Value) ([]byte, error) { return nil, err } - if (t.IsSlice || t.IsArray) && t.T != BytesTy && t.T != FixedBytesTy && t.T != FunctionTy { + if t.T == SliceTy || t.T == ArrayTy { var packed []byte for i := 0; i < v.Len(); i++ { @@ -193,18 +190,17 @@ func (t Type) pack(v reflect.Value) ([]byte, error) { } packed = append(packed, val...) } - if t.IsSlice { + if t.T == SliceTy { return packBytesSlice(packed, v.Len()), nil - } else if t.IsArray { + } else if t.T == ArrayTy { return packed, nil } } - return packElement(t, v), nil } // requireLengthPrefix returns whether the type requires any sort of length // prefixing. func (t Type) requiresLengthPrefix() bool { - return t.T != FixedBytesTy && (t.T == StringTy || t.T == BytesTy || t.IsSlice) + return t.T == StringTy || t.T == BytesTy || t.T == SliceTy } diff --git a/accounts/abi/type_test.go b/accounts/abi/type_test.go index 984a5bb4c13e..7e85a3c513bd 100644 --- a/accounts/abi/type_test.go +++ b/accounts/abi/type_test.go @@ -34,50 +34,58 @@ func TestTypeRegexp(t *testing.T) { blob string kind Type }{ - {"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]"}}, + {"bool", Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}}, + {"bool[]", Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]bool(nil)), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[]"}}, + {"bool[2]", Type{Size: 2, Kind: reflect.Array, T: ArrayTy, Type: reflect.TypeOf([2]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[2]"}}, + {"bool[2][]", Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([][2]bool{}), Elem: &Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][]"}}, + {"bool[][]", Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([][]bool{}), Elem: &Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][]"}}, + {"bool[][2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][]bool{}), Elem: &Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][2]"}}, + {"bool[2][2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][2]bool{}), Elem: &Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][2]"}}, + {"bool[2][][2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][][2]bool{}), Elem: &Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([][2]bool{}), Elem: &Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][]"}, stringKind: "bool[2][][2]"}}, + {"bool[2][2][2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][2][2]bool{}), Elem: &Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][2]bool{}), Elem: &Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][2]"}, stringKind: "bool[2][2][2]"}}, + {"bool[][][]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([][][]bool{}), Elem: &Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([][]bool{}), Elem: &Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][]"}, stringKind: "bool[][][]"}}, + {"bool[][2][]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([][2][]bool{}), Elem: &Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][]bool{}), Elem: &Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][2]"}, 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"}}, - {"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]"}}, + {"int8[]", Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]int8{}), Elem: &Type{Kind: reflect.Int8, Type: int8_t, Size: 8, T: IntTy, stringKind: "int8"}, stringKind: "int8[]"}}, + {"int8[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]int8{}), Elem: &Type{Kind: reflect.Int8, Type: int8_t, Size: 8, T: IntTy, stringKind: "int8"}, stringKind: "int8[2]"}}, + {"int16[]", Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]int16{}), Elem: &Type{Kind: reflect.Int16, Type: int16_t, Size: 16, T: IntTy, stringKind: "int16"}, stringKind: "int16[]"}}, + {"int16[2]", Type{Size: 2, Kind: reflect.Array, T: ArrayTy, Type: reflect.TypeOf([2]int16{}), Elem: &Type{Kind: reflect.Int16, Type: int16_t, Size: 16, T: IntTy, stringKind: "int16"}, stringKind: "int16[2]"}}, + {"int32[]", Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]int32{}), Elem: &Type{Kind: reflect.Int32, Type: int32_t, Size: 32, T: IntTy, stringKind: "int32"}, stringKind: "int32[]"}}, + {"int32[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]int32{}), Elem: &Type{Kind: reflect.Int32, Type: int32_t, Size: 32, T: IntTy, stringKind: "int32"}, stringKind: "int32[2]"}}, + {"int64[]", Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]int64{}), Elem: &Type{Kind: reflect.Int64, Type: int64_t, Size: 64, T: IntTy, stringKind: "int64"}, stringKind: "int64[]"}}, + {"int64[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]int64{}), Elem: &Type{Kind: reflect.Int64, Type: int64_t, Size: 64, T: IntTy, stringKind: "int64"}, stringKind: "int64[2]"}}, + {"int256[]", Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]*big.Int{}), Elem: &Type{Kind: reflect.Ptr, Type: big_t, Size: 256, T: IntTy, stringKind: "int256"}, stringKind: "int256[]"}}, + {"int256[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]*big.Int{}), 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]"}}, + {"uint8[]", Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]uint8{}), Elem: &Type{Kind: reflect.Uint8, Type: uint8_t, Size: 8, T: UintTy, stringKind: "uint8"}, stringKind: "uint8[]"}}, + {"uint8[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]uint8{}), Elem: &Type{Kind: reflect.Uint8, Type: uint8_t, Size: 8, T: UintTy, stringKind: "uint8"}, stringKind: "uint8[2]"}}, + {"uint16[]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]uint16{}), Elem: &Type{Kind: reflect.Uint16, Type: uint16_t, Size: 16, T: UintTy, stringKind: "uint16"}, stringKind: "uint16[]"}}, + {"uint16[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]uint16{}), Elem: &Type{Kind: reflect.Uint16, Type: uint16_t, Size: 16, T: UintTy, stringKind: "uint16"}, stringKind: "uint16[2]"}}, + {"uint32[]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]uint32{}), Elem: &Type{Kind: reflect.Uint32, Type: uint32_t, Size: 32, T: UintTy, stringKind: "uint32"}, stringKind: "uint32[]"}}, + {"uint32[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]uint32{}), Elem: &Type{Kind: reflect.Uint32, Type: uint32_t, Size: 32, T: UintTy, stringKind: "uint32"}, stringKind: "uint32[2]"}}, + {"uint64[]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]uint64{}), Elem: &Type{Kind: reflect.Uint64, Type: uint64_t, Size: 64, T: UintTy, stringKind: "uint64"}, stringKind: "uint64[]"}}, + {"uint64[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]uint64{}), Elem: &Type{Kind: reflect.Uint64, Type: uint64_t, Size: 64, T: UintTy, stringKind: "uint64"}, stringKind: "uint64[2]"}}, + {"uint256[]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]*big.Int{}), Elem: &Type{Kind: reflect.Ptr, Type: big_t, Size: 256, T: UintTy, stringKind: "uint256"}, stringKind: "uint256[]"}}, + {"uint256[2]", Type{Kind: reflect.Array, T: ArrayTy, Type: reflect.TypeOf([2]*big.Int{}), Size: 2, Elem: &Type{Kind: reflect.Ptr, Type: big_t, Size: 256, T: UintTy, stringKind: "uint256"}, stringKind: "uint256[2]"}}, + {"bytes32", Type{Kind: reflect.Array, T: FixedBytesTy, Size: 32, Type: reflect.TypeOf([32]byte{}), stringKind: "bytes32"}}, + {"bytes[]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([][]byte{}), Elem: &Type{Kind: reflect.Slice, Type: reflect.TypeOf([]byte{}), T: BytesTy, stringKind: "bytes"}, stringKind: "bytes[]"}}, + {"bytes[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][]byte{}), Elem: &Type{T: BytesTy, Type: reflect.TypeOf([]byte{}), Kind: reflect.Slice, stringKind: "bytes"}, stringKind: "bytes[2]"}}, + {"bytes32[]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([][32]byte{}), Elem: &Type{Kind: reflect.Array, Type: reflect.TypeOf([32]byte{}), T: FixedBytesTy, Size: 32, stringKind: "bytes32"}, stringKind: "bytes32[]"}}, + {"bytes32[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][32]byte{}), Elem: &Type{Kind: reflect.Array, T: FixedBytesTy, Size: 32, Type: reflect.TypeOf([32]byte{}), stringKind: "bytes32"}, stringKind: "bytes32[2]"}}, + {"string", Type{Kind: reflect.String, T: StringTy, Type: reflect.TypeOf(""), stringKind: "string"}}, + {"string[]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]string{}), Elem: &Type{Kind: reflect.String, Type: reflect.TypeOf(""), T: StringTy, stringKind: "string"}, stringKind: "string[]"}}, + {"string[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]string{}), Elem: &Type{Kind: reflect.String, T: StringTy, Type: reflect.TypeOf(""), stringKind: "string"}, stringKind: "string[2]"}}, {"address", Type{Kind: reflect.Array, Type: address_t, Size: 20, T: AddressTy, stringKind: "address"}}, - {"address[]", Type{IsSlice: true, SliceSize: -1, Kind: reflect.Array, Type: address_t, T: AddressTy, Size: 20, Elem: &Type{Kind: reflect.Array, Type: address_t, Size: 20, T: AddressTy, stringKind: "address"}, stringKind: "address[]"}}, - {"address[2]", Type{IsArray: true, SliceSize: 2, Kind: reflect.Array, Type: address_t, T: AddressTy, Size: 20, Elem: &Type{Kind: reflect.Array, Type: address_t, Size: 20, T: AddressTy, stringKind: "address"}, stringKind: "address[2]"}}, + {"address[]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]common.Address{}), Elem: &Type{Kind: reflect.Array, Type: address_t, Size: 20, T: AddressTy, stringKind: "address"}, stringKind: "address[]"}}, + {"address[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]common.Address{}), Elem: &Type{Kind: reflect.Array, Type: address_t, Size: 20, T: AddressTy, stringKind: "address"}, stringKind: "address[2]"}}, // TODO when fixed types are implemented properly // {"fixed", Type{}}, @@ -93,7 +101,7 @@ func TestTypeRegexp(t *testing.T) { t.Errorf("type %d: failed to parse type string: %v", i, err) } if !reflect.DeepEqual(typ, tt.kind) { - t.Errorf("type %d: parsed type mismatch:\n have %+v\n want %+v", i, typeWithoutStringer(typ), typeWithoutStringer(tt.kind)) + t.Errorf("type %d: parsed type mismatch:\n have %+v\n want %+v.\n more details:\n have %v\n want %v\n have %v\n want %v\n ", i, typeWithoutStringer(typ), typeWithoutStringer(tt.kind), typeWithoutStringer(*typ.Elem), typeWithoutStringer(*tt.kind.Elem)) } } } @@ -104,15 +112,90 @@ func TestTypeCheck(t *testing.T) { input interface{} err string }{ - {"uint", big.NewInt(1), ""}, - {"int", big.NewInt(1), ""}, - {"uint30", big.NewInt(1), ""}, + {"uint", big.NewInt(1), "unsupported arg type: uint"}, + {"int", big.NewInt(1), "unsupported arg type: int"}, + {"uint256", big.NewInt(1), ""}, + {"uint256[][3][]", [][3][]*big.Int{{{}}}, ""}, + {"uint256[][][3]", [3][][]*big.Int{{{}}}, ""}, + {"uint256[3][][]", [][][3]*big.Int{{{}}}, ""}, + {"uint256[3][3][3]", [3][3][3]*big.Int{{{}}}, ""}, + {"uint8[][]", [][]uint8{}, ""}, + {"int256", big.NewInt(1), ""}, + {"uint8", uint8(1), ""}, + {"uint16", uint16(1), ""}, + {"uint32", uint32(1), ""}, + {"uint64", uint64(1), ""}, + {"int8", int8(1), ""}, + {"int16", int16(1), ""}, + {"int32", int32(1), ""}, + {"int64", int64(1), ""}, + {"uint24", big.NewInt(1), ""}, + {"uint40", big.NewInt(1), ""}, + {"uint48", big.NewInt(1), ""}, + {"uint56", big.NewInt(1), ""}, + {"uint72", big.NewInt(1), ""}, + {"uint80", big.NewInt(1), ""}, + {"uint88", big.NewInt(1), ""}, + {"uint96", big.NewInt(1), ""}, + {"uint104", big.NewInt(1), ""}, + {"uint112", big.NewInt(1), ""}, + {"uint120", big.NewInt(1), ""}, + {"uint128", big.NewInt(1), ""}, + {"uint136", big.NewInt(1), ""}, + {"uint144", big.NewInt(1), ""}, + {"uint152", big.NewInt(1), ""}, + {"uint160", big.NewInt(1), ""}, + {"uint168", big.NewInt(1), ""}, + {"uint176", big.NewInt(1), ""}, + {"uint184", big.NewInt(1), ""}, + {"uint192", big.NewInt(1), ""}, + {"uint200", big.NewInt(1), ""}, + {"uint208", big.NewInt(1), ""}, + {"uint216", big.NewInt(1), ""}, + {"uint224", big.NewInt(1), ""}, + {"uint232", big.NewInt(1), ""}, + {"uint240", big.NewInt(1), ""}, + {"uint248", big.NewInt(1), ""}, + {"int24", big.NewInt(1), ""}, + {"int40", big.NewInt(1), ""}, + {"int48", big.NewInt(1), ""}, + {"int56", big.NewInt(1), ""}, + {"int72", big.NewInt(1), ""}, + {"int80", big.NewInt(1), ""}, + {"int88", big.NewInt(1), ""}, + {"int96", big.NewInt(1), ""}, + {"int104", big.NewInt(1), ""}, + {"int112", big.NewInt(1), ""}, + {"int120", big.NewInt(1), ""}, + {"int128", big.NewInt(1), ""}, + {"int136", big.NewInt(1), ""}, + {"int144", big.NewInt(1), ""}, + {"int152", big.NewInt(1), ""}, + {"int160", big.NewInt(1), ""}, + {"int168", big.NewInt(1), ""}, + {"int176", big.NewInt(1), ""}, + {"int184", big.NewInt(1), ""}, + {"int192", big.NewInt(1), ""}, + {"int200", big.NewInt(1), ""}, + {"int208", big.NewInt(1), ""}, + {"int216", big.NewInt(1), ""}, + {"int224", big.NewInt(1), ""}, + {"int232", big.NewInt(1), ""}, + {"int240", big.NewInt(1), ""}, + {"int248", big.NewInt(1), ""}, {"uint30", uint8(1), "abi: cannot use uint8 as type ptr as argument"}, + {"uint8", uint16(1), "abi: cannot use uint16 as type uint8 as argument"}, + {"uint8", uint32(1), "abi: cannot use uint32 as type uint8 as argument"}, + {"uint8", uint64(1), "abi: cannot use uint64 as type uint8 as argument"}, + {"uint8", int8(1), "abi: cannot use int8 as type uint8 as argument"}, + {"uint8", int16(1), "abi: cannot use int16 as type uint8 as argument"}, + {"uint8", int32(1), "abi: cannot use int32 as type uint8 as argument"}, + {"uint8", int64(1), "abi: cannot use int64 as type uint8 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[]", []uint32{1, 2, 3}, "abi: cannot use []uint32 as type [0]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}, ""}, @@ -122,20 +205,61 @@ func TestTypeCheck(t *testing.T) { {"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{}, ""}, + {"bytes31", [31]byte{}, ""}, + {"bytes30", [30]byte{}, ""}, + {"bytes29", [29]byte{}, ""}, + {"bytes28", [28]byte{}, ""}, + {"bytes27", [27]byte{}, ""}, + {"bytes26", [26]byte{}, ""}, + {"bytes25", [25]byte{}, ""}, + {"bytes24", [24]byte{}, ""}, + {"bytes23", [23]byte{}, ""}, + {"bytes22", [22]byte{}, ""}, + {"bytes21", [21]byte{}, ""}, + {"bytes20", [20]byte{}, ""}, + {"bytes19", [19]byte{}, ""}, + {"bytes18", [18]byte{}, ""}, + {"bytes17", [17]byte{}, ""}, + {"bytes16", [16]byte{}, ""}, + {"bytes15", [15]byte{}, ""}, + {"bytes14", [14]byte{}, ""}, + {"bytes13", [13]byte{}, ""}, + {"bytes12", [12]byte{}, ""}, + {"bytes11", [11]byte{}, ""}, + {"bytes10", [10]byte{}, ""}, + {"bytes9", [9]byte{}, ""}, + {"bytes8", [8]byte{}, ""}, + {"bytes7", [7]byte{}, ""}, + {"bytes6", [6]byte{}, ""}, + {"bytes5", [5]byte{}, ""}, + {"bytes4", [4]byte{}, ""}, + {"bytes3", [3]byte{}, ""}, + {"bytes2", [2]byte{}, ""}, + {"bytes1", [1]byte{}, ""}, {"bytes32", [33]byte{}, "abi: cannot use [33]uint8 as type [32]uint8 as argument"}, {"bytes32", common.Hash{1}, ""}, - {"bytes31", [31]byte{}, ""}, + {"bytes31", common.Hash{1}, "abi: cannot use common.Hash as type [31]uint8 as argument"}, {"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}, ""}, + {"bytes", [2]byte{0, 1}, "abi: cannot use array as type slice as argument"}, + {"bytes", common.Hash{1}, "abi: cannot use array as type slice as argument"}, {"string", "hello world", ""}, + {"string", string(""), ""}, + {"string", []byte{}, "abi: cannot use slice as type string as argument"}, {"bytes32[]", [][32]byte{{}}, ""}, {"function", [24]byte{}, ""}, + {"bytes20", common.Address{}, ""}, + {"address", [20]byte{}, ""}, + {"address", common.Address{}, ""}, } { typ, err := NewType(test.typ) - if err != nil { + if err != nil && len(test.err) == 0 { t.Fatal("unexpected parse error:", err) + } else if err != nil && len(test.err) != 0 { + if err.Error() != test.err { + t.Errorf("%d failed. Expected err: '%v' got err: '%v'", i, test.err, err) + } + continue } err = typeCheck(typ, reflect.ValueOf(test.input)) From bd9e77f5df7f574d985ed7fa1028c17ad9f2c232 Mon Sep 17 00:00:00 2001 From: RJ Catalano Date: Fri, 30 Jun 2017 11:55:48 -0500 Subject: [PATCH 3/3] accounts/abi: fixups to implement the previous changes Signed-off-by: RJ Catalano --- accounts/abi/abi_test.go | 2 +- accounts/abi/error.go | 21 +++++++++++++-------- accounts/abi/event_test.go | 2 +- accounts/abi/numbers.go | 32 ++++++++++++++++---------------- accounts/abi/pack_test.go | 4 ++-- accounts/abi/reflect.go | 6 +++--- 6 files changed, 36 insertions(+), 31 deletions(-) diff --git a/accounts/abi/abi_test.go b/accounts/abi/abi_test.go index a3aa9446ef92..2f687969bcac 100644 --- a/accounts/abi/abi_test.go +++ b/accounts/abi/abi_test.go @@ -191,7 +191,7 @@ func TestMethodSignature(t *testing.T) { t.Errorf("expected ids to match %x != %x", m.Id(), idexp) } - uintt, _ := NewType("uint") + uintt, _ := NewType("uint256") m = Method{"foo", false, []Argument{{"bar", uintt, false}}, nil} exp = "foo(uint256)" if m.Sig() != exp { diff --git a/accounts/abi/error.go b/accounts/abi/error.go index 420acf418259..2333b8bfa0b8 100644 --- a/accounts/abi/error.go +++ b/accounts/abi/error.go @@ -39,22 +39,23 @@ func formatSliceString(kind reflect.Kind, sliceSize int) string { // type in t. func sliceTypeCheck(t Type, val reflect.Value) error { if val.Kind() != reflect.Slice && val.Kind() != reflect.Array { - return typeErr(formatSliceString(t.Kind, t.SliceSize), val.Type()) + return typeErr(formatSliceString(t.Kind, t.Size), val.Type()) } - if t.IsArray && val.Len() != t.SliceSize { - return typeErr(formatSliceString(t.Elem.Kind, t.SliceSize), formatSliceString(val.Type().Elem().Kind(), val.Len())) + + if t.T == ArrayTy && val.Len() != t.Size { + return typeErr(formatSliceString(t.Elem.Kind, t.Size), formatSliceString(val.Type().Elem().Kind(), val.Len())) } - if t.Elem.IsSlice { + if t.Elem.T == SliceTy { if val.Len() > 0 { return sliceTypeCheck(*t.Elem, val.Index(0)) } - } else if t.Elem.IsArray { + } else if t.Elem.T == ArrayTy { return sliceTypeCheck(*t.Elem, val.Index(0)) } if elemKind := val.Type().Elem().Kind(); elemKind != t.Elem.Kind { - return typeErr(formatSliceString(t.Elem.Kind, t.SliceSize), val.Type()) + return typeErr(formatSliceString(t.Elem.Kind, t.Size), val.Type()) } return nil } @@ -62,15 +63,19 @@ func sliceTypeCheck(t Type, val reflect.Value) error { // typeCheck checks that the given reflection value can be assigned to the reflection // type in t. func typeCheck(t Type, value reflect.Value) error { - if t.IsSlice || t.IsArray { + if t.T == SliceTy || t.T == ArrayTy { return sliceTypeCheck(t, value) } // Check base type validity. Element types will be checked later on. if t.Kind != value.Kind() { return typeErr(t.Kind, value.Kind()) + } else if t.T == FixedBytesTy && t.Size != value.Len() { + return typeErr(t.Type, value.Type()) + } else { + return nil } - return nil + } // varErr returns a formatted error. diff --git a/accounts/abi/event_test.go b/accounts/abi/event_test.go index b5054a0329a9..7e2f13f76320 100644 --- a/accounts/abi/event_test.go +++ b/accounts/abi/event_test.go @@ -31,7 +31,7 @@ func TestEventId(t *testing.T) { }{ { definition: `[ - { "type" : "event", "name" : "balance", "inputs": [{ "name" : "in", "type": "uint" }] }, + { "type" : "event", "name" : "balance", "inputs": [{ "name" : "in", "type": "uint256" }] }, { "type" : "event", "name" : "check", "inputs": [{ "name" : "t", "type": "address" }, { "name": "b", "type": "uint256" }] } ]`, expectations: map[string]common.Hash{ diff --git a/accounts/abi/numbers.go b/accounts/abi/numbers.go index 5d3efff52ed8..91f2c5d2a47a 100644 --- a/accounts/abi/numbers.go +++ b/accounts/abi/numbers.go @@ -25,22 +25,22 @@ import ( ) var ( - big_t = reflect.TypeOf(big.Int{}) - ubig_t = reflect.TypeOf(big.Int{}) - byte_t = reflect.TypeOf(byte(0)) - byte_ts = reflect.TypeOf([]byte(nil)) - uint_t = reflect.TypeOf(uint(0)) - uint8_t = reflect.TypeOf(uint8(0)) - uint16_t = reflect.TypeOf(uint16(0)) - uint32_t = reflect.TypeOf(uint32(0)) - uint64_t = reflect.TypeOf(uint64(0)) - int_t = reflect.TypeOf(int(0)) - int8_t = reflect.TypeOf(int8(0)) - int16_t = reflect.TypeOf(int16(0)) - int32_t = reflect.TypeOf(int32(0)) - int64_t = reflect.TypeOf(int64(0)) - hash_t = reflect.TypeOf(common.Hash{}) - address_t = reflect.TypeOf(common.Address{}) + big_t = reflect.TypeOf(&big.Int{}) + derefbig_t = reflect.TypeOf(big.Int{}) + byte_t = reflect.TypeOf(byte(0)) + byte_ts = reflect.TypeOf([]byte(nil)) + uint_t = reflect.TypeOf(uint(0)) + uint8_t = reflect.TypeOf(uint8(0)) + uint16_t = reflect.TypeOf(uint16(0)) + uint32_t = reflect.TypeOf(uint32(0)) + uint64_t = reflect.TypeOf(uint64(0)) + int_t = reflect.TypeOf(int(0)) + int8_t = reflect.TypeOf(int8(0)) + int16_t = reflect.TypeOf(int16(0)) + int32_t = reflect.TypeOf(int32(0)) + int64_t = reflect.TypeOf(int64(0)) + hash_t = reflect.TypeOf(common.Hash{}) + address_t = reflect.TypeOf(common.Address{}) uint_ts = reflect.TypeOf([]uint(nil)) uint8_ts = reflect.TypeOf([]uint8(nil)) diff --git a/accounts/abi/pack_test.go b/accounts/abi/pack_test.go index c6cfb56ea053..fcb5c75a8574 100644 --- a/accounts/abi/pack_test.go +++ b/accounts/abi/pack_test.go @@ -322,12 +322,12 @@ func TestPack(t *testing.T) { } { typ, err := NewType(test.typ) if err != nil { - t.Fatal("unexpected parse error:", err) + t.Fatalf("%v failed. Unexpected parse error: %v", i, err) } output, err := typ.pack(reflect.ValueOf(test.input)) if err != nil { - t.Fatal("unexpected pack error:", err) + t.Fatalf("%v failed. Unexpected pack error: %v", i, err) } if !bytes.Equal(output, test.output) { diff --git a/accounts/abi/reflect.go b/accounts/abi/reflect.go index 8fa75df07fc7..104d94232daa 100644 --- a/accounts/abi/reflect.go +++ b/accounts/abi/reflect.go @@ -24,7 +24,7 @@ import ( // indirect recursively dereferences the value until it either gets the value // or finds a big.Int func indirect(v reflect.Value) reflect.Value { - if v.Kind() == reflect.Ptr && v.Elem().Type() != big_t { + if v.Kind() == reflect.Ptr && v.Elem().Type() != derefbig_t { return indirect(v.Elem()) } return v @@ -78,8 +78,8 @@ func set(dst, src reflect.Value, output Argument) error { case dstType.AssignableTo(src.Type()): dst.Set(src) case dstType.Kind() == reflect.Array && srcType.Kind() == reflect.Slice: - if dst.Len() < output.Type.SliceSize { - return fmt.Errorf("abi: cannot unmarshal src (len=%d) in to dst (len=%d)", output.Type.SliceSize, dst.Len()) + if dst.Len() < output.Type.Size { + return fmt.Errorf("abi: cannot unmarshal src (len=%d) in to dst (len=%d)", output.Type.Size, dst.Len()) } reflect.Copy(dst, src) case dstType.Kind() == reflect.Interface: