Skip to content

Commit

Permalink
fix: allow local node to connect without HTTP (#2201)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Aug 2, 2024
1 parent 5021474 commit 9e211ab
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/ape_ethereum/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -1382,10 +1382,9 @@ def _ots_api_level(self) -> Optional[int]:
def _set_web3(self):
# Clear cached version when connecting to another URI.
self._client_version = None
if uri := self.http_uri:
self._web3 = _create_web3(uri, ipc_path=self.ipc_path, ws_uri=self.ws_uri)
else:
raise ProviderError("Missing URI.")
self._web3 = _create_web3(
http_uri=self.http_uri, ipc_path=self.ipc_path, ws_uri=self.ws_uri
)

def _complete_connect(self):
client_version = self.client_version.lower()
Expand Down Expand Up @@ -1480,13 +1479,15 @@ def connect(self):
self._complete_connect()


def _create_web3(uri: str, ipc_path: Optional[Path] = None, ws_uri: Optional[str] = None):
def _create_web3(
http_uri: Optional[str] = None, ipc_path: Optional[Path] = None, ws_uri: Optional[str] = None
):
# NOTE: This list is ordered by try-attempt.
# Try ENV, then IPC, and then HTTP last.
providers: list = [load_provider_from_environment]
if ipc := ipc_path:
providers.append(lambda: IPCProvider(ipc_path=ipc))
if http := uri:
if http := http_uri:
providers.append(
lambda: HTTPProvider(endpoint_uri=http, request_kwargs={"timeout": 30 * 60})
)
Expand Down
8 changes: 8 additions & 0 deletions tests/functional/geth/test_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,14 @@ def test_connect_to_chain_that_started_poa(mock_web3, web3_factory, ethereum):
assert mock_web3.middleware_onion.inject.call_args[1] == {"layer": 0}


@geth_process_test
def test_connect_using_only_ipc_for_uri(project, networks, geth_provider):
ipc_path = geth_provider.ipc_path
with project.temp_config(node={"ethereum": {"local": {"uri": f"{ipc_path}"}}}):
with networks.ethereum.local.use_provider("node") as node:
assert node.uri == f"{ipc_path}"


@geth_process_test
@pytest.mark.parametrize("block_id", (0, "0", "0x0", HexStr("0x0")))
def test_get_block(geth_provider, block_id):
Expand Down

0 comments on commit 9e211ab

Please sign in to comment.