Skip to content

Commit 933acf3

Browse files
account/abi: remove superfluous type checking (#21022)
* accounts/abi: added getType func to Type struct * accounts/abi: fixed tuple unpack * accounts/abi: removed type.Type * accounts/abi: added comment * accounts/abi: removed unused types * accounts/abi: removed superfluous declarations * accounts/abi: typo
1 parent 44a3b8c commit 933acf3

7 files changed

Lines changed: 148 additions & 153 deletions

File tree

‎accounts/abi/error.go‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ func formatSliceString(kind reflect.Kind, sliceSize int) string {
3939
// type in t.
4040
func sliceTypeCheck(t Type, val reflect.Value) error {
4141
if val.Kind() != reflect.Slice && val.Kind() != reflect.Array {
42-
return typeErr(formatSliceString(t.Type.Kind(), t.Size), val.Type())
42+
return typeErr(formatSliceString(t.getType().Kind(), t.Size), val.Type())
4343
}
4444

4545
if t.T == ArrayTy && val.Len() != t.Size {
46-
return typeErr(formatSliceString(t.Elem.Type.Kind(), t.Size), formatSliceString(val.Type().Elem().Kind(), val.Len()))
46+
return typeErr(formatSliceString(t.Elem.getType().Kind(), t.Size), formatSliceString(val.Type().Elem().Kind(), val.Len()))
4747
}
4848

4949
if t.Elem.T == SliceTy || t.Elem.T == ArrayTy {
@@ -52,8 +52,8 @@ func sliceTypeCheck(t Type, val reflect.Value) error {
5252
}
5353
}
5454

55-
if elemKind := val.Type().Elem().Kind(); elemKind != t.Elem.Type.Kind() {
56-
return typeErr(formatSliceString(t.Elem.Type.Kind(), t.Size), val.Type())
55+
if elemKind := val.Type().Elem().Kind(); elemKind != t.Elem.getType().Kind() {
56+
return typeErr(formatSliceString(t.Elem.getType().Kind(), t.Size), val.Type())
5757
}
5858
return nil
5959
}
@@ -66,10 +66,10 @@ func typeCheck(t Type, value reflect.Value) error {
6666
}
6767

6868
// Check base type validity. Element types will be checked later on.
69-
if t.Type.Kind() != value.Kind() {
70-
return typeErr(t.Type.Kind(), value.Kind())
69+
if t.getType().Kind() != value.Kind() {
70+
return typeErr(t.getType().Kind(), value.Kind())
7171
} else if t.T == FixedBytesTy && t.Size != value.Len() {
72-
return typeErr(t.Type, value.Type())
72+
return typeErr(t.getType(), value.Type())
7373
} else {
7474
return nil
7575
}

‎accounts/abi/numbers.go‎

Lines changed: 0 additions & 38 deletions
This file was deleted.

‎accounts/abi/reflect.go‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ package abi
1818

1919
import (
2020
"fmt"
21+
"math/big"
2122
"reflect"
2223
"strings"
2324
)
2425

2526
// indirect recursively dereferences the value until it either gets the value
2627
// or finds a big.Int
2728
func indirect(v reflect.Value) reflect.Value {
28-
if v.Kind() == reflect.Ptr && v.Elem().Type() != derefbigT {
29+
if v.Kind() == reflect.Ptr && v.Elem().Type() != reflect.TypeOf(big.Int{}) {
2930
return indirect(v.Elem())
3031
}
3132
return v
@@ -45,26 +46,26 @@ func reflectIntType(unsigned bool, size int) reflect.Type {
4546
if unsigned {
4647
switch size {
4748
case 8:
48-
return uint8T
49+
return reflect.TypeOf(uint8(0))
4950
case 16:
50-
return uint16T
51+
return reflect.TypeOf(uint16(0))
5152
case 32:
52-
return uint32T
53+
return reflect.TypeOf(uint32(0))
5354
case 64:
54-
return uint64T
55+
return reflect.TypeOf(uint64(0))
5556
}
5657
}
5758
switch size {
5859
case 8:
59-
return int8T
60+
return reflect.TypeOf(int8(0))
6061
case 16:
61-
return int16T
62+
return reflect.TypeOf(int16(0))
6263
case 32:
63-
return int32T
64+
return reflect.TypeOf(int32(0))
6465
case 64:
65-
return int64T
66+
return reflect.TypeOf(int64(0))
6667
}
67-
return bigT
68+
return reflect.TypeOf(&big.Int{})
6869
}
6970

7071
// mustArrayToBytesSlice creates a new byte slice with the exact same size as value
@@ -84,7 +85,7 @@ func set(dst, src reflect.Value) error {
8485
switch {
8586
case dstType.Kind() == reflect.Interface && dst.Elem().IsValid():
8687
return set(dst.Elem(), src)
87-
case dstType.Kind() == reflect.Ptr && dstType.Elem() != derefbigT:
88+
case dstType.Kind() == reflect.Ptr && dstType.Elem() != reflect.TypeOf(big.Int{}):
8889
return set(dst.Elem(), src)
8990
case srcType.AssignableTo(dstType) && dst.CanSet():
9091
dst.Set(src)

‎accounts/abi/type.go‎

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import (
2323
"regexp"
2424
"strconv"
2525
"strings"
26+
27+
"github.com/ethereum/go-ethereum/common"
2628
)
2729

2830
// Type enumerator
@@ -45,16 +47,16 @@ const (
4547
// Type is the reflection of the supported argument type
4648
type Type struct {
4749
Elem *Type
48-
Type reflect.Type
4950
Size int
5051
T byte // Our own type checking
5152

5253
stringKind string // holds the unparsed string for deriving signatures
5354

5455
// Tuple relative fields
55-
TupleRawName string // Raw struct name defined in source code, may be empty.
56-
TupleElems []*Type // Type information of all tuple fields
57-
TupleRawNames []string // Raw field name of all tuple fields
56+
TupleRawName string // Raw struct name defined in source code, may be empty.
57+
TupleElems []*Type // Type information of all tuple fields
58+
TupleRawNames []string // Raw field name of all tuple fields
59+
TupleType reflect.Type // Underlying struct of the tuple
5860
}
5961

6062
var (
@@ -94,7 +96,6 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty
9496
// is a slice
9597
typ.T = SliceTy
9698
typ.Elem = &embeddedType
97-
typ.Type = reflect.SliceOf(embeddedType.Type)
9899
typ.stringKind = embeddedType.stringKind + sliced
99100
} else if len(intz) == 1 {
100101
// is a array
@@ -104,7 +105,6 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty
104105
if err != nil {
105106
return Type{}, fmt.Errorf("abi: error parsing variable size: %v", err)
106107
}
107-
typ.Type = reflect.ArrayOf(typ.Size, embeddedType.Type)
108108
typ.stringKind = embeddedType.stringKind + sliced
109109
} else {
110110
return Type{}, fmt.Errorf("invalid formatting of array type")
@@ -136,31 +136,24 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty
136136
// varType is the parsed abi type
137137
switch varType := parsedType[1]; varType {
138138
case "int":
139-
typ.Type = reflectIntType(false, varSize)
140139
typ.Size = varSize
141140
typ.T = IntTy
142141
case "uint":
143-
typ.Type = reflectIntType(true, varSize)
144142
typ.Size = varSize
145143
typ.T = UintTy
146144
case "bool":
147145
typ.T = BoolTy
148-
typ.Type = reflect.TypeOf(bool(false))
149146
case "address":
150-
typ.Type = addressT
151147
typ.Size = 20
152148
typ.T = AddressTy
153149
case "string":
154-
typ.Type = reflect.TypeOf("")
155150
typ.T = StringTy
156151
case "bytes":
157152
if varSize == 0 {
158153
typ.T = BytesTy
159-
typ.Type = reflect.SliceOf(reflect.TypeOf(byte(0)))
160154
} else {
161155
typ.T = FixedBytesTy
162156
typ.Size = varSize
163-
typ.Type = reflect.ArrayOf(varSize, reflect.TypeOf(byte(0)))
164157
}
165158
case "tuple":
166159
var (
@@ -180,7 +173,7 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty
180173
}
181174
fields = append(fields, reflect.StructField{
182175
Name: ToCamelCase(c.Name), // reflect.StructOf will panic for any exported field.
183-
Type: cType.Type,
176+
Type: cType.getType(),
184177
Tag: reflect.StructTag("json:\"" + c.Name + "\""),
185178
})
186179
elems = append(elems, &cType)
@@ -191,7 +184,8 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty
191184
}
192185
}
193186
expression += ")"
194-
typ.Type = reflect.StructOf(fields)
187+
188+
typ.TupleType = reflect.StructOf(fields)
195189
typ.TupleElems = elems
196190
typ.TupleRawNames = names
197191
typ.T = TupleTy
@@ -210,14 +204,48 @@ func NewType(t string, internalType string, components []ArgumentMarshaling) (ty
210204
case "function":
211205
typ.T = FunctionTy
212206
typ.Size = 24
213-
typ.Type = reflect.ArrayOf(24, reflect.TypeOf(byte(0)))
214207
default:
215208
return Type{}, fmt.Errorf("unsupported arg type: %s", t)
216209
}
217210

218211
return
219212
}
220213

214+
func (t Type) getType() reflect.Type {
215+
switch t.T {
216+
case IntTy:
217+
return reflectIntType(false, t.Size)
218+
case UintTy:
219+
return reflectIntType(true, t.Size)
220+
case BoolTy:
221+
return reflect.TypeOf(false)
222+
case StringTy:
223+
return reflect.TypeOf("")
224+
case SliceTy:
225+
return reflect.SliceOf(t.Elem.getType())
226+
case ArrayTy:
227+
return reflect.ArrayOf(t.Size, t.Elem.getType())
228+
case TupleTy:
229+
return t.TupleType
230+
case AddressTy:
231+
return reflect.TypeOf(common.Address{})
232+
case FixedBytesTy:
233+
return reflect.ArrayOf(t.Size, reflect.TypeOf(byte(0)))
234+
case BytesTy:
235+
return reflect.SliceOf(reflect.TypeOf(byte(0)))
236+
case HashTy:
237+
// hashtype currently not used
238+
return reflect.ArrayOf(32, reflect.TypeOf(byte(0)))
239+
case FixedPointTy:
240+
// fixedpoint type currently not used
241+
return reflect.ArrayOf(32, reflect.TypeOf(byte(0)))
242+
case FunctionTy:
243+
return reflect.ArrayOf(24, reflect.TypeOf(byte(0)))
244+
default:
245+
panic("Invalid type")
246+
}
247+
}
248+
221249
// String implements Stringer
222250
func (t Type) String() (out string) {
223251
return t.stringKind

0 commit comments

Comments
 (0)