Skip to content

Commit 5408a5a

Browse files
committed
Add load_config to Daemon trait
1 parent 392cdc8 commit 5408a5a

File tree

3 files changed

+30
-26
lines changed

3 files changed

+30
-26
lines changed

src/app/context.rs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use revault_hwi::{app::revault::RevaultHWI, HWIError};
1414
use crate::{
1515
app::{config, error::Error, menu::Menu},
1616
conversion::Converter,
17-
daemon::{embedded::EmbeddedDaemon, Daemon},
17+
daemon::Daemon,
1818
revault::Role,
1919
};
2020

@@ -122,13 +122,12 @@ impl Context {
122122
}
123123

124124
pub fn load_daemon_config(&mut self, cfg: DaemonConfig) -> Result<(), Error> {
125-
let mut daemon = EmbeddedDaemon::new();
126-
daemon.start(cfg.clone())?;
127-
128-
let mut old_daemon = self.revaultd.clone();
129-
self.revaultd = Arc::new(daemon);
130-
131-
self.config.daemon = cfg;
125+
loop {
126+
if let Some(daemon) = Arc::get_mut(&mut self.revaultd) {
127+
daemon.load_config(cfg.clone())?;
128+
break;
129+
}
130+
}
132131

133132
let mut daemon_config_file = OpenOptions::new()
134133
.write(true)
@@ -145,16 +144,6 @@ impl Context {
145144
Error::ConfigError(e.to_string())
146145
})?;
147146

148-
loop {
149-
match Arc::get_mut(&mut old_daemon) {
150-
None => {}
151-
Some(old) => {
152-
old.stop()?;
153-
break;
154-
}
155-
}
156-
}
157-
158147
Ok(())
159148
}
160149
}

src/daemon/embedded.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use bitcoin::{consensus::encode, util::psbt::PartiallySignedTransaction as Psbt,
55

66
use super::{model::*, Daemon, RevaultDError};
77
use revaultd::{
8+
commands::CommandError,
89
config::Config,
910
revault_tx::transactions::{
1011
CancelTransaction, EmergencyTransaction, RevaultTransaction, SpendTransaction,
@@ -13,6 +14,12 @@ use revaultd::{
1314
DaemonHandle,
1415
};
1516

17+
impl From<CommandError> for RevaultDError {
18+
fn from(error: CommandError) -> Self {
19+
RevaultDError::Rpc(error.code() as i32, error.to_string())
20+
}
21+
}
22+
1623
pub struct EmbeddedDaemon {
1724
handle: Option<Mutex<DaemonHandle>>,
1825
}
@@ -41,6 +48,17 @@ impl Daemon for EmbeddedDaemon {
4148
false
4249
}
4350

51+
fn load_config(&mut self, cfg: Config) -> Result<(), RevaultDError> {
52+
if self.handle.is_none() {
53+
return Ok(());
54+
}
55+
56+
let next = DaemonHandle::start(cfg).map_err(|e| RevaultDError::Start(e.to_string()))?;
57+
self.handle.take().unwrap().into_inner().unwrap().shutdown();
58+
self.handle = Some(Mutex::new(next));
59+
Ok(())
60+
}
61+
4462
fn stop(&mut self) -> Result<(), RevaultDError> {
4563
if let Some(h) = self.handle.take() {
4664
let handle = h.into_inner().unwrap();

src/daemon/mod.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ pub mod embedded;
33
pub mod model;
44

55
use std::collections::BTreeMap;
6-
use std::convert::From;
76
use std::fmt::Debug;
87
use std::io::ErrorKind;
98

109
use bitcoin::{util::psbt::PartiallySignedTransaction as Psbt, OutPoint, Txid};
11-
use revaultd::commands::CommandError;
10+
use revaultd::config::Config;
1211

1312
use model::*;
1413

@@ -38,15 +37,13 @@ impl std::fmt::Display for RevaultDError {
3837
}
3938
}
4039

41-
impl From<CommandError> for RevaultDError {
42-
fn from(error: CommandError) -> Self {
43-
RevaultDError::Rpc(error.code() as i32, error.to_string())
44-
}
45-
}
46-
4740
pub trait Daemon: Debug {
4841
fn is_external(&self) -> bool;
4942

43+
fn load_config(&mut self, _cfg: Config) -> Result<(), RevaultDError> {
44+
return Ok(());
45+
}
46+
5047
fn stop(&mut self) -> Result<(), RevaultDError>;
5148

5249
fn get_deposit_address(&self) -> Result<bitcoin::Address, RevaultDError>;

0 commit comments

Comments
 (0)