Skip to content

Commit

Permalink
Add block_identifier to config to support forking at a specific block (
Browse files Browse the repository at this point in the history
…#190)

* Add flag to set block_identifier in config when forking

* Update docs

---------

Co-authored-by: Patrick Collins <[email protected]>
  • Loading branch information
benber86 and PatrickAlphaC authored Jan 29, 2025
1 parent c18c2ac commit cb2445b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
12 changes: 11 additions & 1 deletion docs/source/core_concepts/networks/forked_networks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,17 @@ You can also take any non-forked network and run a forked test just by adding th
chain_id = 11155111
fork = false
You can fork at a specific block by specifying a ``block_identifier``, which can be either an integer or one of the preset options: ``safe`` (default), ``latest``, ``earliest``, ``pending``, ``finalized``

.. code-block:: toml
[networks.sepolia]
url = "https://ethereum-sepolia-rpc.publicnode.com"
chain_id = 11155111
is_fork = false
block_identifier = 7582167
Forked network defaults
========================

Expand Down
6 changes: 4 additions & 2 deletions moccasin/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class Network:
url: str | None = None
chain_id: int | None = None
is_fork: bool = False
block_identifier: int | str = "safe"
is_zksync: bool = False
default_account_name: str | None = None
unsafe_password_file: Path | None = None
Expand All @@ -103,9 +104,9 @@ def _set_boa_env(self) -> _AnyEnv:
# 1. Check for forking, and set (You cannot fork from a NetworkEnv, only a "new" Env!)
if self.is_fork:
if self.is_zksync:
set_zksync_fork(url=self.url)
set_zksync_fork(url=self.url, block_identifier=self.block_identifier)
else:
boa.fork(self.url)
boa.fork(self.url, block_identifier=self.block_identifier)

# 2. Non-forked local networks
elif self.name == PYEVM:
Expand Down Expand Up @@ -850,6 +851,7 @@ def __init__(self, toml_data: dict, project_root: Path):
network = Network(
name=network_name,
is_fork=network_data.get("fork", False),
block_identifier=network_data.get("block_identifier", "safe"),
url=network_data.get("url", None),
is_zksync=network_data.get("is_zksync", False),
chain_id=network_data.get("chain_id", None),
Expand Down
1 change: 1 addition & 0 deletions moccasin/constants/vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
RESTRICTED_VALUES_FOR_LOCAL_NETWORK = [
"url",
"chain_id",
"block_identifier",
"fork",
"explorer_uri",
"exploer_api_key",
Expand Down
1 change: 1 addition & 0 deletions tests/data/complex_project/moccasin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ save_to_db = false
url = "${MAINNET_RPC_URL}"
fork = true
explorer_uri = "https://api.etherscan.io/api"
block_identifier = 21000000

[networks.mainnet_fork.contracts]
usdc = { address = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", abi_from_explorer = true }
Expand Down

0 comments on commit cb2445b

Please sign in to comment.