mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
fix: use RO database for read only commands (#3368)
This commit is contained in:
@ -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<WriteMap>>) -> eyre::Result<()> {
|
||||
pub fn execute(self, tool: &DbTool<'_, Env<NoWriteMap>>) -> 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<WriteMap>>,
|
||||
tool: &'a DbTool<'a, Env<NoWriteMap>>,
|
||||
args: &'a Command,
|
||||
}
|
||||
|
||||
|
||||
@ -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<NoWriteMap>> {
|
||||
Env::<NoWriteMap>::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<WriteMap>> {
|
||||
Env::<WriteMap>::open(path, reth_db::mdbx::EnvKind::RW)
|
||||
.with_context(|| format!("Could not open database at path: {}", path.display()))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
Reference in New Issue
Block a user