config: add general bitcoin backend option

This commit is contained in:
jp1ac4 2024-07-09 11:07:07 +01:00 committed by Michael Mallan
parent e267f66be6
commit 34b9a49328
No known key found for this signature in database
GPG Key ID: 5177CDCEDB0EABEB
3 changed files with 31 additions and 11 deletions

View File

@ -85,6 +85,14 @@ fn default_daemon() -> bool {
false
}
/// Bitcoin backend config.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub enum BitcoinBackend {
/// Settings specific to bitcoind as the Bitcoin interface.
#[serde(rename = "bitcoind_config")]
Bitcoind(BitcoindConfig),
}
/// RPC authentication options.
#[derive(Clone, PartialEq, Serialize)]
pub enum BitcoindRpcAuth {
@ -152,8 +160,9 @@ pub struct Config {
pub main_descriptor: LianaDescriptor,
/// Settings for the Bitcoin interface
pub bitcoin_config: BitcoinConfig,
/// Settings specific to bitcoind as the Bitcoin interface
pub bitcoind_config: Option<BitcoindConfig>,
/// Settings specific to the Bitcoin backend.
#[serde(flatten)]
pub bitcoin_backend: Option<BitcoinBackend>,
}
impl Config {

View File

@ -92,6 +92,7 @@ pub enum StartupError {
DefaultDataDirNotFound,
DatadirCreation(path::PathBuf, io::Error),
MissingBitcoindConfig,
MissingBitcoinBackendConfig,
DbMigrateBitcoinTxs(&'static str),
Database(SqliteDbError),
Bitcoind(BitcoindError),
@ -117,6 +118,10 @@ impl fmt::Display for StartupError {
f,
"Our Bitcoin interface is bitcoind but we have no 'bitcoind_config' entry in the configuration."
),
Self::MissingBitcoinBackendConfig => write!(
f,
"No Bitcoin backend entry in the configuration."
),
Self::DbMigrateBitcoinTxs(msg) => write!(
f,
"Error when migrating Bitcoin transaction from Bitcoin backend to database: {}.", msg
@ -260,8 +265,8 @@ fn setup_bitcoind(
#[cfg(target_os = "windows")]
let wo_path_str = wo_path_str.replace("\\\\?\\", "").replace("\\\\?", "");
let bitcoind_config = config
.bitcoind_config
let config::BitcoinBackend::Bitcoind(bitcoind_config) = config
.bitcoin_backend
.as_ref()
.ok_or(StartupError::MissingBitcoindConfig)?;
let bitcoind = BitcoinD::new(bitcoind_config, wo_path_str)?;
@ -374,7 +379,11 @@ impl DaemonHandle {
// Set up the connection to bitcoind (if using it) first as we may need it for the database
// migration when setting up SQLite below.
let bitcoind = if bitcoin.is_none() {
Some(setup_bitcoind(&config, &data_dir, fresh_data_dir)?)
if let Some(config::BitcoinBackend::Bitcoind(_)) = &config.bitcoin_backend {
Some(setup_bitcoind(&config, &data_dir, fresh_data_dir)?)
} else {
None
}
} else {
None
};
@ -392,11 +401,13 @@ impl DaemonHandle {
};
// Finally set up the Bitcoin backend.
let bit = match (bitcoin, bitcoind) {
(Some(bit), None) => sync::Arc::from(sync::Mutex::from(bit)),
(None, Some(bit)) => sync::Arc::from(sync::Mutex::from(bit))
let bit = match (bitcoin, &config.bitcoin_backend) {
(Some(bit), _) => sync::Arc::from(sync::Mutex::from(bit)),
(None, Some(config::BitcoinBackend::Bitcoind(..))) => sync::Arc::from(
sync::Mutex::from(bitcoind.expect("bitcoind must have been set already")),
)
as sync::Arc<sync::Mutex<dyn BitcoinInterface>>,
_ => unreachable!("Either bitcoind or bitcoin interface is always set."),
(None, None) => Err(StartupError::MissingBitcoinBackendConfig)?,
};
// If we are on a UNIX system and they told us to daemonize, do it now.
@ -764,7 +775,7 @@ mod tests {
let change_desc = desc.change_descriptor().clone();
let config = Config {
bitcoin_config,
bitcoind_config: Some(bitcoind_config),
bitcoin_backend: Some(config::BitcoinBackend::Bitcoind(bitcoind_config)),
data_dir: Some(data_dir),
#[cfg(unix)]
daemon: false,

View File

@ -534,7 +534,7 @@ impl DummyLiana {
let desc = descriptors::LianaDescriptor::new(policy);
let config = Config {
bitcoin_config,
bitcoind_config: None,
bitcoin_backend: None,
data_dir: Some(data_dir),
#[cfg(unix)]
daemon: false,