-
Notifications
You must be signed in to change notification settings - Fork 953
feat: refactor ssload/sstore to JournaledAccount #3145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rakita
wants to merge
10
commits into
main
Choose a base branch
from
rakita/journaled-account-storage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+200
−127
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
05fcc26
feat: refactor ssload/sstore to JournaledAccount
rakita e059cd1
cleanup
rakita 25930db
reformat some things, remove inline
rakita a9903a2
simpler sstore
rakita 68c55d3
primitive hashmap
rakita a6e591a
make sload inline never
rakita e7f5288
Merge remote-tracking branch 'origin/main' into journaled-account
rakita f572678
Merge remote-tracking branch 'origin/main' into sload-account
rakita 706cf80
Merge remote-tracking branch 'origin/main' into sload-account
rakita d3cf00a
revert some changes to make PR smaller
rakita File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,26 +3,143 @@ | |
| //! | ||
| //! Useful to encapsulate account and journal entries together. So when account gets changed, we can add a journal entry for it. | ||
|
|
||
| use crate::{ | ||
| context::{SStoreResult, StateLoad}, | ||
| journaled_state::JournalLoadError, | ||
| }; | ||
|
|
||
| use super::entry::JournalEntryTr; | ||
| use core::ops::Deref; | ||
| use primitives::{Address, B256, KECCAK_EMPTY, U256}; | ||
| use state::{Account, Bytecode}; | ||
| use database_interface::Database; | ||
| use primitives::{ | ||
| hash_map::Entry, Address, HashMap, HashSet, StorageKey, StorageValue, B256, KECCAK_EMPTY, U256, | ||
| }; | ||
| use state::{Account, Bytecode, EvmStorageSlot}; | ||
| use std::vec::Vec; | ||
|
|
||
| /// Journaled account contains both mutable account and journal entries. | ||
| /// | ||
| /// Useful to encapsulate account and journal entries together. So when account gets changed, we can add a journal entry for it. | ||
| #[derive(Debug, PartialEq, Eq)] | ||
| pub struct JournaledAccount<'a, ENTRY: JournalEntryTr> { | ||
| pub struct JournaledAccount<'a, 'b, ENTRY: JournalEntryTr, DB> { | ||
| /// Address of the account. | ||
| address: Address, | ||
| /// Mutable account. | ||
| account: &'a mut Account, | ||
| /// Journal entries. | ||
| journal_entries: &'a mut Vec<ENTRY>, | ||
| /// Access list. | ||
| access_list: &'a HashMap<Address, HashSet<StorageKey>>, | ||
| /// Transaction ID. | ||
| transaction_id: usize, | ||
| /// Database used to load storage. | ||
| db: &'b mut DB, | ||
| } | ||
|
|
||
| impl<'a, 'b, ENTRY: JournalEntryTr, DB: Database> JournaledAccount<'a, 'b, ENTRY, DB> { | ||
| /// Creates a new journaled account. | ||
| #[inline(never)] | ||
| pub fn sload( | ||
|
Comment on lines
+39
to
+42
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. from precompile pov this doesn't provide any additional benefits because in there we can not contrain any types, so we'd need to write additioal dyn compat wrappers for this I believe @klkvr |
||
| &mut self, | ||
| key: StorageKey, | ||
| skip_cold_load: bool, | ||
| ) -> Result<StateLoad<&mut EvmStorageSlot>, JournalLoadError<<DB as Database>::Error>> { | ||
| let is_newly_created = self.account.is_created(); | ||
| let (slot, is_cold) = match self.account.storage.entry(key) { | ||
| Entry::Occupied(occ) => { | ||
| let slot = occ.into_mut(); | ||
| // skip load if account is cold. | ||
| let is_cold = slot.is_cold_transaction_id(self.transaction_id); | ||
| if is_cold && skip_cold_load { | ||
| return Err(JournalLoadError::ColdLoadSkipped); | ||
| } | ||
| slot.mark_warm_with_transaction_id(self.transaction_id); | ||
| (slot, is_cold) | ||
| } | ||
| Entry::Vacant(vac) => { | ||
| // is storage cold | ||
| let is_cold = self | ||
| .access_list | ||
| .get(&self.address) | ||
| .and_then(|v| v.get(&key)) | ||
| .is_none(); | ||
|
|
||
| if is_cold && skip_cold_load { | ||
| return Err(JournalLoadError::ColdLoadSkipped); | ||
| } | ||
| // if storage was cleared, we don't need to ping db. | ||
| let value = if is_newly_created { | ||
| StorageValue::ZERO | ||
| } else { | ||
| self.db.storage(self.address, key)? | ||
| }; | ||
|
|
||
| let slot = vac.insert(EvmStorageSlot::new(value, self.transaction_id)); | ||
| (slot, is_cold) | ||
| } | ||
| }; | ||
|
|
||
| if is_cold { | ||
| // add it to journal as cold loaded. | ||
| self.journal_entries | ||
| .push(ENTRY::storage_warmed(self.address, key)); | ||
| } | ||
|
|
||
| Ok(StateLoad::new(slot, is_cold)) | ||
| } | ||
|
|
||
| /// Warm loads storage slot and stores the new value | ||
| #[inline] | ||
| pub fn sstore( | ||
| &mut self, | ||
| key: StorageKey, | ||
| new: StorageValue, | ||
| skip_cold_load: bool, | ||
| ) -> Result<StateLoad<SStoreResult>, JournalLoadError<<DB as Database>::Error>> { | ||
| // assume that acc exists and load the slot. | ||
| let slot = self.sload(key, skip_cold_load)?; | ||
|
|
||
| let ret = Ok(StateLoad::new( | ||
| SStoreResult { | ||
| original_value: slot.original_value(), | ||
| present_value: slot.present_value(), | ||
| new_value: new, | ||
| }, | ||
| slot.is_cold, | ||
| )); | ||
|
|
||
| // when new value is different from present, we need to add a journal entry and make a change. | ||
| if slot.present_value != new { | ||
| let previous_value = slot.present_value; | ||
| // insert value into present state. | ||
| slot.data.present_value = new; | ||
|
|
||
| // add journal entry. | ||
| self.journal_entries | ||
| .push(ENTRY::storage_changed(self.address, key, previous_value)); | ||
| } | ||
|
|
||
| ret | ||
| } | ||
|
|
||
| /// Loads the code of the account. and returns it as reference. | ||
| #[inline] | ||
| pub fn load_code(&mut self) -> Result<&Bytecode, JournalLoadError<<DB as Database>::Error>> { | ||
| if self.account.info.code.is_none() { | ||
| let hash = *self.code_hash(); | ||
| let code = if hash == KECCAK_EMPTY { | ||
| Bytecode::default() | ||
| } else { | ||
| self.db.code_by_hash(hash)? | ||
| }; | ||
| self.account.info.code = Some(code); | ||
| } | ||
|
|
||
| Ok(self.account.info.code.as_ref().unwrap()) | ||
| } | ||
| } | ||
|
|
||
| impl<'a, ENTRY: JournalEntryTr> JournaledAccount<'a, ENTRY> { | ||
| impl<'a, 'b, ENTRY: JournalEntryTr, DB> JournaledAccount<'a, 'b, ENTRY, DB> { | ||
| /// Consumes the journaled account and returns the mutable account. | ||
| #[inline] | ||
| pub fn into_account_ref(self) -> &'a Account { | ||
|
|
@@ -35,11 +152,17 @@ impl<'a, ENTRY: JournalEntryTr> JournaledAccount<'a, ENTRY> { | |
| address: Address, | ||
| account: &'a mut Account, | ||
| journal_entries: &'a mut Vec<ENTRY>, | ||
| db: &'b mut DB, | ||
| access_list: &'a HashMap<Address, HashSet<StorageKey>>, | ||
| transaction_id: usize, | ||
| ) -> Self { | ||
| Self { | ||
| address, | ||
| account, | ||
| journal_entries, | ||
| db, | ||
| access_list, | ||
| transaction_id, | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -207,7 +330,7 @@ impl<'a, ENTRY: JournalEntryTr> JournaledAccount<'a, ENTRY> { | |
| } | ||
| } | ||
|
|
||
| impl<'a, ENTRY: JournalEntryTr> Deref for JournaledAccount<'a, ENTRY> { | ||
| impl<'a, 'b, ENTRY: JournalEntryTr, DB> Deref for JournaledAccount<'a, 'b, ENTRY, DB> { | ||
| type Target = Account; | ||
|
|
||
| fn deref(&self) -> &Self::Target { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if these types are both coming from the JournalTr itself, shouldnt the JournaledAccount then just be generic over that?