Skip to content

Commit b04fe2f

Browse files
Antoine RiardTheBlueMatt
authored andcommitted
Add registration of commitment tx's outputs from
check_spend_remote_transaction
1 parent ecade5c commit b04fe2f

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

src/ln/channelmonitor.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ impl<Key : Send + cmp::Eq + hash::Hash> ChainListener for SimpleManyChannelMonit
6969
fn block_connected(&self, _header: &BlockHeader, height: u32, txn_matched: &[&Transaction], _indexes_of_txn_matched: &[u32]) {
7070
let monitors = self.monitors.lock().unwrap();
7171
for monitor in monitors.values() {
72-
monitor.block_connected(txn_matched, height, &*self.broadcaster);
72+
let outputs = monitor.block_connected(txn_matched, height, &*self.broadcaster);
73+
for output in outputs {
74+
self.chain_monitor.install_watch_script(&output.script_pubkey);
75+
}
7376
}
7477
}
7578

@@ -464,8 +467,9 @@ impl ChannelMonitor {
464467
/// optional, without it this monitor cannot be used in an SPV client, but you may wish to
465468
/// avoid this (or call unset_funding_info) on a monitor you wish to send to a watchtower as it
466469
/// provides slightly better privacy.
470+
/// It is the responsability of the caller to register outpoint and script with passing the former
471+
/// value as key to update current monitor
467472
pub(super) fn set_funding_info(&mut self, funding_info: (OutPoint, Script)) {
468-
//TODO: Need to register the given script here with a chain_monitor
469473
self.funding_txo = Some(funding_info);
470474
}
471475

@@ -908,15 +912,16 @@ impl ChannelMonitor {
908912
/// height > height + CLTV_SHARED_CLAIM_BUFFER. In any case, will install monitoring for
909913
/// HTLC-Success/HTLC-Timeout transactions, and claim them using the revocation key (if
910914
/// applicable) as well.
911-
fn check_spend_remote_transaction(&self, tx: &Transaction, height: u32) -> Vec<Transaction> {
915+
fn check_spend_remote_transaction(&self, tx: &Transaction, height: u32) -> (Vec<Transaction>, Vec<TxOut>) {
912916
// Most secp and related errors trying to create keys means we have no hope of constructing
913917
// a spend transaction...so we return no transactions to broadcast
914918
let mut txn_to_broadcast = Vec::new();
919+
let mut outputs = Vec::new();
915920
macro_rules! ignore_error {
916921
( $thing : expr ) => {
917922
match $thing {
918923
Ok(a) => a,
919-
Err(_) => return txn_to_broadcast
924+
Err(_) => return (txn_to_broadcast, outputs)
920925
}
921926
};
922927
}
@@ -942,7 +947,7 @@ impl ChannelMonitor {
942947
};
943948
let delayed_key = ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key), &self.delayed_payment_base_key));
944949
let a_htlc_key = match self.their_htlc_base_key {
945-
None => return txn_to_broadcast,
950+
None => return (txn_to_broadcast, outputs),
946951
Some(their_htlc_base_key) => ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key), &their_htlc_base_key)),
947952
};
948953

@@ -1009,7 +1014,7 @@ impl ChannelMonitor {
10091014
if htlc.transaction_output_index as usize >= tx.output.len() ||
10101015
tx.output[htlc.transaction_output_index as usize].value != htlc.amount_msat / 1000 ||
10111016
tx.output[htlc.transaction_output_index as usize].script_pubkey != expected_script.to_v0_p2wsh() {
1012-
return txn_to_broadcast; // Corrupted per_commitment_data, fuck this user
1017+
return (txn_to_broadcast, outputs); // Corrupted per_commitment_data, fuck this user
10131018
}
10141019
let input = TxIn {
10151020
previous_output: BitcoinOutPoint {
@@ -1044,10 +1049,10 @@ impl ChannelMonitor {
10441049

10451050
if !inputs.is_empty() || !txn_to_broadcast.is_empty() { // ie we're confident this is actually ours
10461051
// We're definitely a remote commitment transaction!
1047-
// TODO: Register all outputs in commitment_tx with the ChainWatchInterface!
1052+
outputs.append(&mut tx.output.clone());
10481053
self.remote_commitment_txn_on_chain.lock().unwrap().insert(commitment_txid, commitment_number);
10491054
}
1050-
if inputs.is_empty() { return txn_to_broadcast; } // Nothing to be done...probably a false positive/local tx
1055+
if inputs.is_empty() { return (txn_to_broadcast, outputs); } // Nothing to be done...probably a false positive/local tx
10511056

10521057
let outputs = vec!(TxOut {
10531058
script_pubkey: self.destination_script.clone(),
@@ -1077,7 +1082,7 @@ impl ChannelMonitor {
10771082
// already processed the block, resulting in the remote_commitment_txn_on_chain entry
10781083
// not being generated by the above conditional. Thus, to be safe, we go ahead and
10791084
// insert it here.
1080-
// TODO: Register all outputs in commitment_tx with the ChainWatchInterface!
1085+
outputs.append(&mut tx.output.clone());
10811086
self.remote_commitment_txn_on_chain.lock().unwrap().insert(commitment_txid, commitment_number);
10821087

10831088
if let Some(revocation_points) = self.their_cur_revocation_points {
@@ -1098,7 +1103,7 @@ impl ChannelMonitor {
10981103
},
10991104
};
11001105
let a_htlc_key = match self.their_htlc_base_key {
1101-
None => return txn_to_broadcast,
1106+
None => return (txn_to_broadcast, outputs),
11021107
Some(their_htlc_base_key) => ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, revocation_point, &their_htlc_base_key)),
11031108
};
11041109

@@ -1161,7 +1166,7 @@ impl ChannelMonitor {
11611166
}
11621167
}
11631168

1164-
if inputs.is_empty() { return txn_to_broadcast; } // Nothing to be done...probably a false positive/local tx
1169+
if inputs.is_empty() { return (txn_to_broadcast, outputs); } // Nothing to be done...probably a false positive/local tx
11651170

11661171
let outputs = vec!(TxOut {
11671172
script_pubkey: self.destination_script.clone(),
@@ -1189,7 +1194,7 @@ impl ChannelMonitor {
11891194
//TODO: For each input check if its in our remote_commitment_txn_on_chain map!
11901195
}
11911196

1192-
txn_to_broadcast
1197+
(txn_to_broadcast, outputs)
11931198
}
11941199

11951200
fn broadcast_by_local_state(&self, local_tx: &LocalSignedTx) -> Vec<Transaction> {
@@ -1250,11 +1255,13 @@ impl ChannelMonitor {
12501255
Vec::new()
12511256
}
12521257

1253-
fn block_connected(&self, txn_matched: &[&Transaction], height: u32, broadcaster: &BroadcasterInterface) {
1258+
fn block_connected(&self, txn_matched: &[&Transaction], height: u32, broadcaster: &BroadcasterInterface) -> Vec<TxOut> {
1259+
let mut outputs = Vec::new();
12541260
for tx in txn_matched {
12551261
for txin in tx.input.iter() {
12561262
if self.funding_txo.is_none() || (txin.previous_output.txid == self.funding_txo.as_ref().unwrap().0.txid && txin.previous_output.vout == self.funding_txo.as_ref().unwrap().0.index as u32) {
1257-
let mut txn = self.check_spend_remote_transaction(tx, height);
1263+
let (mut txn, out) = self.check_spend_remote_transaction(tx, height);
1264+
outputs = out;
12581265
if txn.is_empty() {
12591266
txn = self.check_spend_local_transaction(tx, height);
12601267
}
@@ -1281,6 +1288,7 @@ impl ChannelMonitor {
12811288
}
12821289
}
12831290
}
1291+
outputs
12841292
}
12851293

12861294
pub fn would_broadcast_at_height(&self, height: u32) -> bool {

0 commit comments

Comments
 (0)