Skip to content
Merged
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
5 changes: 4 additions & 1 deletion src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,13 +451,16 @@ mod tests {
let file_path = tmp_dir.path().join("test.db");
let db = Database::create_new(&file_path).unwrap();

let tx = db.begin_rw().unwrap();
assert_eq!(tx.commit().unwrap(), EMPTY_ROOT_HASH);

let address1 = address!("0xd8da6bf26964af9d7eed9e03e53415d37aa96045");
let account1 = Account::new(1, U256::from(100), EMPTY_ROOT_HASH, KECCAK_EMPTY);

let mut tx = db.begin_rw().unwrap();
tx.set_account(AddressPath::for_address(address1), Some(account1.clone())).unwrap();

tx.commit().unwrap();
assert_ne!(tx.commit().unwrap(), EMPTY_ROOT_HASH);
db.close().unwrap();

let db = Database::open(&file_path).unwrap();
Expand Down
7 changes: 6 additions & 1 deletion src/meta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ use crate::{
snapshot::SnapshotId,
};
use alloy_primitives::B256;
use alloy_trie::EMPTY_ROOT_HASH;
use memmap2::MmapMut;
use std::{
cmp::Ordering,
Expand Down Expand Up @@ -142,7 +143,11 @@ impl MetadataSlot {
#[inline]
#[must_use]
pub fn root_node_hash(&self) -> B256 {
self.root_node_hash.into()
if self.root_node_hash == [0; 32] {
EMPTY_ROOT_HASH
} else {
self.root_node_hash.into()
}
}

/// Returns the ID of the page containing the root node.
Expand Down
8 changes: 4 additions & 4 deletions src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ impl<DB: Deref<Target = Database>> Transaction<DB, RW> {
Ok(())
}

pub fn commit(mut self) -> Result<(), TransactionError> {
pub fn commit(mut self) -> Result<B256, TransactionError> {
let mut changes = self.pending_changes.drain().collect::<Vec<_>>();
if !changes.is_empty() {
self.database.storage_engine.set_values(&mut self.context, changes.as_mut()).unwrap();
Expand All @@ -184,7 +184,7 @@ impl<DB: Deref<Target = Database>> Transaction<DB, RW> {
transaction_manager.remove_tx(self.context.snapshot_id, true);

self.committed = true;
Ok(())
Ok(self.context.root_node_hash)
}

pub fn rollback(mut self) -> Result<(), TransactionError> {
Expand All @@ -197,12 +197,12 @@ impl<DB: Deref<Target = Database>> Transaction<DB, RW> {
}

impl<DB: Deref<Target = Database>> Transaction<DB, RO> {
pub fn commit(mut self) -> Result<(), TransactionError> {
pub fn commit(mut self) -> Result<B256, TransactionError> {
let mut transaction_manager = self.database.transaction_manager.lock();
transaction_manager.remove_tx(self.context.snapshot_id, false);

self.committed = true;
Ok(())
Ok(self.context.root_node_hash)
}
}

Expand Down