revm_primitives/env/handler_cfg.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
use super::{BlockEnv, CfgEnv, Env, SpecId, TxEnv};
use core::ops::{Deref, DerefMut};
use std::boxed::Box;
/// Handler configuration fields. It is used to configure the handler.
/// It contains specification id and the Optimism related field if
/// optimism feature is enabled.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub struct HandlerCfg {
/// Specification identification.
pub spec_id: SpecId,
/// Optimism related field, it will append the Optimism handle register to the EVM.
#[cfg(feature = "optimism")]
pub is_optimism: bool,
}
impl Default for HandlerCfg {
fn default() -> Self {
Self::new(SpecId::default())
}
}
impl HandlerCfg {
/// Creates new `HandlerCfg` instance.
pub fn new(spec_id: SpecId) -> Self {
cfg_if::cfg_if! {
if #[cfg(all(feature = "optimism-default-handler",
not(feature = "negate-optimism-default-handler")))] {
let is_optimism = true;
} else if #[cfg(feature = "optimism")] {
let is_optimism = false;
}
}
Self {
spec_id,
#[cfg(feature = "optimism")]
is_optimism,
}
}
/// Creates new `HandlerCfg` instance with the optimism feature.
#[cfg(feature = "optimism")]
pub fn new_with_optimism(spec_id: SpecId, is_optimism: bool) -> Self {
Self {
spec_id,
is_optimism,
}
}
/// Returns `true` if the optimism feature is enabled and flag is set to `true`.
pub fn is_optimism(&self) -> bool {
cfg_if::cfg_if! {
if #[cfg(feature = "optimism")] {
self.is_optimism
} else {
false
}
}
}
}
/// Configuration environment with the chain spec id.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CfgEnvWithHandlerCfg {
/// Configuration environment.
pub cfg_env: CfgEnv,
/// Handler configuration fields.
pub handler_cfg: HandlerCfg,
}
impl CfgEnvWithHandlerCfg {
/// Returns new instance of `CfgEnvWithHandlerCfg` with the handler configuration.
pub fn new(cfg_env: CfgEnv, handler_cfg: HandlerCfg) -> Self {
Self {
cfg_env,
handler_cfg,
}
}
/// Returns new `CfgEnvWithHandlerCfg` instance with the chain spec id.
///
/// is_optimism will be set to default value depending on `optimism-default-handler` feature.
pub fn new_with_spec_id(cfg_env: CfgEnv, spec_id: SpecId) -> Self {
Self::new(cfg_env, HandlerCfg::new(spec_id))
}
/// Enables the optimism feature.
#[cfg(feature = "optimism")]
pub fn enable_optimism(&mut self) {
self.handler_cfg.is_optimism = true;
}
}
impl DerefMut for CfgEnvWithHandlerCfg {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.cfg_env
}
}
impl Deref for CfgEnvWithHandlerCfg {
type Target = CfgEnv;
fn deref(&self) -> &Self::Target {
&self.cfg_env
}
}
/// Evm environment with the chain spec id.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct EnvWithHandlerCfg {
/// Evm environment.
pub env: Box<Env>,
/// Handler configuration fields.
pub handler_cfg: HandlerCfg,
}
impl EnvWithHandlerCfg {
/// Returns new `EnvWithHandlerCfg` instance.
pub fn new(env: Box<Env>, handler_cfg: HandlerCfg) -> Self {
Self { env, handler_cfg }
}
/// Returns new `EnvWithHandlerCfg` instance with the chain spec id.
///
/// is_optimism will be set to default value depending on `optimism-default-handler` feature.
pub fn new_with_spec_id(env: Box<Env>, spec_id: SpecId) -> Self {
Self::new(env, HandlerCfg::new(spec_id))
}
/// Takes `CfgEnvWithHandlerCfg` and returns new `EnvWithHandlerCfg` instance.
pub fn new_with_cfg_env(cfg: CfgEnvWithHandlerCfg, block: BlockEnv, tx: TxEnv) -> Self {
Self::new(Env::boxed(cfg.cfg_env, block, tx), cfg.handler_cfg)
}
/// Returns the specification id.
pub const fn spec_id(&self) -> SpecId {
self.handler_cfg.spec_id
}
/// Enables the optimism handle register.
#[cfg(feature = "optimism")]
pub fn enable_optimism(&mut self) {
self.handler_cfg.is_optimism = true;
}
}
impl DerefMut for EnvWithHandlerCfg {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.env
}
}
impl Deref for EnvWithHandlerCfg {
type Target = Env;
fn deref(&self) -> &Self::Target {
&self.env
}
}