Skip to content

Commit a604cb6

Browse files
authored
Merge pull request #283 from TheBlueMatt/2019-01-htlc-resolution-log
Simplify and expand logging in is_resolving_htlc_output
2 parents 292ab5e + 90ce971 commit a604cb6

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
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 remote commitment tx (00000000000000000000000000000000000000000000000000000000000000fb:0) in 0000000000000000000000000000000000000000000000000000000000000042 resolves outbound HTLC with payment hash ff00000000000000000000000000000000000000000000000000000000000000 with timeout".to_string())), Some(&1)); // 10
857857
}
858858
}

src/ln/channelmonitor.rs

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,22 +1836,48 @@ impl ChannelMonitor {
18361836

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

18401864
macro_rules! scan_commitment {
1841-
($htlc_outputs: expr, $htlc_sources: expr, $source: expr) => {
1865+
($htlc_outputs: expr, $htlc_sources: expr, $source: expr, $local_tx: expr) => {
18421866
for &(ref payment_hash, ref source, ref vout) in $htlc_sources.iter() {
18431867
if &Some(input.previous_output.vout) == vout {
1844-
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);
1868+
log_claim!($source, $local_tx, true, payment_hash, true);
1869+
// We have a resolution of an HTLC either from one of our latest
1870+
// local commitment transactions or an unrevoked remote commitment
1871+
// transaction. This implies we either learned a preimage, the HTLC
1872+
// has timed out, or we screwed up. In any case, we should now
1873+
// resolve the source HTLC with the original sender.
18451874
payment_data = Some((source.clone(), *payment_hash));
18461875
}
18471876
}
18481877
if payment_data.is_none() {
18491878
for htlc_output in $htlc_outputs {
1850-
if input.previous_output.vout == htlc_output.transaction_output_index && !htlc_output.offered {
1851-
log_info!(self, "Input spending {}:{} in {} resolves inbound HTLC with timeout from {}", input.previous_output.txid, input.previous_output.vout, tx.txid(), $source);
1852-
continue 'outer_loop;
1853-
} else if input.previous_output.vout == htlc_output.transaction_output_index && tx.lock_time > 0 {
1854-
log_info!(self, "Input spending {}:{} in {} resolves offered HTLC with HTLC-timeout from {}", input.previous_output.txid, input.previous_output.vout, tx.txid(), $source);
1879+
if input.previous_output.vout == htlc_output.transaction_output_index {
1880+
log_claim!($source, $local_tx, $local_tx == htlc_output.offered, htlc_output.payment_hash, false);
18551881
continue 'outer_loop;
18561882
}
18571883
}
@@ -1863,31 +1889,28 @@ impl ChannelMonitor {
18631889
if input.previous_output.txid == current_local_signed_commitment_tx.txid {
18641890
scan_commitment!(current_local_signed_commitment_tx.htlc_outputs.iter().map(|&(ref a, _, _)| a),
18651891
current_local_signed_commitment_tx.htlc_sources,
1866-
"our latest local commitment tx");
1892+
"our latest local commitment tx", true);
18671893
}
18681894
}
18691895
if let Some(ref prev_local_signed_commitment_tx) = self.prev_local_signed_commitment_tx {
18701896
if input.previous_output.txid == prev_local_signed_commitment_tx.txid {
18711897
scan_commitment!(prev_local_signed_commitment_tx.htlc_outputs.iter().map(|&(ref a, _, _)| a),
18721898
prev_local_signed_commitment_tx.htlc_sources,
1873-
"our latest local commitment tx");
1899+
"our previous local commitment tx", true);
18741900
}
18751901
}
18761902
if let Some(&(ref htlc_outputs, ref htlc_sources)) = self.remote_claimable_outpoints.get(&input.previous_output.txid) {
1877-
scan_commitment!(htlc_outputs, htlc_sources, "remote commitment tx");
1903+
scan_commitment!(htlc_outputs, htlc_sources, "remote commitment tx", false);
18781904
}
18791905

1880-
// If tx isn't solving htlc output from local/remote commitment tx and htlc isn't outbound we don't need
1881-
// to broadcast solving backward
1906+
// Check that scan_commitment, above, decided there is some source worth relaying an
1907+
// HTLC resolution backwards to and figure out whether we learned a preimage from it.
18821908
if let Some((source, payment_hash)) = payment_data {
18831909
let mut payment_preimage = PaymentPreimage([0; 32]);
1884-
if (input.witness.len() == 3 && input.witness[2].len() == OFFERED_HTLC_SCRIPT_WEIGHT && input.witness[1].len() == 33)
1885-
|| (input.witness.len() == 3 && input.witness[2].len() == ACCEPTED_HTLC_SCRIPT_WEIGHT && input.witness[1].len() == 33) {
1886-
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);
1887-
} else if input.witness.len() == 5 && input.witness[4].len() == ACCEPTED_HTLC_SCRIPT_WEIGHT {
1910+
if accepted_preimage_claim {
18881911
payment_preimage.0.copy_from_slice(&input.witness[3]);
18891912
htlc_updated.push((source, Some(payment_preimage), payment_hash));
1890-
} else if input.witness.len() == 3 && input.witness[2].len() == OFFERED_HTLC_SCRIPT_WEIGHT {
1913+
} else if offered_preimage_claim {
18911914
payment_preimage.0.copy_from_slice(&input.witness[1]);
18921915
htlc_updated.push((source, Some(payment_preimage), payment_hash));
18931916
} else {

0 commit comments

Comments
 (0)