lib: gate the RPC server availability on the 'daemon' feature

This is a temporary hack. We should improve this API.
This commit is contained in:
Antoine Poinsot 2024-03-16 11:55:03 +01:00
parent b7fde6a9e4
commit 58c71c794a
No known key found for this signature in database
GPG Key ID: E13FC145CD3F4304
2 changed files with 38 additions and 16 deletions

View File

@ -295,6 +295,7 @@ pub enum DaemonHandle {
poller_handle: thread::JoinHandle<()>,
control: DaemonControl,
},
#[cfg(feature = "daemon")]
Server {
poller_sender: mpsc::SyncSender<poller::PollerMessage>,
poller_handle: thread::JoinHandle<()>,
@ -318,7 +319,7 @@ impl DaemonHandle {
config: Config,
bitcoin: Option<impl BitcoinInterface + 'static>,
db: Option<impl DatabaseInterface + 'static>,
with_rpc_server: bool,
#[cfg(feature = "daemon")] with_rpc_server: bool,
) -> Result<Self, StartupError> {
#[cfg(not(test))]
setup_panic_hook();
@ -392,7 +393,8 @@ impl DaemonHandle {
// structure or through the JSONRPC server we may setup below.
let control = DaemonControl::new(config, bit, poller_sender.clone(), db, secp);
Ok(if with_rpc_server {
#[cfg(feature = "daemon")]
if with_rpc_server {
let rpcserver_shutdown = sync::Arc::from(sync::atomic::AtomicBool::from(false));
let rpcserver_handle = thread::Builder::new()
.name("Bitcoin Network poller".to_string())
@ -411,18 +413,18 @@ impl DaemonHandle {
})
.expect("Spawning the RPC server thread should never fail.");
DaemonHandle::Server {
return Ok(DaemonHandle::Server {
poller_sender,
poller_handle,
rpcserver_shutdown,
rpcserver_handle,
}
} else {
DaemonHandle::Controller {
poller_sender,
poller_handle,
control,
}
});
}
Ok(DaemonHandle::Controller {
poller_sender,
poller_handle,
control,
})
}
@ -430,12 +432,13 @@ impl DaemonHandle {
/// and SQLite).
pub fn start_default(
config: Config,
with_rpc_server: bool,
#[cfg(feature = "daemon")] with_rpc_server: bool,
) -> Result<DaemonHandle, StartupError> {
Self::start(
config,
Option::<BitcoinD>::None,
Option::<SqliteDb>::None,
#[cfg(feature = "daemon")]
with_rpc_server,
)
}
@ -448,6 +451,7 @@ impl DaemonHandle {
Self::Controller {
ref poller_handle, ..
} => !poller_handle.is_finished(),
#[cfg(feature = "daemon")]
Self::Server {
ref poller_handle,
ref rpcserver_handle,
@ -470,6 +474,7 @@ impl DaemonHandle {
poller_handle.join().expect("Poller thread must not panic");
Ok(())
}
#[cfg(feature = "daemon")]
Self::Server {
poller_sender,
poller_handle,
@ -730,7 +735,12 @@ mod tests {
let t = thread::spawn({
let config = config.clone();
move || {
let handle = DaemonHandle::start_default(config, false).unwrap();
let handle = DaemonHandle::start_default(
config,
#[cfg(feature = "daemon")]
false,
)
.unwrap();
handle.stop().unwrap();
}
});
@ -750,7 +760,12 @@ mod tests {
let t = thread::spawn({
let config = config.clone();
move || {
let handle = DaemonHandle::start_default(config, false).unwrap();
let handle = DaemonHandle::start_default(
config,
#[cfg(feature = "daemon")]
false,
)
.unwrap();
handle.stop().unwrap();
}
});

View File

@ -498,9 +498,14 @@ impl DummyLiana {
main_descriptor: desc,
};
let handle =
DaemonHandle::start(config, Some(bitcoin_interface), Some(database), rpc_server)
.unwrap();
let handle = DaemonHandle::start(
config,
Some(bitcoin_interface),
Some(database),
#[cfg(feature = "daemon")]
rpc_server,
)
.unwrap();
DummyLiana { tmp_dir, handle }
}
@ -513,6 +518,7 @@ impl DummyLiana {
}
/// Creates a new DummyLiana interface which also spins up an RPC server.
#[cfg(feature = "daemon")]
pub fn new_server(
bitcoin_interface: impl BitcoinInterface + 'static,
database: impl DatabaseInterface + 'static,
@ -523,6 +529,7 @@ impl DummyLiana {
pub fn control(&self) -> &DaemonControl {
match self.handle {
DaemonHandle::Controller { ref control, .. } => control,
#[cfg(feature = "daemon")]
DaemonHandle::Server { .. } => unreachable!(),
}
}