Skip to content

Commit ecc876c

Browse files
committed
accounts/abi: fixed return tuple and string, bytes return type parsing
Removed old unmarshalling of return types: `abi.Call(...).([]byte)`. This is now replaced by a new syntax: ``` var a []byte err := abi.Call(&a, ...) ``` It also addresses a few issues with Bytes and Strings and can also handle both fixed and arbitrary sized byte slices, including strings.
1 parent 856b9e9 commit ecc876c

3 files changed

Lines changed: 363 additions & 30 deletions

File tree

‎accounts/abi/abi.go‎

Lines changed: 120 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ import (
2020
"encoding/json"
2121
"fmt"
2222
"io"
23-
"math"
23+
"reflect"
24+
"strings"
2425

2526
"github.com/ethereum/go-ethereum/common"
26-
"github.com/ethereum/go-ethereum/logger"
27-
"github.com/ethereum/go-ethereum/logger/glog"
2827
)
2928

3029
// Executer is an executer method for performing state executions. It takes one
@@ -101,52 +100,143 @@ func (abi ABI) Pack(name string, args ...interface{}) ([]byte, error) {
101100
}
102101

103102
// toGoType parses the input and casts it to the proper type defined by the ABI
104-
// argument in t.
105-
func toGoType(t Argument, input []byte) interface{} {
103+
// argument in T.
104+
func toGoType(i int, t Argument, output []byte) (interface{}, error) {
105+
index := i * 32
106+
107+
if index+32 > len(output) {
108+
return nil, fmt.Errorf("abi: cannot marshal in to go type: length insufficient %d require %d", len(output), index+32)
109+
}
110+
111+
// Parse the given index output and check whether we need to read
112+
// a different offset and length based on the type (i.e. string, bytes)
113+
var returnOutput []byte
114+
switch t.Type.T {
115+
case StringTy, BytesTy: // variable arrays are written at the end of the return bytes
116+
// parse offset from which we should start reading
117+
offset := int(common.BytesToBig(output[index : index+32]).Uint64())
118+
if offset+32 > len(output) {
119+
return nil, fmt.Errorf("abi: cannot marshal in to go type: length insufficient %d require %d", len(output), offset+32)
120+
}
121+
// parse the size up until we should be reading
122+
size := int(common.BytesToBig(output[offset : offset+32]).Uint64())
123+
if offset+32+size > len(output) {
124+
return nil, fmt.Errorf("abi: cannot marshal in to go type: length insufficient %d require %d", len(output), offset+32+size)
125+
}
126+
127+
// get the bytes for this return value
128+
returnOutput = output[offset+32 : offset+32+size]
129+
default:
130+
returnOutput = output[index : index+32]
131+
}
132+
133+
// cast bytes to abi return type
106134
switch t.Type.T {
107135
case IntTy:
108-
return common.BytesToBig(input)
136+
return common.BytesToBig(returnOutput), nil
109137
case UintTy:
110-
return common.BytesToBig(input)
138+
return common.BytesToBig(returnOutput), nil
111139
case BoolTy:
112-
return common.BytesToBig(input).Uint64() > 0
140+
return common.BytesToBig(returnOutput).Uint64() > 0, nil
113141
case AddressTy:
114-
return common.BytesToAddress(input)
142+
return common.BytesToAddress(returnOutput), nil
115143
case HashTy:
116-
return common.BytesToHash(input)
144+
return common.BytesToHash(returnOutput), nil
145+
case BytesTy, FixedBytesTy:
146+
return returnOutput, nil
147+
case StringTy:
148+
return string(returnOutput), nil
117149
}
118-
return nil
150+
return nil, fmt.Errorf("abi: unknown type %v", t.Type.T)
119151
}
120152

121-
// Call executes a call and attemps to parse the return values and returns it as
122-
// an interface. It uses the executer method to perform the actual call since
123-
// the abi knows nothing of the lower level calling mechanism.
153+
// Call will unmarshal the output of the call in v. It will return an error if
154+
// invalid type is given or if the output is too short to conform to the ABI
155+
// spec.
124156
//
125-
// Call supports all abi types and includes multiple return values. When only
126-
// one item is returned a single interface{} will be returned, if a contract
127-
// method returns multiple values an []interface{} slice is returned.
128-
func (abi ABI) Call(executer Executer, name string, args ...interface{}) interface{} {
157+
// Call supports all of the available types and accepts a struct or an interface
158+
// slice if the return is a tuple.
159+
func (abi ABI) Call(executer Executer, v interface{}, name string, args ...interface{}) error {
129160
callData, err := abi.Pack(name, args...)
130161
if err != nil {
131-
glog.V(logger.Debug).Infoln("pack error:", err)
132-
return nil
162+
return err
133163
}
134164

135-
output := executer(callData)
165+
return abi.unmarshal(v, name, executer(callData))
166+
}
136167

137-
method := abi.Methods[name]
138-
ret := make([]interface{}, int(math.Max(float64(len(method.Outputs)), float64(len(output)/32))))
139-
for i := 0; i < len(ret); i += 32 {
140-
index := i / 32
141-
ret[index] = toGoType(method.Outputs[index], output[i:i+32])
168+
var interSlice = reflect.TypeOf([]interface{}{})
169+
170+
// unmarshal output in v according to the abi specification
171+
func (abi ABI) unmarshal(v interface{}, name string, output []byte) error {
172+
var method = abi.Methods[name]
173+
174+
if len(output) == 0 {
175+
return fmt.Errorf("abi: unmarshalling empty output")
142176
}
143177

144-
// return single interface
145-
if len(ret) == 1 {
146-
return ret[0]
178+
value := reflect.ValueOf(v).Elem()
179+
typ := value.Type()
180+
181+
if len(method.Outputs) > 1 {
182+
switch value.Kind() {
183+
// struct will match named return values to the struct's field
184+
// names
185+
case reflect.Struct:
186+
for i := 0; i < len(method.Outputs); i++ {
187+
marshalledValue, err := toGoType(i, method.Outputs[i], output)
188+
if err != nil {
189+
return err
190+
}
191+
reflectValue := reflect.ValueOf(marshalledValue)
192+
193+
for j := 0; j < typ.NumField(); j++ {
194+
field := typ.Field(j)
195+
// TODO read tags: `abi:"fieldName"`
196+
if field.Name == strings.ToUpper(method.Outputs[i].Name[:1])+method.Outputs[i].Name[1:] {
197+
if field.Type.AssignableTo(reflectValue.Type()) {
198+
value.Field(j).Set(reflectValue)
199+
break
200+
} else {
201+
return fmt.Errorf("abi: cannot unmarshal %v in to %v", field.Type, reflectValue.Type())
202+
}
203+
}
204+
}
205+
}
206+
case reflect.Slice:
207+
if !value.Type().AssignableTo(interSlice) {
208+
return fmt.Errorf("abi: cannot marshal tuple in to slice %T (only []interface{} is supported)", v)
209+
}
210+
211+
// create a new slice and start appending the unmarshalled
212+
// values to the new interface slice.
213+
z := reflect.MakeSlice(typ, 0, len(method.Outputs))
214+
for i := 0; i < len(method.Outputs); i++ {
215+
marshalledValue, err := toGoType(i, method.Outputs[i], output)
216+
if err != nil {
217+
return err
218+
}
219+
z = reflect.Append(z, reflect.ValueOf(marshalledValue))
220+
}
221+
value.Set(z)
222+
default:
223+
return fmt.Errorf("abi: cannot unmarshal tuple in to %v", typ)
224+
}
225+
226+
} else {
227+
marshalledValue, err := toGoType(0, method.Outputs[0], output)
228+
if err != nil {
229+
return err
230+
}
231+
reflectValue := reflect.ValueOf(marshalledValue)
232+
if typ.AssignableTo(reflectValue.Type()) {
233+
value.Set(reflectValue)
234+
} else {
235+
return fmt.Errorf("abi: cannot unmarshal %v in to %v", reflectValue.Type(), value.Type())
236+
}
147237
}
148238

149-
return ret
239+
return nil
150240
}
151241

152242
func (abi *ABI) UnmarshalJSON(data []byte) error {

0 commit comments

Comments
 (0)