revm_context_interface/
journaled_state.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
use core::ops::{Deref, DerefMut};
use database_interface::{Database, DatabaseGetter};
use primitives::{Address, B256, U256};
use specification::hardfork::SpecId;
use state::{Account, Bytecode};
use std::boxed::Box;

pub trait JournaledState {
    type Database: Database;
    type FinalOutput;

    fn warm_account_and_storage(
        &mut self,
        address: Address,
        storage_keys: impl IntoIterator<Item = U256>,
    ) -> Result<(), <Self::Database as Database>::Error>;

    fn warm_account(&mut self, address: Address);

    fn set_spec_id(&mut self, spec_id: SpecId);

    fn touch_account(&mut self, address: Address);

    /// TODO instruction result is not known
    fn transfer(
        &mut self,
        from: &Address,
        to: &Address,
        balance: U256,
    ) -> Result<Option<TransferError>, <Self::Database as Database>::Error>;

    fn inc_account_nonce(
        &mut self,
        address: Address,
    ) -> Result<Option<u64>, <Self::Database as Database>::Error>;

    fn load_account(
        &mut self,
        address: Address,
    ) -> Result<StateLoad<&mut Account>, <Self::Database as Database>::Error>;

    fn load_account_code(
        &mut self,
        address: Address,
    ) -> Result<StateLoad<&mut Account>, <Self::Database as Database>::Error>;

    fn load_account_delegated(
        &mut self,
        address: Address,
    ) -> Result<AccountLoad, <Self::Database as Database>::Error>;

    /// Set bytecode with hash. Assume that account is warm.
    fn set_code_with_hash(&mut self, address: Address, code: Bytecode, hash: B256);

    /// Assume account is warm
    #[inline]
    fn set_code(&mut self, address: Address, code: Bytecode) {
        let hash = code.hash_slow();
        self.set_code_with_hash(address, code, hash);
    }

    /// Called at the end of the transaction to clean all residue data from journal.
    fn clear(&mut self);

    fn checkpoint(&mut self) -> JournalCheckpoint;

    fn checkpoint_commit(&mut self);

    fn checkpoint_revert(&mut self, checkpoint: JournalCheckpoint);

    fn create_account_checkpoint(
        &mut self,
        caller: Address,
        address: Address,
        balance: U256,
        spec_id: SpecId,
    ) -> Result<JournalCheckpoint, TransferError>;

    fn depth(&self) -> usize;

    /// Does cleanup and returns modified state.
    ///
    /// This resets the [JournaledState] to its initial state.
    fn finalize(&mut self) -> Result<Self::FinalOutput, <Self::Database as Database>::Error>;
}

/// Transfer and creation result.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum TransferError {
    /// Caller does not have enough funds
    OutOfFunds,
    /// Overflow in target account.
    OverflowPayment,
    /// Create collision.
    CreateCollision,
}

/// SubRoutine checkpoint that will help us to go back from this
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct JournalCheckpoint {
    pub log_i: usize,
    pub journal_i: usize,
}

/// State load information that contains the data and if the account or storage is cold loaded.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct StateLoad<T> {
    /// returned data
    pub data: T,
    /// True if account is cold loaded.
    pub is_cold: bool,
}

impl<T> Deref for StateLoad<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.data
    }
}

impl<T> DerefMut for StateLoad<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.data
    }
}

impl<T> StateLoad<T> {
    /// Returns a new [`StateLoad`] with the given data and cold load status.
    pub fn new(data: T, is_cold: bool) -> Self {
        Self { data, is_cold }
    }

    /// Maps the data of the [`StateLoad`] to a new value.
    ///
    /// Useful for transforming the data of the [`StateLoad`] without changing the cold load status.
    pub fn map<B, F>(self, f: F) -> StateLoad<B>
    where
        F: FnOnce(T) -> B,
    {
        StateLoad::new(f(self.data), self.is_cold)
    }
}

/// Result of the account load from Journal state.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AccountLoad {
    /// Is account and delegate code are loaded
    pub load: Eip7702CodeLoad<()>,
    /// Is account empty, if true account is not created.
    pub is_empty: bool,
}

impl Deref for AccountLoad {
    type Target = Eip7702CodeLoad<()>;

    fn deref(&self) -> &Self::Target {
        &self.load
    }
}

impl DerefMut for AccountLoad {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.load
    }
}

/// EIP-7702 code load result that contains optional delegation is_cold information.
///
/// [`Self::is_delegate_account_cold`] will be [`Some`] if account has delegation.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Eip7702CodeLoad<T> {
    /// returned data
    pub state_load: StateLoad<T>,
    /// True if account has delegate code and delegated account is cold loaded.
    pub is_delegate_account_cold: Option<bool>,
}

impl<T> Deref for Eip7702CodeLoad<T> {
    type Target = StateLoad<T>;

    fn deref(&self) -> &Self::Target {
        &self.state_load
    }
}

impl<T> DerefMut for Eip7702CodeLoad<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.state_load
    }
}

impl<T> Eip7702CodeLoad<T> {
    /// Returns a new [`Eip7702CodeLoad`] with the given data and without delegation.
    pub fn new_state_load(state_load: StateLoad<T>) -> Self {
        Self {
            state_load,
            is_delegate_account_cold: None,
        }
    }

    /// Returns a new [`Eip7702CodeLoad`] with the given data and without delegation.
    pub fn new_not_delegated(data: T, is_cold: bool) -> Self {
        Self {
            state_load: StateLoad::new(data, is_cold),
            is_delegate_account_cold: None,
        }
    }

    /// Deconstructs the [`Eip7702CodeLoad`] by extracting data and
    /// returning a new [`Eip7702CodeLoad`] with empty data.
    pub fn into_components(self) -> (T, Eip7702CodeLoad<()>) {
        let is_cold = self.is_cold;
        (
            self.state_load.data,
            Eip7702CodeLoad {
                state_load: StateLoad::new((), is_cold),
                is_delegate_account_cold: self.is_delegate_account_cold,
            },
        )
    }

    /// Sets the delegation cold load status.
    pub fn set_delegate_load(&mut self, is_delegate_account_cold: bool) {
        self.is_delegate_account_cold = Some(is_delegate_account_cold);
    }

    /// Returns a new [`Eip7702CodeLoad`] with the given data and delegation cold load status.
    pub fn new(state_load: StateLoad<T>, is_delegate_account_cold: bool) -> Self {
        Self {
            state_load,
            is_delegate_account_cold: Some(is_delegate_account_cold),
        }
    }
}

/// Helper that extracts database error from [`JournalStateGetter`].
pub type JournalStateGetterDBError<CTX> =
    <<<CTX as JournalStateGetter>::Journal as JournaledState>::Database as Database>::Error;

pub trait JournalStateGetter: DatabaseGetter {
    type Journal: JournaledState<Database = <Self as DatabaseGetter>::Database>;

    fn journal(&mut self) -> &mut Self::Journal;
}

impl<T: JournalStateGetter> JournalStateGetter for &mut T {
    type Journal = T::Journal;

    fn journal(&mut self) -> &mut Self::Journal {
        T::journal(*self)
    }
}

impl<T: JournalStateGetter> JournalStateGetter for Box<T> {
    type Journal = T::Journal;

    fn journal(&mut self) -> &mut Self::Journal {
        T::journal(self.as_mut())
    }
}