Skip to content
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
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ CONSENSUS_ENDPOINT=https://hoodi-lighthouse

PUBLIC_KEYS_FILE=public_keys.csv

# Validators manager
VALIDATORS_MANAGER_KEY_FILE=validators-manager-key.json
VALIDATORS_MANAGER_PASSWORD_FILE=validators-manager-password.txt

# Maximum number of parallel event scan queries
# EVENT_LOGS_MAX_CONCURRENCY=4
29 changes: 22 additions & 7 deletions src/common/contracts.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import asyncio
import itertools
import json
import os
from functools import cached_property
Expand Down Expand Up @@ -50,15 +52,28 @@ async def _get_last_event(
) -> EventData | None:
blocks_range = self.events_blocks_range_interval

while to_block >= from_block:
events = await event.get_logs(
from_block=BlockNumber(max(to_block - blocks_range, from_block)),
to_block=to_block,
# Build all chunk ranges from newest to oldest
ranges: list[tuple[BlockNumber, BlockNumber]] = []
chunk_to = to_block
while chunk_to >= from_block:
chunk_from = BlockNumber(max(chunk_to - blocks_range + 1, from_block))
ranges.append((chunk_from, chunk_to))
chunk_to = BlockNumber(chunk_to - blocks_range)

# from_block and to_block are both inclusive
async def fetch_chunk(chunk_from: BlockNumber, chunk_to: BlockNumber) -> list[EventData]:
return await event.get_logs(
from_block=chunk_from,
to_block=chunk_to,
argument_filters=argument_filters,
)
if events:
return events[-1]
to_block = BlockNumber(to_block - blocks_range - 1)

# Process chunks in batches (newest-first), abort on first hit
for batch in itertools.batched(ranges, settings.event_logs_max_concurrency):
batch_results = await asyncio.gather(*[fetch_chunk(f, t) for f, t in batch])
for chunk_events in batch_results:
if chunk_events:
return chunk_events[-1]
return None


Expand Down
2 changes: 2 additions & 0 deletions src/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@

validator_lifetime: int = config('VALIDATOR_LIFETIME', default=3600, cast=int)

event_logs_max_concurrency: int = config('EVENT_LOGS_MAX_CONCURRENCY', default=4, cast=int)

validators_manager_key_file: str = config('VALIDATORS_MANAGER_KEY_FILE')
validators_manager_password_file: str = config('VALIDATORS_MANAGER_PASSWORD_FILE')

Expand Down
Loading