Skip to content

Commit

Permalink
Add load_config to Daemon trait
Browse files Browse the repository at this point in the history
  • Loading branch information
edouardparis committed Mar 23, 2022
1 parent 392cdc8 commit 5408a5a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
25 changes: 7 additions & 18 deletions src/app/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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)
Expand All @@ -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(())
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/daemon/embedded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -13,6 +14,12 @@ use revaultd::{
DaemonHandle,
};

impl From<CommandError> for RevaultDError {
fn from(error: CommandError) -> Self {
RevaultDError::Rpc(error.code() as i32, error.to_string())
}
}

pub struct EmbeddedDaemon {
handle: Option<Mutex<DaemonHandle>>,
}
Expand Down Expand Up @@ -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();
Expand Down
13 changes: 5 additions & 8 deletions src/daemon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand Down Expand Up @@ -38,15 +37,13 @@ impl std::fmt::Display for RevaultDError {
}
}

impl From<CommandError> 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<bitcoin::Address, RevaultDError>;
Expand Down

0 comments on commit 5408a5a

Please sign in to comment.