mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 19:09:54 +00:00
perf(trie): post state cursors (#3588)
This commit is contained in:
@ -193,24 +193,34 @@ impl PostState {
|
||||
///
|
||||
/// The hashed post state.
|
||||
pub fn hash_state_slow(&self) -> HashedPostState {
|
||||
let mut accounts = BTreeMap::default();
|
||||
let mut hashed_post_state = HashedPostState::default();
|
||||
|
||||
// Insert accounts with hashed keys from account changes.
|
||||
for (address, account) in self.accounts() {
|
||||
accounts.insert(keccak256(address), *account);
|
||||
}
|
||||
|
||||
let mut storages = BTreeMap::default();
|
||||
for (address, storage) in self.storage() {
|
||||
let mut hashed_storage = BTreeMap::default();
|
||||
for (slot, value) in &storage.storage {
|
||||
hashed_storage.insert(keccak256(H256(slot.to_be_bytes())), *value);
|
||||
let hashed_address = keccak256(address);
|
||||
if let Some(account) = account {
|
||||
hashed_post_state.insert_account(hashed_address, *account);
|
||||
} else {
|
||||
hashed_post_state.insert_cleared_account(hashed_address);
|
||||
}
|
||||
storages.insert(
|
||||
keccak256(address),
|
||||
HashedStorage { wiped: storage.wiped(), storage: hashed_storage },
|
||||
);
|
||||
}
|
||||
|
||||
HashedPostState { accounts, storages }
|
||||
// Insert accounts and storages with hashed keys from storage changes.
|
||||
for (address, storage) in self.storage() {
|
||||
let mut hashed_storage = HashedStorage::new(storage.wiped());
|
||||
for (slot, value) in &storage.storage {
|
||||
let hashed_slot = keccak256(H256(slot.to_be_bytes()));
|
||||
if *value == U256::ZERO {
|
||||
hashed_storage.insert_zero_valued_slot(hashed_slot);
|
||||
} else {
|
||||
hashed_storage.insert_non_zero_valued_storage(hashed_slot, *value);
|
||||
}
|
||||
}
|
||||
|
||||
hashed_post_state.insert_hashed_storage(keccak256(address), hashed_storage);
|
||||
}
|
||||
|
||||
hashed_post_state
|
||||
}
|
||||
|
||||
/// Calculate the state root for this [PostState].
|
||||
@ -248,7 +258,7 @@ impl PostState {
|
||||
&self,
|
||||
tx: &'a TX,
|
||||
) -> Result<H256, StateRootError> {
|
||||
let hashed_post_state = self.hash_state_slow();
|
||||
let hashed_post_state = self.hash_state_slow().sorted();
|
||||
let (account_prefix_set, storage_prefix_set) = hashed_post_state.construct_prefix_sets();
|
||||
let hashed_cursor_factory = HashedPostStateCursorFactory::new(tx, &hashed_post_state);
|
||||
StateRoot::new(tx)
|
||||
@ -1832,7 +1842,7 @@ mod tests {
|
||||
.map(|key| {
|
||||
let account = Account { nonce: 1, balance: U256::from(key), bytecode_hash: None };
|
||||
let storage =
|
||||
(0..10).map(|key| (H256::from_low_u64_be(key), U256::from(key))).collect();
|
||||
(1..11).map(|key| (H256::from_low_u64_be(key), U256::from(key))).collect();
|
||||
(Address::from_low_u64_be(key), (account, storage))
|
||||
})
|
||||
.collect();
|
||||
|
||||
Reference in New Issue
Block a user