From 28b1a2d2add0885dacd81dbe70862b3ff5088b39 Mon Sep 17 00:00:00 2001 From: Dylan Wilson Date: Mon, 30 Jul 2018 10:24:15 -0700 Subject: [PATCH] Disallow merging topic lists --- ...est_contract_createFilter_topic_merging.py | 27 ++++++------------- .../test_construct_event_filter_params.py | 5 ++-- web3/utils/events.py | 11 ++++---- web3/utils/filters.py | 13 +++++---- 4 files changed, 23 insertions(+), 33 deletions(-) diff --git a/tests/core/filtering/test_contract_createFilter_topic_merging.py b/tests/core/filtering/test_contract_createFilter_topic_merging.py index 76b177ada6..61ed78c173 100644 --- a/tests/core/filtering/test_contract_createFilter_topic_merging.py +++ b/tests/core/filtering/test_contract_createFilter_topic_merging.py @@ -1,3 +1,6 @@ +import pytest + + def test_merged_topic_list_event( web3, emitter, @@ -9,22 +12,8 @@ def test_merged_topic_list_event( '0x0000000000000000000000000000000000000000000000000000000000000457', # 1111 '0x0000000000000000000000000000000000000000000000000000000000000457', # 1111 ] - log_filter = emitter.events.LogTripleWithIndex().createFilter( - fromBlock="latest", - topics=manual_topics, - argument_filters={'arg0': 2222, 'arg1': 2222, 'arg2': 2222}) - event_id = getattr(emitter_event_ids, 'LogTripleWithIndex') - txn_hashes = [ - emitter.functions.logTriple(event_id, 1111, 1111, 1111).transact(), - emitter.functions.logTriple(event_id, 1111, 1111, 2222).transact(), - emitter.functions.logTriple(event_id, 1111, 2222, 1111).transact(), - emitter.functions.logTriple(event_id, 2222, 1111, 1111).transact(), - emitter.functions.logTriple(event_id, 2222, 1111, 2222).transact(), - emitter.functions.logTriple(event_id, 1111, 2222, 2222).transact(), - emitter.functions.logTriple(event_id, 2222, 2222, 1111).transact(), - emitter.functions.logTriple(event_id, 2222, 2222, 2222).transact() - ] - while True: - if all(wait_for_transaction(web3, txn_hash) for txn_hash in txn_hashes): - break - assert len(log_filter.get_all_entries()) == 2 + with pytest.raises(TypeError): + emitter.events.LogTripleWithIndex().createFilter( + fromBlock="latest", + topics=manual_topics, + argument_filters={'arg0': 2222, 'arg1': 2222, 'arg2': 2222}) diff --git a/tests/core/utilities/test_construct_event_filter_params.py b/tests/core/utilities/test_construct_event_filter_params.py index 243025c8b9..244cd4750b 100644 --- a/tests/core/utilities/test_construct_event_filter_params.py +++ b/tests/core/utilities/test_construct_event_filter_params.py @@ -21,10 +21,9 @@ (EVENT_1_ABI, {}, { "topics": ['0xb470a829ed7792f06947f0ca3730a570cb378329ddcf09f2b4efabd6326f51f6'], }), - (EVENT_1_ABI, {'topics': ['should-be-preserved']}, { + (EVENT_1_ABI, {'topics': ['should-overwrite-topics']}, { "topics": [ - ['should-be-preserved'], - ['0xb470a829ed7792f06947f0ca3730a570cb378329ddcf09f2b4efabd6326f51f6'], + 'should-overwrite-topics' ] }), (EVENT_1_ABI, {'contract_address': '0xd3CdA913deB6f67967B99D67aCDFa1712C293601'}, { diff --git a/web3/utils/events.py b/web3/utils/events.py index a5a1f87a65..4dff8a9c1d 100644 --- a/web3/utils/events.py +++ b/web3/utils/events.py @@ -28,6 +28,10 @@ from web3.utils.normalizers import ( BASE_RETURN_NORMALIZERS, ) +from web3.utils.toolz import ( + curry, + compose, +) from .abi import ( exclude_indexed_event_inputs, @@ -71,12 +75,7 @@ def construct_event_topic_set(event_abi, arguments=None): for arg, arg_options in zipped_abi_and_args ] - topics = [ - [event_topic] + list(permutation) - if any(value is not None for value in permutation) - else [event_topic] - for permutation in itertools.product(*encoded_args) - ] + topics = list(normalize_topic_list([event_topic] + encoded_args)) return topics diff --git a/web3/utils/filters.py b/web3/utils/filters.py index caccf65072..ba85d2484c 100644 --- a/web3/utils/filters.py +++ b/web3/utils/filters.py @@ -26,11 +26,14 @@ def construct_event_filter_params(event_abi, toBlock=None, address=None): filter_params = {} - - if topics is None: - topic_set = construct_event_topic_set(event_abi, argument_filters) - else: - topic_set = [topics] + construct_event_topic_set(event_abi, argument_filters) + topic_set = construct_event_topic_set(event_abi, argument_filters) + + if topics is not None: + if len(topic_set) > 1: + raise TypeError( + "Merging the topics argument with topics generated " + "from argument_filters is not supported.") + topic_set = topics if len(topic_set) == 1 and is_list_like(topic_set[0]): filter_params['topics'] = topic_set[0]