mirror of
https://github.com/paradigmxyz/reth.git
synced 2026-04-30 03:01:58 -04:00
fix: handle missing rocksdb gracefully in read-only db commands (#22394)
Co-authored-by: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -146,11 +146,24 @@ impl<C: ChainSpecParser> EnvironmentArgs<C> {
|
||||
})
|
||||
}
|
||||
};
|
||||
let rocksdb_provider = RocksDBProvider::builder(data_dir.rocksdb())
|
||||
.with_default_tables()
|
||||
.with_database_log_level(self.db.log_level)
|
||||
.with_read_only(!access.is_read_write())
|
||||
.build()?;
|
||||
let rocksdb_provider = if !access.is_read_write() && !RocksDBProvider::exists(&rocksdb_path)
|
||||
{
|
||||
// RocksDB database doesn't exist yet (e.g. datadir restored from a snapshot
|
||||
// or created before RocksDB storage). Create an empty one so read-only
|
||||
// commands can proceed.
|
||||
debug!(target: "reth::cli", ?rocksdb_path, "RocksDB not found, initializing empty database");
|
||||
reth_fs_util::create_dir_all(&rocksdb_path)?;
|
||||
RocksDBProvider::builder(data_dir.rocksdb())
|
||||
.with_default_tables()
|
||||
.with_database_log_level(self.db.log_level)
|
||||
.build()?
|
||||
} else {
|
||||
RocksDBProvider::builder(data_dir.rocksdb())
|
||||
.with_default_tables()
|
||||
.with_database_log_level(self.db.log_level)
|
||||
.with_read_only(!access.is_read_write())
|
||||
.build()?
|
||||
};
|
||||
|
||||
let provider_factory =
|
||||
self.create_provider_factory(&config, db, sfp, rocksdb_provider, access, runtime)?;
|
||||
|
||||
@@ -691,6 +691,14 @@ impl RocksDBProvider {
|
||||
RocksDBBuilder::new(path)
|
||||
}
|
||||
|
||||
/// Returns `true` if a `RocksDB` database exists at the given path.
|
||||
///
|
||||
/// Checks for the presence of the `CURRENT` file, which `RocksDB` creates
|
||||
/// when initializing a database.
|
||||
pub fn exists(path: impl AsRef<Path>) -> bool {
|
||||
path.as_ref().join("CURRENT").exists()
|
||||
}
|
||||
|
||||
/// Returns `true` if this provider is in read-only mode.
|
||||
pub fn is_read_only(&self) -> bool {
|
||||
matches!(self.0.as_ref(), RocksDBProviderInner::ReadOnly { .. })
|
||||
|
||||
@@ -74,6 +74,13 @@ impl RocksDBProvider {
|
||||
RocksDBBuilder::new(path)
|
||||
}
|
||||
|
||||
/// Returns `true` if a `RocksDB` database exists at the given path (stub implementation).
|
||||
///
|
||||
/// Always returns `false` since `RocksDB` is not available.
|
||||
pub fn exists(_path: impl AsRef<Path>) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// Check consistency of `RocksDB` tables (stub implementation).
|
||||
///
|
||||
/// Returns `None` since there is no `RocksDB` data to check when the feature is disabled.
|
||||
|
||||
Reference in New Issue
Block a user