check if embedded daemon is stopped before request

This commit is contained in:
edouard 2023-08-23 12:23:12 +02:00
parent 031dd45df1
commit caa9d385b9
4 changed files with 26 additions and 19 deletions

View File

@ -25,6 +25,7 @@ impl std::fmt::Display for Error {
Self::Daemon(e) => match e {
DaemonError::Unexpected(e) => write!(f, "{}", e),
DaemonError::NoAnswer => write!(f, "Daemon did not answer"),
DaemonError::DaemonStopped => write!(f, "Daemon stopped"),
DaemonError::Transport(Some(ErrorKind::ConnectionRefused), _) => {
write!(f, "Failed to connect to daemon")
}

View File

@ -33,6 +33,7 @@ impl From<&Error> for WarningMessage {
DaemonError::NoAnswer | DaemonError::Transport(..) => {
WarningMessage("Communication with Daemon failed".to_string())
}
DaemonError::DaemonStopped => WarningMessage("Daemon stopped".to_string()),
},
Error::Unexpected(_) => WarningMessage("Unknown error".to_string()),
Error::HardwareWallet(_) => WarningMessage("Hardware wallet error".to_string()),

View File

@ -4,7 +4,7 @@ use super::{model::*, Daemon, DaemonError};
use liana::{
config::Config,
miniscript::bitcoin::{address, psbt::Psbt, Address, OutPoint, Txid},
DaemonHandle,
DaemonControl, DaemonHandle,
};
pub struct EmbeddedDaemon {
@ -17,6 +17,14 @@ impl EmbeddedDaemon {
let handle = DaemonHandle::start_default(config.clone()).map_err(DaemonError::Start)?;
Ok(Self { handle, config })
}
fn control(&self) -> Result<&DaemonControl, DaemonError> {
if self.handle.shutdown_complete() {
Err(DaemonError::DaemonStopped)
} else {
Ok(&self.handle.control)
}
}
}
impl std::fmt::Debug for EmbeddedDaemon {
@ -43,19 +51,19 @@ impl Daemon for EmbeddedDaemon {
}
fn get_info(&self) -> Result<GetInfoResult, DaemonError> {
Ok(self.handle.control.get_info())
Ok(self.control()?.get_info())
}
fn get_new_address(&self) -> Result<GetAddressResult, DaemonError> {
Ok(self.handle.control.get_new_address())
Ok(self.control()?.get_new_address())
}
fn list_coins(&self) -> Result<ListCoinsResult, DaemonError> {
Ok(self.handle.control.list_coins())
Ok(self.control()?.list_coins())
}
fn list_spend_txs(&self) -> Result<ListSpendResult, DaemonError> {
Ok(self.handle.control.list_spend())
Ok(self.control()?.list_spend())
}
fn list_confirmed_txs(
@ -65,13 +73,12 @@ impl Daemon for EmbeddedDaemon {
limit: u64,
) -> Result<ListTransactionsResult, DaemonError> {
Ok(self
.handle
.control
.control()?
.list_confirmed_transactions(start, end, limit))
}
fn list_txs(&self, txids: &[Txid]) -> Result<ListTransactionsResult, DaemonError> {
Ok(self.handle.control.list_transactions(txids))
Ok(self.control()?.list_transactions(txids))
}
fn create_spend_tx(
@ -80,34 +87,30 @@ impl Daemon for EmbeddedDaemon {
destinations: &HashMap<Address<address::NetworkUnchecked>, u64>,
feerate_vb: u64,
) -> Result<CreateSpendResult, DaemonError> {
self.handle
.control
self.control()?
.create_spend(destinations, coins_outpoints, feerate_vb)
.map_err(|e| DaemonError::Unexpected(e.to_string()))
}
fn update_spend_tx(&self, psbt: &Psbt) -> Result<(), DaemonError> {
self.handle
.control
self.control()?
.update_spend(psbt.clone())
.map_err(|e| DaemonError::Unexpected(e.to_string()))
}
fn delete_spend_tx(&self, txid: &Txid) -> Result<(), DaemonError> {
self.handle.control.delete_spend(txid);
self.control()?.delete_spend(txid);
Ok(())
}
fn broadcast_spend_tx(&self, txid: &Txid) -> Result<(), DaemonError> {
self.handle
.control
self.control()?
.broadcast_spend(txid)
.map_err(|e| DaemonError::Unexpected(e.to_string()))
}
fn start_rescan(&self, t: u32) -> Result<(), DaemonError> {
self.handle
.control
self.control()?
.start_rescan(t)
.map_err(|e| DaemonError::Unexpected(e.to_string()))
}
@ -118,8 +121,7 @@ impl Daemon for EmbeddedDaemon {
feerate_vb: u64,
sequence: Option<u16>,
) -> Result<Psbt, DaemonError> {
self.handle
.control
self.control()?
.create_recovery(address, feerate_vb, sequence)
.map_err(|e| DaemonError::Unexpected(e.to_string()))
.map(|res| res.psbt)

View File

@ -22,6 +22,8 @@ pub enum DaemonError {
Unexpected(String),
/// No response.
NoAnswer,
/// Daemon stopped
DaemonStopped,
// Error at start up.
Start(StartupError),
// Error if the client is not supported.
@ -33,6 +35,7 @@ impl std::fmt::Display for DaemonError {
match self {
Self::Rpc(code, e) => write!(f, "Daemon error rpc call: [{:?}] {}", code, e),
Self::NoAnswer => write!(f, "Daemon returned no answer"),
Self::DaemonStopped => write!(f, "Daemon stopped"),
Self::Transport(kind, e) => write!(f, "Daemon transport error: [{:?}] {}", kind, e),
Self::Unexpected(e) => write!(f, "Daemon unexpected error: {}", e),
Self::Start(e) => write!(f, "Daemon did not start: {}", e),