Skip to content

Commit

Permalink
feat(trie): introduce TRIE_ACCOUNT_RLP_MAX_SIZE constant (#12638)
Browse files Browse the repository at this point in the history
  • Loading branch information
rkrasiuk authored Nov 19, 2024
1 parent 50c875b commit 3408059
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 10 deletions.
4 changes: 2 additions & 2 deletions crates/trie/parallel/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use reth_trie::{
proof::StorageProof,
trie_cursor::{InMemoryTrieCursorFactory, TrieCursorFactory},
walker::TrieWalker,
HashBuilder, MultiProof, Nibbles, TrieAccount, TrieInput,
HashBuilder, MultiProof, Nibbles, TrieAccount, TrieInput, TRIE_ACCOUNT_RLP_MAX_SIZE,
};
use reth_trie_common::proof::ProofRetainer;
use reth_trie_db::{DatabaseHashedCursorFactory, DatabaseTrieCursorFactory};
Expand Down Expand Up @@ -153,7 +153,7 @@ where
let mut hash_builder = HashBuilder::default().with_proof_retainer(retainer);

let mut storages = HashMap::default();
let mut account_rlp = Vec::with_capacity(128);
let mut account_rlp = Vec::with_capacity(TRIE_ACCOUNT_RLP_MAX_SIZE);
let mut account_node_iter = TrieNodeIter::new(
walker,
hashed_cursor_factory.hashed_account_cursor().map_err(ProviderError::Database)?,
Expand Down
4 changes: 2 additions & 2 deletions crates/trie/parallel/src/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use reth_trie::{
trie_cursor::{InMemoryTrieCursorFactory, TrieCursorFactory},
updates::TrieUpdates,
walker::TrieWalker,
HashBuilder, Nibbles, StorageRoot, TrieAccount, TrieInput,
HashBuilder, Nibbles, StorageRoot, TrieAccount, TrieInput, TRIE_ACCOUNT_RLP_MAX_SIZE,
};
use reth_trie_db::{DatabaseHashedCursorFactory, DatabaseTrieCursorFactory};
use std::{collections::HashMap, sync::Arc};
Expand Down Expand Up @@ -149,7 +149,7 @@ where
);

let mut hash_builder = HashBuilder::default().with_updates(retain_updates);
let mut account_rlp = Vec::with_capacity(128);
let mut account_rlp = Vec::with_capacity(TRIE_ACCOUNT_RLP_MAX_SIZE);
while let Some(node) = account_node_iter.try_next().map_err(ProviderError::Database)? {
match node {
TrieElement::Branch(node) => {
Expand Down
24 changes: 24 additions & 0 deletions crates/trie/trie/src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/// The maximum size of RLP encoded trie account in bytes.
/// 2 (header) + 4 * 1 (field lens) + 8 (nonce) + 32 * 3 (balance, storage root, code hash)
pub const TRIE_ACCOUNT_RLP_MAX_SIZE: usize = 110;

#[cfg(test)]
mod tests {
use super::*;
use alloy_primitives::{B256, U256};
use alloy_rlp::Encodable;
use reth_trie_common::TrieAccount;

#[test]
fn account_rlp_max_size() {
let account = TrieAccount {
nonce: u64::MAX,
balance: U256::MAX,
storage_root: B256::from_slice(&[u8::MAX; 32]),
code_hash: B256::from_slice(&[u8::MAX; 32]),
};
let mut encoded = Vec::new();
account.encode(&mut encoded);
assert_eq!(encoded.len(), TRIE_ACCOUNT_RLP_MAX_SIZE);
}
}
4 changes: 4 additions & 0 deletions crates/trie/trie/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

/// Constants related to the trie computation.
mod constants;
pub use constants::*;

/// 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;
Expand Down
4 changes: 2 additions & 2 deletions crates/trie/trie/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
prefix_set::{PrefixSetMut, TriePrefixSetsMut},
trie_cursor::TrieCursorFactory,
walker::TrieWalker,
HashBuilder, Nibbles,
HashBuilder, Nibbles, TRIE_ACCOUNT_RLP_MAX_SIZE,
};
use alloy_primitives::{
keccak256,
Expand Down Expand Up @@ -104,7 +104,7 @@ where
let mut hash_builder = HashBuilder::default().with_proof_retainer(retainer);

let mut storages = HashMap::default();
let mut account_rlp = Vec::with_capacity(128);
let mut account_rlp = Vec::with_capacity(TRIE_ACCOUNT_RLP_MAX_SIZE);
let mut account_node_iter = TrieNodeIter::new(walker, hashed_account_cursor);
while let Some(account_node) = account_node_iter.try_next()? {
match account_node {
Expand Down
4 changes: 2 additions & 2 deletions crates/trie/trie/src/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
trie_cursor::TrieCursorFactory,
updates::{StorageTrieUpdates, TrieUpdates},
walker::TrieWalker,
HashBuilder, Nibbles, TrieAccount,
HashBuilder, Nibbles, TrieAccount, TRIE_ACCOUNT_RLP_MAX_SIZE,
};
use alloy_consensus::EMPTY_ROOT_HASH;
use alloy_primitives::{keccak256, Address, B256};
Expand Down Expand Up @@ -178,7 +178,7 @@ where
}
};

let mut account_rlp = Vec::with_capacity(128);
let mut account_rlp = Vec::with_capacity(TRIE_ACCOUNT_RLP_MAX_SIZE);
let mut hashed_entries_walked = 0;
let mut updated_storage_nodes = 0;
while let Some(node) = account_node_iter.try_next()? {
Expand Down
4 changes: 2 additions & 2 deletions crates/trie/trie/src/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
prefix_set::TriePrefixSetsMut,
proof::{Proof, StorageProof},
trie_cursor::TrieCursorFactory,
HashedPostState,
HashedPostState, TRIE_ACCOUNT_RLP_MAX_SIZE,
};
use alloy_consensus::EMPTY_ROOT_HASH;
use alloy_primitives::{
Expand Down Expand Up @@ -97,7 +97,7 @@ where

// Attempt to compute state root from proofs and gather additional
// information for the witness.
let mut account_rlp = Vec::with_capacity(128);
let mut account_rlp = Vec::with_capacity(TRIE_ACCOUNT_RLP_MAX_SIZE);
let mut account_trie_nodes = BTreeMap::default();
for (hashed_address, hashed_slots) in proof_targets {
let storage_multiproof = account_multiproof
Expand Down

0 comments on commit 3408059

Please sign in to comment.