Skip to content

Commit

Permalink
Use multiprocessing for network validators signature check
Browse files Browse the repository at this point in the history
Signed-off-by: cyc60 <avsysoev60@gmail.com>
  • Loading branch information
cyc60 committed Nov 15, 2024
1 parent 8e23b93 commit 5c5fbff
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions src/validators/execution.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import struct
from multiprocessing import Pool
from typing import Sequence, Set

from eth_typing import BlockNumber, HexStr
Expand Down Expand Up @@ -51,20 +52,23 @@ def process_network_validator_events(events: list[EventData]) -> list[NetworkVal
Processes `ValidatorsRegistry` registration events
and returns the list of valid validators.
"""
result: list[NetworkValidator] = []
for event in events:
public_key = process_network_validator_event(event)
if not public_key:
continue

result.append(
NetworkValidator(public_key=public_key, block_number=BlockNumber(event['blockNumber']))
)

return result
with Pool(processes=settings.pool_size) as pool:
results = [
pool.apply_async(
process_network_validator_event,
[event, settings.network_config.GENESIS_FORK_VERSION],
)
for event in events
]
for result in results:
result.wait()
validators = [result.get() for result in results]
return [val for val in validators if val]


def process_network_validator_event(event: EventData) -> HexStr | None:
def process_network_validator_event(
event: EventData, fork_version: bytes
) -> NetworkValidator | None:
"""
Processes validator deposit event
and returns its public key if the deposit is valid.
Expand All @@ -73,12 +77,12 @@ def process_network_validator_event(event: EventData) -> HexStr | None:
withdrawal_creds = event['args']['withdrawal_credentials']
amount_gwei = struct.unpack('<Q', event['args']['amount'])[0]
signature = event['args']['signature']
fork_version = settings.network_config.GENESIS_FORK_VERSION
if is_valid_deposit_data_signature(
public_key, withdrawal_creds, signature, amount_gwei, fork_version
):
return Web3.to_hex(public_key)

return NetworkValidator(
public_key=Web3.to_hex(public_key), block_number=BlockNumber(event['blockNumber'])
)
return None


Expand All @@ -95,9 +99,11 @@ async def get_latest_network_validator_public_keys() -> Set[HexStr]:
)
new_public_keys: Set[HexStr] = set()
for event in new_events:
public_key = process_network_validator_event(event)
if public_key:
new_public_keys.add(public_key)
validator = process_network_validator_event(
event, settings.network_config.GENESIS_FORK_VERSION
)
if validator:
new_public_keys.add(validator.public_key)

return new_public_keys

Expand Down

0 comments on commit 5c5fbff

Please sign in to comment.