Skip to content

Commit 30c4ede

Browse files
committed
Refactor ChannelMonitor::block_connected
Define an Electrum-friendly interface for ChannelMonitor where transactions are confirmed and the latest connected block is updated independently.
1 parent 58cd2a0 commit 30c4ede

File tree

1 file changed

+62
-8
lines changed

1 file changed

+62
-8
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,6 +1299,36 @@ impl<Signer: Sign> ChannelMonitor<Signer> {
12991299
self.inner.lock().unwrap().block_disconnected(
13001300
header, height, broadcaster, fee_estimator, logger)
13011301
}
1302+
1303+
/// Processes transactions from a block with the given header and height, returning new outputs
1304+
/// to watch. See [`block_connected`] for details.
1305+
///
1306+
/// TODO: Expand docs.
1307+
pub fn transactions_confirmed<B: Deref, F: Deref, L: Deref>(
1308+
&self,
1309+
header: &BlockHeader,
1310+
txdata: &TransactionData,
1311+
height: u32,
1312+
broadcaster: B,
1313+
fee_estimator: F,
1314+
logger: L,
1315+
) -> Vec<(Txid, Vec<(u32, TxOut)>)>
1316+
where
1317+
B::Target: BroadcasterInterface,
1318+
F::Target: FeeEstimator,
1319+
L::Target: Logger,
1320+
{
1321+
self.inner.lock().unwrap().transactions_confirmed(
1322+
header, txdata, height, broadcaster, fee_estimator, &logger)
1323+
}
1324+
1325+
/// Updates the monitor with the current best chain tip.
1326+
///
1327+
/// TODO: Expand docs.
1328+
pub fn update_best_block<L: Deref>(&mut self, header: &BlockHeader, height: u32, logger: L)
1329+
where L::Target: Logger {
1330+
self.inner.lock().unwrap().update_best_block(header, height, &logger)
1331+
}
13021332
}
13031333

13041334
impl<Signer: Sign> ChannelMonitorImpl<Signer> {
@@ -1999,6 +2029,31 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
19992029
where B::Target: BroadcasterInterface,
20002030
F::Target: FeeEstimator,
20012031
L::Target: Logger,
2032+
{
2033+
self.update_best_block(header, height, &logger);
2034+
self.transactions_confirmed(header, txdata, height, broadcaster, fee_estimator, &logger)
2035+
}
2036+
2037+
fn update_best_block<L: Deref>(&mut self, header: &BlockHeader, height: u32, logger: &L)
2038+
where L::Target: Logger {
2039+
let block_hash = header.block_hash();
2040+
log_trace!(logger, "New best block {} at height {}", block_hash, height);
2041+
self.last_block_hash = block_hash;
2042+
}
2043+
2044+
fn transactions_confirmed<B: Deref, F: Deref, L: Deref>(
2045+
&mut self,
2046+
header: &BlockHeader,
2047+
txdata: &TransactionData,
2048+
height: u32,
2049+
broadcaster: B,
2050+
fee_estimator: F,
2051+
logger: &L,
2052+
) -> Vec<(Txid, Vec<(u32, TxOut)>)>
2053+
where
2054+
B::Target: BroadcasterInterface,
2055+
F::Target: FeeEstimator,
2056+
L::Target: Logger,
20022057
{
20032058
let txn_matched = self.filter_block(txdata);
20042059
for tx in &txn_matched {
@@ -2024,12 +2079,12 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
20242079
let prevout = &tx.input[0].previous_output;
20252080
if prevout.txid == self.funding_info.0.txid && prevout.vout == self.funding_info.0.index as u32 {
20262081
if (tx.input[0].sequence >> 8*3) as u8 == 0x80 && (tx.lock_time >> 8*3) as u8 == 0x20 {
2027-
let (mut new_outpoints, new_outputs) = self.check_spend_counterparty_transaction(&tx, height, &logger);
2082+
let (mut new_outpoints, new_outputs) = self.check_spend_counterparty_transaction(&tx, height, logger);
20282083
if !new_outputs.1.is_empty() {
20292084
watch_outputs.push(new_outputs);
20302085
}
20312086
if new_outpoints.is_empty() {
2032-
let (mut new_outpoints, new_outputs) = self.check_spend_holder_transaction(&tx, height, &logger);
2087+
let (mut new_outpoints, new_outputs) = self.check_spend_holder_transaction(&tx, height, logger);
20332088
if !new_outputs.1.is_empty() {
20342089
watch_outputs.push(new_outputs);
20352090
}
@@ -2039,7 +2094,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
20392094
}
20402095
} else {
20412096
if let Some(&commitment_number) = self.counterparty_commitment_txn_on_chain.get(&prevout.txid) {
2042-
let (mut new_outpoints, new_outputs_option) = self.check_spend_counterparty_htlc(&tx, commitment_number, height, &logger);
2097+
let (mut new_outpoints, new_outputs_option) = self.check_spend_counterparty_htlc(&tx, commitment_number, height, logger);
20432098
claimable_outpoints.append(&mut new_outpoints);
20442099
if let Some(new_outputs) = new_outputs_option {
20452100
watch_outputs.push(new_outputs);
@@ -2050,11 +2105,11 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
20502105
// While all commitment/HTLC-Success/HTLC-Timeout transactions have one input, HTLCs
20512106
// can also be resolved in a few other ways which can have more than one output. Thus,
20522107
// we call is_resolving_htlc_output here outside of the tx.input.len() == 1 check.
2053-
self.is_resolving_htlc_output(&tx, height, &logger);
2108+
self.is_resolving_htlc_output(&tx, height, logger);
20542109

2055-
self.is_paying_spendable_output(&tx, height, &logger);
2110+
self.is_paying_spendable_output(&tx, height, logger);
20562111
}
2057-
let should_broadcast = self.would_broadcast_at_height(height, &logger);
2112+
let should_broadcast = self.would_broadcast_at_height(height, logger);
20582113
if should_broadcast {
20592114
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() }});
20602115
}
@@ -2095,8 +2150,7 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
20952150
}
20962151
}
20972152

2098-
self.onchain_tx_handler.update_claims_view(&txn_matched, claimable_outpoints, Some(height), &&*broadcaster, &&*fee_estimator, &&*logger);
2099-
self.last_block_hash = block_hash;
2153+
self.onchain_tx_handler.update_claims_view(&txn_matched, claimable_outpoints, Some(height), &&*broadcaster, &&*fee_estimator, &*logger);
21002154

21012155
// Determine new outputs to watch by comparing against previously known outputs to watch,
21022156
// updating the latter in the process.

0 commit comments

Comments
 (0)