Skip to content

Commit

Permalink
lib: gate the RPC server availability on the 'daemon' feature
Browse files Browse the repository at this point in the history
This is a temporary hack. We should improve this API.
  • Loading branch information
darosior committed Mar 21, 2024
1 parent b7fde6a commit 58c71c7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
41 changes: 28 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()>,
Expand All @@ -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();
Expand Down Expand Up @@ -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())
Expand All @@ -411,31 +413,32 @@ 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,
})
}

/// Start the Liana daemon with the default Bitcoin and database interfaces (`bitcoind` RPC
/// 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,
)
}
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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();
}
});
Expand All @@ -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();
}
});
Expand Down
13 changes: 10 additions & 3 deletions src/testutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}

Expand All @@ -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,
Expand All @@ -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!(),
}
}
Expand Down

0 comments on commit 58c71c7

Please sign in to comment.