Skip to content

Commit

Permalink
chore: move restoring monitors to node builder
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Sep 24, 2024
1 parent e863459 commit 227c447
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
3 changes: 1 addition & 2 deletions bindings/ldk_node.udl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ interface Builder {
Node build();
[Throws=BuildError]
Node build_with_fs_store();
void restore_encoded_channel_monitors(sequence<KeyValue> monitors);
};

interface Node {
Expand Down Expand Up @@ -92,8 +93,6 @@ interface Node {
boolean verify_signature([ByRef]sequence<u8> msg, [ByRef]string sig, [ByRef]PublicKey pkey);
[Throws=NodeError]
sequence<KeyValue> get_encoded_channel_monitors();
[Throws=NodeError]
void restore_encoded_channel_monitors(sequence<KeyValue> monitors);
void force_close_all_channels_without_broadcasting_txn();
};

Expand Down
30 changes: 28 additions & 2 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use crate::payment::store::PaymentStore;
use crate::peer_store::PeerStore;
use crate::tx_broadcaster::TransactionBroadcaster;
use crate::types::{
ChainMonitor, ChannelManager, DynStore, GossipSync, Graph, KeysManager, MessageRouter,
OnionMessenger, PeerManager,
ChainMonitor, ChannelManager, DynStore, GossipSync, Graph, KeyValue, KeysManager,
MessageRouter, OnionMessenger, PeerManager,
};
use crate::wallet::Wallet;
use crate::{LogLevel, Node};
Expand Down Expand Up @@ -172,6 +172,7 @@ pub struct NodeBuilder {
chain_data_source_config: Option<ChainDataSourceConfig>,
gossip_source_config: Option<GossipSourceConfig>,
liquidity_source_config: Option<LiquiditySourceConfig>,
monitors_to_restore: Option<Vec<KeyValue>>,
}

impl NodeBuilder {
Expand All @@ -187,15 +188,23 @@ impl NodeBuilder {
let chain_data_source_config = None;
let gossip_source_config = None;
let liquidity_source_config = None;
let monitors_to_restore = None;
Self {
config,
entropy_source_config,
chain_data_source_config,
gossip_source_config,
liquidity_source_config,
monitors_to_restore,
}
}

/// Alby: set monitors to restore when restoring SCB
pub fn restore_encoded_channel_monitors(&mut self, monitors: Vec<KeyValue>) -> &mut Self {
self.monitors_to_restore = Some(monitors);
self
}

/// Configures the [`Node`] instance to source its wallet entropy from a seed file on disk.
///
/// If the given file does not exist a new random seed file will be generated and
Expand Down Expand Up @@ -316,6 +325,7 @@ impl NodeBuilder {
)
.map_err(|_| BuildError::KVStoreSetupFailed)?,
);

self.build_with_store(kv_store)
}

Expand Down Expand Up @@ -381,6 +391,17 @@ impl NodeBuilder {
)?;
let config = Arc::new(self.config.clone());

// Alby: Restore encoded channel monitors for a recovery of last resort
if self.monitors_to_restore.is_some() {
let monitors = self.monitors_to_restore.clone().unwrap();
for monitor in monitors {
let result = kv_store.write("monitors", "", &monitor.key, &monitor.value);
if result.is_err() {
log_error!(logger, "Failed to restore monitor: {}", result.unwrap_err());
}
}
}

build_with_store_internal(
config,
self.chain_data_source_config.as_ref(),
Expand Down Expand Up @@ -420,6 +441,11 @@ impl ArcedNodeBuilder {
Self { inner }
}

/// Alby: set monitors to restore when restoring SCB
pub fn restore_encoded_channel_monitors(&self, monitors: Vec<KeyValue>) {
self.inner.write().unwrap().restore_encoded_channel_monitors(monitors);
}

/// Configures the [`Node`] instance to source its wallet entropy from a seed file on disk.
///
/// If the given file does not exist a new random seed file will be generated and
Expand Down
17 changes: 0 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1566,23 +1566,6 @@ impl Node {
return Ok(entries);
}

/// Alby: Restore encoded channel monitors for a recovery of last resort
pub fn restore_encoded_channel_monitors(&self, monitors: Vec<KeyValue>) -> Result<(), Error> {
let channel_monitor_store = Arc::clone(&self.kv_store);
let channel_monitor_logger = Arc::clone(&self.logger);

for monitor in monitors {
channel_monitor_store.write("monitors", "", &monitor.key, &monitor.value).map_err(
|e| {
log_error!(channel_monitor_logger, "Failed to restore monitor: {}", e);
Error::ConnectionFailed
},
)?;
}

return Ok(());
}

/// Retrieves an overview of all known balances.
pub fn list_balances(&self) -> BalanceDetails {
let cur_anchor_reserve_sats =
Expand Down

0 comments on commit 227c447

Please sign in to comment.