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

fix: recursion error when a bad URI was configured in node: #2372

Merged
merged 6 commits into from
Nov 4, 2024
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
9 changes: 5 additions & 4 deletions src/ape_ethereum/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,7 @@ def uri(self) -> str:
else:
raise TypeError(f"Not an URI: {uri}")

config = self.config.model_dump().get(self.network.ecosystem.name, None)
config: dict = self.config.get(self.network.ecosystem.name, None)
if config is None:
if rpc := self._get_random_rpc():
return rpc
Expand All @@ -1351,7 +1351,7 @@ def uri(self) -> str:
raise ProviderError(f"Please configure a URL for '{self.network_choice}'.")

# Use value from config file
network_config = config.get(self.network.name) or DEFAULT_SETTINGS
network_config: dict = (config or {}).get(self.network.name) or DEFAULT_SETTINGS

if "url" in network_config:
raise ConfigError("Unknown provider setting 'url'. Did you mean 'uri'?")
Expand All @@ -1370,10 +1370,11 @@ def uri(self) -> str:

settings_uri = network_config.get(key, DEFAULT_SETTINGS["uri"])
if _is_uri(settings_uri):
# Is true if HTTP, WS, or IPC.
return settings_uri

# Likely was an IPC Path (or websockets) and will connect that way.
return super().http_uri or ""
# Is not HTTP, WS, or IPC. Raise an error.
raise ConfigError(f"Invalid URI (not HTTP, WS, or IPC): {settings_uri}")

@property
def http_uri(self) -> Optional[str]:
Expand Down
19 changes: 19 additions & 0 deletions tests/functional/geth/test_provider.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from pathlib import Path
from typing import cast

Expand All @@ -16,6 +17,7 @@
from ape.exceptions import (
APINotImplementedError,
BlockNotFoundError,
ConfigError,
ContractLogicError,
NetworkMismatchError,
ProviderError,
Expand Down Expand Up @@ -127,6 +129,23 @@ def test_uri_non_dev_and_not_configured(mocker, ethereum):
assert actual == expected


def test_uri_invalid(geth_provider, project, ethereum):
settings = geth_provider.provider_settings
geth_provider.provider_settings = {}
value = "I AM NOT A URI OF ANY KIND!"
config = {"node": {"ethereum": {"local": {"uri": value}}}}

try:
with project.temp_config(**config):
# Assert we use the config value.
expected = rf"Invalid URI \(not HTTP, WS, or IPC\): {re.escape(value)}"
with pytest.raises(ConfigError, match=expected):
_ = geth_provider.uri

finally:
geth_provider.provider_settings = settings


@geth_process_test
def test_repr_connected(geth_provider):
actual = repr(geth_provider)
Expand Down
Loading