revm_context/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 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
pub use context_interface::Cfg;
use interpreter::MAX_CODE_SIZE;
use specification::hardfork::SpecId;
use std::{vec, vec::Vec};
/// EVM configuration
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub struct CfgEnv<SPEC: Into<SpecId> = SpecId> {
/// Chain ID of the EVM
///
/// `chain_id` will be compared to the transaction's Chain ID.
///
/// Chain ID is introduced EIP-155.
pub chain_id: u64,
/// Specification for EVM represent the hardfork
pub spec: SPEC,
/// If some it will effects EIP-170: Contract code size limit.
///
/// Useful to increase this because of tests.
///
/// By default it is `0x6000` (~25kb).
pub limit_contract_code_size: Option<usize>,
/// Skips the nonce validation against the account's nonce
pub disable_nonce_check: bool,
/// Blob target count. EIP-7840 Add blob schedule to EL config files.
///
/// Note : Items must be sorted by `SpecId`.
pub blob_target_and_max_count: Vec<(SpecId, u8, u8)>,
/// A hard memory limit in bytes beyond which
/// [OutOfGasError::Memory][context_interface::result::OutOfGasError::Memory] cannot be resized.
///
/// In cases where the gas limit may be extraordinarily high, it is recommended to set this to
/// a sane value to prevent memory allocation panics.
///
/// Defaults to `2^32 - 1` bytes per EIP-1985.
#[cfg(feature = "memory_limit")]
pub memory_limit: u64,
/// Skip balance checks if `true`
///
/// Adds transaction cost to balance to ensure execution doesn't fail.
///
/// By default, it is set to `false`.
#[cfg(feature = "optional_balance_check")]
pub disable_balance_check: bool,
/// There are use cases where it's allowed to provide a gas limit that's higher than a block's gas limit.
///
/// To that end, you can disable the block gas limit validation.
///
/// By default, it is set to `false`.
#[cfg(feature = "optional_block_gas_limit")]
pub disable_block_gas_limit: bool,
/// EIP-3607 rejects transactions from senders with deployed code
///
/// In development, it can be desirable to simulate calls from contracts, which this setting allows.
///
/// By default, it is set to `false`.
#[cfg(feature = "optional_eip3607")]
pub disable_eip3607: bool,
/// Disables all gas refunds
///
/// This is useful when using chains that have gas refunds disabled, e.g. Avalanche.
///
/// Reasoning behind removing gas refunds can be found in EIP-3298.
///
/// By default, it is set to `false`.
#[cfg(feature = "optional_gas_refund")]
pub disable_gas_refund: bool,
/// Disables base fee checks for EIP-1559 transactions
///
/// This is useful for testing method calls with zero gas price.
///
/// By default, it is set to `false`.
#[cfg(feature = "optional_no_base_fee")]
pub disable_base_fee: bool,
}
impl CfgEnv {
pub fn with_chain_id(mut self, chain_id: u64) -> Self {
self.chain_id = chain_id;
self
}
/// Sets the blob target and max count over hardforks.
pub fn set_blob_max_and_target_count(&mut self, mut vec: Vec<(SpecId, u8, u8)>) {
vec.sort_by_key(|(id, _, _)| *id);
self.blob_target_and_max_count = vec;
}
}
impl<SPEC: Into<SpecId> + Copy> Cfg for CfgEnv<SPEC> {
type Spec = SPEC;
fn chain_id(&self) -> u64 {
self.chain_id
}
fn spec(&self) -> Self::Spec {
self.spec
}
#[inline]
fn blob_max_count(&self, spec_id: SpecId) -> u8 {
self.blob_target_and_max_count
.iter()
.rev()
.find_map(|(id, _, max)| {
if spec_id as u8 >= *id as u8 {
return Some(*max);
}
None
})
.unwrap_or(6)
}
fn max_code_size(&self) -> usize {
self.limit_contract_code_size.unwrap_or(MAX_CODE_SIZE)
}
fn is_eip3607_disabled(&self) -> bool {
cfg_if::cfg_if! {
if #[cfg(feature = "optional_eip3607")] {
self.disable_eip3607
} else {
false
}
}
}
fn is_balance_check_disabled(&self) -> bool {
cfg_if::cfg_if! {
if #[cfg(feature = "optional_balance_check")] {
self.disable_balance_check
} else {
false
}
}
}
fn is_gas_refund_disabled(&self) -> bool {
cfg_if::cfg_if! {
if #[cfg(feature = "optional_gas_refund")] {
self.disable_gas_refund
} else {
false
}
}
}
fn is_block_gas_limit_disabled(&self) -> bool {
cfg_if::cfg_if! {
if #[cfg(feature = "optional_block_gas_limit")] {
self.disable_block_gas_limit
} else {
false
}
}
}
fn is_nonce_check_disabled(&self) -> bool {
self.disable_nonce_check
}
fn is_base_fee_check_disabled(&self) -> bool {
cfg_if::cfg_if! {
if #[cfg(feature = "optional_no_base_fee")] {
self.disable_base_fee
} else {
false
}
}
}
}
impl Default for CfgEnv {
fn default() -> Self {
Self {
chain_id: 1,
limit_contract_code_size: None,
spec: SpecId::PRAGUE,
disable_nonce_check: false,
blob_target_and_max_count: vec![(SpecId::CANCUN, 3, 6), (SpecId::PRAGUE, 6, 9)],
#[cfg(feature = "memory_limit")]
memory_limit: (1 << 32) - 1,
#[cfg(feature = "optional_balance_check")]
disable_balance_check: false,
#[cfg(feature = "optional_block_gas_limit")]
disable_block_gas_limit: false,
#[cfg(feature = "optional_eip3607")]
disable_eip3607: false,
#[cfg(feature = "optional_gas_refund")]
disable_gas_refund: false,
#[cfg(feature = "optional_no_base_fee")]
disable_base_fee: false,
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn blob_max_and_target_count() {
let cfg = CfgEnv::default();
assert_eq!(cfg.blob_max_count(SpecId::BERLIN), (6));
assert_eq!(cfg.blob_max_count(SpecId::CANCUN), (6));
assert_eq!(cfg.blob_max_count(SpecId::PRAGUE), (9));
assert_eq!(cfg.blob_max_count(SpecId::OSAKA), (9));
}
}