aurora_evm/backend/
mod.rs

1//! # EVM backends
2//!
3//! Backends store state information of the VM, and exposes it to runtime.
4use crate::prelude::*;
5use primitive_types::{H160, H256, U256};
6
7pub use self::memory::{MemoryAccount, MemoryBackend, MemoryVicinity};
8
9mod memory;
10
11/// Basic account information.
12///
13#[derive(Clone, Eq, PartialEq, Debug, Default)]
14#[cfg_attr(
15    feature = "with-codec",
16    derive(scale_codec::Encode, scale_codec::Decode, scale_info::TypeInfo)
17)]
18#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
19pub struct Basic {
20    /// Account balance.
21    pub balance: U256,
22    /// Account nonce.
23    pub nonce: U256,
24}
25
26#[derive(Clone, Debug, PartialEq, Eq, Default, rlp::RlpEncodable, rlp::RlpDecodable)]
27#[cfg_attr(
28    feature = "with-codec",
29    derive(scale_codec::Encode, scale_codec::Decode, scale_info::TypeInfo)
30)]
31#[cfg_attr(feature = "with-serde", derive(serde::Serialize, serde::Deserialize))]
32pub struct Log {
33    pub address: H160,
34    pub topics: Vec<H256>,
35    pub data: Vec<u8>,
36}
37
38/// Apply state operation.
39#[derive(Clone, Debug)]
40pub enum Apply<I> {
41    /// Modify or create at address.
42    Modify {
43        /// Address.
44        address: H160,
45        /// Basic information of the address.
46        basic: Basic,
47        /// Code. `None` means leaving it unchanged.
48        code: Option<Vec<u8>>,
49        /// Storage iterator.
50        storage: I,
51        /// Whether storage should be wiped empty before applying the storage
52        /// iterator.
53        reset_storage: bool,
54    },
55    /// Delete address.
56    Delete {
57        /// Address.
58        address: H160,
59    },
60}
61
62/// EVM backend.
63#[auto_impl::auto_impl(&, Arc, Box)]
64pub trait Backend {
65    /// Gas price. Unused for London.
66    fn gas_price(&self) -> U256;
67    /// Origin.
68    fn origin(&self) -> H160;
69    /// Environmental block hash.
70    fn block_hash(&self, number: U256) -> H256;
71    /// Environmental block number.
72    fn block_number(&self) -> U256;
73    /// Environmental coinbase.
74    fn block_coinbase(&self) -> H160;
75    /// Environmental block timestamp.
76    fn block_timestamp(&self) -> U256;
77    /// Environmental block difficulty.
78    fn block_difficulty(&self) -> U256;
79    /// Get environmental block randomness.
80    fn block_randomness(&self) -> Option<H256>;
81    /// Environmental block gas limit.
82    fn block_gas_limit(&self) -> U256;
83    /// Environmental block base fee.
84    fn block_base_fee_per_gas(&self) -> U256;
85    /// Environmental chain ID.
86    fn chain_id(&self) -> U256;
87
88    /// Whether account at address exists.
89    fn exists(&self, address: H160) -> bool;
90    /// Get basic account information.
91    fn basic(&self, address: H160) -> Basic;
92    /// Get account code.
93    fn code(&self, address: H160) -> Vec<u8>;
94    /// Get storage value of address at index.
95    fn storage(&self, address: H160, index: H256) -> H256;
96    /// Check if the storage of the address is empty.
97    fn is_empty_storage(&self, address: H160) -> bool;
98    /// Get original storage value of address at index, if available.
99    fn original_storage(&self, address: H160, index: H256) -> Option<H256>;
100    /// CANCUN hard fork
101    /// [EIP-4844]: Shard Blob Transactions
102    /// [EIP-7516]: BLOBBASEFEE instruction
103    fn blob_gas_price(&self) -> Option<u128>;
104    /// Get `blob_hash` from `blob_versioned_hashes` by index
105    /// [EIP-4844]: BLOBHASH - https://eips.ethereum.org/EIPS/eip-4844#opcode-to-get-versioned-hashes
106    fn get_blob_hash(&self, index: usize) -> Option<U256>;
107}
108
109/// EVM backend that can apply changes.
110pub trait ApplyBackend {
111    /// Apply given values and logs at backend.
112    fn apply<A, I, L>(&mut self, values: A, logs: L, delete_empty: bool)
113    where
114        A: IntoIterator<Item = Apply<I>>,
115        I: IntoIterator<Item = (H256, H256)>,
116        L: IntoIterator<Item = Log>;
117}