revmc_examples_runner/
lib.rs

1//! Minimal, `no_std` runner.
2
3#![no_std]
4
5extern crate alloc;
6
7// This dependency is needed to define the necessary symbols used by the compiled bytecodes,
8// but we don't use it directly, so silence the unused crate dependency warning.
9use revmc_builtins as _;
10
11use revm::{
12    context::{BlockEnv, CfgEnv, Context, Journal, TxEnv},
13    database_interface::Database,
14    handler::MainBuilder,
15    primitives::{hardfork::SpecId, hex, B256},
16    MainnetEvm,
17};
18use revmc_context::EvmCompilerFn;
19
20include!("./common.rs");
21
22// The bytecode we statically linked.
23revmc_context::extern_revmc! {
24    fn fibonacci;
25}
26
27/// External context for tracking compiled functions.
28#[derive(Clone, Default)]
29pub struct ExternalContext;
30
31impl ExternalContext {
32    /// Creates a new external context.
33    pub fn new() -> Self {
34        Self
35    }
36
37    /// Get a compiled function for the given bytecode hash.
38    pub fn get_function(&self, bytecode_hash: B256) -> Option<EvmCompilerFn> {
39        // Can use any mapping between bytecode hash and function.
40        if bytecode_hash == B256::from(FIBONACCI_HASH) {
41            return Some(EvmCompilerFn::new(fibonacci));
42        }
43        None
44    }
45}
46
47/// Type alias for mainnet context with custom database.
48pub type MainnetContext<DB> = Context<BlockEnv, TxEnv, CfgEnv, DB, Journal<DB>, ()>;
49
50/// Build a mainnet EVM.
51///
52/// Note: In revm v34, the frame execution is handled differently.
53/// For now, this returns a standard mainnet EVM. To integrate compiled
54/// bytecode, you would need to customize the instruction handler or
55/// use the revmc-context's call_with_interpreter method directly.
56pub fn build_evm<DB: Database>(db: DB) -> MainnetEvm<MainnetContext<DB>> {
57    Context::<BlockEnv, TxEnv, CfgEnv, DB, Journal<DB>, ()>::new(db, SpecId::CANCUN).build_mainnet()
58}