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

Auto-rename of db_dir/mainnet to db_dir/bitcoin #513

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Empty file modified src/bin/electrs.rs
100644 → 100755
Empty file.
40 changes: 40 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,50 @@ pub fn run() -> Result<()> {
result.context("electrs failed")
}

fn rename_db_dir(config: &Config) -> Result<()> {
use std::{fs, io};

match (config.network, config.auto_reindex, config.db_path.parent()) {
(bitcoin::Network::Bitcoin, true, Some(db_parent)) => {
let old_dir = db_parent.join("mainnet");
match fs::rename(&old_dir, &config.db_path) {
Ok(()) => Ok(()),
Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(()),
Err(error) => Err(error).with_context(|| {
format!(
"failed to rename the old directory ({}) to {}",
old_dir.display(),
config.db_path.display()
)
}),
}
}
(bitcoin::Network::Bitcoin, false, Some(db_parent)) => {
let old_dir = db_parent.join("mainnet");
match fs::metadata(&old_dir) {
Ok(_) => Err(anyhow::anyhow!(
"The old directory {} exists but auto reindex was disabled",
old_dir.display()
)),
Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(()),
Err(error) => Err(error).with_context(|| {
format!(
"failed to check whether the old directory ({}) exists",
old_dir.display()
)
}),
}
}
_ => Ok(()),
}
}

fn serve() -> Result<()> {
let config = Config::from_args();
let metrics = Metrics::new(config.monitoring_addr)?;

rename_db_dir(&config)?;

let (server_tx, server_rx) = unbounded();
if !config.disable_electrum_rpc {
let listener = TcpListener::bind(config.electrum_rpc_addr)?;
Expand Down