Skip to content

Commit

Permalink
Add filtering test with many deployed contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanjw committed Aug 13, 2018
1 parent 5f4c05c commit c326da3
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions tests/core/filtering/test_filters_against_many_blocks.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import pytest
from hypothesis import (
strategies as st,
given,
)
import random

from eth_utils import (
to_tuple,
)


@pytest.fixture
@to_tuple
def deployed_contract_addresses(web3, Emitter):
contract_addresses = []
for i in range(25):
tx_hash = Emitter.constructor().transact()
yield web3.eth.getTransactionReceipt(tx_hash)['contractAddress']


def pad_with_transactions(w3):
accounts = w3.eth.accounts
Expand Down Expand Up @@ -64,6 +78,7 @@ def test_event_filter_new_events(
assert len(event_filter.get_new_entries()) == expected_match_counter


@pytest.mark.xfail
@given(
target_block_count=st.integers(min_value=1, max_value=100),
seed=st.integers())
Expand Down Expand Up @@ -102,6 +117,7 @@ def test_transaction_filter_with_mining(
assert len(transaction_filter.get_new_entries()) == transaction_counter


@pytest.mark.xfail
@given(target_tx_count=st.integers(min_value=1, max_value=100))
def test_transaction_filter_without_mining(
web3,
Expand All @@ -118,3 +134,47 @@ def test_transaction_filter_without_mining(
transaction_counter += 1

assert len(transaction_filter.get_new_entries()) == transaction_counter


@given(seed=st.integers())
def test_event_filter_new_events_many_deployed_contracts(
web3,
emitter,
Emitter,
wait_for_transaction,
emitter_event_ids,
create_filter,
seed,
deployed_contract_addresses):

random.seed(seed)

matching_transact = emitter.functions.logNoArgs(
which=1).transact

def gen_non_matching_transact():
while True:
contract_address = deployed_contract_addresses[
random.randint(0, len(deployed_contract_addresses))]
yield web3.eth.contract(
address=contract_address, abi=Emitter.abi).functions.logNoArgs(which=1).transact

non_matching_transact = gen_non_matching_transact()

event_filter = emitter.events.LogNoArguments().createFilter(fromBlock='latest')

expected_match_counter = 0

tx_padder = pad_with_transactions(web3)
tx_padder.send(None)
while web3.eth.blockNumber < 50:
is_match = bool(random.randint(0, 1))
if is_match:
expected_match_counter += 1
tx_padder.send(matching_transact)
next(tx_padder)
continue
tx_padder.send(next(non_matching_transact))
next(tx_padder)

assert len(event_filter.get_new_entries()) == expected_match_counter

0 comments on commit c326da3

Please sign in to comment.