This repository was archived by the owner on Jan 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
LSPS2: ability to signal channel open failure #137
Closed
johncantrell97
wants to merge
1
commit into
lightningdevkit:main
from
johncantrell97:signal-channel-open-failure
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ use crate::prelude::{HashMap, String, ToString, Vec}; | |
use crate::sync::{Arc, Mutex, RwLock}; | ||
|
||
use lightning::events::HTLCDestination; | ||
use lightning::ln::channelmanager::{AChannelManager, InterceptId}; | ||
use lightning::ln::channelmanager::{AChannelManager, FailureCode, InterceptId}; | ||
use lightning::ln::msgs::{ErrorAction, LightningError}; | ||
use lightning::ln::{ChannelId, PaymentHash}; | ||
use lightning::util::errors::APIError; | ||
|
@@ -362,7 +362,13 @@ impl OutboundJITChannelState { | |
let mut payment_queue_lock = payment_queue.lock().unwrap(); | ||
let payment_forwarded = | ||
OutboundJITChannelState::PaymentForwarded { channel_id: *channel_id }; | ||
let forward_htlcs = ForwardHTLCsAction(*channel_id, payment_queue_lock.clear()); | ||
let htlcs = payment_queue_lock | ||
.clear() | ||
.into_iter() | ||
.map(|(_, htlcs)| htlcs) | ||
.flatten() | ||
.collect(); | ||
let forward_htlcs = ForwardHTLCsAction(*channel_id, htlcs); | ||
Ok((payment_forwarded, Some(forward_htlcs))) | ||
}, | ||
OutboundJITChannelState::PaymentForwarded { channel_id } => { | ||
|
@@ -898,6 +904,79 @@ where | |
Ok(()) | ||
} | ||
|
||
/// Used by LSP to fail intercepted htlcs backwards when the channel open fails for any reason. | ||
/// | ||
/// Should be called in response to receiving a [`LSPS2ServiceEvent::OpenChannel`] event. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment is a bit misleading. It should be called after we get the event, but of course only if the channel open failed? |
||
/// | ||
/// The JIT channel state is reset such that the payer can attempt payment again. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we also expose a "abandon" variant that doesn't reset, but just prunes all state related to the channel open? |
||
/// [`LSPS2ServiceEvent::OpenChannel`]: crate::lsps2::event::LSPS2ServiceEvent::OpenChannel | ||
pub fn channel_open_failed( | ||
&self, counterparty_node_id: &PublicKey, user_channel_id: u128, | ||
) -> Result<(), APIError> { | ||
let outer_state_lock = self.per_peer_state.read().unwrap(); | ||
match outer_state_lock.get(counterparty_node_id) { | ||
Some(inner_state_lock) => { | ||
let mut peer_state = inner_state_lock.lock().unwrap(); | ||
|
||
if let Some(intercept_scid) = | ||
peer_state.intercept_scid_by_user_channel_id.get(&user_channel_id).copied() | ||
{ | ||
if let Some(jit_channel) = | ||
peer_state.outbound_channels_by_intercept_scid.get_mut(&intercept_scid) | ||
{ | ||
let new_state = if let OutboundJITChannelState::PendingChannelOpen { | ||
payment_queue, | ||
.. | ||
} = &jit_channel.state | ||
{ | ||
let mut queue = payment_queue.lock().unwrap(); | ||
let payment_hashes = queue | ||
.clear() | ||
.into_iter() | ||
.map(|(payment_hash, _)| payment_hash) | ||
.collect::<Vec<_>>(); | ||
for payment_hash in payment_hashes { | ||
self.channel_manager.get_cm().fail_htlc_backwards_with_reason( | ||
&payment_hash, | ||
FailureCode::TemporaryNodeFailure, | ||
); | ||
} | ||
OutboundJITChannelState::PendingInitialPayment { | ||
payment_queue: payment_queue.clone(), | ||
} | ||
} else { | ||
return Err(APIError::APIMisuseError { | ||
err: format!("Channel is not in the PendingChannelOpen state.",), | ||
}); | ||
}; | ||
jit_channel.state = new_state; | ||
} else { | ||
return Err(APIError::APIMisuseError { | ||
err: format!( | ||
"Failed to map the stored intercept_scid {} for the provided user_channel_id {} to a channel.", | ||
intercept_scid, | ||
user_channel_id, | ||
), | ||
}); | ||
} | ||
} else { | ||
return Err(APIError::APIMisuseError { | ||
err: format!( | ||
"Could not find a channel with user_channel_id {}", | ||
user_channel_id | ||
), | ||
}); | ||
} | ||
}, | ||
None => { | ||
return Err(APIError::APIMisuseError { | ||
err: format!("No counterparty state for: {}", counterparty_node_id), | ||
}); | ||
}, | ||
} | ||
Ok(()) | ||
} | ||
|
||
/// Forward [`Event::ChannelReady`] event parameters into this function. | ||
/// | ||
/// Will forward the intercepted HTLC if it matches a channel | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Not sure this change is worth as it makes things more complex and you only make use of it in one place where you then have to
map
to get rid of theInterceptedHTLC
s. Why not just useInterceptedHTLC::payment_hash
instead? If you're worried about duplicates, could just usededup
or collect aHashSet
?