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

Use a different DB file per network #91

Merged
merged 2 commits into from
Apr 17, 2024
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
4 changes: 2 additions & 2 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use anyhow::{anyhow, Result};
use clap::Parser;
use commands::{handle_command, CliHelper, Command, CommandResult};
use log::{error, info};
use ls_sdk::{Network, Wallet};
use ls_sdk::{Network, Wallet, DEFAULT_DATA_DIR};
use persist::CliPersistence;
use rustyline::{error::ReadlineError, hint::HistoryHinter, Editor};

Expand Down Expand Up @@ -45,7 +45,7 @@ fn main() -> Result<()> {
})
.init();

let data_dir_str = args.data_dir.clone().unwrap_or(".data".to_string());
let data_dir_str = args.data_dir.unwrap_or(DEFAULT_DATA_DIR.to_string());
let data_dir = PathBuf::from(&data_dir_str);
fs::create_dir_all(&data_dir)?;

Expand Down
4 changes: 2 additions & 2 deletions lib/ls-sdk-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ pub use wallet::*;

// To avoid sendrawtransaction error "min relay fee not met"
const CLAIM_ABSOLUTE_FEES: u64 = 134;
const DEFAULT_DATA_DIR: &str = ".data";
const MAIN_DB_FILE: &str = "storage.sql";

pub const DEFAULT_DATA_DIR: &str = ".data";

#[macro_export]
macro_rules! ensure_sdk {
Expand Down
21 changes: 14 additions & 7 deletions lib/ls-sdk-core/src/persist/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,35 @@ mod migrations;
use std::{fs::create_dir_all, path::PathBuf, str::FromStr};

use anyhow::Result;
use migrations::current_migrations;
use rusqlite::{params, Connection};
use rusqlite_migration::{Migrations, M};

use crate::{OngoingSwap, MAIN_DB_FILE};

use migrations::current_migrations;
use crate::{Network, Network::*, OngoingSwap};

pub(crate) struct Persister {
pub(crate) main_db_dir: PathBuf,
main_db_dir: PathBuf,
network: Network,
}

impl Persister {
pub fn new(working_dir: &str) -> Result<Self> {
pub fn new(working_dir: &str, network: Network) -> Result<Self> {
let main_db_dir = PathBuf::from_str(working_dir)?;
if !main_db_dir.exists() {
create_dir_all(&main_db_dir)?;
}
Ok(Persister { main_db_dir })
Ok(Persister {
main_db_dir,
network,
})
}

pub(crate) fn get_connection(&self) -> Result<Connection> {
Ok(Connection::open(self.main_db_dir.join(MAIN_DB_FILE))?)
let db_file = match self.network {
Liquid => "storage.sql",
LiquidTestnet => "storage-testnet.sql",
};
Ok(Connection::open(self.main_db_dir.join(db_file))?)
}

pub fn init(&self) -> Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion lib/ls-sdk-core/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl Wallet {

fs::create_dir_all(&data_dir_path)?;

let persister = Persister::new(&data_dir_path)?;
let persister = Persister::new(&data_dir_path, network)?;
persister.init()?;

let wallet = Arc::new(Wallet {
Expand Down