Skip to main content

anvil/
lib.rs

1//! Anvil is a fast local Ethereum development node.
2
3#![cfg_attr(not(test), warn(unused_crate_dependencies))]
4#![cfg_attr(docsrs, feature(doc_cfg))]
5
6#[cfg(feature = "optimism")]
7use op_alloy_rpc_types as _;
8
9use crate::{
10    error::{NodeError, NodeResult},
11    eth::{
12        EthApi,
13        backend::{info::StorageInfo, mem},
14        fees::{FeeHistoryService, FeeManager},
15        miner::{Miner, MiningMode},
16        pool::Pool,
17        sign::{DevSigner, Signer as EthSigner},
18    },
19    filter::Filters,
20    logging::{LoggingManager, NodeLogLayer},
21    service::NodeService,
22    shutdown::Signal,
23    tasks::TaskManager,
24};
25use alloy_primitives::{Address, U256};
26use alloy_signer_local::PrivateKeySigner;
27use eth::backend::fork::ClientFork;
28use eyre::{Result, WrapErr};
29use foundry_common::provider::{ProviderBuilder, RetryProvider};
30pub use foundry_evm::hardfork::EthereumHardfork;
31use foundry_primitives::FoundryNetwork;
32use futures::{FutureExt, TryFutureExt};
33use parking_lot::Mutex;
34use server::try_spawn_ipc;
35use std::{
36    net::SocketAddr,
37    pin::Pin,
38    sync::Arc,
39    task::{Context, Poll},
40};
41use tokio::{
42    runtime::Handle,
43    task::{JoinError, JoinHandle},
44};
45use tracing_subscriber::EnvFilter;
46
47/// contains the background service that drives the node
48mod service;
49
50mod config;
51pub use config::{
52    AccountGenerator, CHAIN_ID, DEFAULT_GAS_LIMIT, ForkChoice, NodeConfig, VERSION_MESSAGE,
53};
54
55mod error;
56/// ethereum related implementations
57pub mod eth;
58/// Evm related abstractions
59mod evm;
60pub use evm::PrecompileFactory;
61
62/// support for polling filters
63pub mod filter;
64/// commandline output
65pub mod logging;
66/// types for subscriptions
67pub mod pubsub;
68/// axum RPC server implementations
69pub mod server;
70/// Futures for shutdown signal
71mod shutdown;
72/// additional task management
73mod tasks;
74
75/// contains cli command
76#[cfg(feature = "cmd")]
77pub mod cmd;
78
79#[cfg(feature = "cmd")]
80pub mod args;
81
82#[cfg(feature = "cmd")]
83pub mod opts;
84
85#[macro_use]
86extern crate foundry_common;
87
88#[macro_use]
89extern crate tracing;
90
91/// Creates the node and runs the server.
92///
93/// Returns the [EthApi] that can be used to interact with the node and the [JoinHandle] of the
94/// task.
95///
96/// # Panics
97///
98/// Panics if any error occurs. For a non-panicking version, use [`try_spawn`].
99///
100///
101/// # Examples
102///
103/// ```no_run
104/// # use anvil::NodeConfig;
105/// # async fn spawn() -> eyre::Result<()> {
106/// let config = NodeConfig::default();
107/// let (api, handle) = anvil::spawn(config).await;
108///
109/// // use api
110///
111/// // wait forever
112/// handle.await.unwrap().unwrap();
113/// # Ok(())
114/// # }
115/// ```
116pub async fn spawn(config: NodeConfig) -> (EthApi<FoundryNetwork>, NodeHandle) {
117    try_spawn(config).await.expect("failed to spawn node")
118}
119
120/// Creates the node and runs the server
121///
122/// Returns the [EthApi] that can be used to interact with the node and the [JoinHandle] of the
123/// task.
124///
125/// # Examples
126///
127/// ```no_run
128/// # use anvil::NodeConfig;
129/// # async fn spawn() -> eyre::Result<()> {
130/// let config = NodeConfig::default();
131/// let (api, handle) = anvil::try_spawn(config).await?;
132///
133/// // use api
134///
135/// // wait forever
136/// handle.await??;
137/// # Ok(())
138/// # }
139/// ```
140pub async fn try_spawn(mut config: NodeConfig) -> Result<(EthApi<FoundryNetwork>, NodeHandle)> {
141    let logger = if config.enable_tracing { init_tracing() } else { Default::default() };
142    logger.set_enabled(!config.silent);
143
144    let backend = config.setup::<FoundryNetwork>().await?;
145
146    if let Some(state) = config.init_state.clone() {
147        backend.load_state(state).await.wrap_err("failed to load init state")?;
148    }
149
150    let backend = Arc::new(backend);
151
152    if config.enable_auto_impersonate {
153        backend.auto_impersonate_account(true);
154    }
155
156    let fork = backend.get_fork();
157
158    let NodeConfig {
159        signer_accounts,
160        block_time,
161        port,
162        max_transactions,
163        server_config,
164        no_mining,
165        transaction_order,
166        genesis,
167        mixed_mining,
168        ..
169    } = config.clone();
170
171    let pool = Arc::new(Pool::default());
172
173    let mode = if let Some(block_time) = block_time {
174        if mixed_mining {
175            let listener = pool.add_ready_listener();
176            MiningMode::mixed(max_transactions, listener, block_time)
177        } else {
178            MiningMode::interval(block_time)
179        }
180    } else if no_mining {
181        MiningMode::None
182    } else {
183        // get a listener for ready transactions
184        let listener = pool.add_ready_listener();
185        MiningMode::instant(max_transactions, listener)
186    };
187
188    let miner = match &fork {
189        Some(fork) => {
190            Miner::new(mode).with_forced_transactions(fork.config.read().force_transactions.clone())
191        }
192        _ => Miner::new(mode),
193    };
194
195    let dev_signer: Box<dyn EthSigner<foundry_primitives::FoundryNetwork>> =
196        Box::new(DevSigner::new(signer_accounts));
197    let mut signers = vec![dev_signer];
198    if let Some(genesis) = genesis {
199        let genesis_signers = genesis
200            .alloc
201            .values()
202            .filter_map(|acc| acc.private_key)
203            .flat_map(|k| PrivateKeySigner::from_bytes(&k))
204            .collect::<Vec<_>>();
205        if !genesis_signers.is_empty() {
206            signers.push(Box::new(DevSigner::new(genesis_signers)));
207        }
208    }
209
210    let fee_history_cache = Arc::new(Mutex::new(Default::default()));
211    let fee_history_service = FeeHistoryService::new(
212        backend.blob_params(),
213        backend.new_block_notifications(),
214        Arc::clone(&fee_history_cache),
215        StorageInfo::new(Arc::clone(&backend)),
216    );
217    // create an entry for the best block
218    if let Some(header) = backend.get_block(backend.best_number()).map(|block| block.header) {
219        fee_history_service.insert_cache_entry_for_block(header.hash_slow(), &header);
220    }
221
222    let filters = Filters::default();
223
224    // create the cloneable api wrapper
225    let api = EthApi::new(
226        Arc::clone(&pool),
227        Arc::clone(&backend),
228        Arc::new(signers),
229        fee_history_cache,
230        fee_history_service.fee_history_limit(),
231        miner.clone(),
232        logger,
233        filters.clone(),
234        transaction_order,
235    );
236
237    // spawn the node service
238    let node_service =
239        tokio::task::spawn(NodeService::new(pool, backend, miner, fee_history_service, filters));
240
241    let mut servers = Vec::with_capacity(config.host.len());
242    let mut addresses = Vec::with_capacity(config.host.len());
243
244    for addr in &config.host {
245        let sock_addr = SocketAddr::new(*addr, port);
246
247        // Create a TCP listener.
248        let tcp_listener = tokio::net::TcpListener::bind(sock_addr).await?;
249        addresses.push(tcp_listener.local_addr()?);
250
251        // Spawn the server future on a new task.
252        let srv = server::serve_on(tcp_listener, api.clone(), server_config.clone());
253        servers.push(tokio::task::spawn(srv.map_err(Into::into)));
254    }
255
256    let tokio_handle = Handle::current();
257    let (signal, on_shutdown) = shutdown::signal();
258    let task_manager = TaskManager::new(tokio_handle, on_shutdown);
259
260    let ipc_task =
261        config.get_ipc_path().map(|path| try_spawn_ipc(api.clone(), path)).transpose()?;
262
263    let handle = NodeHandle {
264        config,
265        node_service,
266        servers,
267        ipc_task,
268        addresses,
269        _signal: Some(signal),
270        task_manager,
271    };
272
273    handle.print(fork.as_ref())?;
274
275    Ok((api, handle))
276}
277
278type IpcTask = JoinHandle<()>;
279
280/// A handle to the spawned node and server tasks.
281///
282/// This future will resolve if either the node or server task resolve/fail.
283pub struct NodeHandle {
284    config: NodeConfig,
285    /// The address of the running rpc server.
286    addresses: Vec<SocketAddr>,
287    /// Join handle for the Node Service.
288    node_service: JoinHandle<Result<(), NodeError>>,
289    /// Join handles (one per socket) for the Anvil server.
290    servers: Vec<JoinHandle<Result<(), NodeError>>>,
291    /// The future that joins the ipc server, if any.
292    ipc_task: Option<IpcTask>,
293    /// A signal that fires the shutdown, fired on drop.
294    _signal: Option<Signal>,
295    /// A task manager that can be used to spawn additional tasks.
296    task_manager: TaskManager,
297}
298
299impl Drop for NodeHandle {
300    fn drop(&mut self) {
301        // Fire shutdown signal to make sure anvil instance is terminated.
302        if let Some(signal) = self._signal.take() {
303            let _ = signal.fire();
304        }
305        self.node_service.abort();
306        for server in &self.servers {
307            server.abort();
308        }
309        if let Some(ipc_task) = &self.ipc_task {
310            ipc_task.abort();
311        }
312    }
313}
314
315impl NodeHandle {
316    /// The [NodeConfig] the node was launched with.
317    pub const fn config(&self) -> &NodeConfig {
318        &self.config
319    }
320
321    /// Prints the launch info.
322    pub(crate) fn print(&self, fork: Option<&ClientFork>) -> Result<()> {
323        self.config.print(fork)?;
324        if !self.config.silent {
325            if let Some(ipc_path) = self.ipc_path() {
326                sh_println!("IPC path: {ipc_path}")?;
327            }
328            sh_println!(
329                "Listening on {}",
330                self.addresses
331                    .iter()
332                    .map(|addr| { addr.to_string() })
333                    .collect::<Vec<String>>()
334                    .join(", ")
335            )?;
336        }
337        Ok(())
338    }
339
340    /// The address of the launched server.
341    ///
342    /// **N.B.** this may not necessarily be the same `host + port` as configured in the
343    /// `NodeConfig`, if port was set to 0, then the OS auto picks an available port.
344    pub fn socket_address(&self) -> &SocketAddr {
345        &self.addresses[0]
346    }
347
348    /// Returns the http endpoint.
349    pub fn http_endpoint(&self) -> String {
350        format!("http://{}", self.socket_address())
351    }
352
353    /// Returns the websocket endpoint.
354    pub fn ws_endpoint(&self) -> String {
355        format!("ws://{}", self.socket_address())
356    }
357
358    /// Returns the path of the launched ipc server, if any.
359    pub fn ipc_path(&self) -> Option<String> {
360        self.config.get_ipc_path()
361    }
362
363    /// Constructs a [`RetryProvider`] for this handle's HTTP endpoint.
364    pub fn http_provider(&self) -> RetryProvider {
365        ProviderBuilder::new(&self.http_endpoint()).build().expect("failed to build HTTP provider")
366    }
367
368    /// Constructs a [`RetryProvider`] for this handle's WS endpoint.
369    pub fn ws_provider(&self) -> RetryProvider {
370        ProviderBuilder::new(&self.ws_endpoint()).build().expect("failed to build WS provider")
371    }
372
373    /// Constructs a [`RetryProvider`] for this handle's IPC endpoint, if any.
374    pub fn ipc_provider(&self) -> Option<RetryProvider> {
375        ProviderBuilder::new(&self.config.get_ipc_path()?).build().ok()
376    }
377
378    /// Signer accounts that can sign messages/transactions from the EVM node.
379    pub fn dev_accounts(&self) -> impl Iterator<Item = Address> + '_ {
380        self.config.signer_accounts.iter().map(|wallet| wallet.address())
381    }
382
383    /// Signer accounts that can sign messages/transactions from the EVM node.
384    pub fn dev_wallets(&self) -> impl Iterator<Item = PrivateKeySigner> + '_ {
385        self.config.signer_accounts.iter().cloned()
386    }
387
388    /// Accounts that will be initialised with `genesis_balance` in the genesis block.
389    pub fn genesis_accounts(&self) -> impl Iterator<Item = Address> + '_ {
390        self.config.genesis_accounts.iter().map(|w| w.address())
391    }
392
393    /// Native token balance of every genesis account in the genesis block.
394    pub const fn genesis_balance(&self) -> U256 {
395        self.config.genesis_balance
396    }
397
398    /// Default gas price for all txs.
399    pub fn gas_price(&self) -> u128 {
400        self.config.get_gas_price()
401    }
402
403    /// Returns the shutdown signal.
404    pub const fn shutdown_signal(&self) -> &Option<Signal> {
405        &self._signal
406    }
407
408    /// Returns mutable access to the shutdown signal.
409    ///
410    /// This can be used to extract the Signal.
411    pub const fn shutdown_signal_mut(&mut self) -> &mut Option<Signal> {
412        &mut self._signal
413    }
414
415    /// Returns the task manager that can be used to spawn new tasks.
416    ///
417    /// ```
418    /// use anvil::NodeHandle;
419    /// # fn t(handle: NodeHandle) {
420    /// let task_manager = handle.task_manager();
421    /// let on_shutdown = task_manager.on_shutdown();
422    ///
423    /// task_manager.spawn(async move {
424    ///     on_shutdown.await;
425    ///     // do something
426    /// });
427    ///
428    /// # }
429    /// ```
430    pub const fn task_manager(&self) -> &TaskManager {
431        &self.task_manager
432    }
433}
434
435impl Future for NodeHandle {
436    type Output = Result<NodeResult<()>, JoinError>;
437
438    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
439        let pin = self.get_mut();
440
441        // poll the ipc task
442        if let Some(mut ipc) = pin.ipc_task.take() {
443            if let Poll::Ready(res) = ipc.poll_unpin(cx) {
444                return Poll::Ready(res.map(|()| Ok(())));
445            }
446            pin.ipc_task = Some(ipc);
447        }
448
449        // poll the node service task
450        if let Poll::Ready(res) = pin.node_service.poll_unpin(cx) {
451            return Poll::Ready(res);
452        }
453
454        // poll the axum server handles
455        for server in &mut pin.servers {
456            if let Poll::Ready(res) = server.poll_unpin(cx) {
457                return Poll::Ready(res);
458            }
459        }
460
461        Poll::Pending
462    }
463}
464
465#[doc(hidden)]
466pub fn init_tracing() -> LoggingManager {
467    use tracing_subscriber::prelude::*;
468
469    let manager = LoggingManager::default();
470
471    let _ = if let Ok(rust_log_val) = std::env::var("RUST_LOG")
472        && !rust_log_val.contains('=')
473    {
474        // Mutate the given filter to include `node` logs if it is not already present.
475        // This prevents the unexpected behaviour of not seeing any node logs if a RUST_LOG
476        // is already present that doesn't set it.
477        let rust_log_val = if rust_log_val.contains("node") {
478            rust_log_val
479        } else {
480            format!("{rust_log_val},node=info")
481        };
482
483        let env_filter: EnvFilter =
484            rust_log_val.parse().expect("failed to parse modified RUST_LOG");
485        tracing_subscriber::registry()
486            .with(env_filter)
487            .with(tracing_subscriber::fmt::layer())
488            .try_init()
489    } else {
490        tracing_subscriber::Registry::default()
491            .with(NodeLogLayer::new(manager.clone()))
492            .with(
493                tracing_subscriber::fmt::layer()
494                    .without_time()
495                    .with_target(false)
496                    .with_level(false),
497            )
498            .try_init()
499    };
500
501    manager
502}