Skip to content

Commit 4b9adea

Browse files
author
Antoine Riard
committed
Add registration of commitment tx's outputs from
check_spend_remote_transaction Fixup more descriptive var names by Matt Corallo <[email protected]>
1 parent bfb9b46 commit 4b9adea

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

src/ln/channelmanager.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3337,7 +3337,8 @@ mod tests {
33373337
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
33383338
{
33393339
let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
3340-
assert_eq!(node_txn.len(), 2);
3340+
assert_eq!(node_txn.len(), 3);
3341+
assert_eq!(node_txn.pop().unwrap(), node_txn[0]); // An outpoint registration will result in a 2nd block_connected
33413342
assert_eq!(node_txn[0].input.len(), 1);
33423343

33433344
let mut funding_tx_map = HashMap::new();

src/ln/channelmonitor.rs

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ pub enum ChannelMonitorUpdateErr {
4646
/// channel's monitor everywhere (including remote watchtowers) *before* this function returns. If
4747
/// an update occurs and a remote watchtower is left with old state, it may broadcast transactions
4848
/// which we have revoked, allowing our counterparty to claim all funds in the channel!
49+
/// A call to add_update_monitor is needed to register outpoint and its txid with ChainWatchInterface
50+
/// after setting funding_txo in a ChannelMonitor
4951
pub trait ManyChannelMonitor: Send + Sync {
5052
/// Adds or updates a monitor for the given `funding_txo`.
5153
fn add_update_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitor) -> Result<(), ChannelMonitorUpdateErr>;
@@ -69,7 +71,12 @@ impl<Key : Send + cmp::Eq + hash::Hash> ChainListener for SimpleManyChannelMonit
6971
fn block_connected(&self, _header: &BlockHeader, height: u32, txn_matched: &[&Transaction], _indexes_of_txn_matched: &[u32]) {
7072
let monitors = self.monitors.lock().unwrap();
7173
for monitor in monitors.values() {
72-
monitor.block_connected(txn_matched, height, &*self.broadcaster);
74+
let txn_outputs = monitor.block_connected(txn_matched, height, &*self.broadcaster);
75+
for (ref txid, ref outputs) in txn_outputs {
76+
for (idx, output) in outputs.iter().enumerate() {
77+
self.chain_monitor.install_watch_outpoint((txid.clone(), idx as u32), &output.script_pubkey);
78+
}
79+
}
7380
}
7481
}
7582

@@ -464,8 +471,9 @@ impl ChannelMonitor {
464471
/// optional, without it this monitor cannot be used in an SPV client, but you may wish to
465472
/// avoid this (or call unset_funding_info) on a monitor you wish to send to a watchtower as it
466473
/// provides slightly better privacy.
474+
/// It's the responsability of the caller to register outpoint and script with passing the former
475+
/// value as key to add_update_monitor.
467476
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
469477
self.funding_txo = Some(funding_info);
470478
}
471479

@@ -908,22 +916,24 @@ impl ChannelMonitor {
908916
/// height > height + CLTV_SHARED_CLAIM_BUFFER. In any case, will install monitoring for
909917
/// HTLC-Success/HTLC-Timeout transactions, and claim them using the revocation key (if
910918
/// applicable) as well.
911-
fn check_spend_remote_transaction(&self, tx: &Transaction, height: u32) -> Vec<Transaction> {
919+
fn check_spend_remote_transaction(&self, tx: &Transaction, height: u32) -> (Vec<Transaction>, (Sha256dHash, Vec<TxOut>)) {
912920
// Most secp and related errors trying to create keys means we have no hope of constructing
913921
// a spend transaction...so we return no transactions to broadcast
914922
let mut txn_to_broadcast = Vec::new();
923+
let mut watch_outputs = Vec::new();
924+
925+
let commitment_txid = tx.txid(); //TODO: This is gonna be a performance bottleneck for watchtowers!
926+
let per_commitment_option = self.remote_claimable_outpoints.get(&commitment_txid);
927+
915928
macro_rules! ignore_error {
916929
( $thing : expr ) => {
917930
match $thing {
918931
Ok(a) => a,
919-
Err(_) => return txn_to_broadcast
932+
Err(_) => return (txn_to_broadcast, (commitment_txid, watch_outputs))
920933
}
921934
};
922935
}
923936

924-
let commitment_txid = tx.txid(); //TODO: This is gonna be a performance bottleneck for watchtowers!
925-
let per_commitment_option = self.remote_claimable_outpoints.get(&commitment_txid);
926-
927937
let commitment_number = 0xffffffffffff - ((((tx.input[0].sequence as u64 & 0xffffff) << 3*8) | (tx.lock_time as u64 & 0xffffff)) ^ self.commitment_transaction_number_obscure_factor);
928938
if commitment_number >= self.get_min_seen_secret() {
929939
let secret = self.get_secret(commitment_number).unwrap();
@@ -942,7 +952,7 @@ impl ChannelMonitor {
942952
};
943953
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));
944954
let a_htlc_key = match self.their_htlc_base_key {
945-
None => return txn_to_broadcast,
955+
None => return (txn_to_broadcast, (commitment_txid, watch_outputs)),
946956
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)),
947957
};
948958

@@ -1009,7 +1019,7 @@ impl ChannelMonitor {
10091019
if htlc.transaction_output_index as usize >= tx.output.len() ||
10101020
tx.output[htlc.transaction_output_index as usize].value != htlc.amount_msat / 1000 ||
10111021
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
1022+
return (txn_to_broadcast, (commitment_txid, watch_outputs)); // Corrupted per_commitment_data, fuck this user
10131023
}
10141024
let input = TxIn {
10151025
previous_output: BitcoinOutPoint {
@@ -1044,10 +1054,10 @@ impl ChannelMonitor {
10441054

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

10521062
let outputs = vec!(TxOut {
10531063
script_pubkey: self.destination_script.clone(),
@@ -1077,7 +1087,7 @@ impl ChannelMonitor {
10771087
// already processed the block, resulting in the remote_commitment_txn_on_chain entry
10781088
// not being generated by the above conditional. Thus, to be safe, we go ahead and
10791089
// insert it here.
1080-
// TODO: Register all outputs in commitment_tx with the ChainWatchInterface!
1090+
watch_outputs.append(&mut tx.output.clone());
10811091
self.remote_commitment_txn_on_chain.lock().unwrap().insert(commitment_txid, commitment_number);
10821092

10831093
if let Some(revocation_points) = self.their_cur_revocation_points {
@@ -1098,7 +1108,7 @@ impl ChannelMonitor {
10981108
},
10991109
};
11001110
let a_htlc_key = match self.their_htlc_base_key {
1101-
None => return txn_to_broadcast,
1111+
None => return (txn_to_broadcast, (commitment_txid, watch_outputs)),
11021112
Some(their_htlc_base_key) => ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, revocation_point, &their_htlc_base_key)),
11031113
};
11041114

@@ -1161,7 +1171,7 @@ impl ChannelMonitor {
11611171
}
11621172
}
11631173

1164-
if inputs.is_empty() { return txn_to_broadcast; } // Nothing to be done...probably a false positive/local tx
1174+
if inputs.is_empty() { return (txn_to_broadcast, (commitment_txid, watch_outputs)); } // Nothing to be done...probably a false positive/local tx
11651175

11661176
let outputs = vec!(TxOut {
11671177
script_pubkey: self.destination_script.clone(),
@@ -1189,7 +1199,7 @@ impl ChannelMonitor {
11891199
//TODO: For each input check if its in our remote_commitment_txn_on_chain map!
11901200
}
11911201

1192-
txn_to_broadcast
1202+
(txn_to_broadcast, (commitment_txid, watch_outputs))
11931203
}
11941204

11951205
fn broadcast_by_local_state(&self, local_tx: &LocalSignedTx) -> Vec<Transaction> {
@@ -1250,11 +1260,15 @@ impl ChannelMonitor {
12501260
Vec::new()
12511261
}
12521262

1253-
fn block_connected(&self, txn_matched: &[&Transaction], height: u32, broadcaster: &BroadcasterInterface) {
1263+
fn block_connected(&self, txn_matched: &[&Transaction], height: u32, broadcaster: &BroadcasterInterface)-> Vec<(Sha256dHash, Vec<TxOut>)> {
1264+
let mut watch_outputs = Vec::new();
12541265
for tx in txn_matched {
12551266
for txin in tx.input.iter() {
12561267
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);
1268+
let (mut txn, new_outputs) = self.check_spend_remote_transaction(tx, height);
1269+
if !new_outputs.1.is_empty() {
1270+
watch_outputs.push(new_outputs);
1271+
}
12581272
if txn.is_empty() {
12591273
txn = self.check_spend_local_transaction(tx, height);
12601274
}
@@ -1281,6 +1295,7 @@ impl ChannelMonitor {
12811295
}
12821296
}
12831297
}
1298+
watch_outputs
12841299
}
12851300

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

0 commit comments

Comments
 (0)