Skip to content

Commit 6f79ee0

Browse files
committed
Add ChannelMonitor::transaction_unconfirmed
Define an Electrum-friendly interface for ChannelMonitor where transactions are unconfirmed independently from updating the latest block.
1 parent e887cd3 commit 6f79ee0

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,29 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
13331333
header, txdata, height, broadcaster, fee_estimator, logger)
13341334
}
13351335

1336+
/// Processes a transaction that was reorganized out of the chain.
1337+
///
1338+
/// Used instead of [`block_disconnected`] by clients that are notified of transactions rather
1339+
/// than blocks. May be called before or after [`update_best_block`] for transactions in the
1340+
/// corresponding block. See [`update_best_block`] for further calling expectations.
1341+
///
1342+
/// [`block_disconnected`]: Self::block_disconnected
1343+
/// [`update_best_block`]: Self::update_best_block
1344+
pub fn transaction_unconfirmed<B: Deref, F: Deref, L: Deref>(
1345+
&self,
1346+
txid: &Txid,
1347+
broadcaster: B,
1348+
fee_estimator: F,
1349+
logger: L,
1350+
) where
1351+
B::Target: BroadcasterInterface,
1352+
F::Target: FeeEstimator,
1353+
L::Target: Logger,
1354+
{
1355+
self.inner.lock().unwrap().transaction_unconfirmed(
1356+
txid, broadcaster, fee_estimator, logger);
1357+
}
1358+
13361359
/// Updates the monitor with the current best chain tip, returning new outputs to watch. See
13371360
/// [`block_connected`] for details.
13381361
///
@@ -1341,7 +1364,9 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
13411364
/// block.
13421365
///
13431366
/// Must be called after new blocks become available for the most recent block. Intermediary
1344-
/// blocks, however, may be safely skipped.
1367+
/// blocks, however, may be safely skipped. For a chain reorganization, this must be called at
1368+
/// least at the fork point and then again for the most recent block; intermediary blocks may be
1369+
/// skipped as before.
13451370
///
13461371
/// [`block_connected`]: Self::block_connected
13471372
/// [`transactions_confirmed`]: Self::transactions_confirmed
@@ -2289,6 +2314,21 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
22892314
self.best_block = BestBlock::new(header.prev_blockhash, height - 1);
22902315
}
22912316

2317+
fn transaction_unconfirmed<B: Deref, F: Deref, L: Deref>(
2318+
&mut self,
2319+
txid: &Txid,
2320+
broadcaster: B,
2321+
fee_estimator: F,
2322+
logger: L,
2323+
) where
2324+
B::Target: BroadcasterInterface,
2325+
F::Target: FeeEstimator,
2326+
L::Target: Logger,
2327+
{
2328+
self.onchain_events_waiting_threshold_conf.retain(|ref entry| entry.txid != *txid);
2329+
self.onchain_tx_handler.transaction_unconfirmed(txid, broadcaster, fee_estimator, logger);
2330+
}
2331+
22922332
/// Filters a block's `txdata` for transactions spending watched outputs or for any child
22932333
/// transactions thereof.
22942334
fn filter_block<'a>(&self, txdata: &TransactionData<'a>) -> Vec<&'a Transaction> {

lightning/src/ln/onchaintx.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,30 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
869869
}
870870
}
871871

872+
pub(crate) fn transaction_unconfirmed<B: Deref, F: Deref, L: Deref>(
873+
&mut self,
874+
txid: &Txid,
875+
broadcaster: B,
876+
fee_estimator: F,
877+
logger: L,
878+
) where
879+
B::Target: BroadcasterInterface,
880+
F::Target: FeeEstimator,
881+
L::Target: Logger,
882+
{
883+
let mut height = None;
884+
for entry in self.onchain_events_waiting_threshold_conf.iter() {
885+
if entry.txid == *txid {
886+
height = Some(entry.height);
887+
break;
888+
}
889+
}
890+
891+
if let Some(height) = height {
892+
self.block_disconnected(height, broadcaster, fee_estimator, logger);
893+
}
894+
}
895+
872896
pub(crate) fn block_disconnected<B: Deref, F: Deref, L: Deref>(&mut self, height: u32, broadcaster: B, fee_estimator: F, logger: L)
873897
where B::Target: BroadcasterInterface,
874898
F::Target: FeeEstimator,

0 commit comments

Comments
 (0)