Skip to content

Commit

Permalink
address local_filter_middleware bug
Browse files Browse the repository at this point in the history
  • Loading branch information
wolovim committed Apr 28, 2020
1 parent eca392e commit e5c6cc5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
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
3 changes: 3 additions & 0 deletions web3/_utils/module_testing/eth_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
InvalidAddress,
TransactionNotFound,
)
from web3.middleware import (
local_filter_middleware,
)
from web3.types import ( # noqa: F401
BlockData,
FilterParams,
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

0 comments on commit e5c6cc5

Please sign in to comment.