Skip to content

Commit 264d065

Browse files
Antoine RiardTheBlueMatt
authored andcommitted
Detect onchain timeout of a HTLC in ChannelManager block_connected
Pass failure backward
1 parent 29d649f commit 264d065

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/ln/channelmanager.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1558,7 +1558,7 @@ impl ChannelManager {
15581558
rejected_by_dest: !payment_retryable,
15591559
});
15601560
} else {
1561-
panic!("should have onion error packet here");
1561+
//TODO: Pass this back (see GH #243)
15621562
}
15631563
},
15641564
HTLCSource::PreviousHopData(HTLCPreviousHopData { short_channel_id, htlc_id, incoming_packet_shared_secret }) => {
@@ -2715,6 +2715,8 @@ impl ChainListener for ChannelManager {
27152715
for htlc_update in self.monitor.fetch_pending_htlc_updated() {
27162716
if let Some(preimage) = htlc_update.payment_preimage {
27172717
self.claim_funds_internal(self.channel_state.lock().unwrap(), htlc_update.source, preimage);
2718+
} else {
2719+
self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), htlc_update.source, &htlc_update.payment_hash, HTLCFailReason::Reason { failure_code: 0x4000 | 10, data: Vec::new() });
27182720
}
27192721
}
27202722
}

src/ln/channelmonitor.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,31 @@ impl ChannelMonitor {
12871287
txn_to_broadcast.push(single_htlc_tx);
12881288
}
12891289
}
1290+
if !htlc.offered {
1291+
// TODO: If the HTLC has already expired, potentially merge it with the
1292+
// rest of the claim transaction, as above.
1293+
let input = TxIn {
1294+
previous_output: BitcoinOutPoint {
1295+
txid: commitment_txid,
1296+
vout: htlc.transaction_output_index,
1297+
},
1298+
script_sig: Script::new(),
1299+
sequence: idx as u32,
1300+
witness: Vec::new(),
1301+
};
1302+
let mut timeout_tx = Transaction {
1303+
version: 2,
1304+
lock_time: htlc.cltv_expiry,
1305+
input: vec![input],
1306+
output: vec!(TxOut {
1307+
script_pubkey: self.destination_script.clone(),
1308+
value: htlc.amount_msat / 1000,
1309+
}),
1310+
};
1311+
let sighash_parts = bip143::SighashComponents::new(&timeout_tx);
1312+
sign_input!(sighash_parts, timeout_tx.input[0], htlc.amount_msat / 1000, vec![0]);
1313+
txn_to_broadcast.push(timeout_tx);
1314+
}
12901315
}
12911316

12921317
if inputs.is_empty() { return (txn_to_broadcast, (commitment_txid, watch_outputs), spendable_outputs, htlc_updated); } // Nothing to be done...probably a false positive/local tx

0 commit comments

Comments
 (0)