diff --git a/crates/storage/store.rs b/crates/storage/store.rs index b7592847ddc..93d4d562288 100644 --- a/crates/storage/store.rs +++ b/crates/storage/store.rs @@ -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), @@ -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), @@ -3884,9 +3886,9 @@ impl Store { storage_root: H256, ) -> Result { 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, @@ -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, diff --git a/crates/storage/trie.rs b/crates/storage/trie.rs index 9cefdeaa3e8..b90c99bba3d 100644 --- a/crates/storage/trie.rs +++ b/crates/storage/trie.rs @@ -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, } @@ -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, + address_prefix: Option, last_written: Vec, ) -> Result { 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, read_view: Arc, + address_prefix: Option, last_written: Vec, ) -> Result { let last_computed_flatkeyvalue = Nibbles::from_hex(last_written); @@ -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, - address_prefix: H256, - last_written: Vec, - ) -> Result { - 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, - read_view: Arc, - address_prefix: H256, - last_written: Vec, - ) -> Result { - 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, }) } diff --git a/test/tests/storage/trie_db_tests.rs b/test/tests/storage/trie_db_tests.rs index b6d99e97977..a67dc19f32b 100644 --- a/test/tests/storage/trie_db_tests.rs +++ b/test/tests/storage/trie_db_tests.rs @@ -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]; @@ -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);