-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
builder.rs
73 lines (66 loc) · 2.13 KB
/
builder.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
use super::{inspector::InspectorStackBuilder, Executor};
use crate::executor::backend::Backend;
use revm::primitives::{Env, SpecId, U256};
/// The builder that allows to configure an evm [`Executor`] which a stack of optional
/// [`revm::Inspector`]s, such as [`Cheatcodes`].
///
/// By default, the [`Executor`] will be configured with an empty [`InspectorStack`].
///
/// [`Cheatcodes`]: super::inspector::Cheatcodes
/// [`InspectorStack`]: super::inspector::InspectorStack
#[derive(Debug)]
#[must_use = "builders do nothing unless you call `build` on them"]
pub struct ExecutorBuilder {
/// The configuration used to build an [InspectorStack].
stack: InspectorStackBuilder,
/// The gas limit.
gas_limit: Option<U256>,
/// The spec ID.
spec_id: SpecId,
}
impl Default for ExecutorBuilder {
#[inline]
fn default() -> Self {
Self { stack: InspectorStackBuilder::new(), gas_limit: None, spec_id: SpecId::LATEST }
}
}
impl ExecutorBuilder {
/// Create a new executor builder.
#[inline]
pub fn new() -> Self {
Self::default()
}
/// Modify the inspector stack.
#[inline]
pub fn inspectors(
mut self,
f: impl FnOnce(InspectorStackBuilder) -> InspectorStackBuilder,
) -> Self {
self.stack = f(self.stack);
self
}
/// Sets the EVM spec to use
#[inline]
pub fn spec(mut self, spec: SpecId) -> Self {
self.spec_id = spec;
self
}
/// Sets the executor gas limit.
///
/// See [Executor::gas_limit] for more info on why you might want to set this.
#[inline]
pub fn gas_limit(mut self, gas_limit: U256) -> Self {
self.gas_limit = Some(gas_limit);
self
}
/// Builds the executor as configured.
#[inline]
pub fn build(self, mut env: Env, db: Backend) -> Executor {
let Self { mut stack, gas_limit, spec_id } = self;
env.cfg.spec_id = spec_id;
stack.block = Some(env.block.clone());
stack.gas_price = Some(env.tx.gas_price);
let gas_limit = gas_limit.unwrap_or(env.block.gas_limit);
Executor::new(db, env, stack.build(), gas_limit)
}
}