forked from blockchain-etl/ethereum-etl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from p2p-org/ps/rr/wss_event_loop___kafka_topic…
…_mapping WSS event loop + Kafka topic mapping
- Loading branch information
Showing
5 changed files
with
135 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ | |
from blockchainetl.streaming.streaming_utils import configure_signals, configure_logging | ||
from ethereumetl.enumeration.entity_type import EntityType | ||
|
||
from ethereumetl.misc.wss_block_listener import WssBlockListener | ||
from ethereumetl.providers.auto import get_provider_from_uri | ||
from ethereumetl.streaming.item_exporter_creator import create_item_exporters | ||
from ethereumetl.thread_local_proxy import ThreadLocalProxy | ||
|
@@ -37,6 +38,9 @@ | |
@click.option('-p', '--provider-uri', default='https://mainnet.infura.io', show_default=True, type=str, | ||
help='The URI of the web3 provider e.g. ' | ||
'file://$HOME/Library/Ethereum/geth.ipc or https://mainnet.infura.io') | ||
@click.option('-p', '--provider-uri-wss', default='', show_default=False, type=str, | ||
help='The URI of the web3 provider e.g. ' | ||
'file://$HOME/Library/Ethereum/geth.ipc or wss://mainnet.infura.io') | ||
@click.option('-o', '--output', type=str, | ||
help='Either Google PubSub topic path e.g. projects/your-project/topics/crypto_ethereum; ' | ||
'or Postgres connection url e.g. postgresql+pg8000://postgres:[email protected]:5432/ethereum; ' | ||
|
@@ -53,7 +57,7 @@ | |
@click.option('-w', '--max-workers', default=5, show_default=True, type=int, help='The number of workers') | ||
@click.option('--log-file', default=None, show_default=True, type=str, help='Log file') | ||
@click.option('--pid-file', default=None, show_default=True, type=str, help='pid file') | ||
def stream(last_synced_block_file, lag, provider_uri, output, start_block, entity_types, | ||
def stream(last_synced_block_file, lag, provider_uri, provider_uri_wss, output, start_block, entity_types, | ||
period_seconds=10, batch_size=2, block_batch_size=10, max_workers=5, log_file=None, pid_file=None): | ||
"""Streams all data types to console or Google Pub/Sub.""" | ||
configure_logging(log_file) | ||
|
@@ -65,6 +69,7 @@ def stream(last_synced_block_file, lag, provider_uri, output, start_block, entit | |
|
||
# TODO: Implement fallback mechanism for provider uris instead of picking randomly | ||
provider_uri = pick_random_provider_uri(provider_uri) | ||
provider_uri_wss = pick_random_provider_uri(provider_uri_wss) | ||
logging.info('Using ' + provider_uri) | ||
|
||
streamer_adapter = EthStreamerAdapter( | ||
|
@@ -74,14 +79,20 @@ def stream(last_synced_block_file, lag, provider_uri, output, start_block, entit | |
max_workers=max_workers, | ||
entity_types=entity_types | ||
) | ||
|
||
block_listener = None | ||
if provider_uri_wss: | ||
block_listener = WssBlockListener(provider_uri_wss) | ||
|
||
streamer = Streamer( | ||
blockchain_streamer_adapter=streamer_adapter, | ||
last_synced_block_file=last_synced_block_file, | ||
last_synced_block_file=last_synced_block_file, | ||
lag=lag, | ||
start_block=start_block, | ||
period_seconds=period_seconds, | ||
block_batch_size=block_batch_size, | ||
pid_file=pid_file | ||
pid_file=pid_file, | ||
wss_listener=block_listener | ||
) | ||
streamer.stream() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import asyncio | ||
import logging | ||
from websockets import connect | ||
|
||
|
||
class WssBlockListener: | ||
""" | ||
Listens for new block events via a WebSocket connection. | ||
""" | ||
|
||
def __init__(self, wss_uri) -> None: | ||
""" | ||
Initializes the listener with the WebSocket URI. | ||
Args: | ||
wss_uri (str): The WebSocket URI to connect to. | ||
""" | ||
self.logger = logging.getLogger("CompositeItemExporter") | ||
self.wss_uri = wss_uri | ||
self.subscribed = False | ||
|
||
async def subscribe(self) -> None: | ||
""" | ||
Subscribes to new block events. | ||
""" | ||
self.new_block_lock = asyncio.Condition() | ||
self.subscribed = True | ||
async with connect(self.wss_uri) as ws: | ||
await ws.send( | ||
'{"id": 1, "method": "eth_subscribe", "params": ["newHeads"]}' | ||
) | ||
subscription_response = await ws.recv() | ||
self.logger.debug("Subscription acquired %s", subscription_response) | ||
while self.subscribed: | ||
await asyncio.wait_for(ws.recv(), timeout=60) | ||
async with self.new_block_lock: | ||
self.new_block_lock.notify_all() | ||
|
||
async def wait_for_new_block(self) -> None: | ||
async with self.new_block_lock: | ||
await self.new_block_lock.wait() | ||
|
||
def close(self) -> None: | ||
""" | ||
Closes the listener and unsubscribes. | ||
""" | ||
self.subscribed = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters