forked from foundry-rs/foundry
/
executor.rs
175 lines (151 loc) · 6.09 KB
/
executor.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use crate::{cmd::needs_setup, utils};
use cast::executor::inspector::DEFAULT_CREATE2_DEPLOYER;
use ethers::{
prelude::NameOrAddress,
solc::artifacts::CompactContractBytecode,
types::{transaction::eip2718::TypedTransaction, Address, U256},
};
use forge::{
executor::{builder::Backend, ExecutorBuilder},
trace::CallTraceDecoder,
};
use std::collections::VecDeque;
use crate::cmd::forge::script::*;
impl ScriptArgs {
/// Locally deploys and executes the contract method that will collect all broadcastable
/// transactions.
pub async fn execute(
&self,
script_config: &mut ScriptConfig,
contract: CompactContractBytecode,
sender: Address,
predeploy_libraries: &[ethers::types::Bytes],
) -> eyre::Result<ScriptResult> {
let CompactContractBytecode { abi, bytecode, .. } = contract;
let abi = abi.expect("no ABI for contract");
let bytecode = bytecode.expect("no bytecode for contract").object.into_bytes().unwrap();
let mut runner = self.prepare_runner(script_config, sender).await;
let (address, mut result) = runner.setup(
predeploy_libraries,
bytecode,
needs_setup(&abi),
script_config.sender_nonce,
self.broadcast,
script_config.evm_opts.fork_url.is_none(),
)?;
let (func, calldata) = self.get_method_and_calldata(&abi)?;
script_config.called_function = Some(func);
let script_result = runner.script(address, calldata)?;
result.success &= script_result.success;
result.gas = script_result.gas;
result.logs.extend(script_result.logs);
result.traces.extend(script_result.traces);
result.debug = script_result.debug;
result.labeled_addresses.extend(script_result.labeled_addresses);
result.returned = script_result.returned;
match (&mut result.transactions, script_result.transactions) {
(Some(txs), Some(new_txs)) => {
txs.extend(new_txs);
}
(None, Some(new_txs)) => {
result.transactions = Some(new_txs);
}
_ => {}
}
Ok(result)
}
/// Executes a list of transactions locally and persists their state. Returns the transactions
/// and any CREATE2 contract addresses created.
pub async fn execute_transactions(
&self,
transactions: VecDeque<TypedTransaction>,
script_config: &ScriptConfig,
decoder: &mut CallTraceDecoder,
) -> eyre::Result<(VecDeque<TypedTransaction>, Vec<Address>)> {
let mut runner = self.prepare_runner(script_config, script_config.evm_opts.sender).await;
let mut failed = false;
let mut sum_gas = 0;
let mut final_txs = transactions.clone();
let mut create2_contracts = vec![];
if script_config.evm_opts.verbosity > 3 {
println!("==========================");
println!("Simulated On-chain Traces:\n");
}
transactions
.into_iter()
.map(|tx| match tx {
TypedTransaction::Legacy(tx) => (tx.from, tx.to, tx.data, tx.value),
_ => unreachable!(),
})
.map(|(from, to, data, value)| {
runner
.simulate(
from.expect("Transaction doesn't have a `from` address at execution time"),
to,
data,
value,
)
.expect("Internal EVM error")
})
.enumerate()
.for_each(|(i, mut result)| {
match &mut final_txs[i] {
TypedTransaction::Legacy(tx) => {
// We store the CREATE2 address, since it's hard to get it otherwise
if let Some(NameOrAddress::Address(to)) = tx.to {
if to == DEFAULT_CREATE2_DEPLOYER {
create2_contracts.push(Address::from_slice(&result.returned));
}
}
// We inflate the gas used by the transaction by x1.3 since the estimation
// might be off
tx.gas = Some(U256::from(result.gas * 13 / 10))
}
_ => unreachable!(),
}
sum_gas += result.gas;
if !result.success {
failed = true;
}
if script_config.evm_opts.verbosity > 3 {
for (_kind, trace) in &mut result.traces {
decoder.decode(trace);
println!("{}", trace);
}
}
});
if failed {
Err(eyre::Report::msg("Simulated execution failed"))
} else {
println!("\n==========================\n");
println!("Estimated total gas used for script: {}", sum_gas);
println!("==========================");
Ok((final_txs, create2_contracts))
}
}
async fn prepare_runner(
&self,
script_config: &ScriptConfig,
sender: Address,
) -> Runner<Backend> {
let env = script_config.evm_opts.evm_env().await;
// the db backend that serves all the data
let db = Backend::new(
utils::get_fork(&script_config.evm_opts, &script_config.config.rpc_storage_caching),
&env,
)
.await;
let mut builder = ExecutorBuilder::new()
.with_cheatcodes(script_config.evm_opts.ffi)
.with_config(env)
.with_spec(crate::utils::evm_spec(&script_config.config.evm_version))
.with_gas_limit(script_config.evm_opts.gas_limit());
if script_config.evm_opts.verbosity >= 3 {
builder = builder.with_tracing();
}
if self.debug {
builder = builder.with_tracing().with_debugger();
}
Runner::new(builder.build(db), script_config.evm_opts.initial_balance, sender)
}
}