Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit 7770102

Browse files
authored
Merge pull request #199 from benthecarman/chain-updates
2 parents 84bda4c + 16f5b67 commit 7770102

1 file changed

Lines changed: 26 additions & 21 deletions

File tree

node-manager/src/chain.rs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use lightning::chain::chaininterface::{
1010
BroadcasterInterface, ConfirmationTarget, FeeEstimator, FEERATE_FLOOR_SATS_PER_KW,
1111
};
1212
use lightning::chain::{Confirm, Filter, WatchedOutput};
13-
use log::{debug, error, info, warn};
13+
use log::{debug, error, info};
1414
use std::collections::HashSet;
1515
use std::sync::{Arc, Mutex};
1616
use wasm_bindgen_futures::spawn_local;
@@ -46,18 +46,21 @@ impl MutinyChain {
4646
}
4747
}
4848

49-
/// Syncs the LDK wallet via the `Confirm` interface. We run in a loop until we completed a
50-
/// full iteration without
49+
/// Synchronizes the given confirmables via the [`Confirm`] interface. This method should be
50+
/// called regularly to keep LDK up-to-date with current chain data.
51+
///
52+
/// [`Confirm`]: Confirm
5153
pub(crate) async fn sync(
5254
&self,
5355
confirmables: Vec<&(dyn Confirm + Sync)>,
5456
) -> Result<(), MutinyError> {
57+
info!("Starting transaction sync.");
5558
// This lock makes sure we're syncing once at a time.
5659
let mut locked_last_sync_hash = self.last_sync_hash.lock().await;
5760

5861
let client = &*self.wallet.blockchain;
5962

60-
let tip_hash = client.get_tip_hash().await?;
63+
let mut tip_hash = client.get_tip_hash().await?;
6164

6265
loop {
6366
let registrations_are_pending = self.process_queues();
@@ -78,6 +81,9 @@ impl MutinyChain {
7881
Ok(()) => {}
7982
Err(MutinyError::ChainAccessFailed) => {
8083
// Immediately restart syncing when we encounter any inconsistencies.
84+
debug!(
85+
"Encountered inconsistency during transaction sync, restarting."
86+
);
8187
continue;
8288
}
8389
Err(err) => {
@@ -90,8 +96,9 @@ impl MutinyChain {
9096
match self.get_confirmed_transactions().await {
9197
Ok((confirmed_txs, unconfirmed_registered_txs, unspent_registered_outputs)) => {
9298
// Double-check tip hash. If something changed, restart last-minute.
93-
let new_tip_hash = client.get_tip_hash().await?;
94-
if tip_hash != new_tip_hash {
99+
let check_tip_hash = self.wallet.blockchain.get_tip_hash().await?;
100+
if check_tip_hash != tip_hash {
101+
tip_hash = check_tip_hash;
95102
continue;
96103
}
97104

@@ -104,16 +111,19 @@ impl MutinyChain {
104111
}
105112
Err(MutinyError::ChainAccessFailed) => {
106113
// Immediately restart syncing when we encounter any inconsistencies.
114+
debug!("Encountered inconsistency during transaction sync, restarting.");
107115
continue;
108116
}
109117
Err(err) => {
110118
// (Semi-)permanent failure, retry later.
119+
error!("Failed during transaction sync, aborting.");
111120
return Err(err);
112121
}
113122
}
114123
*locked_last_sync_hash = Some(tip_hash);
115124
}
116125
}
126+
info!("Finished transaction sync.");
117127
Ok(())
118128
}
119129

@@ -173,10 +183,6 @@ impl MutinyChain {
173183
unconfirmed_registered_txs: HashSet<Txid>,
174184
unspent_registered_outputs: HashSet<WatchedOutput>,
175185
) {
176-
for c in &confirmed_txs {
177-
debug!("confirming tx! {}", c.tx.txid())
178-
}
179-
180186
for ctx in confirmed_txs {
181187
for c in confirmables {
182188
c.transactions_confirmed(
@@ -194,8 +200,6 @@ impl MutinyChain {
194200
async fn get_confirmed_transactions(
195201
&self,
196202
) -> Result<(Vec<ConfirmedTx>, HashSet<Txid>, HashSet<WatchedOutput>), MutinyError> {
197-
let client = &*self.wallet.blockchain;
198-
199203
// First, check the confirmation status of registered transactions as well as the
200204
// status of dependent transactions of registered outputs.
201205

@@ -208,14 +212,10 @@ impl MutinyChain {
208212
// Remember all registered but unconfirmed transactions for future processing.
209213
let mut unconfirmed_registered_txs = HashSet::new();
210214

211-
info!("registered tx size: {}", registered_txs.len());
212215
for txid in registered_txs {
213-
info!("registered tx: {}", txid);
214216
if let Some(confirmed_tx) = self.get_confirmed_tx(&txid, None, None).await? {
215-
info!("confirmed tx: {}", txid);
216217
confirmed_txs.push(confirmed_tx);
217218
} else {
218-
warn!("unconfirmed tx: {}", txid);
219219
unconfirmed_registered_txs.insert(txid);
220220
}
221221
}
@@ -227,7 +227,9 @@ impl MutinyChain {
227227
let mut unspent_registered_outputs = HashSet::new();
228228

229229
for output in registered_outputs {
230-
if let Some(output_status) = client
230+
if let Some(output_status) = self
231+
.wallet
232+
.blockchain
231233
.get_output_status(&output.outpoint.txid, output.outpoint.index as u64)
232234
.await?
233235
{
@@ -314,9 +316,9 @@ impl MutinyChain {
314316
confirmables: &Vec<&(dyn Confirm + Sync)>,
315317
) -> Result<(), MutinyError> {
316318
let client = &*self.wallet.blockchain;
317-
// Query the interface for relevant txids and check whether they are still
318-
// in the best chain, mark them unconfirmed otherwise.
319-
319+
// Query the interface for relevant txids and check whether the relevant blocks are still
320+
// in the best chain, mark them unconfirmed otherwise. If the transactions have been
321+
// reconfirmed in another block, we'll confirm them in the next sync iteration.
320322
let relevant_txids = confirmables
321323
.iter()
322324
.flat_map(|c| c.get_relevant_txids())
@@ -343,7 +345,10 @@ impl MutinyChain {
343345
}
344346
}
345347

346-
*self.watched_transactions.lock().unwrap() = to_watch;
348+
let mut locked_watched_transactions = self.watched_transactions.lock().unwrap();
349+
for txid in to_watch {
350+
locked_watched_transactions.insert(txid);
351+
}
347352

348353
Ok(())
349354
}

0 commit comments

Comments
 (0)