Skip to main content

foundry_cheatcodes/evm/
fork.rs

1use crate::{
2    Cheatcode, Cheatcodes, CheatcodesExecutor, CheatsCtxt, DatabaseExt, Result, Vm::*,
3    json::json_value_to_token,
4};
5use alloy_dyn_abi::DynSolValue;
6use alloy_evm::EvmEnv;
7use alloy_network::AnyNetwork;
8use alloy_primitives::{B256, U256};
9use alloy_provider::Provider;
10use alloy_rpc_types::Filter;
11use alloy_sol_types::SolValue;
12use foundry_common::provider::ProviderBuilder;
13use foundry_evm_core::{
14    FoundryContextExt, backend::JournaledState, evm::FoundryEvmNetwork, fork::CreateFork,
15};
16use revm::context::ContextTr;
17
18impl Cheatcode for activeForkCall {
19    fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
20        let Self {} = self;
21        ccx.ecx
22            .db()
23            .active_fork_id()
24            .map(|id| id.abi_encode())
25            .ok_or_else(|| fmt_err!("no active fork"))
26    }
27}
28
29impl Cheatcode for createFork_0Call {
30    fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
31        let Self { urlOrAlias } = self;
32        create_fork(ccx, urlOrAlias, None)
33    }
34}
35
36impl Cheatcode for createFork_1Call {
37    fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
38        let Self { urlOrAlias, blockNumber } = self;
39        create_fork(ccx, urlOrAlias, Some(blockNumber.saturating_to()))
40    }
41}
42
43impl Cheatcode for createFork_2Call {
44    fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
45        let Self { urlOrAlias, txHash } = self;
46        create_fork_at_transaction(ccx, urlOrAlias, txHash)
47    }
48}
49
50impl Cheatcode for createSelectFork_0Call {
51    fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
52        let Self { urlOrAlias } = self;
53        create_select_fork(ccx, urlOrAlias, None)
54    }
55}
56
57impl Cheatcode for createSelectFork_1Call {
58    fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
59        let Self { urlOrAlias, blockNumber } = self;
60        create_select_fork(ccx, urlOrAlias, Some(blockNumber.saturating_to()))
61    }
62}
63
64impl Cheatcode for createSelectFork_2Call {
65    fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
66        let Self { urlOrAlias, txHash } = self;
67        create_select_fork_at_transaction(ccx, urlOrAlias, txHash)
68    }
69}
70
71impl Cheatcode for rollFork_0Call {
72    fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
73        let Self { blockNumber } = self;
74        persist_caller(ccx);
75        fork_env_op(ccx.ecx, |db, evm_env, _, inner| {
76            db.roll_fork(None, (*blockNumber).to(), evm_env, inner)
77        })
78    }
79}
80
81impl Cheatcode for rollFork_1Call {
82    fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
83        let Self { txHash } = self;
84        persist_caller(ccx);
85        fork_env_op(ccx.ecx, |db, evm_env, _, inner| {
86            db.roll_fork_to_transaction(None, *txHash, evm_env, inner)
87        })
88    }
89}
90
91impl Cheatcode for rollFork_2Call {
92    fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
93        let Self { forkId, blockNumber } = self;
94        persist_caller(ccx);
95        fork_env_op(ccx.ecx, |db, evm_env, _, inner| {
96            db.roll_fork(Some(*forkId), (*blockNumber).to(), evm_env, inner)
97        })
98    }
99}
100
101impl Cheatcode for rollFork_3Call {
102    fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
103        let Self { forkId, txHash } = self;
104        persist_caller(ccx);
105        fork_env_op(ccx.ecx, |db, evm_env, _, inner| {
106            db.roll_fork_to_transaction(Some(*forkId), *txHash, evm_env, inner)
107        })
108    }
109}
110
111impl Cheatcode for selectForkCall {
112    fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
113        let Self { forkId } = self;
114        persist_caller(ccx);
115        check_broadcast(ccx.state)?;
116        fork_env_op(ccx.ecx, |db, evm_env, tx_env, inner| {
117            db.select_fork(*forkId, evm_env, tx_env, inner)
118        })
119    }
120}
121
122impl Cheatcode for transact_0Call {
123    fn apply_full<FEN: FoundryEvmNetwork>(
124        &self,
125        ccx: &mut CheatsCtxt<'_, '_, FEN>,
126        executor: &mut dyn CheatcodesExecutor<FEN>,
127    ) -> Result {
128        let Self { txHash } = *self;
129        transact(ccx, executor, txHash, None)
130    }
131}
132
133impl Cheatcode for transact_1Call {
134    fn apply_full<FEN: FoundryEvmNetwork>(
135        &self,
136        ccx: &mut CheatsCtxt<'_, '_, FEN>,
137        executor: &mut dyn CheatcodesExecutor<FEN>,
138    ) -> Result {
139        let Self { forkId, txHash } = *self;
140        transact(ccx, executor, txHash, Some(forkId))
141    }
142}
143
144impl Cheatcode for allowCheatcodesCall {
145    fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
146        let Self { account } = self;
147        ccx.ecx.db_mut().allow_cheatcode_access(*account);
148        Ok(Default::default())
149    }
150}
151
152impl Cheatcode for makePersistent_0Call {
153    fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
154        let Self { account } = self;
155        ccx.ecx.db_mut().add_persistent_account(*account);
156        Ok(Default::default())
157    }
158}
159
160impl Cheatcode for makePersistent_1Call {
161    fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
162        let Self { account0, account1 } = self;
163        ccx.ecx.db_mut().add_persistent_account(*account0);
164        ccx.ecx.db_mut().add_persistent_account(*account1);
165        Ok(Default::default())
166    }
167}
168
169impl Cheatcode for makePersistent_2Call {
170    fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
171        let Self { account0, account1, account2 } = self;
172        ccx.ecx.db_mut().add_persistent_account(*account0);
173        ccx.ecx.db_mut().add_persistent_account(*account1);
174        ccx.ecx.db_mut().add_persistent_account(*account2);
175        Ok(Default::default())
176    }
177}
178
179impl Cheatcode for makePersistent_3Call {
180    fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
181        let Self { accounts } = self;
182        for account in accounts {
183            ccx.ecx.db_mut().add_persistent_account(*account);
184        }
185        Ok(Default::default())
186    }
187}
188
189impl Cheatcode for revokePersistent_0Call {
190    fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
191        let Self { account } = self;
192        ccx.ecx.db_mut().remove_persistent_account(account);
193        Ok(Default::default())
194    }
195}
196
197impl Cheatcode for revokePersistent_1Call {
198    fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
199        let Self { accounts } = self;
200        for account in accounts {
201            ccx.ecx.db_mut().remove_persistent_account(account);
202        }
203        Ok(Default::default())
204    }
205}
206
207impl Cheatcode for isPersistentCall {
208    fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
209        let Self { account } = self;
210        Ok(ccx.ecx.db().is_persistent(account).abi_encode())
211    }
212}
213
214impl Cheatcode for rpc_0Call {
215    fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
216        let Self { method, params } = self;
217        let url =
218            ccx.ecx.db().active_fork_url().ok_or_else(|| fmt_err!("no active fork URL found"))?;
219        rpc_call(&url, method, params)
220    }
221}
222
223impl Cheatcode for rpc_1Call {
224    fn apply<FEN: FoundryEvmNetwork>(&self, state: &mut Cheatcodes<FEN>) -> Result {
225        let Self { urlOrAlias, method, params } = self;
226        let url = state.config.rpc_endpoint(urlOrAlias)?.url()?;
227        rpc_call(&url, method, params)
228    }
229}
230
231impl Cheatcode for eth_getLogsCall {
232    fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
233        let Self { fromBlock, toBlock, target, topics } = self;
234        let (Ok(from_block), Ok(to_block)) = (u64::try_from(fromBlock), u64::try_from(toBlock))
235        else {
236            bail!("blocks in block range must be less than 2^64")
237        };
238
239        if topics.len() > 4 {
240            bail!("topics array must contain at most 4 elements")
241        }
242
243        let url =
244            ccx.ecx.db().active_fork_url().ok_or_else(|| fmt_err!("no active fork URL found"))?;
245        let provider = ProviderBuilder::<AnyNetwork>::new(&url).build()?;
246        let mut filter = Filter::new().address(*target).from_block(from_block).to_block(to_block);
247        for (i, &topic) in topics.iter().enumerate() {
248            filter.topics[i] = topic.into();
249        }
250
251        let logs = foundry_common::block_on(provider.get_logs(&filter))
252            .map_err(|e| fmt_err!("failed to get logs: {e}"))?;
253
254        let eth_logs = logs
255            .into_iter()
256            .map(|log| EthGetLogs {
257                emitter: log.address(),
258                topics: log.topics().to_vec(),
259                data: log.inner.data.data,
260                blockHash: log.block_hash.unwrap_or_default(),
261                blockNumber: log.block_number.unwrap_or_default(),
262                transactionHash: log.transaction_hash.unwrap_or_default(),
263                transactionIndex: log.transaction_index.unwrap_or_default(),
264                logIndex: U256::from(log.log_index.unwrap_or_default()),
265                removed: log.removed,
266            })
267            .collect::<Vec<_>>();
268
269        Ok(eth_logs.abi_encode())
270    }
271}
272
273impl Cheatcode for getRawBlockHeaderCall {
274    fn apply_stateful<FEN: FoundryEvmNetwork>(&self, ccx: &mut CheatsCtxt<'_, '_, FEN>) -> Result {
275        let Self { blockNumber } = self;
276        let url = ccx.ecx.db().active_fork_url().ok_or_else(|| fmt_err!("no active fork"))?;
277        let provider = ProviderBuilder::<AnyNetwork>::new(&url).build()?;
278        let block_number = u64::try_from(blockNumber)
279            .map_err(|_| fmt_err!("block number must be less than 2^64"))?;
280        let block =
281            foundry_common::block_on(async move { provider.get_block(block_number.into()).await })
282                .map_err(|e| fmt_err!("failed to get block: {e}"))?
283                .ok_or_else(|| fmt_err!("block {block_number} not found"))?;
284
285        let header: alloy_consensus::Header = block
286            .into_inner()
287            .header
288            .inner
289            .try_into_header()
290            .map_err(|e| fmt_err!("failed to convert to header: {e}"))?;
291        Ok(alloy_rlp::encode(&header).abi_encode())
292    }
293}
294
295/// Creates and then also selects the new fork
296fn create_select_fork<FEN: FoundryEvmNetwork>(
297    ccx: &mut CheatsCtxt<'_, '_, FEN>,
298    url_or_alias: &str,
299    block: Option<u64>,
300) -> Result {
301    check_broadcast(ccx.state)?;
302
303    let fork = create_fork_request(ccx, url_or_alias, block)?;
304    fork_env_op(ccx.ecx, |db, evm_env, tx_env, inner| {
305        db.create_select_fork(fork, evm_env, tx_env, inner)
306    })
307}
308
309/// Creates a new fork
310fn create_fork<FEN: FoundryEvmNetwork>(
311    ccx: &mut CheatsCtxt<'_, '_, FEN>,
312    url_or_alias: &str,
313    block: Option<u64>,
314) -> Result {
315    let fork = create_fork_request(ccx, url_or_alias, block)?;
316    let id = ccx.ecx.db_mut().create_fork(fork)?;
317    Ok(id.abi_encode())
318}
319
320/// Creates and then also selects the new fork at the given transaction
321fn create_select_fork_at_transaction<FEN: FoundryEvmNetwork>(
322    ccx: &mut CheatsCtxt<'_, '_, FEN>,
323    url_or_alias: &str,
324    transaction: &B256,
325) -> Result {
326    check_broadcast(ccx.state)?;
327
328    let fork = create_fork_request(ccx, url_or_alias, None)?;
329    fork_env_op(ccx.ecx, |db, evm_env, tx_env, inner| {
330        db.create_select_fork_at_transaction(fork, evm_env, tx_env, inner, *transaction)
331    })
332}
333
334/// Creates a new fork at the given transaction
335fn create_fork_at_transaction<FEN: FoundryEvmNetwork>(
336    ccx: &mut CheatsCtxt<'_, '_, FEN>,
337    url_or_alias: &str,
338    transaction: &B256,
339) -> Result {
340    let fork = create_fork_request(ccx, url_or_alias, None)?;
341    let id = ccx.ecx.db_mut().create_fork_at_transaction(fork, *transaction)?;
342    Ok(id.abi_encode())
343}
344
345/// Creates the request object for a new fork request
346fn create_fork_request<FEN: FoundryEvmNetwork>(
347    ccx: &mut CheatsCtxt<'_, '_, FEN>,
348    url_or_alias: &str,
349    block: Option<u64>,
350) -> Result<CreateFork> {
351    persist_caller(ccx);
352
353    let rpc_endpoint = ccx.state.config.rpc_endpoint(url_or_alias)?;
354    let url = rpc_endpoint.url()?;
355    let mut evm_opts = ccx.state.config.evm_opts.clone();
356    evm_opts.fork_block_number = block;
357    evm_opts.fork_retries = rpc_endpoint.config.retries;
358    evm_opts.fork_retry_backoff = rpc_endpoint.config.retry_backoff;
359    if let Some(Ok(auth)) = rpc_endpoint.auth {
360        evm_opts.fork_headers = Some(vec![format!("Authorization: {auth}")]);
361    }
362    let fork = CreateFork {
363        enable_caching: !ccx.state.config.no_storage_caching
364            && ccx.state.config.rpc_storage_caching.enable_for_endpoint(&url),
365        url,
366        evm_opts,
367    };
368    Ok(fork)
369}
370
371/// Clones the EVM and tx environments, runs a fork operation that may modify them, then writes
372/// them back. This is the common pattern for all fork-switching cheatcodes (rollFork, selectFork,
373/// createSelectFork).
374fn fork_env_op<CTX: FoundryContextExt, T: SolValue>(
375    ecx: &mut CTX,
376    f: impl FnOnce(
377        &mut CTX::Db,
378        &mut EvmEnv<CTX::Spec, CTX::Block>,
379        &mut CTX::Tx,
380        &mut JournaledState,
381    ) -> eyre::Result<T>,
382) -> Result {
383    let mut evm_env = ecx.evm_clone();
384    let mut tx_env = ecx.tx_clone();
385    let (db, inner) = ecx.db_journal_inner_mut();
386    let result = f(db, &mut evm_env, &mut tx_env, inner)?;
387    ecx.set_evm(evm_env);
388    ecx.set_tx(tx_env);
389    Ok(result.abi_encode())
390}
391
392fn check_broadcast<FEN: FoundryEvmNetwork>(state: &Cheatcodes<FEN>) -> Result<()> {
393    if state.broadcast.is_none() {
394        Ok(())
395    } else {
396        Err(fmt_err!("cannot select forks during a broadcast"))
397    }
398}
399
400fn transact<FEN: FoundryEvmNetwork>(
401    ccx: &mut CheatsCtxt<'_, '_, FEN>,
402    executor: &mut dyn CheatcodesExecutor<FEN>,
403    transaction: B256,
404    fork_id: Option<U256>,
405) -> Result {
406    executor.transact_on_db(ccx.state, ccx.ecx, fork_id, transaction)?;
407    Ok(Default::default())
408}
409
410// Helper to add the caller of fork cheat code as persistent account (in order to make sure that the
411// state of caller contract is not lost when fork changes).
412// Applies to create, select and roll forks actions.
413// https://github.com/foundry-rs/foundry/issues/8004
414fn persist_caller<FEN: FoundryEvmNetwork>(ccx: &mut CheatsCtxt<'_, '_, FEN>) {
415    ccx.ecx.db_mut().add_persistent_account(ccx.caller);
416}
417
418/// Performs an Ethereum JSON-RPC request to the given endpoint.
419fn rpc_call(url: &str, method: &str, params: &str) -> Result {
420    let provider = ProviderBuilder::<AnyNetwork>::new(url).build()?;
421    let params_json: serde_json::Value = serde_json::from_str(params)?;
422    let result =
423        foundry_common::block_on(provider.raw_request(method.to_string().into(), params_json))
424            .map_err(|err| fmt_err!("{method:?}: {err}"))?;
425    let result_as_tokens = convert_to_bytes(
426        &json_value_to_token(&result, None)
427            .map_err(|err| fmt_err!("failed to parse result: {err}"))?,
428    );
429
430    let payload = match &result_as_tokens {
431        DynSolValue::Bytes(b) => b.clone(),
432        _ => result_as_tokens.abi_encode(),
433    };
434    Ok(DynSolValue::Bytes(payload).abi_encode())
435}
436
437/// Convert fixed bytes and address values to bytes in order to prevent encoding issues.
438fn convert_to_bytes(token: &DynSolValue) -> DynSolValue {
439    match token {
440        // Convert fixed bytes to prevent encoding issues.
441        // See: <https://github.com/foundry-rs/foundry/issues/8287>
442        DynSolValue::FixedBytes(bytes, size) => {
443            DynSolValue::Bytes(bytes.as_slice()[..*size].to_vec())
444        }
445        DynSolValue::Address(addr) => DynSolValue::Bytes(addr.to_vec()),
446        val => val.clone(),
447    }
448}