feat(trie): introduce TRIE_ACCOUNT_RLP_MAX_SIZE constant (#12638)

This commit is contained in:
Roman Krasiuk
2024-11-19 17:01:44 +01:00
committed by GitHub
parent 50c875b33c
commit 3408059393
7 changed files with 38 additions and 10 deletions

View File

@@ -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};
@@ -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)?,

View File

@@ -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};
@@ -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) => {

View File

@@ -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);
}
}

View File

@@ -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;

View File

@@ -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,
@@ -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 {

View File

@@ -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};
@@ -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()? {

View File

@@ -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::{
@@ -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