From 3511904aad5e492b271c68db36730d88cab46911 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Fri, 10 Nov 2017 02:28:17 +0100 Subject: [PATCH 1/8] 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 a3899b4a6..d966ad1db 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 9becba5540540bc37d4ad5eaaf7e4c1937a6542f Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Fri, 10 Nov 2017 00:08:28 +0100 Subject: [PATCH 2/8] 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 bd1098d87..0d3c3c4fa 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 0ed8b838a991f81f79cc6ed4fa961c563203a7a2 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Fri, 10 Nov 2017 02:30:26 +0100 Subject: [PATCH 3/8] 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 4691318ce..cd856061e 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 0d3c3c4fa..b67bc96a8 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 66e8751f3..ee79b51cb 100644 --- a/accounts/abi/method.go +++ b/accounts/abi/method.go @@ -101,7 +101,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++ { @@ -117,7 +121,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) @@ -129,19 +133,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 e953b77c1..fbcd576e5 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 051fc1916..5adb91ff7 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 95461e8b2289c1b8f6c588087a8de5f4f64a749c Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Fri, 10 Nov 2017 02:48:51 +0100 Subject: [PATCH 4/8] 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 02b4fa472..6170c7062 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 cd856061e..59bcc117c 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 b67bc96a8..3d4e0b63c 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 ee79b51cb..0df7f618e 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 @@ -91,7 +91,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() { @@ -108,16 +108,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) @@ -126,8 +125,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 } } @@ -137,7 +136,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 } } @@ -160,10 +159,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. @@ -173,35 +169,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 072e80536..36c58265b 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 fba10b96d..a1f13ffa2 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 5adb91ff7..372a86c86 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 1afca33eacd39c8cf240d92e9b4310a73de6510f Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Thu, 23 Nov 2017 23:50:22 +0100 Subject: [PATCH 5/8] 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 14393d230..89b3bce8c 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 81d4cafb324d8c4352a40d2cf33c2085d029cd37 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Fri, 1 Dec 2017 22:32:04 +0100 Subject: [PATCH 6/8] accounts/abi: add unpack into array test --- accounts/abi/event_test.go | 12 +++++++++++- accounts/abi/unpack.go | 2 +- accounts/abi/unpack_test.go | 5 +++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/accounts/abi/event_test.go b/accounts/abi/event_test.go index d966ad1db..34c7eebde 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/unpack.go b/accounts/abi/unpack.go index 372a86c86..377aee874 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 89b3bce8c..391095073 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}, From 73d4a57d47d3381faa0516b319fa5598e71681f9 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Thu, 21 Dec 2017 10:26:30 +0100 Subject: [PATCH 7/8] acounts/abi: refactor abi, generalize abi pack/unpack to Arguments --- accounts/abi/abi.go | 39 ++++----- accounts/abi/abi_test.go | 10 ++- accounts/abi/argument.go | 161 +++++++++++++++++++++++++++++++++++++ accounts/abi/event.go | 89 +------------------- accounts/abi/event_test.go | 75 +++++++++++++++++ accounts/abi/method.go | 128 +---------------------------- accounts/abi/unpack.go | 9 --- 7 files changed, 260 insertions(+), 251 deletions(-) diff --git a/accounts/abi/abi.go b/accounts/abi/abi.go index 6170c7062..7229a67bf 100644 --- a/accounts/abi/abi.go +++ b/accounts/abi/abi.go @@ -50,25 +50,25 @@ func JSON(reader io.Reader) (ABI, error) { // methods string signature. (signature = baz(uint32,string32)) func (abi ABI) Pack(name string, args ...interface{}) ([]byte, error) { // Fetch the ABI of the requested method - var method Method - if name == "" { - method = abi.Constructor - } else { - m, exist := abi.Methods[name] - if !exist { - return nil, fmt.Errorf("method '%s' not found", name) + // constructor + arguments, err := abi.Constructor.Inputs.Pack(args...) + if err != nil { + return nil, err } - method = m + return arguments, nil + } - arguments, err := method.pack(args...) + method, exist := abi.Methods[name] + if !exist { + return nil, fmt.Errorf("method '%s' not found", name) + } + + arguments, err := method.Inputs.Pack(args...) if err != nil { return nil, err } // Pack up the method ID too if not a constructor and return - if name == "" { - return arguments, nil - } return append(method.Id(), arguments...), nil } @@ -77,26 +77,17 @@ func (abi ABI) Unpack(v interface{}, name string, output []byte) (err error) { if len(output) == 0 { return fmt.Errorf("abi: unmarshalling empty output") } - // since there can't be naming collisions with contracts and events, // we need to decide whether we're calling a method or an event - var unpack unpacker if method, ok := abi.Methods[name]; ok { if len(output)%32 != 0 { return fmt.Errorf("abi: improperly formatted output") } - unpack = method + return method.Outputs.Unpack(v, output) } else if event, ok := abi.Events[name]; ok { - unpack = event - } else { - return fmt.Errorf("abi: could not locate named method or event") + return event.Inputs.Unpack(v, output) } - - // requires a struct to unpack into for a tuple return... - if unpack.isTupleReturn() { - return unpack.tupleUnpack(v, output) - } - return unpack.singleUnpack(v, output) + return fmt.Errorf("abi: could not locate named method or event") } // UnmarshalJSON implements json.Unmarshaler interface diff --git a/accounts/abi/abi_test.go b/accounts/abi/abi_test.go index 644a388e3..1ae351730 100644 --- a/accounts/abi/abi_test.go +++ b/accounts/abi/abi_test.go @@ -22,7 +22,6 @@ import ( "fmt" "log" "math/big" - "reflect" "strings" "testing" @@ -75,9 +74,12 @@ func TestReader(t *testing.T) { } // deep equal fails for some reason - t.Skip() - if !reflect.DeepEqual(abi, exp) { - t.Errorf("\nabi: %v\ndoes not match exp: %v", abi, exp) + //t.Skip() + // Check with String() instead + expS := fmt.Sprintf("%v",exp) + gotS := fmt.Sprintf("%v", abi) + if expS != gotS { + t.Errorf("\nGot abi: \n%v\ndoes not match expected \n%v", abi, exp) } } diff --git a/accounts/abi/argument.go b/accounts/abi/argument.go index 59bcc117c..c41c2c6b0 100644 --- a/accounts/abi/argument.go +++ b/accounts/abi/argument.go @@ -19,6 +19,8 @@ package abi import ( "encoding/json" "fmt" + "reflect" + "strings" ) // Argument holds the name of the argument and the corresponding type. @@ -29,6 +31,8 @@ type Argument struct { Indexed bool // indexed is only used by events } +type Arguments []Argument + // UnmarshalJSON implements json.Unmarshaler interface func (a *Argument) UnmarshalJSON(data []byte) error { var extarg struct { @@ -60,3 +64,160 @@ func countNonIndexedArguments(args []Argument) int { } return out } +func (a *Arguments) isTuple() bool { + return a != nil && len(*a) > 1 +} + +func (a *Arguments) Unpack(v interface{}, data []byte) error { + if a.isTuple() { + return a.unpackTuple(v, data) + } + return a.unpackAtomic(v, data) +} + +func (a *Arguments) unpackTuple(v interface{}, output []byte) error { + // make sure the passed value is a pointer + valueOf := reflect.ValueOf(v) + if reflect.Ptr != valueOf.Kind() { + return fmt.Errorf("abi: Unpack(non-pointer %T)", v) + } + + var ( + value = valueOf.Elem() + typ = value.Type() + kind = value.Kind() + ) +/* !TODO add this back + if err := requireUnpackKind(value, typ, kind, (*a), false); 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 _, arg := range(*a) { + + if arg.Indexed { + // can't read, continue + continue + } + i++ + marshalledValue, err := toGoType((i+j)*32, arg.Type, output) + 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 { + case reflect.Struct: + for j := 0; j < typ.NumField(); j++ { + field := typ.Field(j) + // TODO read tags: `abi:"fieldName"` + if field.Name == strings.ToUpper(arg.Name[:1])+arg.Name[1:] { + if err := set(value.Field(j), reflectValue, arg); 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(*a), value.Len()) + } + v := value.Index(i) + if err := requireAssignable(v, reflectValue); err != nil { + return err + } + reflectValue := reflect.ValueOf(marshalledValue) + return set(v.Elem(), reflectValue, arg) + default: + return fmt.Errorf("abi:[2] cannot unmarshal tuple in to %v", typ) + } + } + return nil +} + +func (a *Arguments) unpackAtomic(v interface{}, output []byte) error { + // make sure the passed value is a pointer + valueOf := reflect.ValueOf(v) + if reflect.Ptr != valueOf.Kind() { + return fmt.Errorf("abi: Unpack(non-pointer %T)", v) + } + arg := (*a)[0] + if arg.Indexed { + return fmt.Errorf("abi: attempting to unpack indexed variable into element.") + } + + value := valueOf.Elem() + + marshalledValue, err := toGoType(0, arg.Type, output) + if err != nil { + return err + } + if err := set(value, reflect.ValueOf(marshalledValue), arg); err != nil { + return err + } + return nil +} + +func (arguments *Arguments) Pack(args ...interface{}) ([]byte, error) { + // Make sure arguments match up and pack them + if arguments == nil { + return nil, fmt.Errorf("arguments are nil, programmer error!") + } + + abiArgs := *arguments + if len(args) != len(abiArgs) { + return nil, fmt.Errorf("argument count mismatch: %d for %d", len(args), len(abiArgs)) + } + + // variable input is the output appended at the end of packed + // output. This is used for strings and bytes types input. + var variableInput []byte + + // input offset is the bytes offset for packed output + inputOffset := 0 + for _, abiArg := range abiArgs { + if abiArg.Type.T == ArrayTy { + inputOffset += (32 * abiArg.Type.Size) + } else { + inputOffset += 32 + } + } + + var ret []byte + for i, a := range args { + input := abiArgs[i] + // pack the input + packed, err := input.Type.pack(reflect.ValueOf(a)) + if err != nil { + return nil, err + } + + // check for a slice type (string, bytes, slice) + if input.Type.requiresLengthPrefix() { + // calculate the offset + offset := inputOffset + len(variableInput) + // set the offset + ret = append(ret, packNum(reflect.ValueOf(offset))...) + // Append the packed output to the variable input. The variable input + // will be appended at the end of the input. + variableInput = append(variableInput, packed...) + } else { + // append the packed value to the input + ret = append(ret, packed...) + } + } + // append the variable input at the end of the packed input + ret = append(ret, variableInput...) + + return ret, nil +} diff --git a/accounts/abi/event.go b/accounts/abi/event.go index 3d4e0b63c..726bac90e 100644 --- a/accounts/abi/event.go +++ b/accounts/abi/event.go @@ -18,7 +18,6 @@ package abi import ( "fmt" - "reflect" "strings" "github.com/ethereum/go-ethereum/common" @@ -31,7 +30,7 @@ import ( type Event struct { Name string Anonymous bool - Inputs []Argument + Inputs Arguments } // Id returns the canonical representation of the event's signature used by the @@ -45,89 +44,3 @@ func (e Event) Id() common.Hash { } return common.BytesToHash(crypto.Keccak256([]byte(fmt.Sprintf("%v(%v)", e.Name, strings.Join(types, ","))))) } - -// unpacks an event return tuple into a struct of corresponding go types -// -// Unpacking can be done into a struct or a slice/array. -func (e Event) tupleUnpack(v interface{}, output []byte) error { - // make sure the passed value is a pointer - valueOf := reflect.ValueOf(v) - if reflect.Ptr != valueOf.Kind() { - return fmt.Errorf("abi: Unpack(non-pointer %T)", v) - } - - var ( - value = valueOf.Elem() - typ = value.Type() - kind = value.Kind() - ) - 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 { - // Indexed arguments are not packed into data - continue - } - i++ - marshalledValue, err := toGoType((i+j)*32, input.Type, output) - if err != nil { - return err - } - if input.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 += input.Type.Size - 1 - } - reflectValue := reflect.ValueOf(marshalledValue) - - switch kind { - case reflect.Struct: - for j := 0; j < typ.NumField(); j++ { - field := typ.Field(j) - // TODO read tags: `abi:"fieldName"` - 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: - v := value.Index(i) - if err := requireAssignable(v, reflectValue); err != nil { - return err - } - if err := set(v.Elem(), reflectValue, input); err != nil { - return err - } - } - } - return nil -} - -func (e Event) isTupleReturn() bool { return len(e.Inputs) > 1 } - -func (e Event) singleUnpack(v interface{}, output []byte) error { - // make sure the passed value is a pointer - valueOf := reflect.ValueOf(v) - if reflect.Ptr != valueOf.Kind() { - return fmt.Errorf("abi: Unpack(non-pointer %T)", v) - } - - if e.Inputs[0].Indexed { - return fmt.Errorf("abi: attempting to unpack indexed variable into element") - } - - value := valueOf.Elem() - - marshalledValue, err := toGoType(0, e.Inputs[0].Type, output) - if err != nil { - return err - } - return set(value, reflect.ValueOf(marshalledValue), e.Inputs[0]) -} diff --git a/accounts/abi/event_test.go b/accounts/abi/event_test.go index 34c7eebde..93740f6a5 100644 --- a/accounts/abi/event_test.go +++ b/accounts/abi/event_test.go @@ -241,3 +241,78 @@ func unpackTestEventData(dest interface{}, hexData string, jsonEvent []byte, ass a := ABI{Events: map[string]Event{"e": e}} return a.Unpack(dest, "e", data) } + +/* +!TODO enable these when the fix is in. Taken from +https://github.com/ethereum/go-ethereum/pull/15568 + +*/ +/* +type testResult struct { + Values [2]*big.Int + Value1 *big.Int + Value2 *big.Int +} + +type testCase struct { + definition string + want testResult +} + +func (tc testCase) encoded(intType, arrayType Type) []byte { + var b bytes.Buffer + if tc.want.Value1 != nil { + val, _ := intType.pack(reflect.ValueOf(tc.want.Value1)) + b.Write(val) + } + + if !reflect.DeepEqual(tc.want.Values, [2]*big.Int{nil, nil}) { + val, _ := arrayType.pack(reflect.ValueOf(tc.want.Values)) + b.Write(val) + } + if tc.want.Value2 != nil { + val, _ := intType.pack(reflect.ValueOf(tc.want.Value2)) + b.Write(val) + } + return b.Bytes() +} + +// TestEventUnpackIndexed verifies that indexed field will be skipped by event decoder. +func TestEventUnpackIndexed(t *testing.T) { + definition := `[{"name": "test", "type": "event", "inputs": [{"indexed": true, "name":"value1", "type":"uint8"},{"indexed": false, "name":"value2", "type":"uint8"}]}]` + type testStruct struct { + Value1 uint8 + Value2 uint8 + } + abi, err := JSON(strings.NewReader(definition)) + require.NoError(t, err) + var b bytes.Buffer + b.Write(packNum(reflect.ValueOf(uint8(8)))) + var rst testStruct + require.NoError(t, abi.Unpack(&rst, "test", b.Bytes())) + require.Equal(t, uint8(0), rst.Value1) + require.Equal(t, uint8(8), rst.Value2) +} + +// TestEventIndexedWithArrayUnpack verifies that decoder will not overlow when static array is indexed input. +func TestEventIndexedWithArrayUnpack(t *testing.T) { + definition := `[{"name": "test", "type": "event", "inputs": [{"indexed": true, "name":"value1", "type":"uint8[2]"},{"indexed": false, "name":"value2", "type":"string"}]}]` + type testStruct struct { + Value1 [2]uint8 + Value2 string + } + abi, err := JSON(strings.NewReader(definition)) + require.NoError(t, err) + var b bytes.Buffer + stringOut := "abc" + // number of fields that will be encoded * 32 + b.Write(packNum(reflect.ValueOf(32))) + b.Write(packNum(reflect.ValueOf(len(stringOut)))) + b.Write(common.RightPadBytes([]byte(stringOut), 32)) + fmt.Println(b.Bytes()) + var rst testStruct + require.NoError(t, abi.Unpack(&rst, "test", b.Bytes())) + require.Equal(t, [2]uint8{0, 0}, rst.Value1) + require.Equal(t, stringOut, rst.Value2) +} +*/ diff --git a/accounts/abi/method.go b/accounts/abi/method.go index 0df7f618e..f434ffdbe 100644 --- a/accounts/abi/method.go +++ b/accounts/abi/method.go @@ -18,7 +18,6 @@ package abi import ( "fmt" - "reflect" "strings" "github.com/ethereum/go-ethereum/crypto" @@ -35,131 +34,8 @@ import ( type Method struct { Name string Const bool - Inputs []Argument - Outputs []Argument -} - -func (method Method) pack(args ...interface{}) ([]byte, error) { - // Make sure arguments match up and pack them - if len(args) != len(method.Inputs) { - return nil, fmt.Errorf("argument count mismatch: %d for %d", len(args), len(method.Inputs)) - } - // variable input is the output appended at the end of packed - // output. This is used for strings and bytes types input. - var variableInput []byte - - // input offset is the bytes offset for packed output - inputOffset := 0 - for _, input := range method.Inputs { - if input.Type.T == ArrayTy { - inputOffset += (32 * input.Type.Size) - } else { - inputOffset += 32 - } - } - - var ret []byte - for i, a := range args { - input := method.Inputs[i] - // pack the input - packed, err := input.Type.pack(reflect.ValueOf(a)) - if err != nil { - return nil, fmt.Errorf("`%s` %v", method.Name, err) - } - - // check for a slice type (string, bytes, slice) - if input.Type.requiresLengthPrefix() { - // calculate the offset - offset := inputOffset + len(variableInput) - - // set the offset - ret = append(ret, packNum(reflect.ValueOf(offset))...) - // Append the packed output to the variable input. The variable input - // will be appended at the end of the input. - variableInput = append(variableInput, packed...) - } else { - // append the packed value to the input - ret = append(ret, packed...) - } - } - // append the variable input at the end of the packed input - ret = append(ret, variableInput...) - - return ret, nil -} - -// 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{}, outputSlice []byte) error { - // make sure the passed value is a pointer - valueOf := reflect.ValueOf(v) - if reflect.Ptr != valueOf.Kind() { - return fmt.Errorf("abi: Unpack(non-pointer %T)", v) - } - - 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, output := range method.Outputs { - marshalledValue, err := toGoType((i+j)*32, ouptut.Type, outputSlice) - if err != nil { - return err - } - 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 += output.Type.Size - 1 - } - reflectValue := reflect.ValueOf(marshalledValue) - - switch kind { - case reflect.Struct: - for j := 0; j < typ.NumField(); j++ { - field := typ.Field(j) - // TODO read tags: `abi:"fieldName"` - if field.Name == strings.ToUpper(output.Name[:1])+output.Name[1:] { - if err := set(value.Field(j), reflectValue, output); err != nil { - return err - } - } - } - case reflect.Slice, reflect.Array: - v := value.Index(i) - if err := requireAssignable(v, reflectValue); err != nil { - return err - } - if err := set(v.Elem(), reflectValue, output); err != nil { - return err - } - } - } - return nil -} - -func (method Method) isTupleReturn() bool { return len(method.Outputs) > 1 } - -func (method Method) singleUnpack(v interface{}, output []byte) error { - // make sure the passed value is a pointer - valueOf := reflect.ValueOf(v) - if reflect.Ptr != valueOf.Kind() { - return fmt.Errorf("abi: Unpack(non-pointer %T)", v) - } - - value := valueOf.Elem() - - marshalledValue, err := toGoType(0, method.Outputs[0].Type, output) - if err != nil { - return err - } - return set(value, reflect.ValueOf(marshalledValue), method.Outputs[0]) + Inputs Arguments + Outputs Arguments } // Sig returns the methods string signature according to the ABI spec. diff --git a/accounts/abi/unpack.go b/accounts/abi/unpack.go index 377aee874..80efb3f7e 100644 --- a/accounts/abi/unpack.go +++ b/accounts/abi/unpack.go @@ -25,15 +25,6 @@ import ( "github.com/ethereum/go-ethereum/common" ) -// unpacker is a utility interface that enables us to have -// abstraction between events and methods and also to properly -// "unpack" them; e.g. events use Inputs, methods use Outputs. -type unpacker interface { - tupleUnpack(v interface{}, output []byte) error - singleUnpack(v interface{}, output []byte) error - isTupleReturn() bool -} - // reads the integer based on its kind func readInteger(kind reflect.Kind, b []byte) interface{} { switch kind { From c095c87e117785ba5467487336215f86a958409b Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Thu, 21 Dec 2017 16:18:37 +0100 Subject: [PATCH 8/8] accounts/abi: merging of https://github.com/ethereum/go-ethereum/pull/15452 + lookup by id --- accounts/abi/abi.go | 12 +++++++ accounts/abi/abi_test.go | 65 ++++++++++++++++++++++++++++++---- accounts/abi/argument.go | 71 +++++++++++++++++++------------------- accounts/abi/event_test.go | 10 +++--- accounts/abi/reflect.go | 17 +++++++++ 5 files changed, 128 insertions(+), 47 deletions(-) diff --git a/accounts/abi/abi.go b/accounts/abi/abi.go index 7229a67bf..cbcf4ca92 100644 --- a/accounts/abi/abi.go +++ b/accounts/abi/abi.go @@ -17,6 +17,7 @@ package abi import ( + "bytes" "encoding/json" "fmt" "io" @@ -133,3 +134,14 @@ func (abi *ABI) UnmarshalJSON(data []byte) error { return nil } + +// MethodById looks up a method by the 4-byte id +// returns nil if none found +func (abi *ABI) MethodById(sigdata []byte) *Method { + for _, method := range abi.Methods { + if bytes.Equal(method.Id(), sigdata[:4]) { + return &method + } + } + return nil +} diff --git a/accounts/abi/abi_test.go b/accounts/abi/abi_test.go index 1ae351730..2d43b631c 100644 --- a/accounts/abi/abi_test.go +++ b/accounts/abi/abi_test.go @@ -25,6 +25,8 @@ import ( "strings" "testing" + "reflect" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" ) @@ -74,12 +76,24 @@ func TestReader(t *testing.T) { } // deep equal fails for some reason - //t.Skip() - // Check with String() instead - expS := fmt.Sprintf("%v",exp) - gotS := fmt.Sprintf("%v", abi) - if expS != gotS { - t.Errorf("\nGot abi: \n%v\ndoes not match expected \n%v", abi, exp) + for name, expM := range exp.Methods { + gotM, exist := abi.Methods[name] + if !exist { + t.Errorf("Missing expected method %v", name) + } + if !reflect.DeepEqual(gotM, expM) { + t.Errorf("\nGot abi method: \n%v\ndoes not match expected method\n%v", gotM, expM) + } + } + + for name, gotM := range abi.Methods { + expM, exist := exp.Methods[name] + if !exist { + t.Errorf("Found extra method %v", name) + } + if !reflect.DeepEqual(gotM, expM) { + t.Errorf("\nGot abi method: \n%v\ndoes not match expected method\n%v", gotM, expM) + } } } @@ -643,3 +657,42 @@ func TestUnpackEvent(t *testing.T) { t.Logf("len(data): %d; received event: %+v", len(data), ev) } } + +func TestABI_MethodById(t *testing.T) { + const abiJSON = `[ + {"type":"function","name":"receive","constant":false,"inputs":[{"name":"memo","type":"bytes"}],"outputs":[],"payable":true,"stateMutability":"payable"}, + {"type":"event","name":"received","anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"amount","type":"uint256"},{"indexed":false,"name":"memo","type":"bytes"}]}, + {"type":"function","name":"fixedArrStr","constant":true,"inputs":[{"name":"str","type":"string"},{"name":"fixedArr","type":"uint256[2]"}]}, + {"type":"function","name":"fixedArrBytes","constant":true,"inputs":[{"name":"str","type":"bytes"},{"name":"fixedArr","type":"uint256[2]"}]}, + {"type":"function","name":"mixedArrStr","constant":true,"inputs":[{"name":"str","type":"string"},{"name":"fixedArr","type":"uint256[2]"},{"name":"dynArr","type":"uint256[]"}]}, + {"type":"function","name":"doubleFixedArrStr","constant":true,"inputs":[{"name":"str","type":"string"},{"name":"fixedArr1","type":"uint256[2]"},{"name":"fixedArr2","type":"uint256[3]"}]}, + {"type":"function","name":"multipleMixedArrStr","constant":true,"inputs":[{"name":"str","type":"string"},{"name":"fixedArr1","type":"uint256[2]"},{"name":"dynArr","type":"uint256[]"},{"name":"fixedArr2","type":"uint256[3]"}]}, + {"type":"function","name":"balance","constant":true}, + {"type":"function","name":"send","constant":false,"inputs":[{"name":"amount","type":"uint256"}]}, + {"type":"function","name":"test","constant":false,"inputs":[{"name":"number","type":"uint32"}]}, + {"type":"function","name":"string","constant":false,"inputs":[{"name":"inputs","type":"string"}]}, + {"type":"function","name":"bool","constant":false,"inputs":[{"name":"inputs","type":"bool"}]}, + {"type":"function","name":"address","constant":false,"inputs":[{"name":"inputs","type":"address"}]}, + {"type":"function","name":"uint64[2]","constant":false,"inputs":[{"name":"inputs","type":"uint64[2]"}]}, + {"type":"function","name":"uint64[]","constant":false,"inputs":[{"name":"inputs","type":"uint64[]"}]}, + {"type":"function","name":"foo","constant":false,"inputs":[{"name":"inputs","type":"uint32"}]}, + {"type":"function","name":"bar","constant":false,"inputs":[{"name":"inputs","type":"uint32"},{"name":"string","type":"uint16"}]}, + {"type":"function","name":"_slice","constant":false,"inputs":[{"name":"inputs","type":"uint32[2]"}]}, + {"type":"function","name":"__slice256","constant":false,"inputs":[{"name":"inputs","type":"uint256[2]"}]}, + {"type":"function","name":"sliceAddress","constant":false,"inputs":[{"name":"inputs","type":"address[]"}]}, + {"type":"function","name":"sliceMultiAddress","constant":false,"inputs":[{"name":"a","type":"address[]"},{"name":"b","type":"address[]"}]} + ] +` + abi, err := JSON(strings.NewReader(abiJSON)) + if err != nil { + t.Fatal(err) + } + for name, m := range abi.Methods { + a := fmt.Sprintf("%v", m) + b := fmt.Sprintf("%v", abi.MethodById(m.Id())) + if a != b { + t.Errorf("Method %v (id %v) not 'findable' by id in ABI", name, common.ToHex(m.Id())) + } + } + +} diff --git a/accounts/abi/argument.go b/accounts/abi/argument.go index c41c2c6b0..ad17fbf2b 100644 --- a/accounts/abi/argument.go +++ b/accounts/abi/argument.go @@ -34,7 +34,7 @@ type Argument struct { type Arguments []Argument // UnmarshalJSON implements json.Unmarshaler interface -func (a *Argument) UnmarshalJSON(data []byte) error { +func (argument *Argument) UnmarshalJSON(data []byte) error { var extarg struct { Name string Type string @@ -45,38 +45,43 @@ func (a *Argument) UnmarshalJSON(data []byte) error { return fmt.Errorf("argument json err: %v", err) } - a.Type, err = NewType(extarg.Type) + argument.Type, err = NewType(extarg.Type) if err != nil { return err } - a.Name = extarg.Name - a.Indexed = extarg.Indexed + argument.Name = extarg.Name + argument.Indexed = extarg.Indexed return nil } -func countNonIndexedArguments(args []Argument) int { +// LengthNonIndexed returns the number of arguments when not counting 'indexed' ones. Only events +// can ever have 'indexed' arguments, it should always be false on arguments for method input/output +func (arguments Arguments) LengthNonIndexed() int { out := 0 - for i := range args { - if !args[i].Indexed { + for _, arg := range arguments { + if !arg.Indexed { out++ } } return out } -func (a *Arguments) isTuple() bool { - return a != nil && len(*a) > 1 + +// isTuple returns true for non-atomic constructs, like (uint,uint) or uint[] +func (arguments Arguments) isTuple() bool { + return len(arguments) > 1 } -func (a *Arguments) Unpack(v interface{}, data []byte) error { - if a.isTuple() { - return a.unpackTuple(v, data) +// Unpack performs the operation hexdata -> Go format +func (arguments Arguments) Unpack(v interface{}, data []byte) error { + if arguments.isTuple() { + return arguments.unpackTuple(v, data) } - return a.unpackAtomic(v, data) + return arguments.unpackAtomic(v, data) } -func (a *Arguments) unpackTuple(v interface{}, output []byte) error { - // make sure the passed value is a pointer +func (arguments Arguments) unpackTuple(v interface{}, output []byte) error { + // make sure the passed value is arguments pointer valueOf := reflect.ValueOf(v) if reflect.Ptr != valueOf.Kind() { return fmt.Errorf("abi: Unpack(non-pointer %T)", v) @@ -87,17 +92,16 @@ func (a *Arguments) unpackTuple(v interface{}, output []byte) error { typ = value.Type() kind = value.Kind() ) -/* !TODO add this back - if err := requireUnpackKind(value, typ, kind, (*a), false); err != nil { + + if err := requireUnpackKind(value, typ, kind, arguments); 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 _, arg := range(*a) { + for _, arg := range arguments { if arg.Indexed { // can't read, continue @@ -130,14 +134,16 @@ func (a *Arguments) unpackTuple(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(*a), value.Len()) + return fmt.Errorf("abi: insufficient number of arguments for unpack, want %d, got %d", len(arguments), value.Len()) } v := value.Index(i) if err := requireAssignable(v, reflectValue); err != nil { return err } - reflectValue := reflect.ValueOf(marshalledValue) - return set(v.Elem(), reflectValue, arg) + + if err := set(v.Elem(), reflectValue, arg); err != nil { + return err + } default: return fmt.Errorf("abi:[2] cannot unmarshal tuple in to %v", typ) } @@ -145,13 +151,14 @@ func (a *Arguments) unpackTuple(v interface{}, output []byte) error { return nil } -func (a *Arguments) unpackAtomic(v interface{}, output []byte) error { - // make sure the passed value is a pointer +// unpackAtomic unpacks ( hexdata -> go ) a single value +func (arguments Arguments) unpackAtomic(v interface{}, output []byte) error { + // make sure the passed value is arguments pointer valueOf := reflect.ValueOf(v) if reflect.Ptr != valueOf.Kind() { return fmt.Errorf("abi: Unpack(non-pointer %T)", v) } - arg := (*a)[0] + arg := arguments[0] if arg.Indexed { return fmt.Errorf("abi: attempting to unpack indexed variable into element.") } @@ -162,19 +169,13 @@ func (a *Arguments) unpackAtomic(v interface{}, output []byte) error { if err != nil { return err } - if err := set(value, reflect.ValueOf(marshalledValue), arg); err != nil { - return err - } - return nil + return set(value, reflect.ValueOf(marshalledValue), arg) } -func (arguments *Arguments) Pack(args ...interface{}) ([]byte, error) { +// Unpack performs the operation Go format -> Hexdata +func (arguments Arguments) Pack(args ...interface{}) ([]byte, error) { // Make sure arguments match up and pack them - if arguments == nil { - return nil, fmt.Errorf("arguments are nil, programmer error!") - } - - abiArgs := *arguments + abiArgs := arguments if len(args) != len(abiArgs) { return nil, fmt.Errorf("argument count mismatch: %d for %d", len(args), len(abiArgs)) } diff --git a/accounts/abi/event_test.go b/accounts/abi/event_test.go index 93740f6a5..cca61e433 100644 --- a/accounts/abi/event_test.go +++ b/accounts/abi/event_test.go @@ -225,7 +225,7 @@ func TestEventTupleUnpack(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) + assert.Equal(tc.expected, tc.dest, tc.name) } else { assert.EqualError(err, tc.error) } @@ -243,11 +243,10 @@ func unpackTestEventData(dest interface{}, hexData string, jsonEvent []byte, ass } /* -!TODO enable these when the fix is in. Taken from +Taken from https://github.com/ethereum/go-ethereum/pull/15568 - */ -/* + type testResult struct { Values [2]*big.Int Value1 *big.Int @@ -309,10 +308,9 @@ func TestEventIndexedWithArrayUnpack(t *testing.T) { b.Write(packNum(reflect.ValueOf(32))) b.Write(packNum(reflect.ValueOf(len(stringOut)))) b.Write(common.RightPadBytes([]byte(stringOut), 32)) - fmt.Println(b.Bytes()) + var rst testStruct require.NoError(t, abi.Unpack(&rst, "test", b.Bytes())) require.Equal(t, [2]uint8{0, 0}, rst.Value1) require.Equal(t, stringOut, rst.Value2) } -*/ diff --git a/accounts/abi/reflect.go b/accounts/abi/reflect.go index fbcd576e5..7a9cdacd5 100644 --- a/accounts/abi/reflect.go +++ b/accounts/abi/reflect.go @@ -93,3 +93,20 @@ func requireAssignable(dst, src reflect.Value) error { } return nil } + +// requireUnpackKind verifies preconditions for unpacking `args` into `kind` +func requireUnpackKind(v reflect.Value, t reflect.Type, k reflect.Kind, + args Arguments) error { + + switch k { + case reflect.Struct: + case reflect.Slice, reflect.Array: + if minLen := args.LengthNonIndexed(); 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 +}