Skip to content

Commit e926b7d

Browse files
committed
Implement Listen for Wallet
.. which we'll use to feed blocks to it in following commits.
1 parent 94ff68f commit e926b7d

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

src/wallet/mod.rs

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77

88
use persist::KVStoreWalletPersister;
99

10-
use crate::logger::{log_error, log_info, log_trace, Logger};
10+
use crate::logger::{log_debug, log_error, log_info, log_trace, Logger};
1111

1212
use crate::fee_estimator::{ConfirmationTarget, FeeEstimator};
1313
use crate::Error;
1414

1515
use lightning::chain::chaininterface::BroadcasterInterface;
16+
use lightning::chain::Listen;
1617

1718
use lightning::events::bump_transaction::{Utxo, WalletSource};
1819
use lightning::ln::msgs::{DecodeError, UnsignedGossipMessage};
@@ -285,6 +286,66 @@ where
285286
}
286287
}
287288

289+
impl<B: Deref, E: Deref, L: Deref> Listen for Wallet<B, E, L>
290+
where
291+
B::Target: BroadcasterInterface,
292+
E::Target: FeeEstimator,
293+
L::Target: Logger,
294+
{
295+
fn filtered_block_connected(
296+
&self, _header: &bitcoin::block::Header,
297+
_txdata: &lightning::chain::transaction::TransactionData, _height: u32,
298+
) {
299+
debug_assert!(false, "Syncing filtered blocks is currently not supported");
300+
// As far as we can tell this would be a no-op anyways as we don't have to tell BDK about
301+
// the header chain of intermediate blocks. According to the BDK team, it's sufficient to
302+
// only connect full blocks starting from the last point of disagreement.
303+
}
304+
305+
fn block_connected(&self, block: &bitcoin::Block, height: u32) {
306+
let mut locked_wallet = self.inner.lock().unwrap();
307+
308+
let pre_checkpoint = locked_wallet.latest_checkpoint();
309+
if pre_checkpoint.height() != height - 1
310+
|| pre_checkpoint.hash() != block.header.prev_blockhash
311+
{
312+
log_debug!(
313+
self.logger,
314+
"Detected reorg while applying a connected block to on-chain wallet: new block with hash {} at height {}",
315+
block.header.block_hash(),
316+
height
317+
);
318+
}
319+
320+
match locked_wallet.apply_block(block, height) {
321+
Ok(()) => (),
322+
Err(e) => {
323+
log_error!(
324+
self.logger,
325+
"Failed to apply connected block to on-chain wallet: {}",
326+
e
327+
);
328+
return;
329+
},
330+
};
331+
332+
let mut locked_persister = self.persister.lock().unwrap();
333+
match locked_wallet.persist(&mut locked_persister) {
334+
Ok(_) => (),
335+
Err(e) => {
336+
log_error!(self.logger, "Failed to persist on-chain wallet: {}", e);
337+
return;
338+
},
339+
};
340+
}
341+
342+
fn block_disconnected(&self, _header: &bitcoin::block::Header, _height: u32) {
343+
// This is a no-op as we don't have to tell BDK about disconnections. According to the BDK
344+
// team, it's sufficient in case of a reorg to always connect blocks starting from the last
345+
// point of disagreement.
346+
}
347+
}
348+
288349
impl<B: Deref, E: Deref, L: Deref> WalletSource for Wallet<B, E, L>
289350
where
290351
B::Target: BroadcasterInterface,

0 commit comments

Comments
 (0)