Unverified Commit e872083d authored by Marius van der Wijden's avatar Marius van der Wijden Committed by GitHub

accounts/abi: removed Kind from Type struct (#21009)

* accounts/abi: removed Kind from Type struct

* accounts/abi: removed unused code
parent 510b6f90
...@@ -39,11 +39,11 @@ func formatSliceString(kind reflect.Kind, sliceSize int) string { ...@@ -39,11 +39,11 @@ func formatSliceString(kind reflect.Kind, sliceSize int) string {
// type in t. // type in t.
func sliceTypeCheck(t Type, val reflect.Value) error { func sliceTypeCheck(t Type, val reflect.Value) error {
if val.Kind() != reflect.Slice && val.Kind() != reflect.Array { if val.Kind() != reflect.Slice && val.Kind() != reflect.Array {
return typeErr(formatSliceString(t.Kind, t.Size), val.Type()) return typeErr(formatSliceString(t.Type.Kind(), t.Size), val.Type())
} }
if t.T == ArrayTy && val.Len() != t.Size { if t.T == ArrayTy && val.Len() != t.Size {
return typeErr(formatSliceString(t.Elem.Kind, t.Size), formatSliceString(val.Type().Elem().Kind(), val.Len())) return typeErr(formatSliceString(t.Elem.Type.Kind(), t.Size), formatSliceString(val.Type().Elem().Kind(), val.Len()))
} }
if t.Elem.T == SliceTy || t.Elem.T == ArrayTy { if t.Elem.T == SliceTy || t.Elem.T == ArrayTy {
...@@ -52,8 +52,8 @@ func sliceTypeCheck(t Type, val reflect.Value) error { ...@@ -52,8 +52,8 @@ func sliceTypeCheck(t Type, val reflect.Value) error {
} }
} }
if elemKind := val.Type().Elem().Kind(); elemKind != t.Elem.Kind { if elemKind := val.Type().Elem().Kind(); elemKind != t.Elem.Type.Kind() {
return typeErr(formatSliceString(t.Elem.Kind, t.Size), val.Type()) return typeErr(formatSliceString(t.Elem.Type.Kind(), t.Size), val.Type())
} }
return nil return nil
} }
...@@ -66,8 +66,8 @@ func typeCheck(t Type, value reflect.Value) error { ...@@ -66,8 +66,8 @@ func typeCheck(t Type, value reflect.Value) error {
} }
// Check base type validity. Element types will be checked later on. // Check base type validity. Element types will be checked later on.
if t.Kind != value.Kind() { if t.Type.Kind() != value.Kind() {
return typeErr(t.Kind, value.Kind()) return typeErr(t.Type.Kind(), value.Kind())
} else if t.T == FixedBytesTy && t.Size != value.Len() { } else if t.T == FixedBytesTy && t.Size != value.Len() {
return typeErr(t.Type, value.Type()) return typeErr(t.Type, value.Type())
} else { } else {
......
...@@ -39,32 +39,32 @@ func indirectInterfaceOrPtr(v reflect.Value) reflect.Value { ...@@ -39,32 +39,32 @@ func indirectInterfaceOrPtr(v reflect.Value) reflect.Value {
return v return v
} }
// reflectIntKind returns the reflect using the given size and // reflectIntType returns the reflect using the given size and
// unsignedness. // unsignedness.
func reflectIntKindAndType(unsigned bool, size int) (reflect.Kind, reflect.Type) { func reflectIntType(unsigned bool, size int) reflect.Type {
if unsigned { if unsigned {
switch size { switch size {
case 8: case 8:
return reflect.Uint8, uint8T return uint8T
case 16: case 16:
return reflect.Uint16, uint16T return uint16T
case 32: case 32:
return reflect.Uint32, uint32T return uint32T
case 64: case 64:
return reflect.Uint64, uint64T return uint64T
} }
} }
switch size { switch size {
case 8: case 8:
return reflect.Int8, int8T return int8T
case 16: case 16:
return reflect.Int16, int16T return int16T
case 32: case 32:
return reflect.Int32, int32T return int32T
case 64: case 64:
return reflect.Int64, int64T return int64T
} }
return reflect.Ptr, bigT return bigT
} }
// mustArrayToBytesSlice creates a new byte slice with the exact same size as value // mustArrayToBytesSlice creates a new byte slice with the exact same size as value
......
...@@ -45,7 +45,6 @@ const ( ...@@ -45,7 +45,6 @@ const (
// Type is the reflection of the supported argument type // Type is the reflection of the supported argument type
type Type struct { type Type struct {
Elem *Type Elem *Type
Kind reflect.Kind
Type reflect.Type Type reflect.Type
Size int Size int
T byte // Our own type checking T byte // Our own type checking
...@@ -94,14 +93,12 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty ...@@ -94,14 +93,12 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty
if len(intz) == 0 { if len(intz) == 0 {
// is a slice // is a slice
typ.T = SliceTy typ.T = SliceTy
typ.Kind = reflect.Slice
typ.Elem = &embeddedType typ.Elem = &embeddedType
typ.Type = reflect.SliceOf(embeddedType.Type) typ.Type = reflect.SliceOf(embeddedType.Type)
typ.stringKind = embeddedType.stringKind + sliced typ.stringKind = embeddedType.stringKind + sliced
} else if len(intz) == 1 { } else if len(intz) == 1 {
// is a array // is a array
typ.T = ArrayTy typ.T = ArrayTy
typ.Kind = reflect.Array
typ.Elem = &embeddedType typ.Elem = &embeddedType
typ.Size, err = strconv.Atoi(intz[0]) typ.Size, err = strconv.Atoi(intz[0])
if err != nil { if err != nil {
...@@ -139,34 +136,29 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty ...@@ -139,34 +136,29 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty
// varType is the parsed abi type // varType is the parsed abi type
switch varType := parsedType[1]; varType { switch varType := parsedType[1]; varType {
case "int": case "int":
typ.Kind, typ.Type = reflectIntKindAndType(false, varSize) typ.Type = reflectIntType(false, varSize)
typ.Size = varSize typ.Size = varSize
typ.T = IntTy typ.T = IntTy
case "uint": case "uint":
typ.Kind, typ.Type = reflectIntKindAndType(true, varSize) typ.Type = reflectIntType(true, varSize)
typ.Size = varSize typ.Size = varSize
typ.T = UintTy typ.T = UintTy
case "bool": case "bool":
typ.Kind = reflect.Bool
typ.T = BoolTy typ.T = BoolTy
typ.Type = reflect.TypeOf(bool(false)) typ.Type = reflect.TypeOf(bool(false))
case "address": case "address":
typ.Kind = reflect.Array
typ.Type = addressT typ.Type = addressT
typ.Size = 20 typ.Size = 20
typ.T = AddressTy typ.T = AddressTy
case "string": case "string":
typ.Kind = reflect.String
typ.Type = reflect.TypeOf("") typ.Type = reflect.TypeOf("")
typ.T = StringTy typ.T = StringTy
case "bytes": case "bytes":
if varSize == 0 { if varSize == 0 {
typ.T = BytesTy typ.T = BytesTy
typ.Kind = reflect.Slice
typ.Type = reflect.SliceOf(reflect.TypeOf(byte(0))) typ.Type = reflect.SliceOf(reflect.TypeOf(byte(0)))
} else { } else {
typ.T = FixedBytesTy typ.T = FixedBytesTy
typ.Kind = reflect.Array
typ.Size = varSize typ.Size = varSize
typ.Type = reflect.ArrayOf(varSize, reflect.TypeOf(byte(0))) typ.Type = reflect.ArrayOf(varSize, reflect.TypeOf(byte(0)))
} }
...@@ -199,7 +191,6 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty ...@@ -199,7 +191,6 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty
} }
} }
expression += ")" expression += ")"
typ.Kind = reflect.Struct
typ.Type = reflect.StructOf(fields) typ.Type = reflect.StructOf(fields)
typ.TupleElems = elems typ.TupleElems = elems
typ.TupleRawNames = names typ.TupleRawNames = names
...@@ -217,7 +208,6 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty ...@@ -217,7 +208,6 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty
} }
case "function": case "function":
typ.Kind = reflect.Array
typ.T = FunctionTy typ.T = FunctionTy
typ.Size = 24 typ.Size = 24
typ.Type = reflect.ArrayOf(24, reflect.TypeOf(byte(0))) typ.Type = reflect.ArrayOf(24, reflect.TypeOf(byte(0)))
......
This diff is collapsed.
...@@ -33,28 +33,28 @@ var ( ...@@ -33,28 +33,28 @@ var (
) )
// ReadInteger reads the integer based on its kind and returns the appropriate value // ReadInteger reads the integer based on its kind and returns the appropriate value
func ReadInteger(typ byte, kind reflect.Kind, b []byte) interface{} { func ReadInteger(typ Type, b []byte) interface{} {
switch kind { switch typ.Type {
case reflect.Uint8: case uint8T:
return b[len(b)-1] return b[len(b)-1]
case reflect.Uint16: case uint16T:
return binary.BigEndian.Uint16(b[len(b)-2:]) return binary.BigEndian.Uint16(b[len(b)-2:])
case reflect.Uint32: case uint32T:
return binary.BigEndian.Uint32(b[len(b)-4:]) return binary.BigEndian.Uint32(b[len(b)-4:])
case reflect.Uint64: case uint64T:
return binary.BigEndian.Uint64(b[len(b)-8:]) return binary.BigEndian.Uint64(b[len(b)-8:])
case reflect.Int8: case int8T:
return int8(b[len(b)-1]) return int8(b[len(b)-1])
case reflect.Int16: case int16T:
return int16(binary.BigEndian.Uint16(b[len(b)-2:])) return int16(binary.BigEndian.Uint16(b[len(b)-2:]))
case reflect.Int32: case int32T:
return int32(binary.BigEndian.Uint32(b[len(b)-4:])) return int32(binary.BigEndian.Uint32(b[len(b)-4:]))
case reflect.Int64: case int64T:
return int64(binary.BigEndian.Uint64(b[len(b)-8:])) return int64(binary.BigEndian.Uint64(b[len(b)-8:]))
default: default:
// the only case left for integer is int256/uint256. // the only case left for integer is int256/uint256.
ret := new(big.Int).SetBytes(b) ret := new(big.Int).SetBytes(b)
if typ == UintTy { if typ.T == UintTy {
return ret return ret
} }
// big.SetBytes can't tell if a number is negative or positive in itself. // big.SetBytes can't tell if a number is negative or positive in itself.
...@@ -228,7 +228,7 @@ func ToGoType(index int, t Type, output []byte) (interface{}, error) { ...@@ -228,7 +228,7 @@ func ToGoType(index int, t Type, output []byte) (interface{}, error) {
case StringTy: // variable arrays are written at the end of the return bytes case StringTy: // variable arrays are written at the end of the return bytes
return string(output[begin : begin+length]), nil return string(output[begin : begin+length]), nil
case IntTy, UintTy: case IntTy, UintTy:
return ReadInteger(t.T, t.Kind, returnOutput), nil return ReadInteger(t, returnOutput), nil
case BoolTy: case BoolTy:
return readBool(returnOutput) return readBool(returnOutput)
case AddressTy: case AddressTy:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment