Skip to content

Commit

Permalink
feat: allow forking a forked network (#2349)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Dec 5, 2024
1 parent ae256d2 commit 5b3ad09
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/ape/api/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ def pop_provider(self):
current_id = self.provider_stack.pop()

# Disconnect the provider in same cases.
if self.disconnect_map[current_id]:
if self.disconnect_map.get(current_id):
if provider := self.network_manager.active_provider:
provider.disconnect()

Expand Down
12 changes: 10 additions & 2 deletions src/ape/managers/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,20 @@ def fork(
Returns:
:class:`~ape.api.networks.ProviderContextManager`
"""
network_name = self.network.name
is_fork_already = network_name.endswith("-fork")
forked_network_name = network_name if is_fork_already else f"{network_name}-fork"
try:
forked_network = self.ecosystem.get_network(f"{self.network.name}-fork")
forked_network = self.ecosystem.get_network(forked_network_name)
except NetworkNotFoundError as err:
raise NetworkError(f"Unable to fork network '{self.network.name}'.") from err
raise NetworkError(f"Unable to fork network '{network_name}'.") from err

provider_settings = provider_settings or {}
if is_fork_already and "host" not in provider_settings:
# Forking a fork- to ensure is using a different Port,
# use the "auto-port" feature.
provider_settings["host"] = "auto"

fork_settings = {}
if block_number is not None:
# Negative block_number means relative to HEAD
Expand Down
6 changes: 6 additions & 0 deletions tests/functional/test_network_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,12 @@ def test_fork(networks, mock_sepolia, mock_fork_provider):
with ctx as provider:
assert provider.name == "mock"
assert provider.network.name == "sepolia-fork"
# Fork the fork.
ctx2 = networks.fork()
with ctx2 as provider2:
assert provider2.partial_call[1]["provider_settings"]["host"] == "auto"
assert provider2.name == "mock"
assert provider2.network.name == "sepolia-fork"


def test_fork_specify_provider(networks, mock_sepolia, mock_fork_provider):
Expand Down

0 comments on commit 5b3ad09

Please sign in to comment.