evm_runtime/
handler.rs

1use crate::{Capture, Context, CreateScheme, ExitError, ExitReason, Machine, Opcode, Stack};
2use alloc::vec::Vec;
3use primitive_types::{H160, H256, U256};
4
5/// Transfer from source to target, with given value.
6#[derive(Clone, Debug)]
7pub struct Transfer {
8	/// Source address.
9	pub source: H160,
10	/// Target address.
11	pub target: H160,
12	/// Transfer value.
13	pub value: U256,
14}
15
16/// EVM context handler.
17#[auto_impl::auto_impl(&mut, Box)]
18pub trait Handler {
19	/// Type of `CREATE` interrupt.
20	type CreateInterrupt;
21	/// Feedback value for `CREATE` interrupt.
22	type CreateFeedback;
23	/// Type of `CALL` interrupt.
24	type CallInterrupt;
25	/// Feedback value of `CALL` interrupt.
26	type CallFeedback;
27
28	/// Get balance of address.
29	fn balance(&self, address: H160) -> U256;
30	/// Get code size of address.
31	fn code_size(&self, address: H160) -> U256;
32	/// Get code hash of address.
33	fn code_hash(&self, address: H160) -> H256;
34	/// Get code of address.
35	fn code(&self, address: H160) -> Vec<u8>;
36	/// Get storage value of address at index.
37	fn storage(&self, address: H160, index: H256) -> H256;
38	/// Get transient storage value of address at index.
39	fn transient_storage(&self, address: H160, index: H256) -> H256;
40
41	/// Get original storage value of address at index.
42	fn original_storage(&self, address: H160, index: H256) -> H256;
43
44	/// Get the gas left value.
45	fn gas_left(&self) -> U256;
46	/// Get the gas price value.
47	fn gas_price(&self) -> U256;
48	/// Get execution origin.
49	fn origin(&self) -> H160;
50	/// Get environmental block hash.
51	fn block_hash(&self, number: U256) -> H256;
52	/// Get environmental block number.
53	fn block_number(&self) -> U256;
54	/// Get environmental coinbase.
55	fn block_coinbase(&self) -> H160;
56	/// Get environmental block timestamp.
57	fn block_timestamp(&self) -> U256;
58	/// Get environmental block difficulty.
59	fn block_difficulty(&self) -> U256;
60	/// Get environmental block randomness.
61	fn block_randomness(&self) -> Option<H256>;
62	/// Get environmental gas limit.
63	fn block_gas_limit(&self) -> U256;
64	/// Environmental block base fee.
65	fn block_base_fee_per_gas(&self) -> U256;
66	/// Get environmental chain ID.
67	fn chain_id(&self) -> U256;
68
69	/// Check whether an address exists.
70	fn exists(&self, address: H160) -> bool;
71	/// Check whether an address has already been deleted.
72	fn deleted(&self, address: H160) -> bool;
73	/// Checks if the address or (address, index) pair has been previously accessed
74	/// (or set in `accessed_addresses` / `accessed_storage_keys` via an access list
75	/// transaction).
76	/// References:
77	/// * <https://eips.ethereum.org/EIPS/eip-2929>
78	/// * <https://eips.ethereum.org/EIPS/eip-2930>
79	fn is_cold(&mut self, address: H160, index: Option<H256>) -> Result<bool, ExitError>;
80
81	/// Set storage value of address at index.
82	fn set_storage(&mut self, address: H160, index: H256, value: H256) -> Result<(), ExitError>;
83	/// Set transient storage value of address at index, transient storage gets discarded after every transaction. (see EIP-1153)
84	fn set_transient_storage(&mut self, address: H160, index: H256, value: H256);
85	/// Create a log owned by address with given topics and data.
86	fn log(&mut self, address: H160, topics: Vec<H256>, data: Vec<u8>) -> Result<(), ExitError>;
87	/// Mark an address to be deleted, with funds transferred to target.
88	fn mark_delete(&mut self, address: H160, target: H160) -> Result<(), ExitError>;
89	/// Invoke a create operation.
90	fn create(
91		&mut self,
92		caller: H160,
93		scheme: CreateScheme,
94		value: U256,
95		init_code: Vec<u8>,
96		target_gas: Option<u64>,
97	) -> Capture<(ExitReason, Option<H160>, Vec<u8>), Self::CreateInterrupt>;
98	/// Feed in create feedback.
99	fn create_feedback(&mut self, _feedback: Self::CreateFeedback) -> Result<(), ExitError> {
100		Ok(())
101	}
102	/// Invoke a call operation.
103	fn call(
104		&mut self,
105		code_address: H160,
106		transfer: Option<Transfer>,
107		input: Vec<u8>,
108		target_gas: Option<u64>,
109		is_static: bool,
110		context: Context,
111	) -> Capture<(ExitReason, Vec<u8>), Self::CallInterrupt>;
112	/// Feed in call feedback.
113	fn call_feedback(&mut self, _feedback: Self::CallFeedback) -> Result<(), ExitError> {
114		Ok(())
115	}
116
117	/// Pre-validation step for the runtime.
118	fn pre_validate(
119		&mut self,
120		context: &Context,
121		opcode: Opcode,
122		stack: &Stack,
123	) -> Result<(), ExitError>;
124	/// Handle other unknown external opcodes.
125	fn other(&mut self, opcode: Opcode, _stack: &mut Machine) -> Result<(), ExitError> {
126		Err(ExitError::InvalidCode(opcode))
127	}
128
129	/// Records some associated `ExternalOperation`.
130	fn record_external_operation(&mut self, op: crate::ExternalOperation) -> Result<(), ExitError>;
131}