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
16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ members = ["crates/*"]
resolver = "2"

[workspace.package]
version = "0.6.7"
version = "0.6.8"
edition = "2024"
rust-version = "1.92"
authors = ["init4"]
Expand Down Expand Up @@ -35,13 +35,13 @@ incremental = false

[workspace.dependencies]
# internal
signet-hot = { version = "0.6.7", path = "./crates/hot" }
signet-hot-mdbx = { version = "0.6.7", path = "./crates/hot-mdbx" }
signet-cold = { version = "0.6.7", path = "./crates/cold" }
signet-cold-mdbx = { version = "0.6.7", path = "./crates/cold-mdbx" }
signet-cold-sql = { version = "0.6.7", path = "./crates/cold-sql" }
signet-storage = { version = "0.6.7", path = "./crates/storage" }
signet-storage-types = { version = "0.6.7", path = "./crates/types" }
signet-hot = { version = "0.6.8", path = "./crates/hot" }
signet-hot-mdbx = { version = "0.6.8", path = "./crates/hot-mdbx" }
signet-cold = { version = "0.6.8", path = "./crates/cold" }
signet-cold-mdbx = { version = "0.6.8", path = "./crates/cold-mdbx" }
signet-cold-sql = { version = "0.6.8", path = "./crates/cold-sql" }
signet-storage = { version = "0.6.8", path = "./crates/storage" }
signet-storage-types = { version = "0.6.8", path = "./crates/types" }

# External, in-house
signet-libmdbx = { version = "0.8.0" }
Expand Down
16 changes: 15 additions & 1 deletion crates/hot-mdbx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub use tx::Tx;

mod utils;

use signet_hot::model::{HotKv, HotKvError};
use signet_hot::model::{HotKv, HotKvError, HotKvWrite};

/// 1 KB in bytes
pub const KILOBYTE: usize = 1024;
Expand Down Expand Up @@ -369,9 +369,23 @@ impl DatabaseEnv {
let fsi_cache = Arc::new(RwLock::new(HashMap::new()));
let env = Self { inner: inner_env.open(path)?, fsi_cache, _lock_file };

if kind.is_rw() {
env.create_tables()?;
}

Ok(env)
}

/// Create all standard hot storage tables.
///
/// Called automatically when opening in read-write mode.
fn create_tables(&self) -> Result<(), MdbxError> {
let tx = self.tx_rw()?;
tx.queue_db_init()?;
tx.raw_commit()?;
Ok(())
}

/// Start a new read-only transaction.
pub fn tx(&self) -> Result<Tx<Ro>, MdbxError> {
self.inner
Expand Down
34 changes: 6 additions & 28 deletions crates/hot-mdbx/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use alloy::primitives::Bytes;
use signet_hot::{
db::UnsafeDbWrite,
model::{HotKv, HotKvWrite},
tables::{self, SingleKey, Table},
tables::{SingleKey, Table},
};
use tempfile::{TempDir, tempdir};

Expand All @@ -29,25 +29,13 @@ pub fn create_test_rw_db() -> (TempDir, DatabaseEnv) {
let args = DatabaseArguments::new();
let db = DatabaseEnv::open(dir.path(), DatabaseEnvKind::RW, args).unwrap();

// Create tables from the `crate::tables::hot` module
// Standard tables are created automatically by open() in RW mode.
// Create test-specific tables.
let writer = db.writer().unwrap();

writer.queue_create::<tables::Headers>().unwrap();
writer.queue_create::<tables::HeaderNumbers>().unwrap();
writer.queue_create::<tables::Bytecodes>().unwrap();
writer.queue_create::<tables::PlainAccountState>().unwrap();
writer.queue_create::<tables::AccountsHistory>().unwrap();
writer.queue_create::<tables::StorageHistory>().unwrap();
writer.queue_create::<tables::PlainStorageState>().unwrap();
writer.queue_create::<tables::StorageChangeSets>().unwrap();
writer.queue_create::<tables::AccountChangeSets>().unwrap();

writer.queue_create::<TestTable>().unwrap();

// Create DUP_FIXED table for put_multiple tests
// DUP_FIXED table for put_multiple tests
// key2_size=8, value_size=8 means total fixed value size is 16 bytes
writer.queue_raw_create("put_multiple_test", Some(8), Some(8)).unwrap();

writer.commit().expect("Failed to commit table creation");

(dir, db)
Expand All @@ -66,7 +54,6 @@ mod tests {
use signet_hot::{
KeySer, MAX_KEY_SIZE, ValSer,
conformance::{conformance, test_unwind_conformance},
db::UnsafeDbWrite,
model::{
DualKeyTraverse, DualTableTraverse, HotKv, HotKvRead, HotKvWrite, TableTraverse,
TableTraverseMut,
Expand Down Expand Up @@ -133,9 +120,6 @@ mod tests {
{
let writer: Tx<Rw> = db.writer().unwrap();

// Create tables first
writer.queue_create::<tables::Bytecodes>().unwrap();

// Write account data
writer.queue_put::<tables::PlainAccountState>(&address, &account).unwrap();
writer.queue_put::<tables::Bytecodes>(&hash, &bytecode).unwrap();
Expand Down Expand Up @@ -494,7 +478,6 @@ mod tests {

{
let writer: Tx<Rw> = db.writer().unwrap();
writer.queue_create::<tables::Bytecodes>().unwrap();
writer.queue_put::<tables::Bytecodes>(&hash, &large_bytecode).unwrap();
writer.raw_commit().unwrap();
}
Expand Down Expand Up @@ -1995,15 +1978,10 @@ mod tests {

let dir = tempdir().unwrap();

// Phase 1: create tables and write data, then drop the DatabaseEnv.
// Phase 1: open RW (auto-creates tables), then drop the DatabaseEnv.
{
let args = DatabaseArguments::new();
let db = DatabaseEnv::open(dir.path(), DatabaseEnvKind::RW, args).unwrap();

let writer = db.writer().unwrap();
writer.queue_create::<tables::PlainStorageState>().unwrap();
writer.queue_create::<tables::Headers>().unwrap();
writer.raw_commit().unwrap();
DatabaseEnv::open(dir.path(), DatabaseEnvKind::RW, args).unwrap();
}
// DatabaseEnv (and its in-memory FsiCache) is now dropped.

Expand Down
43 changes: 43 additions & 0 deletions crates/hot/src/model/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@ pub trait HotKvWrite: HotKvRead {

/// Queue a raw create operation for a specific table.
///
/// Implementations MUST be idempotent: if the table already exists,
/// this should be a no-op.
///
/// This abstraction supports table specializations:
/// 1. `dual_key_size` - whether the table is dual-keyed (i.e.,
/// `DUPSORT` in LMDB/MDBX). If so, the argument MUST be the
Expand Down Expand Up @@ -389,6 +392,46 @@ pub trait HotKvWrite: HotKvRead {
self.queue_raw_clear(T::NAME)
}

/// Queue creation of all standard hot storage tables.
///
/// This creates the 9 predefined tables used by the history and state
/// subsystems: [`Headers`], [`HeaderNumbers`], [`Bytecodes`],
/// [`PlainAccountState`], [`PlainStorageState`], [`AccountsHistory`],
/// [`AccountChangeSets`], [`StorageHistory`], and
/// [`StorageChangeSets`].
///
/// This is expected to be a no-op if the tables already exist, as
/// [`queue_raw_create`] is required to be idempotent.
///
/// This does not commit the transaction. The caller must call
/// [`raw_commit`] after this method to persist the tables.
///
/// [`queue_raw_create`]: Self::queue_raw_create
///
/// [`raw_commit`]: Self::raw_commit
/// [`Headers`]: crate::tables::Headers
/// [`HeaderNumbers`]: crate::tables::HeaderNumbers
/// [`Bytecodes`]: crate::tables::Bytecodes
/// [`PlainAccountState`]: crate::tables::PlainAccountState
/// [`PlainStorageState`]: crate::tables::PlainStorageState
/// [`AccountsHistory`]: crate::tables::AccountsHistory
/// [`AccountChangeSets`]: crate::tables::AccountChangeSets
/// [`StorageHistory`]: crate::tables::StorageHistory
/// [`StorageChangeSets`]: crate::tables::StorageChangeSets
fn queue_db_init(&self) -> Result<(), Self::Error> {
use crate::tables;
self.queue_create::<tables::Headers>()?;
self.queue_create::<tables::HeaderNumbers>()?;
self.queue_create::<tables::Bytecodes>()?;
self.queue_create::<tables::PlainAccountState>()?;
self.queue_create::<tables::PlainStorageState>()?;
self.queue_create::<tables::AccountsHistory>()?;
self.queue_create::<tables::AccountChangeSets>()?;
self.queue_create::<tables::StorageHistory>()?;
self.queue_create::<tables::StorageChangeSets>()?;
Ok(())
}

/// Clear all K2 entries for a specific K1 in a dual-keyed table.
fn clear_k1_for<T: DualKey>(&self, key1: &T::Key) -> Result<(), Self::Error> {
let mut cursor = self.traverse_dual_mut::<T>()?;
Expand Down