accounts/abi: Deduplicate code in unpacker
This commit is contained in:
parent
1ede68355d
commit
f0f594d045
@ -84,21 +84,27 @@ func (arguments Arguments) isTuple() bool {
|
|||||||
|
|
||||||
// Unpack performs the operation hexdata -> Go format
|
// Unpack performs the operation hexdata -> Go format
|
||||||
func (arguments Arguments) Unpack(v interface{}, data []byte) error {
|
func (arguments Arguments) Unpack(v interface{}, data []byte) error {
|
||||||
if arguments.isTuple() {
|
|
||||||
return arguments.unpackTuple(v, data)
|
|
||||||
}
|
|
||||||
return arguments.unpackAtomic(v, data)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (arguments Arguments) unpackTuple(v interface{}, output []byte) error {
|
|
||||||
// make sure the passed value is arguments pointer
|
// make sure the passed value is arguments pointer
|
||||||
valueOf := reflect.ValueOf(v)
|
if reflect.Ptr != reflect.ValueOf(v).Kind() {
|
||||||
if reflect.Ptr != valueOf.Kind() {
|
|
||||||
return fmt.Errorf("abi: Unpack(non-pointer %T)", v)
|
return fmt.Errorf("abi: Unpack(non-pointer %T)", v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
marshalledValues, err := arguments.UnpackValues(data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if arguments.isTuple() {
|
||||||
|
return arguments.unpackTuple(v, marshalledValues)
|
||||||
|
}
|
||||||
|
return arguments.unpackAtomic(v, marshalledValues)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (arguments Arguments) unpackTuple(v interface{}, marshalledValues []interface{}) error {
|
||||||
|
|
||||||
var (
|
var (
|
||||||
value = valueOf.Elem()
|
value = reflect.ValueOf(v).Elem()
|
||||||
typ = value.Type()
|
typ = value.Type()
|
||||||
kind = value.Kind()
|
kind = value.Kind()
|
||||||
)
|
)
|
||||||
@ -120,25 +126,9 @@ func (arguments Arguments) unpackTuple(v interface{}, output []byte) error {
|
|||||||
exists[field] = true
|
exists[field] = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// `i` counts the nonindexed arguments.
|
|
||||||
// `j` counts the number of complex types.
|
|
||||||
// both `i` and `j` are used to to correctly compute `data` offset.
|
|
||||||
|
|
||||||
j := 0
|
|
||||||
for i, arg := range arguments.NonIndexed() {
|
for i, arg := range arguments.NonIndexed() {
|
||||||
|
|
||||||
marshalledValue, err := toGoType((i+j)*32, arg.Type, output)
|
reflectValue := reflect.ValueOf(marshalledValues[i])
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if arg.Type.T == ArrayTy {
|
|
||||||
// combined index ('i' + 'j') need to be adjusted only by size of array, thus
|
|
||||||
// we need to decrement 'j' because 'i' was incremented
|
|
||||||
j += arg.Type.Size - 1
|
|
||||||
}
|
|
||||||
|
|
||||||
reflectValue := reflect.ValueOf(marshalledValue)
|
|
||||||
|
|
||||||
switch kind {
|
switch kind {
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
@ -171,23 +161,15 @@ func (arguments Arguments) unpackTuple(v interface{}, output []byte) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// unpackAtomic unpacks ( hexdata -> go ) a single value
|
// unpackAtomic unpacks ( hexdata -> go ) a single value
|
||||||
func (arguments Arguments) unpackAtomic(v interface{}, output []byte) error {
|
func (arguments Arguments) unpackAtomic(v interface{}, marshalledValues []interface{}) error {
|
||||||
// make sure the passed value is arguments pointer
|
|
||||||
valueOf := reflect.ValueOf(v)
|
if len(marshalledValues) != 1 {
|
||||||
if reflect.Ptr != valueOf.Kind() {
|
return fmt.Errorf("abi: wrong length, expected single value, got %d", len(marshalledValues))
|
||||||
return fmt.Errorf("abi: Unpack(non-pointer %T)", v)
|
|
||||||
}
|
|
||||||
arg := arguments[0]
|
|
||||||
if arg.Indexed {
|
|
||||||
return fmt.Errorf("abi: attempting to unpack indexed variable into element.")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
value := valueOf.Elem()
|
elem := reflect.ValueOf(v).Elem()
|
||||||
marshalledValue, err := toGoType(0, arg.Type, output)
|
reflectValue := reflect.ValueOf(marshalledValues[0])
|
||||||
if err != nil {
|
return set(elem, reflectValue, arguments.NonIndexed()[0])
|
||||||
return err
|
|
||||||
}
|
|
||||||
return set(value, reflect.ValueOf(marshalledValue), arg)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnpackValues can be used to unpack ABI-encoded hexdata according to the ABI-specification,
|
// UnpackValues can be used to unpack ABI-encoded hexdata according to the ABI-specification,
|
||||||
@ -226,7 +208,6 @@ func (arguments Arguments) PackValues(args []interface{}) ([]byte, error) {
|
|||||||
return arguments.Pack(args...)
|
return arguments.Pack(args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Pack performs the operation Go format -> Hexdata
|
// Pack performs the operation Go format -> Hexdata
|
||||||
func (arguments Arguments) Pack(args ...interface{}) ([]byte, error) {
|
func (arguments Arguments) Pack(args ...interface{}) ([]byte, error) {
|
||||||
// Make sure arguments match up and pack them
|
// Make sure arguments match up and pack them
|
||||||
|
@ -57,7 +57,6 @@ func TestUnpackV2(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func TestMultiReturnWithArrayV2(t *testing.T) {
|
func TestMultiReturnWithArrayV2(t *testing.T) {
|
||||||
const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3]"}, {"type": "uint64"}]}]`
|
const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3]"}, {"type": "uint64"}]}]`
|
||||||
abi, err := JSON(strings.NewReader(definition))
|
abi, err := JSON(strings.NewReader(definition))
|
||||||
|
Loading…
Reference in New Issue
Block a user