Skip to content

Commit

Permalink
feat(cli)!: Replace sled stores with redb stores (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
oblique authored Apr 18, 2024
1 parent 8b964bc commit 7b66dfe
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 42 deletions.
7 changes: 4 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,11 @@ clap = { version = "4.4.4", features = ["derive"] }
directories = "5.0.1"
dotenvy = "0.15.7"
mime_guess = "2.0"
redb = "2"
rust-embed = { version = "8.0.0", features = ["interpolate-folder-path"] }
serde = "1.0.189"
serde_repr = "0.1"
# Upgrading this dependency invalidates existing persistent dbs.
# Those can be restored by migrating between versions:
# https://docs.rs/sled/latest/sled/struct.Db.html#examples-1
sled = "0.34.7"
tokio = { version = "1.29.0", features = ["fs", "macros", "rt-multi-thread"] }
tokio = { version = "1.29.0", features = ["macros", "rt-multi-thread"] }
tracing = "0.1.37"
tracing-appender = "0.2.2"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
Expand Down
83 changes: 50 additions & 33 deletions cli/src/native.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::env;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Duration;

use anyhow::{bail, Context, Result};
Expand All @@ -8,12 +9,10 @@ use celestia_rpc::Client;
use clap::Parser;
use directories::ProjectDirs;
use libp2p::{identity, multiaddr::Protocol, Multiaddr};
use lumina_node::blockstore::SledBlockstore;
use lumina_node::blockstore::RedbBlockstore;
use lumina_node::network::{canonical_network_bootnodes, network_genesis, network_id, Network};
use lumina_node::node::{Node, NodeConfig};
use lumina_node::store::{SledStore, Store};
use sled::Db;
use tokio::fs;
use lumina_node::store::{RedbStore, Store};
use tokio::task::spawn_blocking;
use tokio::time::sleep;
use tracing::info;
Expand Down Expand Up @@ -60,8 +59,8 @@ pub(crate) async fn run(args: Params) -> Result<()> {

info!("Initializing store");
let db = open_db(args.store, &network_id).await?;
let store = SledStore::new(db.clone()).await?;
let blockstore = SledBlockstore::new(db).await?;
let store = RedbStore::new(db.clone()).await?;
let blockstore = RedbBlockstore::new(db);

match store.head_height().await {
Ok(height) => info!("Initialised store with head height: {height}"),
Expand All @@ -88,35 +87,53 @@ pub(crate) async fn run(args: Params) -> Result<()> {
}
}

async fn open_db(path: Option<PathBuf>, network_id: &str) -> Result<Db> {
if let Some(path) = path {
let db = spawn_blocking(|| sled::open(path)).await??;
return Ok(db);
}
async fn open_db(path: Option<PathBuf>, network_id: &str) -> Result<Arc<redb::Database>> {
let network_id = network_id.to_owned();

spawn_blocking(move || {
use std::fs;

let cache_dir =
ProjectDirs::from("co", "eiger", "lumina").context("Couldn't find lumina's cache dir")?;
let mut cache_dir = cache_dir.cache_dir().to_owned();

// TODO: remove it in 2 months or after a few releases
// If we find an old ('celestia') cache dir, move it to the new one.
if let Some(old_cache_dir) = ProjectDirs::from("co", "eiger", "celestia") {
let old_cache_dir = old_cache_dir.cache_dir();
if old_cache_dir.exists() && !cache_dir.exists() {
warn!(
"Migrating old cache dir to a new location: {} -> {}",
old_cache_dir.display(),
cache_dir.display()
);
fs::rename(old_cache_dir, &cache_dir).await?;
if let Some(path) = path {
let db = redb::Database::create(path)?;
return Ok(Arc::new(db));
}
}

cache_dir.push(network_id);
// TODO: should we create there also a subdirectory for the 'db'
// in case we want to put there some other stuff too?
let db = spawn_blocking(|| sled::open(cache_dir)).await??;
Ok(db)
let cache_dir = ProjectDirs::from("co", "eiger", "lumina")
.context("failed to construct project path")?
.cache_dir()
.join(&network_id)
.to_owned();

let old_cache_dir = ProjectDirs::from("co", "eiger", "celestia")
.context("failed to construct project path")?
.cache_dir()
.join(&network_id)
.to_owned();

if is_sled_db(&old_cache_dir) {
warn!("Removing deprecated store {}", old_cache_dir.display());
fs::remove_dir_all(&old_cache_dir)?;
}

if is_sled_db(&cache_dir) {
warn!("Removing deprecated store {}", cache_dir.display());
fs::remove_dir_all(&cache_dir)?;
}

// Directories need to pre-exist
fs::create_dir_all(&cache_dir)?;

let path = cache_dir.join("db");
let db = redb::Database::create(path)?;

Ok(Arc::new(db))
})
.await?
}

fn is_sled_db(path: impl AsRef<Path>) -> bool {
let path = path.as_ref();
path.join("blobs").is_dir() && path.join("conf").is_file() && path.join("db").is_file()
}

/// Get the address of the local bridge node
Expand Down
2 changes: 1 addition & 1 deletion node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ tracing = "0.1.37"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
backoff = { version = "0.4.0", features = ["tokio"] }
blockstore = { workspace = true, features = ["sled"] }
blockstore = { workspace = true, features = ["sled", "redb"] }
# Upgrading this dependency invalidates existing persistent dbs.
# Those can be restored by migrating between versions:
# https://docs.rs/sled/latest/sled/struct.Db.html#examples-1
Expand Down
6 changes: 6 additions & 0 deletions node/src/blockstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ pub type InMemoryBlockstore = blockstore::InMemoryBlockstore<MAX_MH_SIZE>;
/// [`SledBlockstore`]: blockstore::SledBlockstore
pub type SledBlockstore = blockstore::SledBlockstore;

#[cfg(not(target_arch = "wasm32"))]
/// A [`RedbBlockstore`].
///
/// [`RedbBlockstore`]: blockstore::RedbBlockstore
pub type RedbBlockstore = blockstore::RedbBlockstore;

#[cfg(target_arch = "wasm32")]
/// An [`IndexedDbBlockstore`].
///
Expand Down

0 comments on commit 7b66dfe

Please sign in to comment.