accounts/abi: removed unpack

This commit is contained in:
Marius van der Wijden 2020-05-07 18:51:19 +02:00
parent d0a25e1967
commit 532286efee
2 changed files with 18 additions and 84 deletions

View file

@ -134,75 +134,6 @@ func (arguments Arguments) UnpackIntoMap(v map[string]interface{}, data []byte)
return nil return nil
} }
// unpack sets the unmarshalled value to go format.
// Note the dst here must be settable.
func unpack(t *Type, dst interface{}, src interface{}) error {
var (
dstVal = reflect.ValueOf(dst).Elem()
srcVal = reflect.ValueOf(src)
)
tuple, typ := false, t
for {
if typ.T == SliceTy || typ.T == ArrayTy {
typ = typ.Elem
continue
}
tuple = typ.T == TupleTy
break
}
if !tuple {
return set(dstVal, srcVal)
}
// Dereferences interface or pointer wrapper
dstVal = indirectInterfaceOrPtr(dstVal)
switch t.T {
case TupleTy:
if dstVal.Kind() != reflect.Struct {
return fmt.Errorf("abi: invalid dst value for unpack, want struct, got %s", dstVal.Kind())
}
fieldmap, err := mapArgNamesToStructFields(t.TupleRawNames, dstVal)
if err != nil {
return err
}
for i, elem := range t.TupleElems {
fname := fieldmap[t.TupleRawNames[i]]
field := dstVal.FieldByName(fname)
if !field.IsValid() {
return fmt.Errorf("abi: field %s can't found in the given value", t.TupleRawNames[i])
}
if err := unpack(elem, field.Addr().Interface(), srcVal.Field(i).Interface()); err != nil {
return err
}
}
return nil
case SliceTy:
if dstVal.Kind() != reflect.Slice {
return fmt.Errorf("abi: invalid dst value for unpack, want slice, got %s", dstVal.Kind())
}
slice := reflect.MakeSlice(dstVal.Type(), srcVal.Len(), srcVal.Len())
for i := 0; i < slice.Len(); i++ {
if err := unpack(t.Elem, slice.Index(i).Addr().Interface(), srcVal.Index(i).Interface()); err != nil {
return err
}
}
dstVal.Set(slice)
case ArrayTy:
if dstVal.Kind() != reflect.Array {
return fmt.Errorf("abi: invalid dst value for unpack, want array, got %s", dstVal.Kind())
}
array := reflect.New(dstVal.Type()).Elem()
for i := 0; i < array.Len(); i++ {
if err := unpack(t.Elem, array.Index(i).Addr().Interface(), srcVal.Index(i).Interface()); err != nil {
return err
}
}
dstVal.Set(array)
}
return nil
}
// unpackAtomic unpacks ( hexdata -> go ) a single value // unpackAtomic unpacks ( hexdata -> go ) a single value
func (arguments Arguments) unpackAtomic(v interface{}, marshalledValues interface{}) error { func (arguments Arguments) unpackAtomic(v interface{}, marshalledValues interface{}) error {
elem := reflect.ValueOf(v).Elem() elem := reflect.ValueOf(v).Elem()
@ -220,23 +151,28 @@ func (arguments Arguments) unpackTuple(v interface{}, marshalledValues []interfa
value = reflect.ValueOf(v).Elem() value = reflect.ValueOf(v).Elem()
typ = value.Type() typ = value.Type()
kind = value.Kind() kind = value.Kind()
nonIndexedArgs = arguments.NonIndexed()
) )
switch kind { switch kind {
case reflect.Struct: case reflect.Struct:
k := 0 var abi2struct map[string]string
for i := 0; i < len(arguments); i++ { argNames := make([]string, len(nonIndexedArgs))
// Skip indexed fields for i, arg := range nonIndexedArgs {
if arguments[i].Indexed { argNames[i] = arg.Name
continue
} }
if i >= value.NumField() { var err error
return fmt.Errorf("Invalid field length while unpacking: %v", i) if abi2struct, err = mapArgNamesToStructFields(argNames, value); err != nil {
} return err
if err := set(value.Field(i), reflect.ValueOf(marshalledValues[k])); err != nil { }
for i, arg := range nonIndexedArgs {
field := value.FieldByName(abi2struct[arg.Name])
if !field.IsValid() {
return fmt.Errorf("abi: field %s can't be found in the given value", arg.Name)
}
if err := set(field, reflect.ValueOf(marshalledValues[i])); err != nil {
return err return err
} }
k++
} }
case reflect.Slice, reflect.Array: case reflect.Slice, reflect.Array:
for i := 0; i < value.Len(); i++ { for i := 0; i < value.Len(); i++ {

View file

@ -167,8 +167,6 @@ func TestEventMultiValueWithArrayUnpack(t *testing.T) {
func TestEventTupleUnpack(t *testing.T) { func TestEventTupleUnpack(t *testing.T) {
type EventTransfer struct { type EventTransfer struct {
From common.Address
To common.Address
Value *big.Int Value *big.Int
} }