Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add key-value-db and sqlite-db features, separate wallet directories #71

Merged
merged 4 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Make `regtest` the default network.
- Add experimental `regtest-*` features to automatically deploy local regtest nodes
(bitcoind, and electrs) while running cli commands.
- Put cached wallet data in separate directories: ~/.bdk-bitcoin/<wallet_name>
- Add distinct `key-value-db` and `sqlite-db` features, keep default as `key-value-db`

## [0.4.0]

Expand Down
68 changes: 68 additions & 0 deletions Cargo.lock

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

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ bdk-reserves = { version = "0.17", optional = true}
electrsd = { version= "0.12", features = ["trigger", "bitcoind_22_0"], optional = true}

[features]
default = ["cli", "repl"]
cli = ["bdk/key-value-db", "clap", "dirs-next", "env_logger"]
default = ["cli", "repl", "key-value-db"]
cli = ["clap", "dirs-next", "env_logger"]
repl = ["regex", "rustyline", "fd-lock"]
key-value-db = ["bdk/key-value-db"]
sqlite-db = ["bdk/sqlite"]
electrum = ["bdk/electrum"]
esplora = []
esplora-ureq = ["esplora", "bdk/use-esplora-ureq"]
Expand Down
17 changes: 17 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,21 @@ fn main() {
if blockchain_features.len() > 1 {
panic!("At most one blockchain client feature can be enabled but these features were enabled: {:?}", blockchain_features)
}

let key_value_db =
env::var_os("CARGO_FEATURE_KEY_VALUE_DB").map(|_| "key-value-db".to_string());
let sqlite_db = env::var_os("CARGO_FEATURE_SQLITE_DB").map(|_| "sqlite-db".to_string());

let database_features: Vec<String> = vec![key_value_db, sqlite_db]
.iter()
.map(|f| f.to_owned())
.flatten()
.collect();

if database_features.len() > 1 {
panic!(
"At most one database feature can be enabled but these features were enabled: {:?}",
database_features
)
}
}
92 changes: 73 additions & 19 deletions src/bdk_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ use bdk::blockchain::{AnyBlockchain, AnyBlockchainConfig, ConfigurableBlockchain
#[cfg(feature = "rpc")]
use bdk::blockchain::rpc::{Auth, RpcConfig};

use bdk::database::BatchDatabase;
use bdk::sled;
use bdk::sled::Tree;
#[cfg(feature = "key-value-db")]
use bdk::database::any::SledDbConfiguration;
#[cfg(feature = "sqlite-db")]
use bdk::database::any::SqliteDbConfiguration;
use bdk::database::{AnyDatabase, AnyDatabaseConfig, BatchDatabase, ConfigurableDatabase};
use bdk::wallet::wallet_name_from_descriptor;
use bdk::Wallet;
use bdk::{bitcoin, Error};
Expand Down Expand Up @@ -88,7 +90,8 @@ enum ReplSubCommand {
Exit,
}

fn prepare_home_dir() -> Result<PathBuf, Error> {
/// prepare bdk_cli home and wallet directory
fn prepare_home_wallet_dir(wallet_name: &str) -> Result<PathBuf, Error> {
let mut dir = PathBuf::new();
dir.push(
&dirs_next::home_dir().ok_or_else(|| Error::Generic("home dir not found".to_string()))?,
Expand All @@ -100,25 +103,75 @@ fn prepare_home_dir() -> Result<PathBuf, Error> {
fs::create_dir(&dir).map_err(|e| Error::Generic(e.to_string()))?;
}

#[cfg(not(feature = "compact_filters"))]
dir.push("database.sled");
dir.push(wallet_name);

if !dir.exists() {
info!("Creating wallet directory {}", dir.as_path().display());
fs::create_dir(&dir).map_err(|e| Error::Generic(e.to_string()))?;
}

#[cfg(feature = "compact_filters")]
dir.push("compact_filters");
Ok(dir)
}

fn open_database(wallet_opts: &WalletOpts) -> Result<Tree, Error> {
let mut database_path = prepare_home_dir()?;
let wallet_name = wallet_opts
.wallet
.as_deref()
.expect("We should always have a wallet name at this point");
database_path.push(wallet_name);
let database = sled::open(database_path)?;
let tree = database.open_tree(&wallet_name)?;
/// Prepare wallet database directory
fn prepare_wallet_db_dir(wallet_name: &str) -> Result<PathBuf, Error> {
let mut db_dir = prepare_home_wallet_dir(wallet_name)?;

#[cfg(feature = "key-value-db")]
db_dir.push("wallet.sled");

#[cfg(feature = "sqlite-db")]
db_dir.push("wallet.sqlite");

#[cfg(not(feature = "sqlite-db"))]
if !db_dir.exists() {
info!("Creating database directory {}", db_dir.as_path().display());
fs::create_dir(&db_dir).map_err(|e| Error::Generic(e.to_string()))?;
}
Comment on lines +126 to +130
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not understand the reason of feature guard here. For both sled and sqlite we would want to check if the dir exists right??

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason is that sqlite creates only a single file and doesn't need a directory to exist to put it's files in.


Ok(db_dir)
}

/// Prepare blockchain data directory (for compact filters)
#[cfg(feature = "compact_filters")]
fn prepare_bc_dir(wallet_name: &str) -> Result<PathBuf, Error> {
let mut bc_dir = prepare_home_wallet_dir(wallet_name)?;

bc_dir.push("compact_filters");

if !bc_dir.exists() {
info!(
"Creating blockchain directory {}",
bc_dir.as_path().display()
);
fs::create_dir(&bc_dir).map_err(|e| Error::Generic(e.to_string()))?;
}

Ok(bc_dir)
}

fn open_database(wallet_opts: &WalletOpts) -> Result<AnyDatabase, Error> {
let wallet_name = wallet_opts.wallet.as_ref().expect("wallet name");
let database_path = prepare_wallet_db_dir(wallet_name)?;

#[cfg(feature = "key-value-db")]
let config = AnyDatabaseConfig::Sled(SledDbConfiguration {
path: database_path
.into_os_string()
.into_string()
.expect("path string"),
tree_name: wallet_name.to_string(),
});
#[cfg(feature = "sqlite-db")]
let config = AnyDatabaseConfig::Sqlite(SqliteDbConfiguration {
path: database_path
.into_os_string()
.into_string()
.expect("path string"),
});
let database = AnyDatabase::from_config(&config)?;
debug!("database opened successfully");
Ok(tree)
Ok(database)
}

#[allow(dead_code)]
Expand Down Expand Up @@ -180,10 +233,11 @@ fn new_blockchain(
}
}

let wallet_name = wallet_opts.wallet.as_ref().expect("wallet name");
AnyBlockchainConfig::CompactFilters(CompactFiltersBlockchainConfig {
peers,
network: _network,
storage_dir: prepare_home_dir()?
storage_dir: prepare_bc_dir(wallet_name)?
.into_os_string()
.into_string()
.map_err(|_| Error::Generic("Internal OS_String conversion error".to_string()))?,
Expand Down