Skip to content

Commit

Permalink
Add test for event scanner chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
cyc60 committed Nov 18, 2024
1 parent 9e56318 commit 8741068
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ exclude_dirs = ["sw_utils/tests/"]

[tool.pylint."pre-commit-hook"]
disable=["C0103", "C0114", "C0115", "C0116", "R0801", "R0903", "W0703", "W1514", "W0511"]
ignore-paths=["sw_utils/tests/.*"]

[tool.pylint."BASIC"]
good-names = ["i", "el", "e", "w", "f", "w3"]
Expand Down
5 changes: 1 addition & 4 deletions sw_utils/event_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ def __init__(
self._contract_call = lambda from_block, to_block: getattr(
processor.contract.events, processor.contract_event
).get_logs(argument_filters=argument_filters, fromBlock=from_block, toBlock=to_block)
# todo: remove type ignore after move Contract wrapper to sw-utils
self.provider = self.processor.contract.contract.w3.provider # type: ignore
# Scan in chunks, commit between
self.chunk_size = chunk_size or self.max_scan_chunk_size // 2

Expand Down Expand Up @@ -99,8 +97,7 @@ async def _scan_chunk(
for i in range(retries):
to_block = min(last_block, BlockNumber(from_block + self.chunk_size))
try:
with self.provider.disable_retries():
return to_block, await self._contract_call(from_block, to_block)
return to_block, await self._contract_call(from_block, to_block)
except Exception as e:
if i < retries - 1:
# Decrease the `eth_getBlocks` range
Expand Down
41 changes: 41 additions & 0 deletions sw_utils/tests/test_event_scanner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import pytest
from eth_typing import BlockNumber
from web3.types import EventData

from sw_utils.event_scanner import EventProcessor, EventScanner


class MockedEventProcessor(EventProcessor):
@staticmethod
async def get_from_block() -> BlockNumber:
return BlockNumber(777)

@staticmethod
async def process_events(events: list[EventData], to_block: BlockNumber) -> None:
pass


async def fetch_events(a, b):
return []


async def fetch_events_broken(a, b):
raise ConnectionError


class TestExitSignatureCrud:
async def test_basic(self):
default_chunk_size = 500000
p = MockedEventProcessor()
s = EventScanner(processor=p)
s._contract_call = fetch_events
assert s.chunk_size == default_chunk_size

s._contract_call = fetch_events_broken
s.max_request_retries = 1
with pytest.raises(ConnectionError):
await s.process_new_events(888)
assert s.chunk_size == default_chunk_size / 2

await s.process_new_events(888)
assert s.chunk_size == default_chunk_size / 4

0 comments on commit 8741068

Please sign in to comment.