-
Notifications
You must be signed in to change notification settings - Fork 1.8k
node independent filter middleware #732
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2a8ace6
Add locally managed filter middleware
dylanjw 11c3bc0
Run filtering tests with local_filter_middleware
dylanjw 93ccd48
Pass through methods when filter not created by middleware
dylanjw d62f7c3
Increase block request range
dylanjw cd581d6
Wait for matching transactions
dylanjw 70a3600
Fix bug in block_ranges
dylanjw 6fa2f96
Use w3 var name for Web3 instance
dylanjw 77817c1
Handle case where blocks havent caught up with filter params
dylanjw b0677e7
Test filters with bounded block range
dylanjw 03452cc
Make review request changes
dylanjw ce0d947
Add local_filter_middleware to docs
dylanjw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
|
||
|
||
def test_filtering_sequential_blocks_with_bounded_range( | ||
web3, | ||
emitter, | ||
Emitter, | ||
wait_for_transaction): | ||
builder = emitter.events.LogNoArguments.build_filter() | ||
builder.fromBlock = "latest" | ||
|
||
initial_block_number = web3.eth.blockNumber | ||
|
||
builder.toBlock = initial_block_number + 100 | ||
filter_ = builder.deploy(web3) | ||
for i in range(100): | ||
emitter.functions.logNoArgs(which=1).transact() | ||
assert web3.eth.blockNumber == initial_block_number + 100 | ||
assert len(filter_.get_new_entries()) == 100 | ||
|
||
|
||
def test_filtering_starting_block_range( | ||
web3, | ||
emitter, | ||
Emitter, | ||
wait_for_transaction): | ||
for i in range(10): | ||
emitter.functions.logNoArgs(which=1).transact() | ||
builder = emitter.events.LogNoArguments.build_filter() | ||
filter_ = builder.deploy(web3) | ||
initial_block_number = web3.eth.blockNumber | ||
for i in range(10): | ||
emitter.functions.logNoArgs(which=1).transact() | ||
assert web3.eth.blockNumber == initial_block_number + 10 | ||
assert len(filter_.get_new_entries()) == 10 | ||
|
||
|
||
def test_requesting_results_with_no_new_blocks(web3, emitter): | ||
builder = emitter.events.LogNoArguments.build_filter() | ||
filter_ = builder.deploy(web3) | ||
assert len(filter_.get_new_entries()) == 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import pytest | ||
|
||
from web3 import Web3 | ||
from web3.middleware import ( | ||
construct_result_generator_middleware, | ||
local_filter_middleware, | ||
) | ||
from web3.middleware.filter import ( | ||
block_ranges, | ||
iter_latest_block_ranges, | ||
) | ||
from web3.providers.base import ( | ||
BaseProvider, | ||
) | ||
|
||
|
||
class DummyProvider(BaseProvider): | ||
def make_request(self, method, params): | ||
raise NotImplementedError("Cannot make request for {0}:{1}".format( | ||
method, | ||
params, | ||
)) | ||
|
||
|
||
@pytest.fixture(scope='function') | ||
def iter_block_number(start=0): | ||
def iterator(): | ||
block_number = start | ||
while True: | ||
sent_value = (yield block_number) | ||
if sent_value is not None: | ||
block_number = sent_value | ||
block_number = iterator() | ||
next(block_number) | ||
return block_number | ||
|
||
|
||
@pytest.fixture(scope='function') | ||
def result_generator_middleware(iter_block_number): | ||
return construct_result_generator_middleware({ | ||
'eth_getLogs': lambda *_: ["middleware"], | ||
'eth_getBlockByNumber': lambda *_: type('block', (object,), {'hash': 'middleware'}), | ||
'net_version': lambda *_: 1, | ||
'eth_blockNumber': lambda *_: next(iter_block_number), | ||
}) | ||
|
||
|
||
@pytest.fixture(scope='function') | ||
def w3_base(): | ||
return Web3(providers=[DummyProvider()], middlewares=[]) | ||
|
||
|
||
@pytest.fixture(scope='function') | ||
def w3(w3_base, result_generator_middleware): | ||
w3_base.middleware_stack.add(result_generator_middleware) | ||
w3_base.middleware_stack.add(local_filter_middleware) | ||
return w3_base | ||
|
||
|
||
@pytest.mark.parametrize("start, stop, expected", [ | ||
(2, 7, [ | ||
(2, 6), | ||
(7, 7) | ||
]), | ||
(0, 12, [ | ||
(0, 4), | ||
(5, 9), | ||
(10, 12) | ||
]), | ||
(0, 15, [ | ||
(0, 4), | ||
(5, 9), | ||
(10, 14), | ||
(15, 15) | ||
]), | ||
(0, 0, [ | ||
(0, 0), | ||
]), | ||
(1, 1, [ | ||
(1, 1), | ||
]), | ||
(5, 0, TypeError), | ||
]) | ||
def test_block_ranges(start, stop, expected): | ||
if isinstance(expected, type) and issubclass(expected, Exception): | ||
with pytest.raises(expected): | ||
block_ranges(start, stop) | ||
else: | ||
actual = tuple(block_ranges(start, stop)) | ||
assert len(actual) == len(expected) | ||
for actual, expected in zip(actual, expected): | ||
assert actual == expected | ||
|
||
|
||
@pytest.mark.parametrize("from_block,to_block,current_block,expected", [ | ||
(0, 10, [10], [ | ||
(0, 10), | ||
]), | ||
(0, 55, [0, 19, 55], [ | ||
(0, 0), | ||
(1, 19), | ||
(20, 55), | ||
]), | ||
]) | ||
def test_iter_latest_block_ranges( | ||
w3, | ||
iter_block_number, | ||
from_block, | ||
to_block, | ||
current_block, | ||
expected): | ||
latest_block_ranges = iter_latest_block_ranges(w3, from_block, to_block) | ||
for index, block in enumerate(current_block): | ||
iter_block_number.send(block) | ||
expected_tuple = expected[index] | ||
actual_tuple = next(latest_block_ranges) | ||
assert actual_tuple == expected_tuple | ||
|
||
|
||
def test_pending_block_filter_middleware(w3): | ||
with pytest.raises(NotImplementedError): | ||
w3.eth.filter('pending') | ||
|
||
|
||
def test_local_filter_middleware(w3, iter_block_number): | ||
block_filter = w3.eth.filter('latest') | ||
iter_block_number.send(1) | ||
|
||
log_filter = w3.eth.filter(filter_params={'fromBlock': 'latest'}) | ||
|
||
assert w3.eth.getFilterChanges(block_filter.filter_id) == ["middleware"] | ||
|
||
iter_block_number.send(2) | ||
results = w3.eth.getFilterChanges(log_filter.filter_id) | ||
assert results == ["middleware"] | ||
|
||
assert w3.eth.getFilterLogs(log_filter.filter_id) == ["middleware"] | ||
|
||
filter_ids = ( | ||
block_filter.filter_id, | ||
log_filter.filter_id | ||
) | ||
|
||
# Test that all ids are str types | ||
assert all(isinstance(_filter_id, (str,)) for _filter_id in filter_ids) | ||
|
||
# Test that all ids are unique | ||
assert len(filter_ids) == len(set(filter_ids)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not highly opposed to this but
"function"
is the defaultscope
forpytest.fixture