-
Notifications
You must be signed in to change notification settings - Fork 644
/
Copy pathcontext.rs
177 lines (153 loc) · 4.67 KB
/
context.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
mod context_precompiles;
pub(crate) mod evm_context;
mod inner_evm_context;
pub use context_precompiles::{
ContextPrecompile, ContextPrecompiles, ContextStatefulPrecompile, ContextStatefulPrecompileArc,
ContextStatefulPrecompileBox, ContextStatefulPrecompileMut,
};
pub use evm_context::EvmContext;
pub use inner_evm_context::InnerEvmContext;
use crate::{
db::{Database, EmptyDB},
interpreter::{Host, LoadAccountResult, SStoreResult, SelfDestructResult},
primitives::{Address, Bytecode, Env, HandlerCfg, Log, B256, U256},
};
use std::boxed::Box;
/// Main Context structure that contains both EvmContext and External context.
pub struct Context<EXT, DB: Database> {
/// Evm Context (internal context).
pub evm: EvmContext<DB>,
/// External contexts.
pub external: EXT,
}
impl<EXT: Clone, DB: Database + Clone> Clone for Context<EXT, DB>
where
DB::Error: Clone,
{
fn clone(&self) -> Self {
Self {
evm: self.evm.clone(),
external: self.external.clone(),
}
}
}
impl Default for Context<(), EmptyDB> {
fn default() -> Self {
Self::new_empty()
}
}
impl Context<(), EmptyDB> {
/// Creates empty context. This is useful for testing.
pub fn new_empty() -> Context<(), EmptyDB> {
Context {
evm: EvmContext::new(EmptyDB::new()),
external: (),
}
}
}
impl<DB: Database> Context<(), DB> {
/// Creates new context with database.
pub fn new_with_db(db: DB) -> Context<(), DB> {
Context {
evm: EvmContext::new_with_env(db, Box::default()),
external: (),
}
}
}
impl<EXT, DB: Database> Context<EXT, DB> {
/// Creates new context with external and database.
pub fn new(evm: EvmContext<DB>, external: EXT) -> Context<EXT, DB> {
Context { evm, external }
}
}
/// Context with handler configuration.
pub struct ContextWithHandlerCfg<EXT, DB: Database> {
/// Context of execution.
pub context: Context<EXT, DB>,
/// Handler configuration.
pub cfg: HandlerCfg,
}
impl<EXT, DB: Database> ContextWithHandlerCfg<EXT, DB> {
/// Creates new context with handler configuration.
pub fn new(context: Context<EXT, DB>, cfg: HandlerCfg) -> Self {
Self { cfg, context }
}
}
impl<EXT: Clone, DB: Database + Clone> Clone for ContextWithHandlerCfg<EXT, DB>
where
DB::Error: Clone,
{
fn clone(&self) -> Self {
Self {
context: self.context.clone(),
cfg: self.cfg,
}
}
}
impl<EXT, DB: Database> Host for Context<EXT, DB> {
fn env(&self) -> &Env {
&self.evm.env
}
fn env_mut(&mut self) -> &mut Env {
&mut self.evm.env
}
fn block_hash(&mut self, number: U256) -> Option<B256> {
self.evm
.block_hash(number)
.map_err(|e| self.evm.error = Err(e))
.ok()
}
fn load_account(&mut self, address: Address) -> Option<LoadAccountResult> {
self.evm
.load_account_exist(address)
.map_err(|e| self.evm.error = Err(e))
.ok()
}
fn balance(&mut self, address: Address) -> Option<(U256, bool)> {
self.evm
.balance(address)
.map_err(|e| self.evm.error = Err(e))
.ok()
}
fn code(&mut self, address: Address) -> Option<(Bytecode, bool)> {
self.evm
.code(address)
.map_err(|e| self.evm.error = Err(e))
.ok()
}
fn code_hash(&mut self, address: Address) -> Option<(B256, bool)> {
self.evm
.code_hash(address)
.map_err(|e| self.evm.error = Err(e))
.ok()
}
fn sload(&mut self, address: Address, index: U256) -> Option<(U256, bool)> {
self.evm
.sload(address, index)
.map_err(|e| self.evm.error = Err(e))
.ok()
}
fn sstore(&mut self, address: Address, index: U256, value: U256) -> Option<SStoreResult> {
self.evm
.sstore(address, index, value)
.map_err(|e| self.evm.error = Err(e))
.ok()
}
fn tload(&mut self, address: Address, index: U256) -> U256 {
self.evm.tload(address, index)
}
fn tstore(&mut self, address: Address, index: U256, value: U256) {
self.evm.tstore(address, index, value)
}
fn log(&mut self, log: Log) {
self.evm.journaled_state.log(log);
}
fn selfdestruct(&mut self, address: Address, target: Address) -> Option<SelfDestructResult> {
self.evm
.inner
.journaled_state
.selfdestruct(address, target, &mut self.evm.inner.db)
.map_err(|e| self.evm.error = Err(e))
.ok()
}
}