Skip to content

Commit

Permalink
fix: fix in ape test and subprocess
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Oct 13, 2023
1 parent 6389483 commit b51c7b1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
5 changes: 5 additions & 0 deletions src/ape/api/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1744,6 +1744,11 @@ def _stdout_logger(self) -> Logger:
def _stderr_logger(self) -> Logger:
return self._get_process_output_logger("stderr", self.stderr_logs_path)

@property
def connection_id(self) -> Optional[str]:
cmd_id = ",".join(self.build_command())
return f"{self.network_choice}:-{cmd_id}"

def _get_process_output_logger(self, name: str, path: Path):
logger = getLogger(f"{self.name}_{name}_subprocessProviderLogger")
path.parent.mkdir(parents=True, exist_ok=True)
Expand Down
24 changes: 10 additions & 14 deletions src/ape_geth/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from itertools import tee
from pathlib import Path
from subprocess import DEVNULL, PIPE, Popen
from typing import Any, Dict, Iterator, List, Optional, Tuple, Union, cast
from typing import Any, Dict, Iterator, List, Optional, Tuple, Union

import ijson # type: ignore
import requests
Expand Down Expand Up @@ -267,18 +267,14 @@ def uri(self) -> str:
# Use adhoc, scripted value
return self.provider_settings["uri"]

config = self.config.dict().get(self.network.ecosystem.name, None)
config = self.settings.dict().get(self.network.ecosystem.name, None)
if config is None:
return DEFAULT_SETTINGS["uri"]

# Use value from config file
network_config = config.get(self.network.name) or DEFAULT_SETTINGS
return network_config.get("uri", DEFAULT_SETTINGS["uri"])

@property
def geth_config(self) -> GethConfig:
return cast(GethConfig, self.config_manager.get_config("geth"))

@property
def _clean_uri(self) -> str:
url = URL(self.uri).with_user(None).with_password(None)
Expand All @@ -288,12 +284,12 @@ def _clean_uri(self) -> str:

@property
def ipc_path(self) -> Path:
return self.geth_config.ipc_path or self.data_dir / "geth.ipc"
return self.settings.ipc_path or self.data_dir / "geth.ipc"

@property
def data_dir(self) -> Path:
if self.geth_config.data_dir:
return self.geth_config.data_dir.expanduser()
if self.settings.data_dir:
return self.settings.data_dir.expanduser()

return _get_default_data_dir()

Expand Down Expand Up @@ -481,12 +477,12 @@ def process_name(self) -> str:

@property
def chain_id(self) -> int:
return self.geth_config.ethereum.local.get("chain_id", DEFAULT_TEST_CHAIN_ID)
return self.settings.ethereum.local.get("chain_id", DEFAULT_TEST_CHAIN_ID)

@property
def data_dir(self) -> Path:
# Overridden from BaseGeth class for placing debug logs in ape data folder.
return self.geth_config.data_dir or self.data_folder / self.name
return self.settings.data_dir or self.data_folder / self.name

def __repr__(self):
try:
Expand All @@ -505,16 +501,16 @@ def start(self, timeout: int = 20):
test_config = self.config_manager.get_config("test").dict()

# Allow configuring a custom executable besides your $PATH geth.
if self.geth_config.executable is not None:
test_config["executable"] = self.geth_config.executable
if self.settings.executable is not None:
test_config["executable"] = self.settings.executable

test_config["ipc_path"] = self.ipc_path
test_config["auto_disconnect"] = self._test_runner is None or test_config.get(
"disconnect_providers_after", True
)

# Include extra accounts to allocated funds to at genesis.
extra_accounts = self.geth_config.ethereum.local.get("extra_funded_accounts", [])
extra_accounts = self.settings.ethereum.local.get("extra_funded_accounts", [])
extra_accounts.extend(self.provider_settings.get("extra_funded_accounts", []))
extra_accounts = list(set([HexBytes(a).hex().lower() for a in extra_accounts]))
test_config["extra_funded_accounts"] = extra_accounts
Expand Down
15 changes: 11 additions & 4 deletions src/ape_test/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ def evm_backend(self) -> PyEVMBackend:
return self._evm_backend

def connect(self):
chain_id = self.provider_settings.get("chain_id", self.config.provider.chain_id)
chain_id = self.settings.chain_id
if self._web3 is not None:
connected_chain_id = self.chain_id
if connected_chain_id == chain_id:
# Is already connected and settings have not changed.
return

self._evm_backend = PyEVMBackend.from_mnemonic(
mnemonic=self.config["mnemonic"],
num_accounts=self.config["number_of_accounts"],
mnemonic=self.config.mnemonic,
num_accounts=self.config.number_of_accounts,
)
endpoints = {**API_ENDPOINTS}
endpoints["eth"]["chainId"] = static_return(chain_id)
Expand Down Expand Up @@ -108,6 +108,13 @@ def estimate_gas_cost(self, txn: TransactionAPI, **kwargs) -> int:
message, base_err=ape_err, txn=txn, source_traceback=ape_err.source_traceback
) from ape_err

@property
def settings(self) -> EthTesterProviderConfig:
self.config.provider = EthTesterProviderConfig.parse_obj(
{**self.config.provider.dict(), **self.provider_settings}
)
return self.config.provider

@property
def chain_id(self) -> int:
if self.cached_chain_id:
Expand All @@ -116,7 +123,7 @@ def chain_id(self) -> int:
try:
result = self._make_request("eth_chainId", [])
except ProviderNotConnectedError:
result = self.provider_settings.get("chain_id", self.config.provider.chain_id)
result = self.settings.chain_id

self.cached_chain_id = result
return result
Expand Down

0 comments on commit b51c7b1

Please sign in to comment.