Skip to content

Commit 0baf07e

Browse files
committed
Simplify and expand logging in is_resolving_htlc_output
This clarifies all the conditions we can hit and also ensures they are all logged in a clear and consistent manner.
1 parent 8c99089 commit 0baf07e

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

fuzz/fuzz_targets/full_stack_target.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,6 @@ mod tests {
853853
assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling UpdateHTLCs event in peer_handler for node 030200000000000000000000000000000000000000000000000000000000000000 with 1 adds, 0 fulfills, 0 fails for channel 3f00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&3)); // 7
854854
assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling UpdateHTLCs event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000000 with 0 adds, 1 fulfills, 0 fails for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&1)); // 8
855855
assert_eq!(log_entries.get(&("lightning::ln::peer_handler".to_string(), "Handling UpdateHTLCs event in peer_handler for node 030000000000000000000000000000000000000000000000000000000000000000 with 0 adds, 0 fulfills, 1 fails for channel 3d00000000000000000000000000000000000000000000000000000000000000".to_string())), Some(&2)); // 9
856-
assert_eq!(log_entries.get(&("lightning::ln::channelmonitor".to_string(), "Input spending 00000000000000000000000000000000000000000000000000000000000000fb:0 resolves HTLC with payment hash ff00000000000000000000000000000000000000000000000000000000000000 from remote commitment tx".to_string())), Some(&1)); // 10
856+
assert_eq!(log_entries.get(&("lightning::ln::channelmonitor".to_string(), "Input spending 00000000000000000000000000000000000000000000000000000000000000fb:0 in 0000000000000000000000000000000000000000000000000000000000000042 resolves outbound HTLC with payment hash ff00000000000000000000000000000000000000000000000000000000000000 from remote commitment tx with timeout".to_string())), Some(&1)); // 10
857857
}
858858
}

src/ln/channelmonitor.rs

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,22 +1833,43 @@ impl ChannelMonitor {
18331833

18341834
'outer_loop: for input in &tx.input {
18351835
let mut payment_data = None;
1836+
let revocation_sig_claim = (input.witness.len() == 3 && input.witness[2].len() == OFFERED_HTLC_SCRIPT_WEIGHT && input.witness[1].len() == 33)
1837+
|| (input.witness.len() == 3 && input.witness[2].len() == ACCEPTED_HTLC_SCRIPT_WEIGHT && input.witness[1].len() == 33);
1838+
let accepted_preimage_claim = input.witness.len() == 5 && input.witness[4].len() == ACCEPTED_HTLC_SCRIPT_WEIGHT;
1839+
let offered_preimage_claim = input.witness.len() == 3 && input.witness[2].len() == OFFERED_HTLC_SCRIPT_WEIGHT;
1840+
1841+
macro_rules! log_claim {
1842+
($source: expr, $local_tx: expr, $outbound_htlc: expr, $payment_hash: expr, $source_avail: expr) => {
1843+
// We found the output in question, but aren't failing it backwards
1844+
// as we have no corresponding source. This implies either it is an
1845+
// inbound HTLC or an outbound HTLC on a revoked transaction.
1846+
if ($local_tx && revocation_sig_claim) ||
1847+
($outbound_htlc && !$source_avail && (accepted_preimage_claim || offered_preimage_claim)) {
1848+
log_error!(self, "Input spending {}:{} in {} resolves {} HTLC with payment hash {} from {} with {}!",
1849+
input.previous_output.txid, input.previous_output.vout, tx.txid(),
1850+
if $outbound_htlc { "outbound" } else { "inbound" }, log_bytes!($payment_hash.0), $source,
1851+
if revocation_sig_claim { "revocation sig" } else { "preimage claim after we'd passed the HTLC resolution back" });
1852+
} else {
1853+
log_info!(self, "Input spending {}:{} in {} resolves {} HTLC with payment hash {} from {} with {}",
1854+
input.previous_output.txid, input.previous_output.vout, tx.txid(),
1855+
if $outbound_htlc { "outbound" } else { "inbound" }, log_bytes!($payment_hash.0), $source,
1856+
if revocation_sig_claim { "revocation sig" } else if accepted_preimage_claim || offered_preimage_claim { "preimage" } else { "timeout" });
1857+
}
1858+
}
1859+
}
18361860

18371861
macro_rules! scan_commitment {
1838-
($htlc_outputs: expr, $htlc_sources: expr, $source: expr) => {
1862+
($htlc_outputs: expr, $htlc_sources: expr, $source: expr, $local_tx: expr) => {
18391863
for &(ref payment_hash, ref source, ref vout) in $htlc_sources.iter() {
18401864
if &Some(input.previous_output.vout) == vout {
1841-
log_trace!(self, "Input spending {}:{} resolves HTLC with payment hash {} from {}", input.previous_output.txid, input.previous_output.vout, log_bytes!(payment_hash.0), $source);
1865+
log_claim!($source, $local_tx, true, payment_hash, true);
18421866
payment_data = Some((source.clone(), *payment_hash));
18431867
}
18441868
}
18451869
if payment_data.is_none() {
18461870
for htlc_output in $htlc_outputs {
1847-
if input.previous_output.vout == htlc_output.transaction_output_index && !htlc_output.offered {
1848-
log_info!(self, "Input spending {}:{} in {} resolves inbound HTLC with timeout from {}", input.previous_output.txid, input.previous_output.vout, tx.txid(), $source);
1849-
continue 'outer_loop;
1850-
} else if input.previous_output.vout == htlc_output.transaction_output_index && tx.lock_time > 0 {
1851-
log_info!(self, "Input spending {}:{} in {} resolves offered HTLC with HTLC-timeout from {}", input.previous_output.txid, input.previous_output.vout, tx.txid(), $source);
1871+
if input.previous_output.vout == htlc_output.transaction_output_index {
1872+
log_claim!($source, $local_tx, $local_tx == htlc_output.offered, htlc_output.payment_hash, false);
18521873
continue 'outer_loop;
18531874
}
18541875
}
@@ -1860,31 +1881,28 @@ impl ChannelMonitor {
18601881
if input.previous_output.txid == current_local_signed_commitment_tx.txid {
18611882
scan_commitment!(current_local_signed_commitment_tx.htlc_outputs.iter().map(|&(ref a, _, _)| a),
18621883
current_local_signed_commitment_tx.htlc_sources,
1863-
"our latest local commitment tx");
1884+
"our latest local commitment tx", true);
18641885
}
18651886
}
18661887
if let Some(ref prev_local_signed_commitment_tx) = self.prev_local_signed_commitment_tx {
18671888
if input.previous_output.txid == prev_local_signed_commitment_tx.txid {
18681889
scan_commitment!(prev_local_signed_commitment_tx.htlc_outputs.iter().map(|&(ref a, _, _)| a),
18691890
prev_local_signed_commitment_tx.htlc_sources,
1870-
"our previous local commitment tx");
1891+
"our previous local commitment tx", true);
18711892
}
18721893
}
18731894
if let Some(&(ref htlc_outputs, ref htlc_sources)) = self.remote_claimable_outpoints.get(&input.previous_output.txid) {
1874-
scan_commitment!(htlc_outputs, htlc_sources, "remote commitment tx");
1895+
scan_commitment!(htlc_outputs, htlc_sources, "remote commitment tx", false);
18751896
}
18761897

18771898
// If tx isn't solving htlc output from local/remote commitment tx and htlc isn't outbound we don't need
18781899
// to broadcast solving backward
18791900
if let Some((source, payment_hash)) = payment_data {
18801901
let mut payment_preimage = PaymentPreimage([0; 32]);
1881-
if (input.witness.len() == 3 && input.witness[2].len() == OFFERED_HTLC_SCRIPT_WEIGHT && input.witness[1].len() == 33)
1882-
|| (input.witness.len() == 3 && input.witness[2].len() == ACCEPTED_HTLC_SCRIPT_WEIGHT && input.witness[1].len() == 33) {
1883-
log_error!(self, "Remote used revocation sig to take a {} HTLC output at index {} from commitment_tx {}", if input.witness[2].len() == OFFERED_HTLC_SCRIPT_WEIGHT { "offered" } else { "accepted" }, input.previous_output.vout, input.previous_output.txid);
1884-
} else if input.witness.len() == 5 && input.witness[4].len() == ACCEPTED_HTLC_SCRIPT_WEIGHT {
1902+
if accepted_preimage_claim {
18851903
payment_preimage.0.copy_from_slice(&input.witness[3]);
18861904
htlc_updated.push((source, Some(payment_preimage), payment_hash));
1887-
} else if input.witness.len() == 3 && input.witness[2].len() == OFFERED_HTLC_SCRIPT_WEIGHT {
1905+
} else if offered_preimage_claim {
18881906
payment_preimage.0.copy_from_slice(&input.witness[1]);
18891907
htlc_updated.push((source, Some(payment_preimage), payment_hash));
18901908
} else {

0 commit comments

Comments
 (0)