Skip to content

Commit

Permalink
fix: recursion error when a bad URI was configured in node: (#2372)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Nov 4, 2024
1 parent 0a227e1 commit 6a8a737
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
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

0 comments on commit 6a8a737

Please sign in to comment.