Skip to content

Commit a878651

Browse files
committed
add initial fork cli
1 parent 39b669b commit a878651

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

crates/anvil-polkadot/src/cmd.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use anvil_server::ServerConfig;
88
use clap::Parser;
99
use foundry_common::shell;
1010
use foundry_config::Chain;
11+
use polkadot_sdk::sp_core::H256;
1112
use rand_08::{SeedableRng, rngs::StdRng};
1213
use std::{net::IpAddr, path::PathBuf, time::Duration};
1314

@@ -134,7 +135,11 @@ impl NodeArgs {
134135
.with_code_size_limit(self.evm.code_size_limit)
135136
.disable_code_size_limit(self.evm.disable_code_size_limit)
136137
.with_disable_default_create2_deployer(self.evm.disable_default_create2_deployer)
137-
.with_memory_limit(self.evm.memory_limit);
138+
.with_memory_limit(self.evm.memory_limit)
139+
.with_fork_url(self.fork.fork_url)
140+
.with_fork_block_hash(self.fork.fork_block_hash)
141+
.with_fork_delay(self.fork.fork_delay)
142+
.with_fork_retries(self.fork.fork_retries);
138143

139144
let substrate_node_config = SubstrateNodeConfig::new(&anvil_config);
140145

@@ -245,6 +250,26 @@ pub struct AnvilEvmArgs {
245250
pub memory_limit: Option<u64>,
246251
}
247252

253+
#[derive(Clone, Debug, Parser)]
254+
#[command(next_help_heading = "Fork options")]
255+
pub struct ForkArgs {
256+
/// Fetch state over a remote endpoint instead of starting from an empty state.
257+
#[arg(long = "fork-url", short = 'f', value_name = "URL")]
258+
pub fork_url: Option<String>,
259+
260+
/// Fetch state from a specific block hash over a remote endpoint.
261+
#[arg(long, value_name = "BLOCK")]
262+
pub fork_block_hash: Option<H256>,
263+
264+
/// Delay between RPC requests in milliseconds to avoid rate limiting.
265+
#[arg(long, default_value = "0", value_name = "MS")]
266+
pub fork_delay: u32,
267+
268+
/// Maximum number of retries per RPC request.
269+
#[arg(long, default_value = "3", value_name = "NUM")]
270+
pub fork_retries: u32,
271+
}
272+
248273
/// Clap's value parser for genesis. Loads a genesis.json file.
249274
fn read_genesis_file(path: &str) -> Result<Genesis, String> {
250275
foundry_common::fs::read_json_file(path.as_ref()).map_err(|err| err.to_string())

crates/anvil-polkadot/src/config.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use polkadot_sdk::{
1818
RPC_DEFAULT_MAX_SUBS_PER_CONN, RPC_DEFAULT_MESSAGE_CAPACITY_PER_CONN,
1919
},
2020
sc_service,
21+
sp_core::H256,
2122
};
2223
use rand_08::thread_rng;
2324
use serde_json::{Value, json};
@@ -328,6 +329,14 @@ pub struct AnvilNodeConfig {
328329
pub memory_limit: Option<u64>,
329330
/// Do not print log messages.
330331
pub silent: bool,
332+
/// Fetch state over a remote endpoint instead of starting from an empty state.
333+
pub fork_url: Option<String>,
334+
/// Fetch state from a specific block hash over a remote endpoint.
335+
pub fork_block_hash: Option<H256>,
336+
/// Delay between RPC requests in milliseconds when forking.
337+
pub fork_delay: u32,
338+
/// Maximum number of retries per RPC request when forking.
339+
pub fork_retries: u32,
331340
}
332341

333342
impl AnvilNodeConfig {
@@ -547,6 +556,10 @@ impl Default for AnvilNodeConfig {
547556
disable_default_create2_deployer: false,
548557
memory_limit: None,
549558
silent: false,
559+
fork_url: None,
560+
fork_block_hash: None,
561+
fork_delay: 0,
562+
fork_retries: 3,
550563
}
551564
}
552565
}
@@ -850,6 +863,34 @@ impl AnvilNodeConfig {
850863
self.silent = silent;
851864
self
852865
}
866+
867+
/// Sets the fork url
868+
#[must_use]
869+
pub fn with_fork_url(mut self, fork_url: Option<String>) -> Self {
870+
self.fork_url = fork_url;
871+
self
872+
}
873+
874+
/// Sets the fork block
875+
#[must_use]
876+
pub fn with_fork_block_hash(mut self, fork_block_hash: Option<H256>) -> Self {
877+
self.fork_block_hash = fork_block_hash;
878+
self
879+
}
880+
881+
/// Sets the fork delay between RPC requests
882+
#[must_use]
883+
pub fn with_fork_delay(mut self, fork_delay: u32) -> Self {
884+
self.fork_delay = fork_delay;
885+
self
886+
}
887+
888+
/// Sets the fork max retries per RPC request
889+
#[must_use]
890+
pub fn with_fork_retries(mut self, fork_retries: u32) -> Self {
891+
self.fork_retries = fork_retries;
892+
self
893+
}
853894
}
854895

855896
/// Can create dev accounts

0 commit comments

Comments
 (0)