diff --git a/bin/reth/src/db/list.rs b/bin/reth/src/db/list.rs index a4059fcf9c..ee9de7eeb4 100644 --- a/bin/reth/src/db/list.rs +++ b/bin/reth/src/db/list.rs @@ -5,7 +5,7 @@ use super::tui::DbListTUI; use eyre::WrapErr; use reth_db::{ database::Database, - mdbx::{Env, WriteMap}, + mdbx::{Env, NoWriteMap}, table::Table, TableType, TableViewer, Tables, }; @@ -34,7 +34,7 @@ pub struct Command { impl Command { /// Execute `db list` command - pub fn execute(self, tool: &DbTool<'_, Env>) -> eyre::Result<()> { + pub fn execute(self, tool: &DbTool<'_, Env>) -> eyre::Result<()> { if self.table.table_type() == TableType::DupSort { error!(target: "reth::cli", "Unsupported table."); } @@ -46,7 +46,7 @@ impl Command { } struct ListTableViewer<'a> { - tool: &'a DbTool<'a, Env>, + tool: &'a DbTool<'a, Env>, args: &'a Command, } diff --git a/bin/reth/src/db/mod.rs b/bin/reth/src/db/mod.rs index 27f188c2a4..8940a1b544 100644 --- a/bin/reth/src/db/mod.rs +++ b/bin/reth/src/db/mod.rs @@ -10,12 +10,12 @@ use eyre::WrapErr; use human_bytes::human_bytes; use reth_db::{ database::Database, + mdbx::{Env, NoWriteMap, WriteMap}, version::{get_db_version, DatabaseVersionError, DB_VERSION}, Tables, }; use reth_primitives::ChainSpec; -use reth_staged_sync::utils::init::init_db; -use std::sync::Arc; +use std::{path::Path, sync::Arc}; mod get; mod list; @@ -81,13 +81,11 @@ impl Command { let data_dir = self.datadir.unwrap_or_chain_default(self.chain.chain); let db_path = data_dir.db_path(); - let db = init_db(&db_path)?; - - let mut tool = DbTool::new(&db, self.chain.clone())?; - match self.command { // TODO: We'll need to add this on the DB trait. Subcommands::Stats { .. } => { + let db = read_only_db(&db_path)?; + let tool = DbTool::new(&db, self.chain.clone())?; let mut stats_table = ComfyTable::new(); stats_table.load_preset(comfy_table::presets::ASCII_MARKDOWN); stats_table.set_header([ @@ -137,12 +135,18 @@ impl Command { println!("{stats_table}"); } Subcommands::List(command) => { + let db = read_only_db(&db_path)?; + let tool = DbTool::new(&db, self.chain.clone())?; command.execute(&tool)?; } Subcommands::Get(command) => { + let db = read_only_db(&db_path)?; + let tool = DbTool::new(&db, self.chain.clone())?; command.execute(&tool)?; } Subcommands::Drop => { + let db = read_write_db(&db_path)?; + let mut tool = DbTool::new(&db, self.chain.clone())?; tool.drop(db_path)?; } Subcommands::Version => { @@ -169,6 +173,16 @@ impl Command { } } +fn read_only_db(path: &Path) -> eyre::Result> { + Env::::open(path, reth_db::mdbx::EnvKind::RO) + .with_context(|| format!("Could not open database at path: {}", path.display())) +} + +fn read_write_db(path: &Path) -> eyre::Result> { + Env::::open(path, reth_db::mdbx::EnvKind::RW) + .with_context(|| format!("Could not open database at path: {}", path.display())) +} + #[cfg(test)] mod tests { use super::*;