-
Notifications
You must be signed in to change notification settings - Fork 224
refactor(l1): deduplicate the account-update loop in Store
#7184
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
ilitteri
wants to merge
4
commits into
main
Choose a base branch
from
dedup-store-account-update-loop
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fb25186
refactor(l1): deduplicate the account-update loop in Store
ilitteri 5fa7f94
Restore the blank lines the extraction stripped: spacing between logi…
ilitteri 596cf04
Fix the L1 Lint failure and put the restored blank lines where they b…
ilitteri 91346e6
Flatten the storage-trie acquisition in the deduplicated account-upda…
ilitteri 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2379,65 +2379,7 @@ impl Store { | |
| state_trie: &mut Trie, | ||
| account_updates: impl IntoIterator<Item = &'a AccountUpdate>, | ||
| ) -> Result<AccountUpdatesList, StoreError> { | ||
| let mut ret_storage_updates = Vec::new(); | ||
| let mut code_updates = Vec::new(); | ||
| let state_root = state_trie.hash_no_commit(&NativeCrypto); | ||
| for update in account_updates { | ||
| let hashed_address = hash_address_fixed(&update.address); | ||
| if update.removed { | ||
| // Remove account from trie | ||
| state_trie.remove(hashed_address.as_bytes())?; | ||
| continue; | ||
| } | ||
| // Add or update AccountState in the trie | ||
| // Fetch current state or create a new state to be inserted | ||
| let mut account_state = match state_trie.get(hashed_address.as_bytes())? { | ||
| Some(encoded_state) => AccountState::decode(&encoded_state)?, | ||
| None => AccountState::default(), | ||
| }; | ||
| if update.removed_storage { | ||
| account_state.storage_root = EMPTY_TRIE_HASH; | ||
| } | ||
| if let Some(info) = &update.info { | ||
| account_state.nonce = info.nonce; | ||
| account_state.balance = info.balance; | ||
| account_state.code_hash = info.code_hash; | ||
| // Store updated code in DB | ||
| if let Some(code) = &update.code { | ||
| code_updates.push((info.code_hash, code.clone())); | ||
| } | ||
| } | ||
| // Store the added storage in the account's storage trie and compute its new root | ||
| if !update.added_storage.is_empty() { | ||
| let mut storage_trie = | ||
| self.open_storage_trie(hashed_address, state_root, account_state.storage_root)?; | ||
| for (storage_key, storage_value) in &update.added_storage { | ||
| let hashed_key = hash_key(storage_key); | ||
| if storage_value.is_zero() { | ||
| storage_trie.remove(&hashed_key)?; | ||
| } else { | ||
| storage_trie.insert(hashed_key, storage_value.encode_to_vec())?; | ||
| } | ||
| } | ||
| let (storage_hash, storage_updates) = | ||
| storage_trie.collect_changes_since_last_hash(&NativeCrypto); | ||
| account_state.storage_root = storage_hash; | ||
| ret_storage_updates.push((hashed_address, storage_updates)); | ||
| } | ||
| state_trie.insert( | ||
| hashed_address.as_bytes().to_vec(), | ||
| account_state.encode_to_vec(), | ||
| )?; | ||
| } | ||
| let (state_trie_hash, state_updates) = | ||
| state_trie.collect_changes_since_last_hash(&NativeCrypto); | ||
|
|
||
| Ok(AccountUpdatesList { | ||
| state_trie_hash, | ||
| state_updates, | ||
| storage_updates: ret_storage_updates, | ||
| code_updates, | ||
| }) | ||
| self.apply_account_updates_from_trie_inner(state_trie, account_updates, None) | ||
| } | ||
|
|
||
| /// Performs the same actions as apply_account_updates_from_trie | ||
|
|
@@ -2448,25 +2390,45 @@ impl Store { | |
| account_updates: &[AccountUpdate], | ||
| mut storage_tries: StorageTries, | ||
| ) -> Result<(StorageTries, AccountUpdatesList), StoreError> { | ||
| let account_updates_list = self.apply_account_updates_from_trie_inner( | ||
| &mut state_trie, | ||
| account_updates, | ||
| Some(&mut storage_tries), | ||
| )?; | ||
| Ok((storage_tries, account_updates_list)) | ||
| } | ||
|
|
||
| /// Applies account updates to the given state trie. | ||
| /// | ||
| /// With `storage_tries`, storage tries are opened through [`TrieLogger`] and | ||
| /// cached in the map (keyed by unhashed address) so witness recording spans | ||
| /// updates; without it, a fresh storage trie is opened per account at its | ||
| /// current storage root. | ||
| fn apply_account_updates_from_trie_inner<'a>( | ||
| &self, | ||
| state_trie: &mut Trie, | ||
| account_updates: impl IntoIterator<Item = &'a AccountUpdate>, | ||
| mut storage_tries: Option<&mut StorageTries>, | ||
| ) -> Result<AccountUpdatesList, StoreError> { | ||
| let mut ret_storage_updates = Vec::new(); | ||
|
|
||
| let mut code_updates = Vec::new(); | ||
|
|
||
| let state_root = state_trie.hash_no_commit(&NativeCrypto); | ||
|
|
||
| for update in account_updates.iter() { | ||
| let hashed_address = hash_address(&update.address); | ||
| for update in account_updates { | ||
| let hashed_address = hash_address_fixed(&update.address); | ||
|
|
||
| if update.removed { | ||
| // Remove account from trie | ||
| state_trie.remove(&hashed_address)?; | ||
| state_trie.remove(hashed_address.as_bytes())?; | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
| // Add or update AccountState in the trie | ||
| // Fetch current state or create a new state to be inserted | ||
| let mut account_state = match state_trie.get(&hashed_address)? { | ||
| let mut account_state = match state_trie.get(hashed_address.as_bytes())? { | ||
| Some(encoded_state) => AccountState::decode(&encoded_state)?, | ||
| None => AccountState::default(), | ||
| }; | ||
|
|
@@ -2490,16 +2452,20 @@ impl Store { | |
|
|
||
| // Store the added storage in the account's storage trie and compute its new root | ||
| if !update.added_storage.is_empty() { | ||
| let (_witness, storage_trie) = match storage_tries.entry(update.address) { | ||
| Entry::Occupied(value) => value.into_mut(), | ||
| Entry::Vacant(vacant) => { | ||
| let trie = self.open_storage_trie( | ||
| H256::from_slice(&hashed_address), | ||
| state_root, | ||
| account_state.storage_root, | ||
| )?; | ||
| vacant.insert(TrieLogger::open_trie(trie)) | ||
| } | ||
| let open_fresh_trie = || { | ||
| self.open_storage_trie(hashed_address, state_root, account_state.storage_root) | ||
| }; | ||
|
|
||
| let mut local_trie = None; | ||
|
|
||
| let storage_trie = match storage_tries.as_deref_mut() { | ||
| Some(tries) => match tries.entry(update.address) { | ||
| Entry::Occupied(cached) => &mut cached.into_mut().1, | ||
| Entry::Vacant(vacant) => { | ||
| &mut vacant.insert(TrieLogger::open_trie(open_fresh_trie()?)).1 | ||
| } | ||
| }, | ||
| None => local_trie.insert(open_fresh_trie()?), | ||
| }; | ||
|
|
||
| for (storage_key, storage_value) in &update.added_storage { | ||
|
|
@@ -2517,23 +2483,24 @@ impl Store { | |
|
|
||
| account_state.storage_root = storage_hash; | ||
|
|
||
| ret_storage_updates.push((H256::from_slice(&hashed_address), storage_updates)); | ||
| ret_storage_updates.push((hashed_address, storage_updates)); | ||
| } | ||
|
|
||
| state_trie.insert(hashed_address, account_state.encode_to_vec())?; | ||
| state_trie.insert( | ||
| hashed_address.as_bytes().to_vec(), | ||
| account_state.encode_to_vec(), | ||
| )?; | ||
| } | ||
|
|
||
| let (state_trie_hash, state_updates) = | ||
| state_trie.collect_changes_since_last_hash(&NativeCrypto); | ||
|
|
||
| let account_updates_list = AccountUpdatesList { | ||
| Ok(AccountUpdatesList { | ||
|
Comment on lines
-2529
to
+2498
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. This is also not a good way to reduce lines of code |
||
| state_trie_hash, | ||
| state_updates, | ||
| storage_updates: ret_storage_updates, | ||
| code_updates, | ||
| }; | ||
|
|
||
| Ok((storage_tries, account_updates_list)) | ||
| }) | ||
| } | ||
|
|
||
| /// Adds all genesis accounts and returns the genesis block's state_root | ||
|
|
||
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.
This would be a good way to reduce lines of code: