Skip to main content

revm_primitives/
lib.rs

1//! # revm-primitives
2//!
3//! Core primitive types and constants for the Ethereum Virtual Machine (EVM) implementation.
4//!
5//! This crate provides:
6//! - EVM constants and limits (gas, stack, code size)
7//! - Ethereum hard fork management and version control
8//! - EIP-specific constants and configuration values
9//! - Cross-platform synchronization primitives
10//! - Type aliases for common EVM concepts (storage keys/values)
11//! - Re-exports of alloy primitive types for convenience
12#![cfg_attr(not(test), warn(unused_crate_dependencies))]
13#![cfg_attr(not(feature = "std"), no_std)]
14
15#[cfg(not(feature = "std"))]
16extern crate alloc as std;
17
18pub mod constants;
19pub mod eip170;
20pub mod eip2780;
21pub mod eip3860;
22pub mod eip4844;
23pub mod eip7702;
24pub mod eip7708;
25pub mod eip7823;
26pub mod eip7825;
27pub mod eip7907;
28pub mod eip7954;
29pub mod eip8037;
30pub mod eip8038;
31pub mod hardfork;
32pub mod hints_util;
33mod once_lock;
34
35pub use constants::*;
36pub use once_lock::OnceLock;
37
38// Reexport alloy primitives.
39
40pub use alloy_primitives::{
41    self, address, b256, bytes, fixed_bytes, hex, hex_literal, keccak256,
42    map::{
43        self, hash_map, hash_set, indexmap, AddressIndexMap, AddressMap, AddressSet, B256Map,
44        HashMap, HashSet, IndexMap, U256Map,
45    },
46    ruint, uint, Address, Bytes, FixedBytes, Log, LogData, TxKind, B256, I128, I256, U128, U256,
47};
48
49/// Type alias for EVM storage keys (256-bit unsigned integers).
50/// Used to identify storage slots within smart contract storage.
51pub type StorageKey = U256;
52
53/// Type alias for EVM storage values (256-bit unsigned integers).
54/// Used to store data values in smart contract storage slots.
55pub type StorageValue = U256;
56
57/// Type alias for a map with storage keys (U256) as keys.
58pub type StorageKeyMap<V> = U256Map<V>;
59
60/// Optimize short address access.
61pub const SHORT_ADDRESS_CAP: usize = 300;
62
63/// Returns the short address from Address.
64///
65/// Short address is considered address that has 18 leading zeros
66/// and last two bytes are less than [`SHORT_ADDRESS_CAP`].
67#[inline]
68pub fn short_address(address: &Address) -> Option<usize> {
69    let (zeros, value) = address.split_at(18);
70    if zeros.iter().all(|b| *b == 0) {
71        let short_address = u16::from_be_bytes([value[0], value[1]]) as usize;
72        if short_address < SHORT_ADDRESS_CAP {
73            return Some(short_address);
74        }
75    }
76    None
77}
78
79/// 1 ether = 10^18 wei
80pub const ONE_ETHER: u128 = 1_000_000_000_000_000_000;
81
82/// 1 gwei = 10^9 wei
83pub const ONE_GWEI: u128 = 1_000_000_000;