-
Notifications
You must be signed in to change notification settings - Fork 418
Add LSPS5 DOS protections. #3993
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -107,9 +107,18 @@ struct ForwardPaymentAction(ChannelId, FeePayment); | |
#[derive(Debug, PartialEq)] | ||
struct ForwardHTLCsAction(ChannelId, Vec<InterceptedHTLC>); | ||
|
||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)] | ||
pub(crate) enum OutboundJITStage { | ||
PendingInitialPayment, | ||
PendingChannelOpen, | ||
PendingPaymentForward, | ||
PendingPayment, | ||
PaymentForwarded, | ||
} | ||
|
||
/// The different states a requested JIT channel can be in. | ||
#[derive(Debug)] | ||
enum OutboundJITChannelState { | ||
#[derive(Debug, PartialEq, Eq, Clone)] | ||
pub(crate) enum OutboundJITChannelState { | ||
/// The JIT channel SCID was created after a buy request, and we are awaiting an initial payment | ||
/// of sufficient size to open the channel. | ||
PendingInitialPayment { payment_queue: PaymentQueue }, | ||
|
@@ -134,6 +143,36 @@ enum OutboundJITChannelState { | |
PaymentForwarded { channel_id: ChannelId }, | ||
} | ||
|
||
impl OutboundJITChannelState { | ||
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. Do we need this explicit implementation? Wouldn't 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 would need putting also the happy to do it but wanted to minimize changes 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. seems like I can't define Ord but derive PartialOrd from the linter:
|
||
pub(crate) fn stage(&self) -> OutboundJITStage { | ||
match self { | ||
OutboundJITChannelState::PendingInitialPayment { .. } => { | ||
OutboundJITStage::PendingInitialPayment | ||
}, | ||
OutboundJITChannelState::PendingChannelOpen { .. } => { | ||
OutboundJITStage::PendingChannelOpen | ||
}, | ||
OutboundJITChannelState::PendingPaymentForward { .. } => { | ||
OutboundJITStage::PendingPaymentForward | ||
}, | ||
OutboundJITChannelState::PendingPayment { .. } => OutboundJITStage::PendingPayment, | ||
OutboundJITChannelState::PaymentForwarded { .. } => OutboundJITStage::PaymentForwarded, | ||
} | ||
} | ||
} | ||
|
||
impl PartialOrd for OutboundJITChannelState { | ||
fn partial_cmp(&self, other: &Self) -> Option<CmpOrdering> { | ||
Some(self.cmp(other)) | ||
} | ||
} | ||
|
||
impl Ord for OutboundJITChannelState { | ||
fn cmp(&self, other: &Self) -> core::cmp::Ordering { | ||
self.stage().cmp(&other.stage()) | ||
} | ||
} | ||
|
||
impl OutboundJITChannelState { | ||
fn new() -> Self { | ||
OutboundJITChannelState::PendingInitialPayment { payment_queue: PaymentQueue::new() } | ||
|
@@ -572,6 +611,16 @@ where | |
&self.config | ||
} | ||
|
||
pub(crate) fn highest_state_for_peer( | ||
&self, counterparty_node_id: &PublicKey, | ||
) -> Option<OutboundJITChannelState> { | ||
let outer_state_lock = self.per_peer_state.read().unwrap(); | ||
outer_state_lock.get(counterparty_node_id).and_then(|inner| { | ||
let peer_state = inner.lock().unwrap(); | ||
peer_state.outbound_channels_by_intercept_scid.values().map(|c| c.state.clone()).max() | ||
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 don't buy we need to clone here. 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.
|
||
}) | ||
} | ||
|
||
/// Used by LSP to inform a client requesting a JIT Channel the token they used is invalid. | ||
/// | ||
/// Should be called in response to receiving a [`LSPS2ServiceEvent::GetInfo`] event. | ||
|
@@ -1905,4 +1954,55 @@ mod tests { | |
); | ||
} | ||
} | ||
|
||
#[test] | ||
fn highest_state_for_peer_orders() { | ||
let opening_fee_params = LSPS2OpeningFeeParams { | ||
min_fee_msat: 0, | ||
proportional: 0, | ||
valid_until: LSPSDateTime::from_str("1970-01-01T00:00:00Z").unwrap(), | ||
min_lifetime: 0, | ||
max_client_to_self_delay: 0, | ||
min_payment_size_msat: 0, | ||
max_payment_size_msat: 0, | ||
promise: String::new(), | ||
}; | ||
|
||
let mut map = new_hash_map(); | ||
map.insert( | ||
0, | ||
OutboundJITChannel { | ||
state: OutboundJITChannelState::PendingInitialPayment { | ||
payment_queue: PaymentQueue::new(), | ||
}, | ||
user_channel_id: 0, | ||
opening_fee_params: opening_fee_params.clone(), | ||
payment_size_msat: None, | ||
}, | ||
); | ||
map.insert( | ||
1, | ||
OutboundJITChannel { | ||
state: OutboundJITChannelState::PendingChannelOpen { | ||
payment_queue: PaymentQueue::new(), | ||
opening_fee_msat: 0, | ||
}, | ||
user_channel_id: 1, | ||
opening_fee_params: opening_fee_params.clone(), | ||
payment_size_msat: None, | ||
}, | ||
); | ||
map.insert( | ||
2, | ||
OutboundJITChannel { | ||
state: OutboundJITChannelState::PaymentForwarded { channel_id: ChannelId([0; 32]) }, | ||
user_channel_id: 2, | ||
opening_fee_params, | ||
payment_size_msat: None, | ||
}, | ||
); | ||
|
||
let max_state = map.values().map(|c| c.state.clone()).max().unwrap(); | ||
assert!(matches!(max_state, OutboundJITChannelState::PaymentForwarded { .. })); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
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.
May be a tad easier to read:
(here and below)