Skip to content
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

address local_filter_middleware bug #1632

Merged
merged 1 commit into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/1514.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix local_filter_middleware new entries bug
12 changes: 12 additions & 0 deletions tests/core/middleware/test_filter_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ def test_block_ranges(start, stop, expected):
(1, 19),
(20, 55),
]),
(0, None, [10], [
(0, 10),
]),
(0, 10, [12], [
(None, None),
]),
(12, 10, [12], [
(None, None),
]),
(12, 10, [None], [
(None, None),
]),
])
def test_iter_latest_block_ranges(
w3,
Expand Down
36 changes: 15 additions & 21 deletions web3/middleware/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,18 @@ def iter_latest_block(
"""Returns a generator that dispenses the latest block, if
any new blocks have been mined since last iteration.

If there are no new blocks None is returned.
If there are no new blocks or the latest block is greater than
the ``to_block`` None is returned.

If ``to_block`` is defined, ``StopIteration`` is raised
after to_block is reached.

>>> mined_blocks = dispense_mined_blocks(w3, 0, 10)
>>> new_blocks = iter_latest_block(w3, 0, 10)
>>> next(new_blocks) # Latest block = 0
0
>>> next(new_blocks) # No new blocks
>>> next(new_blocks) # Latest block = 1
1
>>> next(new_blocks) # Latest block = 10
10
>>> next(new_blocks)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
>>>
>>> next(new_blocks) # latest block > to block
"""
_last = None

Expand All @@ -151,7 +145,7 @@ def iter_latest_block(
latest_block = w3.eth.blockNumber
# type ignored b/c is_bounded_range prevents unsupported comparison
if is_bounded_range and latest_block > to_block: # type: ignore
return
yield None
# No new blocks since last iteration.
if _last is not None and _last == latest_block:
yield None
Expand Down Expand Up @@ -256,16 +250,16 @@ def _get_filter_changes(self) -> Iterator[List[LogReceipt]]:
for start, stop in iter_latest_block_ranges(self.w3, self.from_block, self.to_block):
if None in (start, stop):
yield []

yield list(
concat(
get_logs_multipart(
self.w3,
start,
stop,
self.address,
self.topics,
max_blocks=MAX_BLOCK_REQUEST)))
else:
yield list(
concat(
get_logs_multipart(
self.w3,
start,
stop,
self.address,
self.topics,
max_blocks=MAX_BLOCK_REQUEST)))

def get_logs(self) -> List[LogReceipt]:
return list(
Expand Down