From 5408a5ac89adf510a280c1984b7e716c4566f53a Mon Sep 17 00:00:00 2001 From: edouard Date: Wed, 23 Mar 2022 15:35:17 +0100 Subject: [PATCH] Add load_config to Daemon trait --- src/app/context.rs | 25 +++++++------------------ src/daemon/embedded.rs | 18 ++++++++++++++++++ src/daemon/mod.rs | 13 +++++-------- 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/src/app/context.rs b/src/app/context.rs index a84cb65f..ded854a3 100644 --- a/src/app/context.rs +++ b/src/app/context.rs @@ -14,7 +14,7 @@ use revault_hwi::{app::revault::RevaultHWI, HWIError}; use crate::{ app::{config, error::Error, menu::Menu}, conversion::Converter, - daemon::{embedded::EmbeddedDaemon, Daemon}, + daemon::Daemon, revault::Role, }; @@ -122,13 +122,12 @@ impl Context { } pub fn load_daemon_config(&mut self, cfg: DaemonConfig) -> Result<(), Error> { - let mut daemon = EmbeddedDaemon::new(); - daemon.start(cfg.clone())?; - - let mut old_daemon = self.revaultd.clone(); - self.revaultd = Arc::new(daemon); - - self.config.daemon = cfg; + loop { + if let Some(daemon) = Arc::get_mut(&mut self.revaultd) { + daemon.load_config(cfg.clone())?; + break; + } + } let mut daemon_config_file = OpenOptions::new() .write(true) @@ -145,16 +144,6 @@ impl Context { Error::ConfigError(e.to_string()) })?; - loop { - match Arc::get_mut(&mut old_daemon) { - None => {} - Some(old) => { - old.stop()?; - break; - } - } - } - Ok(()) } } diff --git a/src/daemon/embedded.rs b/src/daemon/embedded.rs index fd253b62..ef3fd2f8 100644 --- a/src/daemon/embedded.rs +++ b/src/daemon/embedded.rs @@ -5,6 +5,7 @@ use bitcoin::{consensus::encode, util::psbt::PartiallySignedTransaction as Psbt, use super::{model::*, Daemon, RevaultDError}; use revaultd::{ + commands::CommandError, config::Config, revault_tx::transactions::{ CancelTransaction, EmergencyTransaction, RevaultTransaction, SpendTransaction, @@ -13,6 +14,12 @@ use revaultd::{ DaemonHandle, }; +impl From for RevaultDError { + fn from(error: CommandError) -> Self { + RevaultDError::Rpc(error.code() as i32, error.to_string()) + } +} + pub struct EmbeddedDaemon { handle: Option>, } @@ -41,6 +48,17 @@ impl Daemon for EmbeddedDaemon { false } + fn load_config(&mut self, cfg: Config) -> Result<(), RevaultDError> { + if self.handle.is_none() { + return Ok(()); + } + + let next = DaemonHandle::start(cfg).map_err(|e| RevaultDError::Start(e.to_string()))?; + self.handle.take().unwrap().into_inner().unwrap().shutdown(); + self.handle = Some(Mutex::new(next)); + Ok(()) + } + fn stop(&mut self) -> Result<(), RevaultDError> { if let Some(h) = self.handle.take() { let handle = h.into_inner().unwrap(); diff --git a/src/daemon/mod.rs b/src/daemon/mod.rs index a9a13e08..2e68df37 100644 --- a/src/daemon/mod.rs +++ b/src/daemon/mod.rs @@ -3,12 +3,11 @@ pub mod embedded; pub mod model; use std::collections::BTreeMap; -use std::convert::From; use std::fmt::Debug; use std::io::ErrorKind; use bitcoin::{util::psbt::PartiallySignedTransaction as Psbt, OutPoint, Txid}; -use revaultd::commands::CommandError; +use revaultd::config::Config; use model::*; @@ -38,15 +37,13 @@ impl std::fmt::Display for RevaultDError { } } -impl From for RevaultDError { - fn from(error: CommandError) -> Self { - RevaultDError::Rpc(error.code() as i32, error.to_string()) - } -} - pub trait Daemon: Debug { fn is_external(&self) -> bool; + fn load_config(&mut self, _cfg: Config) -> Result<(), RevaultDError> { + return Ok(()); + } + fn stop(&mut self) -> Result<(), RevaultDError>; fn get_deposit_address(&self) -> Result;