mirror of
https://github.com/paradigmxyz/reth.git
synced 2026-04-30 03:01:58 -04:00
feat(primitives): state root methods (#5694)
This commit is contained in:
@@ -1,39 +0,0 @@
|
||||
use alloy_rlp::{RlpDecodable, RlpEncodable};
|
||||
use reth_primitives::{constants::EMPTY_ROOT_HASH, Account, B256, KECCAK_EMPTY, U256};
|
||||
|
||||
/// An Ethereum account as represented in the trie.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, RlpEncodable, RlpDecodable)]
|
||||
pub struct EthAccount {
|
||||
/// Account nonce.
|
||||
nonce: u64,
|
||||
/// Account balance.
|
||||
balance: U256,
|
||||
/// Account's storage root.
|
||||
storage_root: B256,
|
||||
/// Hash of the account's bytecode.
|
||||
code_hash: B256,
|
||||
}
|
||||
|
||||
impl From<Account> for EthAccount {
|
||||
fn from(acc: Account) -> Self {
|
||||
EthAccount {
|
||||
nonce: acc.nonce,
|
||||
balance: acc.balance,
|
||||
storage_root: EMPTY_ROOT_HASH,
|
||||
code_hash: acc.bytecode_hash.unwrap_or(KECCAK_EMPTY),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl EthAccount {
|
||||
/// Set storage root on account.
|
||||
pub fn with_storage_root(mut self, storage_root: B256) -> Self {
|
||||
self.storage_root = storage_root;
|
||||
self
|
||||
}
|
||||
|
||||
/// Get account's storage root.
|
||||
pub fn storage_root(&self) -> B256 {
|
||||
self.storage_root
|
||||
}
|
||||
}
|
||||
@@ -15,9 +15,6 @@
|
||||
#![deny(unused_must_use, rust_2018_idioms)]
|
||||
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
|
||||
|
||||
/// The Ethereum account as represented in the trie.
|
||||
pub mod account;
|
||||
|
||||
/// The implementation of a container for storing intermediate changes to a trie.
|
||||
/// The container indicates when the trie has been modified.
|
||||
pub mod prefix_set;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
use crate::{
|
||||
account::EthAccount,
|
||||
hashed_cursor::{HashedCursorFactory, HashedStorageCursor},
|
||||
node_iter::{AccountNode, AccountNodeIter, StorageNode, StorageNodeIter},
|
||||
prefix_set::PrefixSetMut,
|
||||
@@ -12,7 +11,7 @@ use reth_db::{tables, transaction::DbTx};
|
||||
use reth_primitives::{
|
||||
constants::EMPTY_ROOT_HASH,
|
||||
keccak256,
|
||||
trie::{AccountProof, HashBuilder, Nibbles, StorageProof},
|
||||
trie::{AccountProof, HashBuilder, Nibbles, StorageProof, TrieAccount},
|
||||
Address, B256,
|
||||
};
|
||||
|
||||
@@ -81,7 +80,7 @@ where
|
||||
};
|
||||
|
||||
account_rlp.clear();
|
||||
let account = EthAccount::from(account).with_storage_root(storage_root);
|
||||
let account = TrieAccount::from((account, storage_root));
|
||||
account.encode(&mut account_rlp as &mut dyn BufMut);
|
||||
|
||||
hash_builder.add_leaf(Nibbles::unpack(hashed_address), &account_rlp);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use crate::account::EthAccount;
|
||||
use alloy_rlp::{encode_fixed_size, Encodable};
|
||||
use reth_primitives::{proofs::triehash::KeccakHasher, Account, Address, B256, U256};
|
||||
use reth_primitives::{
|
||||
proofs::triehash::KeccakHasher, trie::TrieAccount, Account, Address, B256, U256,
|
||||
};
|
||||
|
||||
/// Re-export of [triehash].
|
||||
pub use triehash;
|
||||
@@ -14,7 +15,7 @@ where
|
||||
let encoded_accounts = accounts.map(|(address, (account, storage))| {
|
||||
let storage_root = storage_root(storage.into_iter());
|
||||
let mut out = Vec::new();
|
||||
EthAccount::from(account).with_storage_root(storage_root).encode(&mut out);
|
||||
TrieAccount::from((account, storage_root)).encode(&mut out);
|
||||
(address, out)
|
||||
});
|
||||
|
||||
@@ -37,7 +38,7 @@ where
|
||||
let encoded_accounts = accounts.map(|(address, (account, storage))| {
|
||||
let storage_root = storage_root_prehashed(storage.into_iter());
|
||||
let mut out = Vec::new();
|
||||
EthAccount::from(account).with_storage_root(storage_root).encode(&mut out);
|
||||
TrieAccount::from((account, storage_root)).encode(&mut out);
|
||||
(address, out)
|
||||
});
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
use crate::{
|
||||
account::EthAccount,
|
||||
hashed_cursor::{HashedCursorFactory, HashedStorageCursor},
|
||||
node_iter::{AccountNode, AccountNodeIter, StorageNode, StorageNodeIter},
|
||||
prefix_set::{PrefixSet, PrefixSetLoader, PrefixSetMut},
|
||||
@@ -14,7 +13,7 @@ use reth_db::{tables, transaction::DbTx};
|
||||
use reth_primitives::{
|
||||
constants::EMPTY_ROOT_HASH,
|
||||
keccak256,
|
||||
trie::{HashBuilder, Nibbles},
|
||||
trie::{HashBuilder, Nibbles, TrieAccount},
|
||||
Address, BlockNumber, B256,
|
||||
};
|
||||
use std::{
|
||||
@@ -287,7 +286,7 @@ where
|
||||
storage_root_calculator.root()?
|
||||
};
|
||||
|
||||
let account = EthAccount::from(account).with_storage_root(storage_root);
|
||||
let account = TrieAccount::from((account, storage_root));
|
||||
|
||||
account_rlp.clear();
|
||||
account.encode(&mut account_rlp as &mut dyn BufMut);
|
||||
@@ -769,10 +768,7 @@ mod tests {
|
||||
}
|
||||
|
||||
fn encode_account(account: Account, storage_root: Option<B256>) -> Vec<u8> {
|
||||
let mut account = EthAccount::from(account);
|
||||
if let Some(storage_root) = storage_root {
|
||||
account = account.with_storage_root(storage_root);
|
||||
}
|
||||
let account = TrieAccount::from((account, storage_root.unwrap_or(EMPTY_ROOT_HASH)));
|
||||
let mut account_rlp = Vec::with_capacity(account.length());
|
||||
account.encode(&mut account_rlp);
|
||||
account_rlp
|
||||
|
||||
Reference in New Issue
Block a user