Skip to content

Commit b85634d

Browse files
author
Antoine Riard
committed
Add preimages extraction in block_connected to claim funds
backward Add caching of PreviousHopData even in case of non-terminal peer to be able to route backward from on-chain resolution and prune them when htlc is updated by other node
1 parent 826233e commit b85634d

File tree

2 files changed

+198
-34
lines changed

2 files changed

+198
-34
lines changed

src/ln/channel.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,7 +1508,7 @@ impl Channel {
15081508

15091509
/// Removes an outbound HTLC which has been commitment_signed by the remote end
15101510
#[inline]
1511-
fn mark_outbound_htlc_removed(&mut self, htlc_id: u64, check_preimage: Option<[u8; 32]>, fail_reason: Option<HTLCFailReason>) -> Result<&HTLCSource, ChannelError> {
1511+
fn mark_outbound_htlc_removed(&mut self, htlc_id: u64, check_preimage: Option<[u8; 32]>, fail_reason: Option<HTLCFailReason>) -> Result<(&HTLCSource, [u8;32]), ChannelError> {
15121512
for htlc in self.pending_outbound_htlcs.iter_mut() {
15131513
if htlc.htlc_id == htlc_id {
15141514
match check_preimage {
@@ -1528,13 +1528,13 @@ impl Channel {
15281528
OutboundHTLCState::AwaitingRemoteRevokeToRemove | OutboundHTLCState::AwaitingRemovedRemoteRevoke | OutboundHTLCState::RemoteRemoved =>
15291529
return Err(ChannelError::Close("Remote tried to fulfill HTLC that they'd already fulfilled")),
15301530
}
1531-
return Ok(&htlc.source);
1531+
return Ok((&htlc.source, htlc.payment_hash));
15321532
}
15331533
}
15341534
Err(ChannelError::Close("Remote tried to fulfill/fail an HTLC we couldn't find"))
15351535
}
15361536

1537-
pub fn update_fulfill_htlc(&mut self, msg: &msgs::UpdateFulfillHTLC) -> Result<&HTLCSource, ChannelError> {
1537+
pub fn update_fulfill_htlc(&mut self, msg: &msgs::UpdateFulfillHTLC) -> Result<(&HTLCSource, [u8;32]), ChannelError> {
15381538
if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
15391539
return Err(ChannelError::Close("Got fulfill HTLC message when channel was not in an operational state"));
15401540
}
@@ -1550,7 +1550,7 @@ impl Channel {
15501550
self.mark_outbound_htlc_removed(msg.htlc_id, Some(payment_hash), None)
15511551
}
15521552

1553-
pub fn update_fail_htlc(&mut self, msg: &msgs::UpdateFailHTLC, fail_reason: HTLCFailReason) -> Result<&HTLCSource, ChannelError> {
1553+
pub fn update_fail_htlc(&mut self, msg: &msgs::UpdateFailHTLC, fail_reason: HTLCFailReason) -> Result<(&HTLCSource, [u8;32]), ChannelError> {
15541554
if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
15551555
return Err(ChannelError::Close("Got fail HTLC message when channel was not in an operational state"));
15561556
}
@@ -1561,7 +1561,7 @@ impl Channel {
15611561
self.mark_outbound_htlc_removed(msg.htlc_id, None, Some(fail_reason))
15621562
}
15631563

1564-
pub fn update_fail_malformed_htlc<'a>(&mut self, msg: &msgs::UpdateFailMalformedHTLC, fail_reason: HTLCFailReason) -> Result<&HTLCSource, ChannelError> {
1564+
pub fn update_fail_malformed_htlc<'a>(&mut self, msg: &msgs::UpdateFailMalformedHTLC, fail_reason: HTLCFailReason) -> Result<(&HTLCSource, [u8;32]), ChannelError> {
15651565
if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
15661566
return Err(ChannelError::Close("Got fail malformed HTLC message when channel was not in an operational state"));
15671567
}
@@ -2928,7 +2928,7 @@ impl Channel {
29282928
/// those explicitly stated to be allowed after shutdown completes, eg some simple getters).
29292929
/// Also returns the list of payment_hashes for channels which we can safely fail backwards
29302930
/// immediately (others we will have to allow to time out).
2931-
pub fn force_shutdown(&mut self) -> (Vec<Transaction>, Vec<(HTLCSource, [u8; 32])>) {
2931+
pub fn force_shutdown(&mut self) -> (Vec<Transaction>, Vec<(HTLCSource, [u8; 32])>, Vec<[u8;32]>) {
29322932
assert!(self.channel_state != ChannelState::ShutdownComplete as u32);
29332933

29342934
// We go ahead and "free" any holding cell HTLCs or HTLCs we haven't yet committed to and
@@ -2943,17 +2943,19 @@ impl Channel {
29432943
}
29442944
}
29452945

2946-
for _htlc in self.pending_outbound_htlcs.drain(..) {
2947-
//TODO: Do something with the remaining HTLCs
2948-
//(we need to have the ChannelManager monitor them so we can claim the inbound HTLCs
2949-
//which correspond)
2946+
let mut unsolved_htlcs = Vec::with_capacity(self.pending_outbound_htlcs.len() + self.pending_inbound_htlcs.len());
2947+
for outbound_htlcs in self.pending_outbound_htlcs.iter() {
2948+
unsolved_htlcs.push(outbound_htlcs.payment_hash);
2949+
}
2950+
for inbound_htlcs in self.pending_inbound_htlcs.iter() {
2951+
unsolved_htlcs.push(inbound_htlcs.payment_hash);
29502952
}
29512953

29522954
self.channel_state = ChannelState::ShutdownComplete as u32;
29532955
self.channel_update_count += 1;
29542956
let mut res = Vec::new();
29552957
mem::swap(&mut res, &mut self.last_local_commitment_txn);
2956-
(res, dropped_outbound_htlcs)
2958+
(res, dropped_outbound_htlcs, unsolved_htlcs)
29572959
}
29582960
}
29592961

0 commit comments

Comments
 (0)