From 43f13dbaef34a7a8d3304c3cfcf34735b02f48eb Mon Sep 17 00:00:00 2001 From: Marius van der Wijden Date: Mon, 6 Apr 2020 12:35:37 +0200 Subject: [PATCH] accounts/abi/bind: error on tuples in topics --- accounts/abi/bind/topics.go | 31 ++++++++++++++++--------------- accounts/abi/bind/topics_test.go | 16 ++++++++++++++++ 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/accounts/abi/bind/topics.go b/accounts/abi/bind/topics.go index f21ca61619..126a1edef8 100644 --- a/accounts/abi/bind/topics.go +++ b/accounts/abi/bind/topics.go @@ -105,27 +105,27 @@ func makeTopics(query ...[]interface{}) ([][]common.Hash, error) { // parseTopics converts the indexed topic fields into actual log field values. func parseTopics(out interface{}, fields abi.Arguments, topics []common.Hash) error { - store := func(arg abi.Argument, reconstr interface{}) { - field := reflect.ValueOf(out).Elem().FieldByName(capitalise(arg.Name)) - field.Set(reflect.ValueOf(reconstr)) - } - return parseTopicIntoFunc(fields, topics, store) + return parseTopicWithSetter(fields, topics, + func(arg abi.Argument, reconstr interface{}) { + field := reflect.ValueOf(out).Elem().FieldByName(capitalise(arg.Name)) + field.Set(reflect.ValueOf(reconstr)) + }) } // parseTopicsIntoMap converts the indexed topic field-value pairs into map key-value pairs func parseTopicsIntoMap(out map[string]interface{}, fields abi.Arguments, topics []common.Hash) error { - store := func(arg abi.Argument, reconstr interface{}) { - out[arg.Name] = reconstr - } - return parseTopicIntoFunc(fields, topics, store) + return parseTopicWithSetter(fields, topics, + func(arg abi.Argument, reconstr interface{}) { + out[arg.Name] = reconstr + }) } -// parseTopicIntoFunc converts the indexed topic field-value pairs and stores them using the -// provided store function. +// parseTopicWithSetter converts the indexed topic field-value pairs and stores them using the +// provided set function. // // Note, dynamic types cannot be reconstructed since they get mapped to Keccak256 // hashes as the topic value! -func parseTopicIntoFunc(fields abi.Arguments, topics []common.Hash, store func(abi.Argument, interface{})) error { +func parseTopicWithSetter(fields abi.Arguments, topics []common.Hash, set func(abi.Argument, interface{})) error { // Sanity check that the fields and topics match up if len(fields) != len(topics) { return errors.New("topic/field count mismatch") @@ -135,9 +135,10 @@ func parseTopicIntoFunc(fields abi.Arguments, topics []common.Hash, store func(a if !arg.Indexed { return errors.New("non-indexed field in topic reconstruction") } - //reconstr, err := parseField(arg, topics[i]) var reconstr interface{} switch arg.Type.T { + case abi.TupleTy: + return errors.New("tuple type in topic reconstruction") case abi.StringTy, abi.BytesTy, abi.SliceTy, abi.ArrayTy: // Array types (including strings and bytes) have their keccak256 hashes stored in the topic- not a hash // whose bytes can be decoded to the actual value- so the best we can do is retrieve that hash @@ -156,8 +157,8 @@ func parseTopicIntoFunc(fields abi.Arguments, topics []common.Hash, store func(a return err } } - // Use the store function to store the value - store(arg, reconstr) + // Use the set function to store the value + set(arg, reconstr) } return nil diff --git a/accounts/abi/bind/topics_test.go b/accounts/abi/bind/topics_test.go index c62f5bab32..df5c8f7e88 100644 --- a/accounts/abi/bind/topics_test.go +++ b/accounts/abi/bind/topics_test.go @@ -84,6 +84,7 @@ func setupTopicsTests() []topicTest { bytesType, _ := abi.NewType("bytes5", "", nil) int8Type, _ := abi.NewType("int8", "", nil) int256Type, _ := abi.NewType("int256", "", nil) + tupleType, _ := abi.NewType("tuple(int256,int8)", "", nil) tests := []topicTest{ { @@ -145,6 +146,21 @@ func setupTopicsTests() []topicTest { }, wantErr: false, }, + { + name: "tuple(int256, int8)", + args: args{ + createObj: func() interface{} { return nil }, + resultObj: func() interface{} { return nil }, + resultMap: func() map[string]interface{} { return make(map[string]interface{}) }, + fields: abi.Arguments{abi.Argument{ + Name: "tupletype", + Type: tupleType, + Indexed: true, + }}, + topics: []common.Hash{}, + }, + wantErr: true, + }, } return tests