Skip to content

Commit 6fe917e

Browse files
committed
accounts/abi: support for typed array
Added support for fixed size and arbitrary length byte arrays to be marshallable in fixed size (typed) byte slices.
1 parent cb85923 commit 6fe917e

2 files changed

Lines changed: 114 additions & 43 deletions

File tree

‎accounts/abi/abi.go‎

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,14 @@ func (abi ABI) Call(executer Executer, v interface{}, name string, args ...inter
165165
return abi.unmarshal(v, name, executer(callData))
166166
}
167167

168-
var interSlice = reflect.TypeOf([]interface{}{})
168+
// these variable are used to determine certain types during type assertion for
169+
// assignment.
170+
var (
171+
r_interSlice = reflect.TypeOf([]interface{}{})
172+
r_hash = reflect.TypeOf(common.Hash{})
173+
r_bytes = reflect.TypeOf([]byte{})
174+
r_byte = reflect.TypeOf(byte(0))
175+
)
169176

170177
// unmarshal output in v according to the abi specification
171178
func (abi ABI) unmarshal(v interface{}, name string, output []byte) error {
@@ -194,17 +201,14 @@ func (abi ABI) unmarshal(v interface{}, name string, output []byte) error {
194201
field := typ.Field(j)
195202
// TODO read tags: `abi:"fieldName"`
196203
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())
204+
if err := set(value.Field(j), reflectValue, method.Outputs[i]); err != nil {
205+
return err
202206
}
203207
}
204208
}
205209
}
206210
case reflect.Slice:
207-
if !value.Type().AssignableTo(interSlice) {
211+
if !value.Type().AssignableTo(r_interSlice) {
208212
return fmt.Errorf("abi: cannot marshal tuple in to slice %T (only []interface{} is supported)", v)
209213
}
210214

@@ -228,17 +232,40 @@ func (abi ABI) unmarshal(v interface{}, name string, output []byte) error {
228232
if err != nil {
229233
return err
230234
}
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())
235+
if err := set(value, reflect.ValueOf(marshalledValue), method.Outputs[0]); err != nil {
236+
return err
236237
}
237238
}
238239

239240
return nil
240241
}
241242

243+
// set attempts to assign src to dst by either setting, copying or otherwise.
244+
//
245+
// set is a bit more lenient when it comes to assignment and doesn't force an as
246+
// strict ruleset as bare `reflect` does.
247+
func set(dst, src reflect.Value, output Argument) error {
248+
dstType := dst.Type()
249+
srcType := src.Type()
250+
251+
switch {
252+
case dstType.AssignableTo(src.Type()):
253+
dst.Set(src)
254+
case dstType.Kind() == reflect.Array && srcType.Kind() == reflect.Slice:
255+
if !dstType.Elem().AssignableTo(r_byte) {
256+
return fmt.Errorf("abi: cannot unmarshal %v in to array of elem %v", src.Type(), dstType.Elem())
257+
}
258+
259+
if dst.Len() < output.Type.Size {
260+
return fmt.Errorf("abi: cannot unmarshal src (len=%d) in to dst (len=%d)", output.Type.Size, dst.Len())
261+
}
262+
reflect.Copy(dst, src)
263+
default:
264+
return fmt.Errorf("abi: cannot unmarshal %v in to %v", src.Type(), dst.Type())
265+
}
266+
return nil
267+
}
268+
242269
func (abi *ABI) UnmarshalJSON(data []byte) error {
243270
var fields []struct {
244271
Type string

‎accounts/abi/abi_test.go‎

Lines changed: 75 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -394,37 +394,6 @@ func TestBytes(t *testing.T) {
394394
}
395395
}
396396

397-
/*
398-
func TestReturn(t *testing.T) {
399-
const definition = `[
400-
{ "type" : "function", "name" : "balance", "const" : true, "inputs" : [], "outputs" : [ { "name": "", "type": "hash" } ] },
401-
{ "type" : "function", "name" : "name", "const" : true, "inputs" : [], "outputs" : [ { "name": "", "type": "address" } ] }]`
402-
403-
abi, err := JSON(strings.NewReader(definition))
404-
if err != nil {
405-
t.Fatal(err)
406-
}
407-
408-
r := abi.Call(func([]byte) []byte {
409-
t := make([]byte, 32)
410-
t[0] = 1
411-
return t
412-
}, "balance")
413-
if _, ok := r.(common.Hash); !ok {
414-
t.Errorf("expected type common.Hash, got %T", r)
415-
}
416-
417-
r = abi.Call(func([]byte) []byte {
418-
t := make([]byte, 32)
419-
t[0] = 1
420-
return t
421-
}, "name")
422-
if _, ok := r.(common.Address); !ok {
423-
t.Errorf("expected type common.Address, got %T", r)
424-
}
425-
}
426-
*/
427-
428397
func TestDefaultFunctionParsing(t *testing.T) {
429398
const definition = `[{ "name" : "balance" }]`
430399

@@ -550,11 +519,71 @@ func TestMultiReturnWithSlice(t *testing.T) {
550519
}
551520
}
552521

522+
func TestMarshalArrays(t *testing.T) {
523+
const definition = `[
524+
{ "name" : "bytes32", "const" : false, "outputs": [ { "type": "bytes32" } ] },
525+
{ "name" : "bytes10", "const" : false, "outputs": [ { "type": "bytes10" } ] }
526+
]`
527+
528+
abi, err := JSON(strings.NewReader(definition))
529+
if err != nil {
530+
t.Fatal(err)
531+
}
532+
533+
output := common.LeftPadBytes([]byte{1}, 32)
534+
535+
var bytes10 [10]byte
536+
err = abi.unmarshal(&bytes10, "bytes32", output)
537+
if err == nil || err.Error() != "abi: cannot unmarshal src (len=32) in to dst (len=10)" {
538+
t.Error("expected error or bytes32 not be assignable to bytes10:", err)
539+
}
540+
541+
var bytes32 [32]byte
542+
err = abi.unmarshal(&bytes32, "bytes32", output)
543+
if err != nil {
544+
t.Error("didn't expect error:", err)
545+
}
546+
if !bytes.Equal(bytes32[:], output) {
547+
t.Error("expected bytes32[31] to be 1 got", bytes32[31])
548+
}
549+
550+
type (
551+
B10 [10]byte
552+
B32 [32]byte
553+
)
554+
555+
var b10 B10
556+
err = abi.unmarshal(&b10, "bytes32", output)
557+
if err == nil || err.Error() != "abi: cannot unmarshal src (len=32) in to dst (len=10)" {
558+
t.Error("expected error or bytes32 not be assignable to bytes10:", err)
559+
}
560+
561+
var b32 B32
562+
err = abi.unmarshal(&b32, "bytes32", output)
563+
if err != nil {
564+
t.Error("didn't expect error:", err)
565+
}
566+
if !bytes.Equal(b32[:], output) {
567+
t.Error("expected bytes32[31] to be 1 got", bytes32[31])
568+
}
569+
570+
output[10] = 1
571+
var shortAssignLong [32]byte
572+
err = abi.unmarshal(&shortAssignLong, "bytes10", output)
573+
if err != nil {
574+
t.Error("didn't expect error:", err)
575+
}
576+
if !bytes.Equal(output, shortAssignLong[:]) {
577+
t.Errorf("expected %x to be %x", shortAssignLong, output)
578+
}
579+
}
580+
553581
func TestUnmarshal(t *testing.T) {
554582
const definition = `[
555583
{ "name" : "int", "const" : false, "outputs": [ { "type": "uint256" } ] },
556584
{ "name" : "bool", "const" : false, "outputs": [ { "type": "bool" } ] },
557585
{ "name" : "bytes", "const" : false, "outputs": [ { "type": "bytes" } ] },
586+
{ "name" : "fixed", "const" : false, "outputs": [ { "type": "bytes32" } ] },
558587
{ "name" : "multi", "const" : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] },
559588
{ "name" : "mixedBytes", "const" : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]`
560589

@@ -655,6 +684,21 @@ func TestUnmarshal(t *testing.T) {
655684
t.Errorf("expected %x got %x", bytesOut, Bytes)
656685
}
657686

687+
// marshal dynamic bytes length 5
688+
buff.Reset()
689+
buff.Write(common.RightPadBytes([]byte("hello"), 32))
690+
691+
var hash common.Hash
692+
err = abi.unmarshal(&hash, "fixed", buff.Bytes())
693+
if err != nil {
694+
t.Error(err)
695+
}
696+
697+
helloHash := common.BytesToHash(common.RightPadBytes([]byte("hello"), 32))
698+
if hash != helloHash {
699+
t.Errorf("Expected %x to equal %x", hash, helloHash)
700+
}
701+
658702
// marshal error
659703
buff.Reset()
660704
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))

0 commit comments

Comments
 (0)