Skip to content

Commit

Permalink
accounts/abi: removed Kind from Type struct (ethereum#21009)
Browse files Browse the repository at this point in the history
* accounts/abi: removed Kind from Type struct

* accounts/abi: removed unused code
  • Loading branch information
MariusVanDerWijden authored and enriquefynn committed Feb 15, 2021
1 parent 4ddb16b commit d13bd06
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 100 deletions.
12 changes: 6 additions & 6 deletions accounts/abi/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ 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.Size), val.Type())
return typeErr(formatSliceString(t.Type.Kind(), t.Size), val.Type())
}

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 {
Expand All @@ -52,8 +52,8 @@ func sliceTypeCheck(t Type, val reflect.Value) error {
}
}

if elemKind := val.Type().Elem().Kind(); elemKind != t.Elem.Kind {
return typeErr(formatSliceString(t.Elem.Kind, t.Size), val.Type())
if elemKind := val.Type().Elem().Kind(); elemKind != t.Elem.Type.Kind() {
return typeErr(formatSliceString(t.Elem.Type.Kind(), t.Size), val.Type())
}
return nil
}
Expand All @@ -66,8 +66,8 @@ func typeCheck(t Type, value reflect.Value) error {
}

// Check base type validity. Element types will be checked later on.
if t.Kind != value.Kind() {
return typeErr(t.Kind, value.Kind())
if t.Type.Kind() != value.Kind() {
return typeErr(t.Type.Kind(), value.Kind())
} else if t.T == FixedBytesTy && t.Size != value.Len() {
return typeErr(t.Type, value.Type())
} else {
Expand Down
22 changes: 11 additions & 11 deletions accounts/abi/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,32 @@ func indirectInterfaceOrPtr(v reflect.Value) reflect.Value {
return v
}

// reflectIntKind returns the reflect using the given size and
// reflectIntType returns the reflect using the given size and
// unsignedness.
func reflectIntKindAndType(unsigned bool, size int) (reflect.Kind, reflect.Type) {
func reflectIntType(unsigned bool, size int) reflect.Type {
if unsigned {
switch size {
case 8:
return reflect.Uint8, uint8T
return uint8T
case 16:
return reflect.Uint16, uint16T
return uint16T
case 32:
return reflect.Uint32, uint32T
return uint32T
case 64:
return reflect.Uint64, uint64T
return uint64T
}
}
switch size {
case 8:
return reflect.Int8, int8T
return int8T
case 16:
return reflect.Int16, int16T
return int16T
case 32:
return reflect.Int32, int32T
return int32T
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
Expand Down
14 changes: 2 additions & 12 deletions accounts/abi/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ const (
// Type is the reflection of the supported argument type
type Type struct {
Elem *Type
Kind reflect.Kind
Type reflect.Type
Size int
T byte // Our own type checking
Expand Down Expand Up @@ -94,14 +93,12 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty
if len(intz) == 0 {
// is a slice
typ.T = SliceTy
typ.Kind = reflect.Slice
typ.Elem = &embeddedType
typ.Type = reflect.SliceOf(embeddedType.Type)
typ.stringKind = embeddedType.stringKind + sliced
} 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 {
Expand Down Expand Up @@ -139,34 +136,29 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty
// varType is the parsed abi type
switch varType := parsedType[1]; varType {
case "int":
typ.Kind, typ.Type = reflectIntKindAndType(false, varSize)
typ.Type = reflectIntType(false, varSize)
typ.Size = varSize
typ.T = IntTy
case "uint":
typ.Kind, typ.Type = reflectIntKindAndType(true, varSize)
typ.Type = reflectIntType(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 = addressT
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)))
}
Expand Down Expand Up @@ -199,7 +191,6 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty
}
}
expression += ")"
typ.Kind = reflect.Struct
typ.Type = reflect.StructOf(fields)
typ.TupleElems = elems
typ.TupleRawNames = names
Expand All @@ -217,7 +208,6 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty
}

case "function":
typ.Kind = reflect.Array
typ.T = FunctionTy
typ.Size = 24
typ.Type = reflect.ArrayOf(24, reflect.TypeOf(byte(0)))
Expand Down
Loading

0 comments on commit d13bd06

Please sign in to comment.