Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions crates/storage/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3823,6 +3823,7 @@ impl Store {
self.gated_snapshot(state_root)?,
Box::new(BackendTrieDB::new_for_storages(
self.backend.clone(),
None,
self.last_written()?,
)?),
Some(account_hash),
Expand Down Expand Up @@ -3869,6 +3870,7 @@ impl Store {
Box::new(BackendTrieDB::new_for_storages_with_view(
self.backend.clone(),
read_view,
None,
last_written,
)?),
Some(account_hash),
Expand All @@ -3884,9 +3886,9 @@ impl Store {
storage_root: H256,
) -> Result<Trie, StoreError> {
Ok(Trie::open(
Box::new(BackendTrieDB::new_for_account_storage(
Box::new(BackendTrieDB::new_for_storages(
self.backend.clone(),
account_hash,
Some(account_hash),
self.last_written()?,
)?),
storage_root,
Expand Down Expand Up @@ -5121,10 +5123,10 @@ fn flatkeyvalue_generator(
}

let mut iter_inner = Trie::open(
Box::new(BackendTrieDB::new_for_account_storage_with_view(
Box::new(BackendTrieDB::new_for_storages_with_view(
backend.clone(),
read_tx.clone(),
account_hash,
Some(account_hash),
path.as_ref().to_vec(),
)?),
account_state.storage_root,
Expand Down
40 changes: 9 additions & 31 deletions crates/storage/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct BackendTrieDB {
nodes_table: &'static str,
fkv_table: &'static str,
/// Storage trie address prefix (for storage tries)
/// None for state tries, Some(address) for storage tries
/// None for state tries, or when the caller prefixes the paths itself
address_prefix: Option<H256>,
}

Expand Down Expand Up @@ -54,18 +54,24 @@ impl BackendTrieDB {
}

/// Create a new BackendTrieDB for the storage tries
/// `address_prefix` is `Some(account_hash)` to scope reads and writes to a single
/// account's storage trie, or `None` when the caller already prefixes the paths
/// (e.g. a `TrieWrapper` built with `Some(account_hash)`) - passing the hash twice
/// would double-prefix every key
pub fn new_for_storages(
db: Arc<dyn StorageBackend>,
address_prefix: Option<H256>,
last_written: Vec<u8>,
) -> Result<Self, StoreError> {
let read_view = db.begin_read()?;
Self::new_for_storages_with_view(db, read_view, last_written)
Self::new_for_storages_with_view(db, read_view, address_prefix, last_written)
}

/// Create a new BackendTrieDB for the storage tries with a shared read view
pub fn new_for_storages_with_view(
db: Arc<dyn StorageBackend>,
read_view: Arc<dyn StorageReadView>,
address_prefix: Option<H256>,
last_written: Vec<u8>,
) -> Result<Self, StoreError> {
let last_computed_flatkeyvalue = Nibbles::from_hex(last_written);
Expand All @@ -75,35 +81,7 @@ impl BackendTrieDB {
last_computed_flatkeyvalue,
nodes_table: STORAGE_TRIE_NODES,
fkv_table: STORAGE_FLATKEYVALUE,
address_prefix: None,
})
}

/// Create a new BackendTrieDB for a specific storage trie
pub fn new_for_account_storage(
db: Arc<dyn StorageBackend>,
address_prefix: H256,
last_written: Vec<u8>,
) -> Result<Self, StoreError> {
let read_view = db.begin_read()?;
Self::new_for_account_storage_with_view(db, read_view, address_prefix, last_written)
}

/// Create a new BackendTrieDB for a specific storage trie with a shared read view
pub fn new_for_account_storage_with_view(
db: Arc<dyn StorageBackend>,
read_view: Arc<dyn StorageReadView>,
address_prefix: H256,
last_written: Vec<u8>,
) -> Result<Self, StoreError> {
let last_computed_flatkeyvalue = Nibbles::from_hex(last_written);
Ok(Self {
db,
read_view,
last_computed_flatkeyvalue,
nodes_table: STORAGE_TRIE_NODES,
fkv_table: STORAGE_FLATKEYVALUE,
address_prefix: Some(address_prefix),
address_prefix,
})
}

Expand Down
4 changes: 2 additions & 2 deletions test/tests/storage/trie_db_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn test_trie_db_with_address_prefix() {

// Create TrieDB with address prefix and write data
let address = H256::from([0xaa; 32]);
let trie_db = BackendTrieDB::new_for_account_storage(backend.clone(), address, vec![]).unwrap();
let trie_db = BackendTrieDB::new_for_storages(backend.clone(), Some(address), vec![]).unwrap();

let node_hash = Nibbles::from_hex(vec![1]);
let node_data = vec![1, 2, 3, 4, 5];
Expand All @@ -45,7 +45,7 @@ fn test_trie_db_with_address_prefix() {
.unwrap();

// Create a fresh TrieDB to read back
let trie_db = BackendTrieDB::new_for_account_storage(backend, address, vec![]).unwrap();
let trie_db = BackendTrieDB::new_for_storages(backend, Some(address), vec![]).unwrap();

let retrieved_data = trie_db.get(node_hash).unwrap().unwrap();
assert_eq!(retrieved_data, node_data);
Expand Down
Loading