Skip to content

Commit

Permalink
fix: issue where wouldn't use expected URI in .fork context (ApeWor…
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Feb 27, 2024
1 parent 410fdb5 commit d7e74d7
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repos:
- id: isort

- repo: https://github.com/psf/black
rev: 24.1.1
rev: 24.2.0
hooks:
- id: black
name: black
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"hypothesis-jsonschema==0.19.0", # JSON Schema fuzzer extension
],
"lint": [
"black>=24.1.1,<25", # Auto-formatter and linter
"black>=24.2.0,<25", # Auto-formatter and linter
"mypy>=1.8.0,<2", # Static type analyzer
"types-PyYAML", # Needed due to mypy typeshed
"types-requests", # Needed due to mypy typeshed
Expand Down
19 changes: 10 additions & 9 deletions src/ape/managers/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def fork(
raise NetworkError(f"Unable to fork network '{self.network.name}'.") from err

provider_settings = provider_settings or {}

fork_settings = {}
if block_number is not None:
# Negative block_number means relative to HEAD
if block_number < 0:
Expand All @@ -120,14 +120,15 @@ def fork(
raise NetworkError("Unable to fork past genesis block.")

# Ensure block_number is set in config for this network
_dict_overlay(
provider_settings,
{
"fork": {
self.ecosystem.name: {self.network.name: {"block_number": block_number}}
}
},
)
fork_settings["block_number"] = block_number

if uri := self.provider.connection_str:
fork_settings["upstream_provider"] = uri

_dict_overlay(
provider_settings,
{"fork": {self.ecosystem.name: {self.network.name: fork_settings}}},
)

shared_kwargs: dict = {"provider_settings": provider_settings, "disconnect_after": True}
return (
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ def mock_sepolia(ethereum, eth_tester_provider, vyper_contract_instance):
network.
"""
# Ensuring contract exists before hack.
# This allow the nework to be past genesis which is more realistic.
# This allow the network to be past genesis which is more realistic.
_ = vyper_contract_instance
eth_tester_provider.network.name = "sepolia"
yield eth_tester_provider.network
Expand Down
39 changes: 39 additions & 0 deletions tests/functional/geth/test_network_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import pytest

from ape.api.networks import LOCAL_NETWORK_NAME
from tests.conftest import geth_process_test


@pytest.fixture
def mock_geth_sepolia(ethereum, geth_provider, geth_contract):
"""
Temporarily tricks Ape into thinking the local network
is Sepolia so we can test features that require a live
network.
"""
# Ensuring contract exists before hack.
# This allow the network to be past genesis which is more realistic.
_ = geth_contract
geth_provider.network.name = "sepolia"
yield geth_provider.network
geth_provider.network.name = LOCAL_NETWORK_NAME


@geth_process_test
def test_fork_upstream_provider(networks, mock_geth_sepolia, geth_provider, mock_fork_provider):
uri = "http://example.com/node"
orig = geth_provider.provider_settings.get("uri")
geth_provider.provider_settings["uri"] = uri
try:
with networks.fork():
call = mock_fork_provider.partial_call

settings = call[1]["provider_settings"]["fork"]["ethereum"]["sepolia"]
actual = settings["upstream_provider"]
assert actual == uri
finally:
# Restore.
if orig:
geth_provider.provider_settings["uri"] = orig
else:
del geth_provider.provider_settings["uri"]
3 changes: 2 additions & 1 deletion tests/functional/test_network_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,8 @@ def test_fork_with_positive_block_number(networks, mock_sepolia, mock_fork_provi
with networks.fork(block_number=block_id):
call = mock_fork_provider.partial_call

actual = call[1]["provider_settings"]["fork"]["ethereum"]["sepolia"]["block_number"]
settings = call[1]["provider_settings"]["fork"]["ethereum"]["sepolia"]
actual = settings["block_number"]
assert actual == block_id


Expand Down

0 comments on commit d7e74d7

Please sign in to comment.