Skip to content

Commit 603e39a

Browse files
authored
evm: unit tests for evm_with_inspector and evm_with_env_and_inspector (#10253)
1 parent 532ea9d commit 603e39a

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

crates/ethereum/evm/src/lib.rs

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ mod tests {
131131
};
132132
use reth_revm::{
133133
db::{CacheDB, EmptyDBTyped},
134+
inspectors::NoOpInspector,
134135
JournaledState,
135136
};
136137
use revm_primitives::{CfgEnvWithHandlerCfg, EnvWithHandlerCfg, HandlerCfg};
@@ -302,4 +303,127 @@ mod tests {
302303
// Check that the spec ID is setup properly
303304
assert_eq!(evm.handler.spec_id(), SpecId::CONSTANTINOPLE);
304305
}
306+
307+
#[test]
308+
fn test_evm_with_inspector() {
309+
let evm_config = EthEvmConfig::default();
310+
311+
let db = CacheDB::<EmptyDBTyped<ProviderError>>::default();
312+
313+
// No operation inspector
314+
let noop = NoOpInspector;
315+
316+
let evm = evm_config.evm_with_inspector(db, noop);
317+
318+
// Check that the inspector is set correctly
319+
assert_eq!(evm.context.external, noop);
320+
321+
// Check that the EVM environment is initialized with default values
322+
assert_eq!(evm.context.evm.inner.env, Box::default());
323+
324+
// Latest spec ID and no warm preloaded addresses
325+
assert_eq!(
326+
evm.context.evm.inner.journaled_state,
327+
JournaledState::new(SpecId::LATEST, HashSet::default())
328+
);
329+
330+
// Ensure that the accounts database is empty
331+
assert!(evm.context.evm.inner.db.accounts.is_empty());
332+
333+
// Ensure that the block hashes database is empty
334+
assert!(evm.context.evm.inner.db.block_hashes.is_empty());
335+
336+
// Verify that there are two default contracts in the contracts database
337+
assert_eq!(evm.context.evm.inner.db.contracts.len(), 2);
338+
assert!(evm.context.evm.inner.db.contracts.contains_key(&KECCAK_EMPTY));
339+
assert!(evm.context.evm.inner.db.contracts.contains_key(&B256::ZERO));
340+
341+
// Ensure that the logs database is empty
342+
assert!(evm.context.evm.inner.db.logs.is_empty());
343+
344+
// Ensure that there are no valid authorizations in the EVM context
345+
assert!(evm.context.evm.inner.valid_authorizations.is_empty());
346+
}
347+
348+
#[test]
349+
fn test_evm_with_env_and_default_inspector() {
350+
let evm_config = EthEvmConfig::default();
351+
let db = CacheDB::<EmptyDBTyped<ProviderError>>::default();
352+
353+
let env_with_handler = EnvWithHandlerCfg::default();
354+
355+
let evm =
356+
evm_config.evm_with_env_and_inspector(db, env_with_handler.clone(), NoOpInspector);
357+
358+
// Check that the EVM environment is set to default values
359+
assert_eq!(evm.context.evm.env, env_with_handler.env);
360+
assert_eq!(evm.context.external, NoOpInspector);
361+
assert_eq!(evm.handler.spec_id(), SpecId::LATEST);
362+
}
363+
364+
#[test]
365+
fn test_evm_with_env_inspector_and_custom_cfg() {
366+
let evm_config = EthEvmConfig::default();
367+
let db = CacheDB::<EmptyDBTyped<ProviderError>>::default();
368+
369+
let cfg = CfgEnv::default().with_chain_id(111);
370+
let block = BlockEnv::default();
371+
let tx = TxEnv::default();
372+
let env_with_handler = EnvWithHandlerCfg {
373+
env: Box::new(Env { cfg: cfg.clone(), block, tx }),
374+
handler_cfg: Default::default(),
375+
};
376+
377+
let evm = evm_config.evm_with_env_and_inspector(db, env_with_handler, NoOpInspector);
378+
379+
// Check that the EVM environment is set with custom configuration
380+
assert_eq!(evm.context.evm.env.cfg, cfg);
381+
assert_eq!(evm.context.external, NoOpInspector);
382+
assert_eq!(evm.handler.spec_id(), SpecId::LATEST);
383+
}
384+
385+
#[test]
386+
fn test_evm_with_env_inspector_and_custom_block_tx() {
387+
let evm_config = EthEvmConfig::default();
388+
let db = CacheDB::<EmptyDBTyped<ProviderError>>::default();
389+
390+
// Create custom block and tx environment
391+
let block = BlockEnv {
392+
basefee: U256::from(1000),
393+
gas_limit: U256::from(10_000_000),
394+
number: U256::from(42),
395+
..Default::default()
396+
};
397+
let tx = TxEnv { gas_limit: 5_000_000, gas_price: U256::from(50), ..Default::default() };
398+
let env_with_handler = EnvWithHandlerCfg {
399+
env: Box::new(Env { cfg: CfgEnv::default(), block, tx }),
400+
handler_cfg: Default::default(),
401+
};
402+
403+
let evm =
404+
evm_config.evm_with_env_and_inspector(db, env_with_handler.clone(), NoOpInspector);
405+
406+
// Verify that the block and transaction environments are set correctly
407+
assert_eq!(evm.context.evm.env.block, env_with_handler.env.block);
408+
assert_eq!(evm.context.evm.env.tx, env_with_handler.env.tx);
409+
assert_eq!(evm.context.external, NoOpInspector);
410+
assert_eq!(evm.handler.spec_id(), SpecId::LATEST);
411+
}
412+
413+
#[test]
414+
fn test_evm_with_env_inspector_and_spec_id() {
415+
let evm_config = EthEvmConfig::default();
416+
let db = CacheDB::<EmptyDBTyped<ProviderError>>::default();
417+
418+
let handler_cfg = HandlerCfg { spec_id: SpecId::CONSTANTINOPLE, ..Default::default() };
419+
let env_with_handler = EnvWithHandlerCfg { env: Box::new(Env::default()), handler_cfg };
420+
421+
let evm =
422+
evm_config.evm_with_env_and_inspector(db, env_with_handler.clone(), NoOpInspector);
423+
424+
// Check that the spec ID is set properly
425+
assert_eq!(evm.handler.spec_id(), SpecId::CONSTANTINOPLE);
426+
assert_eq!(evm.context.evm.env, env_with_handler.env);
427+
assert_eq!(evm.context.external, NoOpInspector);
428+
}
305429
}

0 commit comments

Comments
 (0)