From 2aeb2555fad6cb3300f01ae01b97c0314128b52b Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Fri, 1 Dec 2017 22:32:04 +0100 Subject: [PATCH] accounts/abi: add unpack into array test --- accounts/abi/event_test.go | 12 +++++++++++- accounts/abi/method.go | 2 +- accounts/abi/reflect.go | 21 +++++++++++++++++++++ accounts/abi/unpack.go | 2 +- accounts/abi/unpack_test.go | 5 +++++ 5 files changed, 39 insertions(+), 3 deletions(-) diff --git a/accounts/abi/event_test.go b/accounts/abi/event_test.go index d966ad1dbf..34c7eebde2 100644 --- a/accounts/abi/event_test.go +++ b/accounts/abi/event_test.go @@ -18,10 +18,10 @@ package abi import ( "bytes" - "reflect" "encoding/hex" "encoding/json" "math/big" + "reflect" "strings" "testing" @@ -178,6 +178,16 @@ func TestEventTupleUnpack(t *testing.T) { jsonEventPledge, "", "Can unpack Pledge event into slice", + }, { + pledgeData1, + &[3]interface{}{&common.Address{}, &bigint, &[3]byte{}}, + &[3]interface{}{ + &addr, + &bigintExpected2, + &[3]byte{'u', 's', 'd'}}, + jsonEventPledge, + "", + "Can unpack Pledge event into an array", }, { pledgeData1, &[]interface{}{new(int), 0, 0}, diff --git a/accounts/abi/method.go b/accounts/abi/method.go index 838106728d..540268a974 100644 --- a/accounts/abi/method.go +++ b/accounts/abi/method.go @@ -98,7 +98,7 @@ func (method Method) tupleUnpack(v interface{}, outputSlice []byte) error { j := 0 for i, output := range method.Outputs { - marshalledValue, err := toGoType((i+j)*32, ouptut.Type, outputSlice) + marshalledValue, err := toGoType((i+j)*32, output.Type, outputSlice) if err != nil { return err } diff --git a/accounts/abi/reflect.go b/accounts/abi/reflect.go index fbcd576e5c..43db4b130c 100644 --- a/accounts/abi/reflect.go +++ b/accounts/abi/reflect.go @@ -86,6 +86,27 @@ func set(dst, src reflect.Value, output Argument) error { return nil } +// requireUnpackKind verifies preconditions for unpacking `args` into `kind` +func requireUnpackKind(v reflect.Value, t reflect.Type, k reflect.Kind, + args []Argument, ignoreIndexed bool) error { + + switch k { + case reflect.Struct: + case reflect.Slice, reflect.Array: + minLen := len(args) + if ignoreIndexed { + minLen = countNonIndexedArguments(args) + } + if v.Len() < minLen { + return fmt.Errorf("abi: insufficient number of elements in the list/array for unpack, want %d, got %d", + minLen, v.Len()) + } + default: + return fmt.Errorf("abi: cannot unmarshal tuple into %v", t) + } + return nil +} + // requireAssignable assures that `dest` is a pointer and it's not an interface. func requireAssignable(dst, src reflect.Value) error { if dst.Kind() != reflect.Ptr && dst.Kind() != reflect.Interface { diff --git a/accounts/abi/unpack.go b/accounts/abi/unpack.go index 372a86c869..377aee874d 100644 --- a/accounts/abi/unpack.go +++ b/accounts/abi/unpack.go @@ -202,4 +202,4 @@ func lengthPrefixPointsTo(index int, output []byte) (start int, length int, err //fmt.Printf("LENGTH PREFIX INFO: \nsize: %v\noffset: %v\nstart: %v\n", length, offset, start) return -} \ No newline at end of file +} diff --git a/accounts/abi/unpack_test.go b/accounts/abi/unpack_test.go index 89b3bce8ca..3910950732 100644 --- a/accounts/abi/unpack_test.go +++ b/accounts/abi/unpack_test.go @@ -336,6 +336,11 @@ func TestMethodMultiReturn(t *testing.T) { &[]interface{}{&expected.Int, &expected.String}, "", "Can unpack into a slice", + }, { + &[2]interface{}{&bigint, new(string)}, + &[2]interface{}{&expected.Int, &expected.String}, + "", + "Can unpack into an array", }, { &[]interface{}{new(int), new(int)}, &[]interface{}{&expected.Int, &expected.String},