Skip to content

Commit 9842820

Browse files
committed
Add ChannelMonitor::update_best_block
Expose a way for Electrum users to update the best block on a ChannelMonitor independently of confirming transactions.
1 parent 9effd92 commit 9842820

File tree

1 file changed

+78
-6
lines changed

1 file changed

+78
-6
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,12 +1306,15 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
13061306
header, height, broadcaster, fee_estimator, logger)
13071307
}
13081308

1309-
/// Processes transactions from a block with the given header and height, returning new outputs
1310-
/// to watch. See [`block_connected`] for details.
1309+
/// Processes transactions confirmed in a block with the given header and height, returning new
1310+
/// outputs to watch. See [`block_connected`] for details.
13111311
///
1312-
/// TODO: Expand docs.
1312+
/// Used instead of [`block_connected`] by clients that are notified of transactions rather than
1313+
/// blocks. May be called before or after [`update_best_block`] for transactions in the
1314+
/// corresponding block. See [`update_best_block`] for further calling expectations.
13131315
///
13141316
/// [`block_connected`]: Self::block_connected
1317+
/// [`update_best_block`]: Self::update_best_block
13151318
pub fn transactions_confirmed<B: Deref, F: Deref, L: Deref>(
13161319
&self,
13171320
header: &BlockHeader,
@@ -1329,6 +1332,35 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
13291332
self.inner.lock().unwrap().transactions_confirmed(
13301333
header, txdata, height, broadcaster, fee_estimator, logger)
13311334
}
1335+
1336+
/// Updates the monitor with the current best chain tip, returning new outputs to watch. See
1337+
/// [`block_connected`] for details.
1338+
///
1339+
/// Used instead of [`block_connected`] by clients that are notified of transactions rather than
1340+
/// blocks. May be called before or after [`transactions_confirmed`] for the corresponding
1341+
/// block.
1342+
///
1343+
/// Must be called after new blocks become available for the most recent block. Intermediary
1344+
/// blocks, however, may be safely skipped.
1345+
///
1346+
/// [`block_connected`]: Self::block_connected
1347+
/// [`transactions_confirmed`]: Self::transactions_confirmed
1348+
pub fn update_best_block<B: Deref, F: Deref, L: Deref>(
1349+
&self,
1350+
header: &BlockHeader,
1351+
height: u32,
1352+
broadcaster: B,
1353+
fee_estimator: F,
1354+
logger: L,
1355+
) -> Vec<(Txid, Vec<(u32, TxOut)>)>
1356+
where
1357+
B::Target: BroadcasterInterface,
1358+
F::Target: FeeEstimator,
1359+
L::Target: Logger,
1360+
{
1361+
self.inner.lock().unwrap().update_best_block(
1362+
header, height, broadcaster, fee_estimator, logger)
1363+
}
13321364
}
13331365

13341366
impl<Signer: Sign> ChannelMonitorImpl<Signer> {
@@ -2030,10 +2062,33 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
20302062
F::Target: FeeEstimator,
20312063
L::Target: Logger,
20322064
{
2033-
self.best_block = BestBlock::new(header.block_hash(), height);
2065+
let block_hash = header.block_hash();
2066+
log_trace!(logger, "New best block {} at height {}", block_hash, height);
2067+
self.best_block = BestBlock::new(block_hash, height);
2068+
20342069
self.transactions_confirmed(header, txdata, height, broadcaster, fee_estimator, logger)
20352070
}
20362071

2072+
fn update_best_block<B: Deref, F: Deref, L: Deref>(
2073+
&mut self,
2074+
header: &BlockHeader,
2075+
height: u32,
2076+
broadcaster: B,
2077+
fee_estimator: F,
2078+
logger: L,
2079+
) -> Vec<(Txid, Vec<(u32, TxOut)>)>
2080+
where
2081+
B::Target: BroadcasterInterface,
2082+
F::Target: FeeEstimator,
2083+
L::Target: Logger,
2084+
{
2085+
let block_hash = header.block_hash();
2086+
log_trace!(logger, "New best block {} at height {}", block_hash, height);
2087+
self.best_block = BestBlock::new(block_hash, height);
2088+
2089+
self.block_confirmed(height, vec![], vec![], vec![], broadcaster, fee_estimator, logger)
2090+
}
2091+
20372092
fn transactions_confirmed<B: Deref, F: Deref, L: Deref>(
20382093
&mut self,
20392094
header: &BlockHeader,
@@ -2102,11 +2157,28 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
21022157

21032158
self.is_paying_spendable_output(&tx, height, &logger);
21042159
}
2160+
2161+
self.block_confirmed(height, txn_matched, watch_outputs, claimable_outpoints, broadcaster, fee_estimator, logger)
2162+
}
2163+
2164+
fn block_confirmed<B: Deref, F: Deref, L: Deref>(
2165+
&mut self,
2166+
height: u32,
2167+
txn_matched: Vec<&Transaction>,
2168+
mut watch_outputs: Vec<(Txid, Vec<(u32, TxOut)>)>,
2169+
mut claimable_outpoints: Vec<ClaimRequest>,
2170+
broadcaster: B,
2171+
fee_estimator: F,
2172+
logger: L,
2173+
) -> Vec<(Txid, Vec<(u32, TxOut)>)>
2174+
where
2175+
B::Target: BroadcasterInterface,
2176+
F::Target: FeeEstimator,
2177+
L::Target: Logger,
2178+
{
21052179
let should_broadcast = self.would_broadcast_at_height(height, &logger);
21062180
if should_broadcast {
21072181
claimable_outpoints.push(ClaimRequest { absolute_timelock: height, aggregable: false, outpoint: BitcoinOutPoint { txid: self.funding_info.0.txid.clone(), vout: self.funding_info.0.index as u32 }, witness_data: InputMaterial::Funding { funding_redeemscript: self.funding_redeemscript.clone() }});
2108-
}
2109-
if should_broadcast {
21102182
self.pending_monitor_events.push(MonitorEvent::CommitmentTxBroadcasted(self.funding_info.0));
21112183
let commitment_tx = self.onchain_tx_handler.get_fully_signed_holder_tx(&self.funding_redeemscript);
21122184
self.holder_tx_signed = true;

0 commit comments

Comments
 (0)