-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathrunner.rs
447 lines (402 loc) · 14.6 KB
/
runner.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
use crate::TestFilter;
use ethers::{
abi::{Abi, Function, RawLog},
types::{Address, Bytes, U256},
};
use eyre::Result;
use foundry_evm::{
executor::{CallResult, DatabaseRef, DeployResult, EvmError, Executor},
fuzz::{CounterExample, FuzzedCases, FuzzedExecutor},
trace::{CallTraceArena, TraceKind},
CALLER,
};
use proptest::test_runner::TestRunner;
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use serde::{Deserialize, Serialize};
use std::{
collections::BTreeMap,
fmt,
time::{Duration, Instant},
};
/// Results and duration for a set of tests included in the same test contract
#[derive(Clone, Serialize)]
pub struct SuiteResult {
/// Total duration of the test run for this block of tests
pub duration: Duration,
/// Individual test results. `test method name -> TestResult`
pub test_results: BTreeMap<String, TestResult>,
}
impl SuiteResult {
pub fn new(duration: Duration, test_results: BTreeMap<String, TestResult>) -> Self {
Self { duration, test_results }
}
pub fn is_empty(&self) -> bool {
self.test_results.is_empty()
}
pub fn len(&self) -> usize {
self.test_results.len()
}
}
/// The result of an executed solidity test
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct TestResult {
/// Whether the test case was successful. This means that the transaction executed
/// properly, or that there was a revert and that the test was expected to fail
/// (prefixed with `testFail`)
pub success: bool,
/// If there was a revert, this field will be populated. Note that the test can
/// still be successful (i.e self.success == true) when it's expected to fail.
pub reason: Option<String>,
/// Minimal reproduction test case for failing fuzz tests
pub counterexample: Option<CounterExample>,
/// Any captured & parsed as strings logs along the test's execution which should
/// be printed to the user.
#[serde(skip)]
pub logs: Vec<RawLog>,
/// What kind of test this was
pub kind: TestKind,
/// Traces
pub traces: Vec<(TraceKind, CallTraceArena)>,
/// Labeled addresses
pub labeled_addresses: BTreeMap<Address, String>,
}
impl TestResult {
/// Returns `true` if this is the result of a fuzz test
pub fn is_fuzz(&self) -> bool {
matches!(self.kind, TestKind::Fuzz(_))
}
}
/// Used gas by a test
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum TestKindGas {
Standard(u64),
Fuzz { runs: usize, include_fuzz_test_gas: bool, mean: u64, median: u64 },
}
impl fmt::Display for TestKindGas {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
TestKindGas::Standard(gas) => {
write!(f, "(gas: {})", gas)
}
TestKindGas::Fuzz { runs, include_fuzz_test_gas, mean, median } => {
if *include_fuzz_test_gas {
write!(f, "(runs: {}, μ: {}, ~: {})", runs, mean, median)
} else {
write!(f, "(runs: {})", runs)
}
}
}
}
}
impl TestKindGas {
/// Returns the main gas value to compare against
pub fn gas(&self) -> u64 {
match self {
TestKindGas::Standard(gas) => *gas,
// We use the median for comparisons
TestKindGas::Fuzz { median, .. } => *median,
}
}
}
/// Various types of tests
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum TestKind {
/// A standard test that consists of calling the defined solidity function
///
/// Holds the consumed gas
Standard(u64),
/// A solidity fuzz test, that stores all test cases
Fuzz(FuzzedCases),
}
impl TestKind {
/// The gas consumed by this test
pub fn gas_used(&self, include_fuzz_test_gas: bool) -> TestKindGas {
match self {
TestKind::Standard(gas) => TestKindGas::Standard(*gas),
TestKind::Fuzz(fuzzed) => TestKindGas::Fuzz {
runs: fuzzed.cases().len(),
include_fuzz_test_gas,
median: fuzzed.median_gas(false),
mean: fuzzed.mean_gas(false),
},
}
}
}
#[derive(Clone, Debug, Default)]
pub struct TestSetup {
/// The address at which the test contract was deployed
pub address: Address,
/// The logs emitted during setup
pub logs: Vec<RawLog>,
/// Call traces of the setup
pub traces: Vec<(TraceKind, CallTraceArena)>,
/// Addresses labeled during setup
pub labeled_addresses: BTreeMap<Address, String>,
/// Whether the setup failed
pub setup_failed: bool,
/// The reason the setup failed
pub reason: Option<String>,
}
pub struct ContractRunner<'a, DB: DatabaseRef> {
/// The executor used by the runner.
pub executor: Executor<DB>,
/// Library contracts to be deployed before the test contract
pub predeploy_libs: &'a [Bytes],
/// The deployed contract's code
pub code: Bytes,
/// The test contract's ABI
pub contract: &'a Abi,
/// All known errors, used to decode reverts
pub errors: Option<&'a Abi>,
/// The initial balance of the test contract
pub initial_balance: U256,
/// The address which will be used as the `from` field in all EVM calls
pub sender: Address,
}
impl<'a, DB: DatabaseRef> ContractRunner<'a, DB> {
#[allow(clippy::too_many_arguments)]
pub fn new(
executor: Executor<DB>,
contract: &'a Abi,
code: Bytes,
initial_balance: U256,
sender: Option<Address>,
errors: Option<&'a Abi>,
predeploy_libs: &'a [Bytes],
) -> Self {
Self {
executor,
contract,
code,
initial_balance,
sender: sender.unwrap_or_default(),
errors,
predeploy_libs,
}
}
}
impl<'a, DB: DatabaseRef + Send + Sync> ContractRunner<'a, DB> {
/// Deploys the test contract inside the runner from the sending account, and optionally runs
/// the `setUp` function on the test contract.
pub fn setup(&mut self, setup: bool) -> Result<TestSetup> {
// We max out their balance so that they can deploy and make calls.
self.executor.set_balance(self.sender, U256::MAX);
self.executor.set_balance(*CALLER, U256::MAX);
// We set the nonce of the deployer accounts to 1 to get the same addresses as DappTools
self.executor.set_nonce(self.sender, 1);
// Deploy libraries
let mut traces: Vec<(TraceKind, CallTraceArena)> = self
.predeploy_libs
.iter()
.filter_map(|code| {
let DeployResult { traces, .. } = self
.executor
.deploy(self.sender, code.0.clone(), 0u32.into())
.expect("couldn't deploy library");
traces
})
.map(|traces| (TraceKind::Deployment, traces))
.collect();
// Deploy an instance of the contract
let DeployResult { address, mut logs, traces: constructor_traces, .. } = self
.executor
.deploy(self.sender, self.code.0.clone(), 0u32.into())
.expect("couldn't deploy");
traces.extend(constructor_traces.map(|traces| (TraceKind::Deployment, traces)).into_iter());
self.executor.set_balance(address, self.initial_balance);
// Optionally call the `setUp` function
Ok(if setup {
tracing::trace!("setting up");
let (setup_failed, setup_logs, setup_traces, labeled_addresses, reason) = match self
.executor
.setup(address)
{
Ok(CallResult { traces, labels, logs, .. }) => (false, logs, traces, labels, None),
Err(EvmError::Execution { traces, labels, logs, reason, .. }) => {
(true, logs, traces, labels, Some(format!("Setup failed: {}", reason)))
}
Err(e) => (
true,
Vec::new(),
None,
BTreeMap::new(),
Some(format!("Setup failed: {}", &e.to_string())),
),
};
traces.extend(setup_traces.map(|traces| (TraceKind::Setup, traces)).into_iter());
logs.extend_from_slice(&setup_logs);
TestSetup { address, logs, traces, labeled_addresses, setup_failed, reason }
} else {
TestSetup { address, logs, traces, ..Default::default() }
})
}
/// Runs all tests for a contract whose names match the provided regular expression
pub fn run_tests(
&mut self,
filter: &impl TestFilter,
fuzzer: Option<TestRunner>,
) -> Result<SuiteResult> {
tracing::info!("starting tests");
let start = Instant::now();
let needs_setup = self.contract.functions().any(|func| func.name == "setUp");
let setup = self.setup(needs_setup)?;
if setup.setup_failed {
// The setup failed, so we return a single test result for `setUp`
return Ok(SuiteResult::new(
start.elapsed(),
[(
"setUp()".to_string(),
TestResult {
success: false,
reason: setup.reason,
counterexample: None,
logs: setup.logs,
kind: TestKind::Standard(0),
traces: setup.traces,
labeled_addresses: setup.labeled_addresses,
},
)]
.into(),
))
}
// Collect valid test functions
let tests: Vec<_> = self
.contract
.functions()
.into_iter()
.filter(|func| func.name.starts_with("test") && filter.matches_test(func.signature()))
.map(|func| (func, func.name.starts_with("testFail")))
.collect();
let test_results = tests
.par_iter()
.filter_map(|(func, should_fail)| {
let result = if func.inputs.is_empty() {
Some(self.run_test(func, *should_fail, setup.clone()))
} else {
fuzzer.as_ref().map(|fuzzer| {
self.run_fuzz_test(func, *should_fail, fuzzer.clone(), setup.clone())
})
};
result.map(|result| Ok((func.signature(), result?)))
})
.collect::<Result<BTreeMap<_, _>>>()?;
let duration = start.elapsed();
if !test_results.is_empty() {
let successful = test_results.iter().filter(|(_, tst)| tst.success).count();
tracing::info!(
duration = ?duration,
"done. {}/{} successful",
successful,
test_results.len()
);
}
Ok(SuiteResult::new(duration, test_results))
}
#[tracing::instrument(name = "test", skip_all, fields(name = %func.signature(), %should_fail))]
pub fn run_test(
&self,
func: &Function,
should_fail: bool,
setup: TestSetup,
) -> Result<TestResult> {
let TestSetup { address, mut logs, mut traces, mut labeled_addresses, .. } = setup;
// Run unit test
let start = Instant::now();
let (reverted, reason, gas, stipend, execution_traces, state_changeset) = match self
.executor
.call::<(), _, _>(self.sender, address, func.clone(), (), 0.into(), self.errors)
{
Ok(CallResult {
reverted,
gas,
stipend,
logs: execution_logs,
traces: execution_trace,
labels: new_labels,
state_changeset,
..
}) => {
labeled_addresses.extend(new_labels);
logs.extend(execution_logs);
(reverted, None, gas, stipend, execution_trace, state_changeset)
}
Err(EvmError::Execution {
reverted,
reason,
gas,
stipend,
logs: execution_logs,
traces: execution_trace,
labels: new_labels,
state_changeset,
..
}) => {
labeled_addresses.extend(new_labels);
logs.extend(execution_logs);
(reverted, Some(reason), gas, stipend, execution_trace, state_changeset)
}
Err(err) => {
tracing::error!(?err);
return Err(err.into())
}
};
traces.extend(execution_traces.map(|traces| (TraceKind::Execution, traces)).into_iter());
let success = self.executor.is_success(
setup.address,
reverted,
state_changeset.expect("we should have a state changeset"),
should_fail,
);
// Record test execution time
tracing::debug!(
duration = ?start.elapsed(),
%success,
%gas
);
Ok(TestResult {
success,
reason,
counterexample: None,
logs,
kind: TestKind::Standard(gas.overflowing_sub(stipend).0),
traces,
labeled_addresses,
})
}
#[tracing::instrument(name = "fuzz-test", skip_all, fields(name = %func.signature(), %should_fail))]
pub fn run_fuzz_test(
&self,
func: &Function,
should_fail: bool,
runner: TestRunner,
setup: TestSetup,
) -> Result<TestResult> {
let TestSetup { address, mut logs, mut traces, mut labeled_addresses, .. } = setup;
// Run fuzz test
let start = Instant::now();
let mut result = FuzzedExecutor::new(&self.executor, runner, self.sender).fuzz(
func,
address,
should_fail,
self.errors,
);
// Record logs, labels and traces
logs.append(&mut result.logs);
labeled_addresses.append(&mut result.labeled_addresses);
traces.extend(result.traces.map(|traces| (TraceKind::Execution, traces)).into_iter());
// Record test execution time
tracing::debug!(
duration = ?start.elapsed(),
success = %result.success
);
Ok(TestResult {
success: result.success,
reason: result.reason,
counterexample: result.counterexample,
logs,
kind: TestKind::Fuzz(result.cases),
traces,
labeled_addresses,
})
}
}