From ebb68bda5040a2e79fbd75e878c52c4705952ecb Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Fri, 10 Nov 2017 02:28:17 +0100 Subject: [PATCH 1/6] accounts/abi: adding event unpacker tests --- accounts/abi/event_test.go | 154 +++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) diff --git a/accounts/abi/event_test.go b/accounts/abi/event_test.go index a3899b4a6f..d966ad1dbf 100644 --- a/accounts/abi/event_test.go +++ b/accounts/abi/event_test.go @@ -19,14 +19,51 @@ package abi import ( "bytes" "reflect" + "encoding/hex" + "encoding/json" + "math/big" "strings" "testing" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) +var jsonEventTransfer = []byte(`{ + "anonymous": false, + "inputs": [ + { + "indexed": true, "name": "from", "type": "address" + }, { + "indexed": true, "name": "to", "type": "address" + }, { + "indexed": false, "name": "value", "type": "uint256" + }], + "name": "Transfer", + "type": "event" +}`) + +var jsonEventPledge = []byte(`{ + "anonymous": false, + "inputs": [{ + "indexed": false, "name": "who", "type": "address" + }, { + "indexed": false, "name": "wad", "type": "uint128" + }, { + "indexed": false, "name": "currency", "type": "bytes3" + }], + "name": "Pledge", + "type": "event" +}`) + +// 1000000 +var transferData1 = "00000000000000000000000000000000000000000000000000000000000f4240" + +// "0x00Ce0d46d924CC8437c806721496599FC3FFA268", 2218516807680, "usd" +var pledgeData1 = "00000000000000000000000000ce0d46d924cc8437c806721496599fc3ffa2680000000000000000000000000000000000000000000000000000020489e800007573640000000000000000000000000000000000000000000000000000000000" + func TestEventId(t *testing.T) { var table = []struct { definition string @@ -77,3 +114,120 @@ func TestEventMultiValueWithArrayUnpack(t *testing.T) { require.Equal(t, [2]uint8{1, 2}, rst.Value1) require.Equal(t, uint8(3), rst.Value2) } + +func TestEventTupleUnpack(t *testing.T) { + + type EventTransfer struct { + Value *big.Int + } + + type EventPledge struct { + Who common.Address + Wad *big.Int + Currency [3]byte + } + + type BadEventPledge struct { + Who string + Wad int + Currency [3]byte + } + + bigint := new(big.Int) + bigintExpected := big.NewInt(1000000) + bigintExpected2 := big.NewInt(2218516807680) + addr := common.HexToAddress("0x00Ce0d46d924CC8437c806721496599FC3FFA268") + var testCases = []struct { + data string + dest interface{} + expected interface{} + jsonLog []byte + error string + name string + }{{ + transferData1, + &EventTransfer{}, + &EventTransfer{Value: bigintExpected}, + jsonEventTransfer, + "", + "Can unpack ERC20 Transfer event into structure", + }, { + transferData1, + &[]interface{}{&bigint}, + &[]interface{}{&bigintExpected}, + jsonEventTransfer, + "", + "Can unpack ERC20 Transfer event into slice", + }, { + pledgeData1, + &EventPledge{}, + &EventPledge{ + addr, + bigintExpected2, + [3]byte{'u', 's', 'd'}}, + jsonEventPledge, + "", + "Can unpack Pledge event into structure", + }, { + pledgeData1, + &[]interface{}{&common.Address{}, &bigint, &[3]byte{}}, + &[]interface{}{ + &addr, + &bigintExpected2, + &[3]byte{'u', 's', 'd'}}, + jsonEventPledge, + "", + "Can unpack Pledge event into slice", + }, { + pledgeData1, + &[]interface{}{new(int), 0, 0}, + &[]interface{}{}, + jsonEventPledge, + "abi: cannot unmarshal common.Address in to int", + "Can not unpack Pledge event into slice with wrong types", + }, { + pledgeData1, + &BadEventPledge{}, + &BadEventPledge{}, + jsonEventPledge, + "abi: cannot unmarshal common.Address in to string", + "Can not unpack Pledge event into struct with wrong filed types", + }, { + pledgeData1, + &[]interface{}{common.Address{}, new(big.Int)}, + &[]interface{}{}, + jsonEventPledge, + "abi: insufficient number of elements in the list/array for unpack, want 3, got 2", + "Can not unpack Pledge event into too short slice", + }, { + pledgeData1, + new(map[string]interface{}), + &[]interface{}{}, + jsonEventPledge, + "abi: cannot unmarshal tuple into map[string]interface {}", + "Can not unpack Pledge event into map", + }} + + for _, tc := range testCases { + assert := assert.New(t) + tc := tc + t.Run(tc.name, func(t *testing.T) { + err := unpackTestEventData(tc.dest, tc.data, tc.jsonLog, assert) + if tc.error == "" { + assert.Nil(err, "Should be able to unpack event data.") + assert.Equal(tc.expected, tc.dest) + } else { + assert.EqualError(err, tc.error) + } + }) + } +} + +func unpackTestEventData(dest interface{}, hexData string, jsonEvent []byte, assert *assert.Assertions) error { + data, err := hex.DecodeString(hexData) + assert.NoError(err, "Hex data should be a correct hex-string") + var e Event + assert.NoError(json.Unmarshal(jsonEvent, &e), "Should be able to unmarshal event ABI") + a := ABI{Events: map[string]Event{"e": e}} + return a.Unpack(dest, "e", data) +} From 6d604a87c249aa5d984c07704a8ca92b5ad7c176 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Fri, 10 Nov 2017 00:08:28 +0100 Subject: [PATCH 2/6] accounts/abi: fix event tupleUnpack Event.tupleUnpack doesn't handle correctly Indexed arguments, hence it can't unpack an event with indexed arguments. --- accounts/abi/event.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/accounts/abi/event.go b/accounts/abi/event.go index bd1098d878..0d3c3c4fac 100644 --- a/accounts/abi/event.go +++ b/accounts/abi/event.go @@ -65,13 +65,13 @@ func (e Event) tupleUnpack(v interface{}, output []byte) error { return fmt.Errorf("abi: cannot unmarshal tuple in to %v", typ) } - j := 0 - for i := 0; i < len(e.Inputs); i++ { - input := e.Inputs[i] + i, j := -1, 0 + for _, input := range e.Inputs { if input.Indexed { // can't read, continue continue } + i++ marshalledValue, err := toGoType((i+j)*32, input.Type, output) if err != nil { return err @@ -88,22 +88,22 @@ func (e Event) tupleUnpack(v interface{}, output []byte) error { for j := 0; j < typ.NumField(); j++ { field := typ.Field(j) // TODO read tags: `abi:"fieldName"` - if field.Name == strings.ToUpper(e.Inputs[i].Name[:1])+e.Inputs[i].Name[1:] { - if err := set(value.Field(j), reflectValue, e.Inputs[i]); err != nil { + if field.Name == strings.ToUpper(input.Name[:1])+input.Name[1:] { + if err := set(value.Field(j), reflectValue, input); err != nil { return err } } } case reflect.Slice, reflect.Array: if value.Len() < i { - return fmt.Errorf("abi: insufficient number of arguments for unpack, want %d, got %d", len(e.Inputs), value.Len()) + return fmt.Errorf("abi: insufficient number of arguments for unpack, want %d, got %d", i, value.Len()) } v := value.Index(i) if v.Kind() != reflect.Ptr && v.Kind() != reflect.Interface { return fmt.Errorf("abi: cannot unmarshal %v in to %v", v.Type(), reflectValue.Type()) } reflectValue := reflect.ValueOf(marshalledValue) - if err := set(v.Elem(), reflectValue, e.Inputs[i]); err != nil { + if err := set(v.Elem(), reflectValue, input); err != nil { return err } default: From f1e9e8303ab6a2b4b8fe024f0367a2c7ca675c1e Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Fri, 10 Nov 2017 02:30:26 +0100 Subject: [PATCH 3/6] accounts/abi: fix event unpack into slice + The event slice unpacker doesn't correctly extract element from the slice. The indexed arguments are not ignored as they should be (the data offset should not include the indexed arguments). + The `Elem()` call in the slice unpack doesn't work. The Slice related tests fails because of that. + the check in the loop are suboptimal and have been extracted out of the loop. + extracted common code from event and method tupleUnpack --- accounts/abi/argument.go | 10 ++++++++++ accounts/abi/event.go | 23 ++++++++++------------- accounts/abi/method.go | 16 +++++++--------- accounts/abi/reflect.go | 8 ++++++++ accounts/abi/unpack.go | 2 +- 5 files changed, 36 insertions(+), 23 deletions(-) diff --git a/accounts/abi/argument.go b/accounts/abi/argument.go index 4691318ce7..cd856061ee 100644 --- a/accounts/abi/argument.go +++ b/accounts/abi/argument.go @@ -49,3 +49,13 @@ func (a *Argument) UnmarshalJSON(data []byte) error { return nil } + +func countNonIndexedArguments(args []Argument) int { + out := 0 + for i := range args { + if !args[i].Indexed { + out++ + } + } + return out +} diff --git a/accounts/abi/event.go b/accounts/abi/event.go index 0d3c3c4fac..b67bc96a86 100644 --- a/accounts/abi/event.go +++ b/accounts/abi/event.go @@ -59,16 +59,19 @@ func (e Event) tupleUnpack(v interface{}, output []byte) error { var ( value = valueOf.Elem() typ = value.Type() + kind = value.Kind() ) - - if value.Kind() != reflect.Struct { - return fmt.Errorf("abi: cannot unmarshal tuple in to %v", typ) + if err := requireUnpackKind(value, typ, kind, e.Inputs, true); err != nil { + return err } + // `i` counts the nonindexed arguments. + // `j` counts the number of complex types. + // both `i` and `j` are used to to correctly compute `data` offset. i, j := -1, 0 for _, input := range e.Inputs { if input.Indexed { - // can't read, continue + // Indexed arguments are not packed into data continue } i++ @@ -83,7 +86,7 @@ func (e Event) tupleUnpack(v interface{}, output []byte) error { } reflectValue := reflect.ValueOf(marshalledValue) - switch value.Kind() { + switch kind { case reflect.Struct: for j := 0; j < typ.NumField(); j++ { field := typ.Field(j) @@ -95,19 +98,13 @@ func (e Event) tupleUnpack(v interface{}, output []byte) error { } } case reflect.Slice, reflect.Array: - if value.Len() < i { - return fmt.Errorf("abi: insufficient number of arguments for unpack, want %d, got %d", i, value.Len()) - } v := value.Index(i) - if v.Kind() != reflect.Ptr && v.Kind() != reflect.Interface { - return fmt.Errorf("abi: cannot unmarshal %v in to %v", v.Type(), reflectValue.Type()) + if err := requireAssignable(v, reflectValue); err != nil { + return err } - reflectValue := reflect.ValueOf(marshalledValue) if err := set(v.Elem(), reflectValue, input); err != nil { return err } - default: - return fmt.Errorf("abi: cannot unmarshal tuple in to %v", typ) } } return nil diff --git a/accounts/abi/method.go b/accounts/abi/method.go index 6b9aa011ef..7240343542 100644 --- a/accounts/abi/method.go +++ b/accounts/abi/method.go @@ -90,7 +90,11 @@ func (method Method) tupleUnpack(v interface{}, output []byte) error { var ( value = valueOf.Elem() typ = value.Type() + kind = value.Kind() ) + if err := requireUnpackKind(value, typ, kind, method.Outputs, false); err != nil { + return err + } j := 0 for i := 0; i < len(method.Outputs); i++ { @@ -106,7 +110,7 @@ func (method Method) tupleUnpack(v interface{}, output []byte) error { } reflectValue := reflect.ValueOf(marshalledValue) - switch value.Kind() { + switch kind { case reflect.Struct: for j := 0; j < typ.NumField(); j++ { field := typ.Field(j) @@ -118,19 +122,13 @@ func (method Method) tupleUnpack(v interface{}, output []byte) error { } } case reflect.Slice, reflect.Array: - if value.Len() < i { - return fmt.Errorf("abi: insufficient number of arguments for unpack, want %d, got %d", len(method.Outputs), value.Len()) - } v := value.Index(i) - if v.Kind() != reflect.Ptr && v.Kind() != reflect.Interface { - return fmt.Errorf("abi: cannot unmarshal %v in to %v", v.Type(), reflectValue.Type()) + if err := requireAssignable(v, reflectValue); err != nil { + return err } - reflectValue := reflect.ValueOf(marshalledValue) if err := set(v.Elem(), reflectValue, method.Outputs[i]); err != nil { return err } - default: - return fmt.Errorf("abi: cannot unmarshal tuple in to %v", typ) } } return nil diff --git a/accounts/abi/reflect.go b/accounts/abi/reflect.go index e953b77c18..fbcd576e5c 100644 --- a/accounts/abi/reflect.go +++ b/accounts/abi/reflect.go @@ -85,3 +85,11 @@ func set(dst, src reflect.Value, output Argument) error { } 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 { + return fmt.Errorf("abi: cannot unmarshal %v into %v", src.Type(), dst.Type()) + } + return nil +} diff --git a/accounts/abi/unpack.go b/accounts/abi/unpack.go index 051fc1916f..5adb91ff75 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 From f0fc23bcf64a4ae60cefb6d0fc22e29c3ebcd918 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Fri, 10 Nov 2017 02:48:51 +0100 Subject: [PATCH 4/6] accounts/abi: satisfy most of the linter warnings + adding missing comments + small cleanups which won't significantly change function body. + unify Method receiver name --- accounts/abi/abi.go | 3 +- accounts/abi/argument.go | 1 + accounts/abi/event.go | 7 +-- accounts/abi/method.go | 50 ++++++++--------- accounts/abi/pack.go | 3 +- accounts/abi/type.go | 116 +++++++++++++++++++-------------------- accounts/abi/unpack.go | 4 +- 7 files changed, 88 insertions(+), 96 deletions(-) diff --git a/accounts/abi/abi.go b/accounts/abi/abi.go index 02b4fa472c..6170c7062c 100644 --- a/accounts/abi/abi.go +++ b/accounts/abi/abi.go @@ -89,7 +89,7 @@ func (abi ABI) Unpack(v interface{}, name string, output []byte) (err error) { } else if event, ok := abi.Events[name]; ok { unpack = event } else { - return fmt.Errorf("abi: could not locate named method or event.") + return fmt.Errorf("abi: could not locate named method or event") } // requires a struct to unpack into for a tuple return... @@ -99,6 +99,7 @@ func (abi ABI) Unpack(v interface{}, name string, output []byte) (err error) { return unpack.singleUnpack(v, output) } +// UnmarshalJSON implements json.Unmarshaler interface func (abi *ABI) UnmarshalJSON(data []byte) error { var fields []struct { Type string diff --git a/accounts/abi/argument.go b/accounts/abi/argument.go index cd856061ee..59bcc117ce 100644 --- a/accounts/abi/argument.go +++ b/accounts/abi/argument.go @@ -29,6 +29,7 @@ type Argument struct { Indexed bool // indexed is only used by events } +// UnmarshalJSON implements json.Unmarshaler interface func (a *Argument) UnmarshalJSON(data []byte) error { var extarg struct { Name string diff --git a/accounts/abi/event.go b/accounts/abi/event.go index b67bc96a86..3d4e0b63ce 100644 --- a/accounts/abi/event.go +++ b/accounts/abi/event.go @@ -120,7 +120,7 @@ func (e Event) singleUnpack(v interface{}, output []byte) error { } if e.Inputs[0].Indexed { - return fmt.Errorf("abi: attempting to unpack indexed variable into element.") + return fmt.Errorf("abi: attempting to unpack indexed variable into element") } value := valueOf.Elem() @@ -129,8 +129,5 @@ func (e Event) singleUnpack(v interface{}, output []byte) error { if err != nil { return err } - if err := set(value, reflect.ValueOf(marshalledValue), e.Inputs[0]); err != nil { - return err - } - return nil + return set(value, reflect.ValueOf(marshalledValue), e.Inputs[0]) } diff --git a/accounts/abi/method.go b/accounts/abi/method.go index 7240343542..838106728d 100644 --- a/accounts/abi/method.go +++ b/accounts/abi/method.go @@ -24,7 +24,7 @@ import ( "github.com/ethereum/go-ethereum/crypto" ) -// Callable method given a `Name` and whether the method is a constant. +// Method represents a callable given a `Name` and whether the method is a constant. // If the method is `Const` no transaction needs to be created for this // particular Method call. It can easily be simulated using a local VM. // For example a `Balance()` method only needs to retrieve something @@ -80,7 +80,7 @@ func (method Method) pack(args ...interface{}) ([]byte, error) { // unpacks a method return tuple into a struct of corresponding go types // // Unpacking can be done into a struct or a slice/array. -func (method Method) tupleUnpack(v interface{}, output []byte) error { +func (method Method) tupleUnpack(v interface{}, outputSlice []byte) error { // make sure the passed value is a pointer valueOf := reflect.ValueOf(v) if reflect.Ptr != valueOf.Kind() { @@ -97,16 +97,15 @@ func (method Method) tupleUnpack(v interface{}, output []byte) error { } j := 0 - for i := 0; i < len(method.Outputs); i++ { - toUnpack := method.Outputs[i] - marshalledValue, err := toGoType((i+j)*32, toUnpack.Type, output) + for i, output := range method.Outputs { + marshalledValue, err := toGoType((i+j)*32, ouptut.Type, outputSlice) if err != nil { return err } - if toUnpack.Type.T == ArrayTy { + if output.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 += toUnpack.Type.Size - 1 + j += output.Type.Size - 1 } reflectValue := reflect.ValueOf(marshalledValue) @@ -115,8 +114,8 @@ func (method Method) tupleUnpack(v interface{}, output []byte) error { for j := 0; j < typ.NumField(); j++ { field := typ.Field(j) // TODO read tags: `abi:"fieldName"` - if field.Name == strings.ToUpper(method.Outputs[i].Name[:1])+method.Outputs[i].Name[1:] { - if err := set(value.Field(j), reflectValue, method.Outputs[i]); err != nil { + if field.Name == strings.ToUpper(output.Name[:1])+output.Name[1:] { + if err := set(value.Field(j), reflectValue, output); err != nil { return err } } @@ -126,7 +125,7 @@ func (method Method) tupleUnpack(v interface{}, output []byte) error { if err := requireAssignable(v, reflectValue); err != nil { return err } - if err := set(v.Elem(), reflectValue, method.Outputs[i]); err != nil { + if err := set(v.Elem(), reflectValue, output); err != nil { return err } } @@ -149,10 +148,7 @@ func (method Method) singleUnpack(v interface{}, output []byte) error { if err != nil { return err } - if err := set(value, reflect.ValueOf(marshalledValue), method.Outputs[0]); err != nil { - return err - } - return nil + return set(value, reflect.ValueOf(marshalledValue), method.Outputs[0]) } // Sig returns the methods string signature according to the ABI spec. @@ -162,35 +158,35 @@ func (method Method) singleUnpack(v interface{}, output []byte) error { // function foo(uint32 a, int b) = "foo(uint32,int256)" // // Please note that "int" is substitute for its canonical representation "int256" -func (m Method) Sig() string { - types := make([]string, len(m.Inputs)) +func (method Method) Sig() string { + types := make([]string, len(method.Inputs)) i := 0 - for _, input := range m.Inputs { + for _, input := range method.Inputs { types[i] = input.Type.String() i++ } - return fmt.Sprintf("%v(%v)", m.Name, strings.Join(types, ",")) + return fmt.Sprintf("%v(%v)", method.Name, strings.Join(types, ",")) } -func (m Method) String() string { - inputs := make([]string, len(m.Inputs)) - for i, input := range m.Inputs { +func (method Method) String() string { + inputs := make([]string, len(method.Inputs)) + for i, input := range method.Inputs { inputs[i] = fmt.Sprintf("%v %v", input.Name, input.Type) } - outputs := make([]string, len(m.Outputs)) - for i, output := range m.Outputs { + outputs := make([]string, len(method.Outputs)) + for i, output := range method.Outputs { if len(output.Name) > 0 { outputs[i] = fmt.Sprintf("%v ", output.Name) } outputs[i] += output.Type.String() } constant := "" - if m.Const { + if method.Const { constant = "constant " } - return fmt.Sprintf("function %v(%v) %sreturns(%v)", m.Name, strings.Join(inputs, ", "), constant, strings.Join(outputs, ", ")) + return fmt.Sprintf("function %v(%v) %sreturns(%v)", method.Name, strings.Join(inputs, ", "), constant, strings.Join(outputs, ", ")) } -func (m Method) Id() []byte { - return crypto.Keccak256([]byte(m.Sig()))[:4] +func (method Method) Id() []byte { + return crypto.Keccak256([]byte(method.Sig()))[:4] } diff --git a/accounts/abi/pack.go b/accounts/abi/pack.go index 072e805368..36c58265bd 100644 --- a/accounts/abi/pack.go +++ b/accounts/abi/pack.go @@ -48,9 +48,8 @@ func packElement(t Type, reflectValue reflect.Value) []byte { case BoolTy: if reflectValue.Bool() { return math.PaddedBigBytes(common.Big1, 32) - } else { - return math.PaddedBigBytes(common.Big0, 32) } + return math.PaddedBigBytes(common.Big0, 32) case BytesTy: if reflectValue.Kind() == reflect.Array { reflectValue = mustArrayToByteSlice(reflectValue) diff --git a/accounts/abi/type.go b/accounts/abi/type.go index fba10b96d2..a1f13ffa29 100644 --- a/accounts/abi/type.go +++ b/accounts/abi/type.go @@ -24,6 +24,7 @@ import ( "strings" ) +// Type enumerator const ( IntTy byte = iota UintTy @@ -100,69 +101,66 @@ func NewType(t string) (typ Type, err error) { return Type{}, fmt.Errorf("invalid formatting of array type") } return typ, err - } else { - // parse the type and size of the abi-type. - parsedType := typeRegex.FindAllStringSubmatch(t, -1)[0] - // varSize is the size of the variable - var varSize int - if len(parsedType[3]) > 0 { - var err error - varSize, err = strconv.Atoi(parsedType[2]) - if err != nil { - return Type{}, fmt.Errorf("abi: error parsing variable size: %v", err) - } - } else { - if parsedType[0] == "uint" || parsedType[0] == "int" { - // this should fail because it means that there's something wrong with - // the abi type (the compiler should always format it to the size...always) - return Type{}, fmt.Errorf("unsupported arg type: %s", t) - } + } + // parse the type and size of the abi-type. + parsedType := typeRegex.FindAllStringSubmatch(t, -1)[0] + // varSize is the size of the variable + var varSize int + if len(parsedType[3]) > 0 { + var err error + varSize, err = strconv.Atoi(parsedType[2]) + if err != nil { + return Type{}, fmt.Errorf("abi: error parsing variable size: %v", err) } - // varType is the parsed abi type - varType := parsedType[1] - - switch varType { - case "int": - typ.Kind, typ.Type = reflectIntKindAndType(false, varSize) - typ.Size = varSize - typ.T = IntTy - case "uint": - typ.Kind, typ.Type = reflectIntKindAndType(true, varSize) - typ.Size = varSize - typ.T = UintTy - case "bool": - typ.Kind = reflect.Bool - typ.T = BoolTy - typ.Type = reflect.TypeOf(bool(false)) - case "address": - typ.Kind = reflect.Array - typ.Type = address_t - typ.Size = 20 - typ.T = AddressTy - case "string": - typ.Kind = reflect.String - typ.Type = reflect.TypeOf("") - typ.T = StringTy - case "bytes": - if varSize == 0 { - typ.T = BytesTy - typ.Kind = reflect.Slice - typ.Type = reflect.SliceOf(reflect.TypeOf(byte(0))) - } else { - typ.T = FixedBytesTy - typ.Kind = reflect.Array - typ.Size = varSize - typ.Type = reflect.ArrayOf(varSize, reflect.TypeOf(byte(0))) - } - case "function": - typ.Kind = reflect.Array - typ.T = FunctionTy - typ.Size = 24 - typ.Type = reflect.ArrayOf(24, reflect.TypeOf(byte(0))) - default: + } else { + if parsedType[0] == "uint" || parsedType[0] == "int" { + // this should fail because it means that there's something wrong with + // the abi type (the compiler should always format it to the size...always) return Type{}, fmt.Errorf("unsupported arg type: %s", t) } } + // varType is the parsed abi type + switch varType := parsedType[1]; varType { + case "int": + typ.Kind, typ.Type = reflectIntKindAndType(false, varSize) + typ.Size = varSize + typ.T = IntTy + case "uint": + typ.Kind, typ.Type = reflectIntKindAndType(true, varSize) + typ.Size = varSize + typ.T = UintTy + case "bool": + typ.Kind = reflect.Bool + typ.T = BoolTy + typ.Type = reflect.TypeOf(bool(false)) + case "address": + typ.Kind = reflect.Array + typ.Type = address_t + typ.Size = 20 + typ.T = AddressTy + case "string": + typ.Kind = reflect.String + typ.Type = reflect.TypeOf("") + typ.T = StringTy + case "bytes": + if varSize == 0 { + typ.T = BytesTy + typ.Kind = reflect.Slice + typ.Type = reflect.SliceOf(reflect.TypeOf(byte(0))) + } else { + typ.T = FixedBytesTy + typ.Kind = reflect.Array + typ.Size = varSize + typ.Type = reflect.ArrayOf(varSize, reflect.TypeOf(byte(0))) + } + case "function": + typ.Kind = reflect.Array + typ.T = FunctionTy + typ.Size = 24 + typ.Type = reflect.ArrayOf(24, reflect.TypeOf(byte(0))) + default: + return Type{}, fmt.Errorf("unsupported arg type: %s", t) + } return } diff --git a/accounts/abi/unpack.go b/accounts/abi/unpack.go index 5adb91ff75..372a86c869 100644 --- a/accounts/abi/unpack.go +++ b/accounts/abi/unpack.go @@ -79,7 +79,7 @@ func readBool(word []byte) (bool, error) { // This enforces that standard by always presenting it as a 24-array (address + sig = 24 bytes) func readFunctionType(t Type, word []byte) (funcTy [24]byte, err error) { if t.T != FunctionTy { - return [24]byte{}, fmt.Errorf("abi: invalid type in call to make function type byte array.") + return [24]byte{}, fmt.Errorf("abi: invalid type in call to make function type byte array") } if garbage := binary.BigEndian.Uint64(word[24:32]); garbage != 0 { err = fmt.Errorf("abi: got improperly encoded function type, got %v", word) @@ -92,7 +92,7 @@ func readFunctionType(t Type, word []byte) (funcTy [24]byte, err error) { // through reflection, creates a fixed array to be read from func readFixedBytes(t Type, word []byte) (interface{}, error) { if t.T != FixedBytesTy { - return nil, fmt.Errorf("abi: invalid type in call to make fixed byte array.") + return nil, fmt.Errorf("abi: invalid type in call to make fixed byte array") } // convert array := reflect.New(t.Type).Elem() From 882a32a5b762b6f6a1dc0376806761b303955840 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Thu, 23 Nov 2017 23:50:22 +0100 Subject: [PATCH 5/6] accounts/abi: add Method Unpack tests + Reworked Method Unpack tests into more readable components + Added Method Unpack into slice test --- accounts/abi/unpack_test.go | 95 +++++++++++++++++++++++-------------- 1 file changed, 59 insertions(+), 36 deletions(-) diff --git a/accounts/abi/unpack_test.go b/accounts/abi/unpack_test.go index 14393d2309..89b3bce8ca 100644 --- a/accounts/abi/unpack_test.go +++ b/accounts/abi/unpack_test.go @@ -27,6 +27,7 @@ import ( "testing" "github.com/ethereum/go-ethereum/common" + "github.com/stretchr/testify/require" ) type unpackTest struct { @@ -286,56 +287,78 @@ func TestUnpack(t *testing.T) { } } -func TestMultiReturnWithStruct(t *testing.T) { +type methodMultiOutput struct { + Int *big.Int + String string +} + +func methodMultiReturn(require *require.Assertions) (ABI, []byte, methodMultiOutput) { const definition = `[ { "name" : "multi", "constant" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]` + var expected = methodMultiOutput{big.NewInt(1), "hello"} abi, err := JSON(strings.NewReader(definition)) - if err != nil { - t.Fatal(err) - } - + require.NoError(err) // using buff to make the code readable buff := new(bytes.Buffer) buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005")) - stringOut := "hello" - buff.Write(common.RightPadBytes([]byte(stringOut), 32)) + buff.Write(common.RightPadBytes([]byte(expected.String), 32)) + return abi, buff.Bytes(), expected +} - var inter struct { - Int *big.Int - String string - } - err = abi.Unpack(&inter, "multi", buff.Bytes()) - if err != nil { - t.Error(err) - } - - if inter.Int == nil || inter.Int.Cmp(big.NewInt(1)) != 0 { - t.Error("expected Int to be 1 got", inter.Int) - } - - if inter.String != stringOut { - t.Error("expected String to be", stringOut, "got", inter.String) - } - - var reversed struct { +func TestMethodMultiReturn(t *testing.T) { + type reversed struct { String string Int *big.Int } - err = abi.Unpack(&reversed, "multi", buff.Bytes()) - if err != nil { - t.Error(err) - } - - if reversed.Int == nil || reversed.Int.Cmp(big.NewInt(1)) != 0 { - t.Error("expected Int to be 1 got", reversed.Int) - } - - if reversed.String != stringOut { - t.Error("expected String to be", stringOut, "got", reversed.String) + abi, data, expected := methodMultiReturn(require.New(t)) + bigint := new(big.Int) + var testCases = []struct { + dest interface{} + expected interface{} + error string + name string + }{{ + &methodMultiOutput{}, + &expected, + "", + "Can unpack into structure", + }, { + &reversed{}, + &reversed{expected.String, expected.Int}, + "", + "Can unpack into reversed structure", + }, { + &[]interface{}{&bigint, new(string)}, + &[]interface{}{&expected.Int, &expected.String}, + "", + "Can unpack into a slice", + }, { + &[]interface{}{new(int), new(int)}, + &[]interface{}{&expected.Int, &expected.String}, + "abi: cannot unmarshal *big.Int in to int", + "Can not unpack into a slice with wrong types", + }, { + &[]interface{}{new(int)}, + &[]interface{}{}, + "abi: insufficient number of elements in the list/array for unpack, want 2, got 1", + "Can not unpack into a slice with wrong types", + }} + for _, tc := range testCases { + tc := tc + t.Run(tc.name, func(t *testing.T) { + require := require.New(t) + err := abi.Unpack(tc.dest, "multi", data) + if tc.error == "" { + require.Nil(err, "Should be able to unpack method outputs.") + require.Equal(tc.expected, tc.dest) + } else { + require.EqualError(err, tc.error) + } + }) } } From 2aeb2555fad6cb3300f01ae01b97c0314128b52b Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Fri, 1 Dec 2017 22:32:04 +0100 Subject: [PATCH 6/6] 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},