Skip to content

Commit

Permalink
feat: allow funding extra accounts in local geth provider [APE-1257] (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Aug 3, 2023
1 parent 26e9247 commit af4b39a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
19 changes: 18 additions & 1 deletion src/ape_geth/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def __init__(
initial_balance: Union[str, int] = to_wei(10000, "ether"),
executable: Optional[str] = None,
auto_disconnect: bool = True,
extra_funded_accounts: Optional[List[str]] = None,
):
executable = executable or "geth"
if not shutil.which(executable):
Expand Down Expand Up @@ -108,6 +109,10 @@ def __init__(
sealer = ensure_account_exists(**geth_kwargs).decode().replace("0x", "")
geth_kwargs["miner_etherbase"] = sealer
accounts = generate_dev_accounts(mnemonic, number_of_accounts=number_of_accounts)
addresses = [a.address for a in accounts]
addresses.extend(extra_funded_accounts or [])
bal_dict = {"balance": str(initial_balance)}
alloc = {a: bal_dict for a in addresses}
genesis_data: Dict = {
"overwrite": True,
"coinbase": "0x0000000000000000000000000000000000000000",
Expand All @@ -130,7 +135,7 @@ def __init__(
"parisBlock": 0,
"clique": {"period": 0, "epoch": 30000},
},
"alloc": {a.address: {"balance": str(initial_balance)} for a in accounts},
"alloc": alloc,
}

initialize_chain(genesis_data, **geth_kwargs)
Expand All @@ -147,6 +152,10 @@ def from_uri(cls, uri: str, data_folder: Path, **kwargs):
port = parsed_uri.port if parsed_uri.port is not None else DEFAULT_PORT
mnemonic = kwargs.get("mnemonic", DEFAULT_TEST_MNEMONIC)
number_of_accounts = kwargs.get("number_of_accounts", DEFAULT_NUMBER_OF_TEST_ACCOUNTS)
extra_accounts = [
HexBytes(a).hex().lower() for a in kwargs.get("extra_funded_accounts", [])
]

return cls(
data_folder,
hostname=parsed_uri.host,
Expand All @@ -155,6 +164,7 @@ def from_uri(cls, uri: str, data_folder: Path, **kwargs):
number_of_accounts=number_of_accounts,
executable=kwargs.get("executable"),
auto_disconnect=kwargs.get("auto_disconnect", True),
extra_funded_accounts=extra_accounts,
)

def connect(self, timeout: int = 60):
Expand Down Expand Up @@ -461,6 +471,13 @@ def start(self, timeout: int = 20):
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.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

process = GethDevProcess.from_uri(self.uri, self.data_dir, **test_config)
process.connect(timeout=timeout)
if not self.web3.is_connected():
Expand Down
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,9 @@ def networks_connected_to_tester(eth_tester_provider):
@pytest.fixture
def geth_provider(networks):
if not networks.active_provider or networks.provider.name != "geth":
test_acct_100 = "0x63c7f11162dBFC374DC6f5C0B3Aa26C618846a85"
with networks.ethereum.local.use_provider(
"geth", provider_settings={"uri": GETH_URI}
"geth", provider_settings={"uri": GETH_URI, "extra_funded_accounts": [test_acct_100]}
) as provider:
yield provider
else:
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/cli/projects/geth/ape-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ geth:
uri: http://localhost:5000
local:
uri: http://127.0.0.1:5550 # NOTE: Has to be same as tests/conftest.py::GETH_URI
extra_funded_accounts:
- 0x63c7f11162dBFC374DC6f5C0B3Aa26C618846a85 # Test account 100.

test:
# `false` because running pytest within pytest.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ def test_provider(project, networks):
assert networks.provider.is_connected


def test_extra_account(chain):
"""
Show we can fund accounts from the config option.
"""
addr = "0x63c7f11162dBFC374DC6f5C0B3Aa26C618846a85"
actual = chain.provider.get_balance(addr)
assert actual > 0


def test_contract_interaction(owner, contract):
"""
Traditional ape-test style test.
Expand Down
10 changes: 6 additions & 4 deletions tests/integration/cli/test_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
from pathlib import Path
from typing import Optional

import pytest

Expand Down Expand Up @@ -88,13 +89,14 @@ def setup(project_name: str):
pytester.makepyfile(**test_files)

# Make other files
def _make_all_files(base: Path):
def _make_all_files(base: Path, prefix: Optional[Path] = None):
for file in base.iterdir():
if file.is_dir() and not file.name == "tests":
_make_all_files(file)
_make_all_files(file, prefix=Path(file.name))
elif file.is_file():
name = {file.as_posix(): file.read_text().splitlines()}
pytester.makefile(file.suffix, **name)
name = (prefix / file.name).as_posix() if prefix else file.name
src = {name: file.read_text().splitlines()}
pytester.makefile(file.suffix, **src)

_make_all_files(project_path)

Expand Down

0 comments on commit af4b39a

Please sign in to comment.