Skip to main content

revmc_examples_runner/
lib.rs

1//! Minimal, `no_std` runner that hooks a statically compiled bytecode into a revm mainnet EVM.
2
3#![no_std]
4#![cfg_attr(not(test), warn(unused_extern_crates))]
5#![cfg_attr(docsrs, feature(doc_cfg))]
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_context::{BlockEnv, CfgEnv, Context, Journal, TxEnv};
12use revm_database_interface::Database;
13use revm_handler::{MainBuilder, MainnetEvm};
14use revm_primitives::{B256, hardfork::SpecId, hex, map::B256Map};
15use revmc_context::{JitEvm, RawEvmCompilerFn};
16
17include!("./common.rs");
18
19// The bytecode we statically linked.
20revmc_context::extern_revmc! {
21    fn fibonacci;
22}
23
24/// Type alias for mainnet context with custom database.
25pub type MainnetContext<DB> = Context<BlockEnv, TxEnv, CfgEnv, DB, Journal<DB>, ()>;
26
27/// Build a mainnet EVM wrapped in [`JitEvm`] so that calls to known code hashes
28/// are dispatched to compiled functions instead of the interpreter.
29pub fn build_evm<DB: Database>(db: DB) -> JitEvm<MainnetEvm<MainnetContext<DB>>> {
30    let inner = Context::<BlockEnv, TxEnv, CfgEnv, DB, Journal<DB>, ()>::new(db, SpecId::CANCUN)
31        .build_mainnet();
32
33    let mut functions = B256Map::default();
34    functions.insert(B256::from(FIBONACCI_HASH), fibonacci as RawEvmCompilerFn);
35
36    JitEvm::new(inner, functions)
37}