foundry_evm/executors/
builder.rsuse crate::{executors::Executor, inspectors::InspectorStackBuilder};
use foundry_evm_core::backend::Backend;
use revm::primitives::{Env, EnvWithHandlerCfg, SpecId};
#[derive(Clone, Debug)]
#[must_use = "builders do nothing unless you call `build` on them"]
pub struct ExecutorBuilder {
stack: InspectorStackBuilder,
gas_limit: Option<u64>,
spec_id: SpecId,
legacy_assertions: bool,
}
impl Default for ExecutorBuilder {
#[inline]
fn default() -> Self {
Self {
stack: InspectorStackBuilder::new(),
gas_limit: None,
spec_id: SpecId::LATEST,
legacy_assertions: false,
}
}
}
impl ExecutorBuilder {
#[inline]
pub fn new() -> Self {
Self::default()
}
#[inline]
pub fn inspectors(
mut self,
f: impl FnOnce(InspectorStackBuilder) -> InspectorStackBuilder,
) -> Self {
self.stack = f(self.stack);
self
}
#[inline]
pub fn spec(mut self, spec: SpecId) -> Self {
self.spec_id = spec;
self
}
#[inline]
pub fn gas_limit(mut self, gas_limit: u64) -> Self {
self.gas_limit = Some(gas_limit);
self
}
#[inline]
pub fn legacy_assertions(mut self, legacy_assertions: bool) -> Self {
self.legacy_assertions = legacy_assertions;
self
}
#[inline]
pub fn build(self, env: Env, db: Backend) -> Executor {
let Self { mut stack, gas_limit, spec_id, legacy_assertions } = self;
if stack.block.is_none() {
stack.block = Some(env.block.clone());
}
if stack.gas_price.is_none() {
stack.gas_price = Some(env.tx.gas_price);
}
let gas_limit = gas_limit.unwrap_or_else(|| env.block.gas_limit.saturating_to());
let env = EnvWithHandlerCfg::new_with_spec_id(Box::new(env), spec_id);
Executor::new(db, env, stack.build(), gas_limit, legacy_assertions)
}
}