Skip to content

Commit 0bafe5c

Browse files
committed
f - more descriptive var names + outpoints, not txn
1 parent b04fe2f commit 0bafe5c

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

src/ln/channelmanager.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3297,7 +3297,8 @@ mod tests {
32973297
nodes[1].chain_monitor.block_connected_with_filtering(&Block { header, txdata: vec![revoked_local_txn[0].clone()] }, 1);
32983298
{
32993299
let mut node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap();
3300-
assert_eq!(node_txn.len(), 2);
3300+
assert_eq!(node_txn.len(), 3);
3301+
assert_eq!(node_txn.pop().unwrap(), node_txn[0]); // An outpoint registration will result in a 2nd block_connected
33013302
assert_eq!(node_txn[0].input.len(), 1);
33023303

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

src/ln/channelmonitor.rs

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,11 @@ 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-
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);
72+
let txn_outputs = monitor.block_connected(txn_matched, height, &*self.broadcaster);
73+
for (ref txid, ref outputs) in txn_outputs {
74+
for (idx, output) in outputs.iter().enumerate() {
75+
self.chain_monitor.install_watch_outpoint((txid.clone(), idx as u32), &output.script_pubkey);
76+
}
7577
}
7678
}
7779
}
@@ -467,7 +469,7 @@ impl ChannelMonitor {
467469
/// optional, without it this monitor cannot be used in an SPV client, but you may wish to
468470
/// avoid this (or call unset_funding_info) on a monitor you wish to send to a watchtower as it
469471
/// provides slightly better privacy.
470-
/// It is the responsability of the caller to register outpoint and script with passing the former
472+
/// It is the responsability of the caller to register outpoint and script with passing the former
471473
/// value as key to update current monitor
472474
pub(super) fn set_funding_info(&mut self, funding_info: (OutPoint, Script)) {
473475
self.funding_txo = Some(funding_info);
@@ -912,23 +914,24 @@ impl ChannelMonitor {
912914
/// height > height + CLTV_SHARED_CLAIM_BUFFER. In any case, will install monitoring for
913915
/// HTLC-Success/HTLC-Timeout transactions, and claim them using the revocation key (if
914916
/// applicable) as well.
915-
fn check_spend_remote_transaction(&self, tx: &Transaction, height: u32) -> (Vec<Transaction>, Vec<TxOut>) {
917+
fn check_spend_remote_transaction(&self, tx: &Transaction, height: u32) -> (Vec<Transaction>, (Sha256dHash, Vec<TxOut>)) {
916918
// Most secp and related errors trying to create keys means we have no hope of constructing
917919
// a spend transaction...so we return no transactions to broadcast
918920
let mut txn_to_broadcast = Vec::new();
919-
let mut outputs = Vec::new();
921+
let mut watch_outputs = Vec::new();
922+
923+
let commitment_txid = tx.txid(); //TODO: This is gonna be a performance bottleneck for watchtowers!
924+
let per_commitment_option = self.remote_claimable_outpoints.get(&commitment_txid);
925+
920926
macro_rules! ignore_error {
921927
( $thing : expr ) => {
922928
match $thing {
923929
Ok(a) => a,
924-
Err(_) => return (txn_to_broadcast, outputs)
930+
Err(_) => return (txn_to_broadcast, (commitment_txid, watch_outputs))
925931
}
926932
};
927933
}
928934

929-
let commitment_txid = tx.txid(); //TODO: This is gonna be a performance bottleneck for watchtowers!
930-
let per_commitment_option = self.remote_claimable_outpoints.get(&commitment_txid);
931-
932935
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);
933936
if commitment_number >= self.get_min_seen_secret() {
934937
let secret = self.get_secret(commitment_number).unwrap();
@@ -947,7 +950,7 @@ impl ChannelMonitor {
947950
};
948951
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));
949952
let a_htlc_key = match self.their_htlc_base_key {
950-
None => return (txn_to_broadcast, outputs),
953+
None => return (txn_to_broadcast, (commitment_txid, watch_outputs)),
951954
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)),
952955
};
953956

@@ -1014,7 +1017,7 @@ impl ChannelMonitor {
10141017
if htlc.transaction_output_index as usize >= tx.output.len() ||
10151018
tx.output[htlc.transaction_output_index as usize].value != htlc.amount_msat / 1000 ||
10161019
tx.output[htlc.transaction_output_index as usize].script_pubkey != expected_script.to_v0_p2wsh() {
1017-
return (txn_to_broadcast, outputs); // Corrupted per_commitment_data, fuck this user
1020+
return (txn_to_broadcast, (commitment_txid, watch_outputs)); // Corrupted per_commitment_data, fuck this user
10181021
}
10191022
let input = TxIn {
10201023
previous_output: BitcoinOutPoint {
@@ -1049,10 +1052,10 @@ impl ChannelMonitor {
10491052

10501053
if !inputs.is_empty() || !txn_to_broadcast.is_empty() { // ie we're confident this is actually ours
10511054
// We're definitely a remote commitment transaction!
1052-
outputs.append(&mut tx.output.clone());
1055+
watch_outputs.append(&mut tx.output.clone());
10531056
self.remote_commitment_txn_on_chain.lock().unwrap().insert(commitment_txid, commitment_number);
10541057
}
1055-
if inputs.is_empty() { return (txn_to_broadcast, outputs); } // Nothing to be done...probably a false positive/local tx
1058+
if inputs.is_empty() { return (txn_to_broadcast, (commitment_txid, watch_outputs)); } // Nothing to be done...probably a false positive/local tx
10561059

10571060
let outputs = vec!(TxOut {
10581061
script_pubkey: self.destination_script.clone(),
@@ -1082,7 +1085,7 @@ impl ChannelMonitor {
10821085
// already processed the block, resulting in the remote_commitment_txn_on_chain entry
10831086
// not being generated by the above conditional. Thus, to be safe, we go ahead and
10841087
// insert it here.
1085-
outputs.append(&mut tx.output.clone());
1088+
watch_outputs.append(&mut tx.output.clone());
10861089
self.remote_commitment_txn_on_chain.lock().unwrap().insert(commitment_txid, commitment_number);
10871090

10881091
if let Some(revocation_points) = self.their_cur_revocation_points {
@@ -1103,7 +1106,7 @@ impl ChannelMonitor {
11031106
},
11041107
};
11051108
let a_htlc_key = match self.their_htlc_base_key {
1106-
None => return (txn_to_broadcast, outputs),
1109+
None => return (txn_to_broadcast, (commitment_txid, watch_outputs)),
11071110
Some(their_htlc_base_key) => ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, revocation_point, &their_htlc_base_key)),
11081111
};
11091112

@@ -1166,7 +1169,7 @@ impl ChannelMonitor {
11661169
}
11671170
}
11681171

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

11711174
let outputs = vec!(TxOut {
11721175
script_pubkey: self.destination_script.clone(),
@@ -1194,7 +1197,7 @@ impl ChannelMonitor {
11941197
//TODO: For each input check if its in our remote_commitment_txn_on_chain map!
11951198
}
11961199

1197-
(txn_to_broadcast, outputs)
1200+
(txn_to_broadcast, (commitment_txid, watch_outputs))
11981201
}
11991202

12001203
fn broadcast_by_local_state(&self, local_tx: &LocalSignedTx) -> Vec<Transaction> {
@@ -1255,13 +1258,15 @@ impl ChannelMonitor {
12551258
Vec::new()
12561259
}
12571260

1258-
fn block_connected(&self, txn_matched: &[&Transaction], height: u32, broadcaster: &BroadcasterInterface) -> Vec<TxOut> {
1259-
let mut outputs = Vec::new();
1261+
fn block_connected(&self, txn_matched: &[&Transaction], height: u32, broadcaster: &BroadcasterInterface)-> Vec<(Sha256dHash, Vec<TxOut>)> {
1262+
let mut watch_outputs = Vec::new();
12601263
for tx in txn_matched {
12611264
for txin in tx.input.iter() {
12621265
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) {
1263-
let (mut txn, out) = self.check_spend_remote_transaction(tx, height);
1264-
outputs = out;
1266+
let (mut txn, new_outputs) = self.check_spend_remote_transaction(tx, height);
1267+
if !new_outputs.1.is_empty() {
1268+
watch_outputs.push(new_outputs);
1269+
}
12651270
if txn.is_empty() {
12661271
txn = self.check_spend_local_transaction(tx, height);
12671272
}
@@ -1288,7 +1293,7 @@ impl ChannelMonitor {
12881293
}
12891294
}
12901295
}
1291-
outputs
1296+
watch_outputs
12921297
}
12931298

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

0 commit comments

Comments
 (0)