Skip to content

Commit df5a957

Browse files
kariyclaude[bot]
andauthored
feat(db): include database version in DbEnv (#155)
* feat(db): include database version in `DbEnv` - Add private `version` field to `DbEnvInner` struct - Modify `DbEnv::open()` to accept version parameter - Update `open_ephemeral()` to use `CURRENT_DB_VERSION` - Add public `version()` getter method to access stored version - Update `open_db()` and `init_db()` to read and pass version from file - Update CLI database opening functions to handle version parameter The database version is now accessible at the provider level through `DbEnv::version()` method as requested in issue #154. Co-authored-by: Ammar Arif <kariy@users.noreply.github.com> * wip * wip * wip * add back ephemeral db geometry * wip * fix --------- Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com> Co-authored-by: Ammar Arif <kariy@users.noreply.github.com>
1 parent eaea715 commit df5a957

File tree

17 files changed

+312
-240
lines changed

17 files changed

+312
-240
lines changed

bin/katana/src/cli/db/mod.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use std::path::{self};
22

3-
use anyhow::{Context, Result};
3+
use anyhow::Result;
44
use clap::{Args, Subcommand};
55
use comfy_table::modifiers::UTF8_ROUND_CORNERS;
66
use comfy_table::presets::UTF8_FULL;
77
use comfy_table::Table;
8-
use katana_db::mdbx::{DbEnv, DbEnvKind};
98

109
mod prune;
1110
mod stats;
@@ -43,16 +42,16 @@ impl DbArgs {
4342
///
4443
/// The path is expanded and resolved to an absolute path before opening the database for clearer
4544
/// error messages.
46-
pub fn open_db_ro(path: &str) -> Result<DbEnv> {
47-
let path = path::absolute(shellexpand::full(path)?.into_owned())?;
48-
DbEnv::open(&path, DbEnvKind::RO).with_context(|| {
49-
format!("Opening database file in read-only mode at path {}", path.display())
50-
})
45+
pub fn open_db_ro(path: &str) -> Result<katana_db::Db> {
46+
katana_db::Db::open_ro(&path::absolute(shellexpand::full(path)?.into_owned())?)
5147
}
5248

53-
pub fn open_db_rw(path: &str) -> Result<DbEnv> {
54-
let path = path::absolute(shellexpand::full(path)?.into_owned())?;
55-
katana_db::open_db(path)
49+
/// Open the database at `path` in read-write mode.
50+
///
51+
/// The path is expanded and resolved to an absolute path before opening the database for clearer
52+
/// error messages.
53+
pub fn open_db_rw(path: &str) -> Result<katana_db::Db> {
54+
katana_db::Db::open(&path::absolute(shellexpand::full(path)?.into_owned())?)
5655
}
5756

5857
/// Create a table with the default UTF-8 full border and rounded corners.

bin/katana/tests/fixtures.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::collections::{BTreeMap, BTreeSet};
22

3-
use katana_db::mdbx::DbEnv;
43
use katana_primitives::block::{Block, BlockHash, FinalityStatus};
54
use katana_primitives::class::{ClassHash, CompiledClassHash};
65
use katana_primitives::contract::{ContractAddress, Nonce, StorageKey, StorageValue};
@@ -20,7 +19,7 @@ pub struct TempDb {
2019
impl TempDb {
2120
pub fn new() -> Self {
2221
let temp_dir = tempfile::tempdir().expect("failed to create temp dir");
23-
katana_db::init_db(temp_dir.path()).expect("failed to initialize database");
22+
katana_db::Db::new(temp_dir.path()).expect("failed to initialize database");
2423
Self { temp_dir }
2524
}
2625

@@ -32,11 +31,11 @@ impl TempDb {
3231
DbProvider::new(self.open_rw())
3332
}
3433

35-
fn open_ro(&self) -> DbEnv {
34+
fn open_ro(&self) -> katana_db::Db {
3635
katana::cli::db::open_db_ro(self.path_str()).unwrap()
3736
}
3837

39-
fn open_rw(&self) -> DbEnv {
38+
fn open_rw(&self) -> katana_db::Db {
4039
katana::cli::db::open_db_rw(self.path_str()).unwrap()
4140
}
4241

crates/chain-spec/src/rollup/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ mod tests {
409409
fn valid_transactions() {
410410
let chain_spec = chain_spec(1, true);
411411

412-
let provider = DbProvider::new_ephemeral();
412+
let provider = DbProvider::new_in_memory();
413413
let ef = executor(&chain_spec);
414414

415415
let mut executor = ef.with_state(provider.latest().unwrap());
@@ -426,7 +426,7 @@ mod tests {
426426
fn genesis_states() {
427427
let chain_spec = chain_spec(1, true);
428428

429-
let provider = DbProvider::new_ephemeral();
429+
let provider = DbProvider::new_in_memory();
430430
let ef = executor(&chain_spec);
431431

432432
let mut executor = ef.with_state(provider.latest().unwrap());

crates/core/benches/commit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ fn commit_small(c: &mut Criterion) {
142142
small_transactions,
143143
small_receipts.as_slice(),
144144
&small_state_updates,
145-
DbProvider::new_ephemeral(),
145+
DbProvider::new_in_memory(),
146146
);
147147

148148
c.bench_function("Serial", |b| {
@@ -168,7 +168,7 @@ fn commit_big(c: &mut Criterion) {
168168
big_transactions,
169169
big_receipts.as_slice(),
170170
&big_state_updates,
171-
DbProvider::new_ephemeral(),
171+
DbProvider::new_in_memory(),
172172
);
173173

174174
c.bench_function("Serial", |b| {

crates/core/src/backend/storage.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::sync::Arc;
22

33
use anyhow::{anyhow, bail, Context, Result};
4-
use katana_db::mdbx::DbEnv;
54
use katana_primitives::block::{
65
BlockHashOrNumber, BlockIdOrTag, BlockNumber, FinalityStatus, GasPrice, SealedBlockWithStatus,
76
};
@@ -85,13 +84,13 @@ impl Blockchain {
8584
}
8685

8786
/// Creates a new [Blockchain] from a database at `path` and `genesis` state.
88-
pub fn new_with_db(db: DbEnv) -> Self {
87+
pub fn new_with_db(db: katana_db::Db) -> Self {
8988
Self::new(DbProvider::new(db))
9089
}
9190

9291
/// Builds a new blockchain with a forked block.
9392
pub async fn new_from_forked(
94-
db: DbEnv,
93+
db: katana_db::Db,
9594
fork_url: Url,
9695
fork_block: Option<BlockHashOrNumber>,
9796
chain: &mut katana_chain_spec::dev::ChainSpec,

crates/core/src/service/block_producer_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::backend::storage::Blockchain;
1313
fn test_backend() -> Arc<Backend<NoopExecutorFactory>> {
1414
let chain_spec = Arc::new(ChainSpec::dev());
1515
let executor_factory = NoopExecutorFactory::new();
16-
let blockchain = Blockchain::new(DbProvider::new_ephemeral());
16+
let blockchain = Blockchain::new(DbProvider::new_in_memory());
1717
let gas_oracle = GasOracle::fixed(Default::default(), Default::default());
1818
let backend = Arc::new(Backend::new(chain_spec, blockchain, gas_oracle, executor_factory));
1919
backend.init_genesis().expect("failed to initialize genesis");

crates/core/tests/backend.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn executor(chain_spec: &ChainSpec) -> BlockifierFactory {
3333
}
3434

3535
fn backend(chain_spec: &ChainSpec) -> Backend<BlockifierFactory> {
36-
backend_with_db(chain_spec, DbProvider::new_ephemeral())
36+
backend_with_db(chain_spec, DbProvider::new_in_memory())
3737
}
3838

3939
fn backend_with_db(chain_spec: &ChainSpec, provider: impl Database) -> Backend<BlockifierFactory> {
@@ -83,7 +83,7 @@ fn can_initialize_genesis(#[case] chain: ChainSpec) {
8383
#[case::dev(ChainSpec::Dev(dev_chain_spec()))]
8484
#[case::rollup(ChainSpec::Rollup(rollup_chain_spec()))]
8585
fn can_reinitialize_genesis(#[case] chain: ChainSpec) {
86-
let db = DbProvider::new_ephemeral();
86+
let db = DbProvider::new_in_memory();
8787

8888
let backend = backend_with_db(&chain, db.clone());
8989
backend.init_genesis().expect("failed to initialize genesis");
@@ -94,7 +94,7 @@ fn can_reinitialize_genesis(#[case] chain: ChainSpec) {
9494

9595
#[test]
9696
fn reinitialize_with_different_rollup_chain_spec() {
97-
let db = DbProvider::new_ephemeral();
97+
let db = DbProvider::new_in_memory();
9898

9999
let chain1 = ChainSpec::Rollup(rollup_chain_spec());
100100
let backend1 = backend_with_db(&chain1, db.clone());

crates/executor/tests/fixtures/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub fn state_provider(chain: &ChainSpec) -> Box<dyn StateProvider> {
7272
let ChainSpec::Dev(chain) = chain else { panic!("should be dev chain spec") };
7373

7474
let states = chain.state_updates();
75-
let provider = DbProvider::new_ephemeral();
75+
let provider = DbProvider::new_in_memory();
7676

7777
let block = SealedBlockWithStatus {
7878
status: FinalityStatus::AcceptedOnL2,

crates/node/src/full/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use std::sync::Arc;
88

99
use anyhow::Result;
1010
use exit::NodeStoppedFuture;
11-
use katana_db::mdbx::DbEnv;
1211
use katana_feeder_gateway::client::SequencerGateway;
1312
use katana_metrics::exporters::prometheus::PrometheusRecorder;
1413
use katana_metrics::{Report, Server as MetricsServer};
@@ -38,7 +37,7 @@ pub struct Config {
3837

3938
#[derive(Debug)]
4039
pub struct Node {
41-
pub db: DbEnv,
40+
pub db: katana_db::Db,
4241
pub pool: TxPool,
4342
pub config: Arc<Config>,
4443
pub task_manager: TaskManager,
@@ -62,7 +61,7 @@ impl Node {
6261
let path = config.db.dir.clone().expect("database path must exist");
6362

6463
info!(target: "node", path = %path.display(), "Initializing database.");
65-
let db = katana_db::init_db(path)?;
64+
let db = katana_db::Db::new(path)?;
6665

6766
let provider = DbProvider::new(db.clone());
6867

@@ -134,7 +133,7 @@ impl Node {
134133

135134
#[derive(Debug)]
136135
pub struct LaunchedNode {
137-
pub db: DbEnv,
136+
pub db: katana_db::Db,
138137
pub pool: TxPool,
139138
pub task_manager: TaskManager,
140139
pub config: Arc<Config>,

crates/node/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use katana_core::constants::{
2525
};
2626
use katana_core::env::BlockContextGenerator;
2727
use katana_core::service::block_producer::BlockProducer;
28-
use katana_db::mdbx::DbEnv;
28+
use katana_db::Db;
2929
use katana_executor::implementation::blockifier::cache::ClassCache;
3030
use katana_executor::implementation::blockifier::BlockifierFactory;
3131
use katana_executor::ExecutionFlags;
@@ -63,7 +63,7 @@ use crate::exit::NodeStoppedFuture;
6363
pub struct Node {
6464
config: Arc<Config>,
6565
pool: TxPool,
66-
db: DbEnv,
66+
db: katana_db::Db,
6767
rpc_server: RpcServer,
6868
task_manager: TaskManager,
6969
backend: Arc<Backend<BlockifierFactory>>,
@@ -138,7 +138,7 @@ impl Node {
138138
return Err(anyhow::anyhow!("Forking is only supported in dev mode for now"));
139139
};
140140

141-
let db = katana_db::init_ephemeral_db()?;
141+
let db = katana_db::Db::in_memory()?;
142142
let (bc, block_num) =
143143
Blockchain::new_from_forked(db.clone(), cfg.url.clone(), cfg.block, chain_spec)
144144
.await?;
@@ -149,10 +149,10 @@ impl Node {
149149

150150
(bc, db, Some(forked_client))
151151
} else if let Some(db_path) = &config.db.dir {
152-
let db = katana_db::init_db(db_path)?;
152+
let db = katana_db::Db::new(db_path)?;
153153
(Blockchain::new_with_db(db.clone()), db, None)
154154
} else {
155-
let db = katana_db::init_ephemeral_db()?;
155+
let db = katana_db::Db::in_memory()?;
156156
(Blockchain::new_with_db(db.clone()), db, None)
157157
};
158158

@@ -371,7 +371,7 @@ impl Node {
371371
}
372372

373373
/// Returns a reference to the node's database environment (if any).
374-
pub fn db(&self) -> &DbEnv {
374+
pub fn db(&self) -> &Db {
375375
&self.db
376376
}
377377

0 commit comments

Comments
 (0)