Skip to content

Commit 6f54df4

Browse files
committed
add initial fork cli
1 parent a49c124 commit 6f54df4

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
@@ -21,6 +21,7 @@ use polkadot_sdk::{
2121
RPC_DEFAULT_MAX_SUBS_PER_CONN, RPC_DEFAULT_MESSAGE_CAPACITY_PER_CONN,
2222
},
2323
sc_service,
24+
sp_core::H256,
2425
};
2526
use rand_08::thread_rng;
2627
use serde_json::{Value, json};
@@ -331,6 +332,14 @@ pub struct AnvilNodeConfig {
331332
pub memory_limit: Option<u64>,
332333
/// Do not print log messages.
333334
pub silent: bool,
335+
/// Fetch state over a remote endpoint instead of starting from an empty state.
336+
pub fork_url: Option<String>,
337+
/// Fetch state from a specific block hash over a remote endpoint.
338+
pub fork_block_hash: Option<H256>,
339+
/// Delay between RPC requests in milliseconds when forking.
340+
pub fork_delay: u32,
341+
/// Maximum number of retries per RPC request when forking.
342+
pub fork_retries: u32,
334343
}
335344

336345
impl AnvilNodeConfig {
@@ -554,6 +563,10 @@ impl Default for AnvilNodeConfig {
554563
disable_default_create2_deployer: false,
555564
memory_limit: None,
556565
silent: false,
566+
fork_url: None,
567+
fork_block_hash: None,
568+
fork_delay: 0,
569+
fork_retries: 3,
557570
}
558571
}
559572
}
@@ -857,6 +870,34 @@ impl AnvilNodeConfig {
857870
self.silent = silent;
858871
self
859872
}
873+
874+
/// Sets the fork url
875+
#[must_use]
876+
pub fn with_fork_url(mut self, fork_url: Option<String>) -> Self {
877+
self.fork_url = fork_url;
878+
self
879+
}
880+
881+
/// Sets the fork block
882+
#[must_use]
883+
pub fn with_fork_block_hash(mut self, fork_block_hash: Option<H256>) -> Self {
884+
self.fork_block_hash = fork_block_hash;
885+
self
886+
}
887+
888+
/// Sets the fork delay between RPC requests
889+
#[must_use]
890+
pub fn with_fork_delay(mut self, fork_delay: u32) -> Self {
891+
self.fork_delay = fork_delay;
892+
self
893+
}
894+
895+
/// Sets the fork max retries per RPC request
896+
#[must_use]
897+
pub fn with_fork_retries(mut self, fork_retries: u32) -> Self {
898+
self.fork_retries = fork_retries;
899+
self
900+
}
860901
}
861902

862903
/// Can create dev accounts

0 commit comments

Comments
 (0)