Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add info logger to display connection status and chain ID #2268

Closed
wants to merge 2 commits into from
Closed
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
23 changes: 21 additions & 2 deletions src/ape/api/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1054,7 +1054,21 @@ def disconnect(self):
self.stop()

def start(self, timeout: int = 20):
"""Start the process and wait for its RPC to be ready."""
"""
Start the process and wait for its RPC to be ready.

This method initializes the provider process and waits until it is
connected and ready. It logs messages based on the connection status
and the chain ID.

Args:
timeout (int): The time in seconds to wait for the RPC to be ready.
Defaults to 20 seconds.

Raises:
RPCTimeoutError: If the RPC does not become ready within the timeout
period.
"""
Ninjagod1251 marked this conversation as resolved.
Show resolved Hide resolved

if self.is_connected:
logger.info(f"Connecting to existing '{self.process_name}' process.")
Expand All @@ -1075,7 +1089,12 @@ def start(self, timeout: int = 20):
with RPCTimeoutError(self, seconds=timeout) as _timeout:
while True:
if self.is_connected:
break
chain_id = self.chain_id # Ensure this is how chain_id is accessed
if chain_id == 1337:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The chain ID is configurable and does not mean it is an Ethereum tester node. Any node can use this port!

logger.info(f"Connecting to Test chain with chain_id={chain_id}")
else:
logger.info(f"Connected to {self.process_name} network with chain_id={chain_id}")
break

time.sleep(0.1)
_timeout.check()
Expand Down
18 changes: 18 additions & 0 deletions tests/functional/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,3 +543,21 @@ def test_ipc_per_network(project, key):
# TODO: 0.9 investigate not using random if ipc set.

assert node.ipc_path == Path(ipc)

@mock.patch('ape_ethereum.provider.logger')
def test_start_logging_local_test_chain(mock_logger, ethereum):
"""Test logging when connecting to a local test chain."""

# Use the provider with custom settings
with ethereum.local.use_provider("test", provider_settings={"chain_id": 1337}) as provider:
# Ensure provider is disconnected initially
provider.disconnect()
assert not provider.is_connected
# Start the provider
provider.connect()

# Verify connection and chain_id
assert provider.is_connected
assert provider.chain_id == 1337
# Check logging output
mock_logger.info.assert_any_call("Connecting to Test chain with chain_id=1337")
Loading