|
7 | 7 |
|
8 | 8 | use persist::KVStoreWalletPersister;
|
9 | 9 |
|
10 |
| -use crate::logger::{log_error, log_info, log_trace, Logger}; |
| 10 | +use crate::logger::{log_debug, log_error, log_info, log_trace, Logger}; |
11 | 11 |
|
12 | 12 | use crate::fee_estimator::{ConfirmationTarget, FeeEstimator};
|
13 | 13 | use crate::Error;
|
14 | 14 |
|
15 | 15 | use lightning::chain::chaininterface::BroadcasterInterface;
|
| 16 | +use lightning::chain::Listen; |
16 | 17 |
|
17 | 18 | use lightning::events::bump_transaction::{Utxo, WalletSource};
|
18 | 19 | use lightning::ln::msgs::{DecodeError, UnsignedGossipMessage};
|
@@ -285,6 +286,66 @@ where
|
285 | 286 | }
|
286 | 287 | }
|
287 | 288 |
|
| 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 | + |
288 | 349 | impl<B: Deref, E: Deref, L: Deref> WalletSource for Wallet<B, E, L>
|
289 | 350 | where
|
290 | 351 | B::Target: BroadcasterInterface,
|
|
0 commit comments