evm_nogas/backend/
mod.rs

1//! # EVM backends
2//!
3//! Backends store state information of the VM, and exposes it to runtime.
4
5mod memory;
6
7pub use self::memory::{MemoryAccount, MemoryBackend, MemoryVicinity};
8
9use alloc::vec::Vec;
10use primitive_types::{H160, H256, U256};
11
12/// Basic account information.
13#[derive(Clone, Eq, PartialEq, Debug, Default)]
14#[cfg_attr(
15    feature = "with-codec",
16    derive(codec::Encode, 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
26pub use ethereum::Log;
27
28/// Apply state operation.
29#[derive(Clone, Debug)]
30pub enum Apply<I> {
31    /// Modify or create at address.
32    Modify {
33        /// Address.
34        address: H160,
35        /// Basic information of the address.
36        basic: Basic,
37        /// Code. `None` means leaving it unchanged.
38        code: Option<Vec<u8>>,
39        /// Storage iterator.
40        storage: I,
41        /// Whether storage should be wiped empty before applying the storage
42        /// iterator.
43        reset_storage: bool,
44    },
45    /// Delete address.
46    Delete {
47        /// Address.
48        address: H160,
49    },
50}
51
52/// EVM backend.
53#[auto_impl::auto_impl(&, Arc, Box)]
54pub trait Backend {
55    /// Gas price. Unused for London.
56    fn gas_price(&self) -> U256;
57    /// Ges left
58    fn gas_left(&self) -> U256;
59    /// Origin.
60    fn origin(&self) -> H160;
61    /// Environmental block hash.
62    fn block_hash(&self, number: U256) -> H256;
63    /// Environmental block number.
64    fn block_number(&self) -> U256;
65    /// Environmental coinbase.
66    fn block_coinbase(&self) -> H160;
67    /// Environmental block timestamp.
68    fn block_timestamp(&self) -> U256;
69    /// Environmental block difficulty.
70    fn block_difficulty(&self) -> U256;
71    /// Environmental block gas limit.
72    fn block_gas_limit(&self) -> U256;
73    /// Environmental block base fee.
74    fn block_base_fee_per_gas(&self) -> U256;
75    /// Environmental chain ID.
76    fn chain_id(&self) -> U256;
77
78    /// Whether account at address exists.
79    fn exists(&self, address: H160) -> bool;
80    /// Get basic account information.
81    fn basic(&self, address: H160) -> Basic;
82    /// Get account code.
83    fn code(&self, address: H160) -> Vec<u8>;
84    /// Get storage value of address at index.
85    fn storage(&self, address: H160, index: H256) -> H256;
86    /// Get original storage value of address at index, if available.
87    fn original_storage(&self, address: H160, index: H256) -> Option<H256>;
88}
89
90/// EVM backend that can apply changes.
91pub trait ApplyBackend {
92    /// Apply given values and logs at backend.
93    fn apply<A, I, L>(&mut self, values: A, logs: L, delete_empty: bool)
94    where
95        A: IntoIterator<Item = Apply<I>>,
96        I: IntoIterator<Item = (H256, H256)>,
97        L: IntoIterator<Item = Log>;
98}