diff --git a/src/ape/api/networks.py b/src/ape/api/networks.py index e6f7dd2611..f83510086f 100644 --- a/src/ape/api/networks.py +++ b/src/ape/api/networks.py @@ -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() diff --git a/src/ape/managers/networks.py b/src/ape/managers/networks.py index e3d1f81a2a..773e947879 100644 --- a/src/ape/managers/networks.py +++ b/src/ape/managers/networks.py @@ -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 diff --git a/tests/functional/test_network_manager.py b/tests/functional/test_network_manager.py index e302b37142..c90d01ad29 100644 --- a/tests/functional/test_network_manager.py +++ b/tests/functional/test_network_manager.py @@ -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):