Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

allow overriding min/max payment sizes #97

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/lsps2/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,6 @@ where
request_id,
counterparty_node_id: *counterparty_node_id,
opening_fee_params_menu: result.opening_fee_params_menu,
min_payment_size_msat: result.min_payment_size_msat,
max_payment_size_msat: result.max_payment_size_msat,
},
));
}
Expand Down
4 changes: 0 additions & 4 deletions src/lsps2/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ pub enum LSPS2ClientEvent {
/// The menu of fee parameters the LSP is offering at this time.
/// You must select one of these if you wish to proceed.
opening_fee_params_menu: Vec<OpeningFeeParams>,
/// The min payment size allowed when opening the channel.
min_payment_size_msat: u64,
/// The max payment size allowed when opening the channel.
max_payment_size_msat: u64,
},
/// Provides the necessary information to generate a payable invoice that then may be given to
/// the payer.
Expand Down
34 changes: 29 additions & 5 deletions src/lsps2/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ pub struct RawOpeningFeeParams {
pub valid_until: chrono::DateTime<Utc>,
/// The number of blocks after confirmation that the LSP promises it will keep the channel alive without closing.
pub min_lifetime: u32,
/// T maximum number of blocks that the client is allowed to set its `to_self_delay` parameter.
/// The maximum number of blocks that the client is allowed to set its `to_self_delay` parameter.
pub max_client_to_self_delay: u32,
/// The minimum payment size that the LSP will accept when opening a channel.
pub min_payment_size_msat: u64,
/// The maximum payment size that the LSP will accept when opening a channel.
pub max_payment_size_msat: u64,
}

impl RawOpeningFeeParams {
Expand All @@ -53,6 +57,8 @@ impl RawOpeningFeeParams {
hmac.input(self.valid_until.to_rfc3339().as_bytes());
hmac.input(&self.min_lifetime.to_be_bytes());
hmac.input(&self.max_client_to_self_delay.to_be_bytes());
hmac.input(&self.min_payment_size_msat.to_be_bytes());
hmac.input(&self.max_payment_size_msat.to_be_bytes());
let promise_bytes = Hmac::from_engine(hmac).to_byte_array();
let promise = utils::hex_str(&promise_bytes[..]);
OpeningFeeParams {
Expand All @@ -61,6 +67,8 @@ impl RawOpeningFeeParams {
valid_until: self.valid_until.clone(),
min_lifetime: self.min_lifetime,
max_client_to_self_delay: self.max_client_to_self_delay,
min_payment_size_msat: self.min_payment_size_msat,
max_payment_size_msat: self.max_payment_size_msat,
promise,
}
}
Expand All @@ -83,6 +91,10 @@ pub struct OpeningFeeParams {
pub min_lifetime: u32,
/// The maximum number of blocks that the client is allowed to set its `to_self_delay` parameter.
pub max_client_to_self_delay: u32,
/// The minimum payment size that the LSP will accept when opening a channel.
pub min_payment_size_msat: u64,
/// The maximum payment size that the LSP will accept when opening a channel.
pub max_payment_size_msat: u64,
/// The HMAC used to verify the authenticity of these parameters.
pub promise: String,
}
Expand All @@ -92,10 +104,6 @@ pub struct OpeningFeeParams {
pub struct GetInfoResponse {
/// A set of opening fee parameters.
pub opening_fee_params_menu: Vec<OpeningFeeParams>,
/// The minimum payment size required to open a channel.
pub min_payment_size_msat: u64,
/// The maximum payment size the lsp will tolerate.
pub max_payment_size_msat: u64,
}

/// A request to buy a JIT channel.
Expand Down Expand Up @@ -215,13 +223,17 @@ mod tests {
chrono::DateTime::parse_from_rfc3339("2035-05-20T08:30:45Z").unwrap().into();
let min_lifetime = 144;
let max_client_to_self_delay = 128;
let min_payment_size_msat = 1;
let max_payment_size_msat = 100_000_000;

let raw = RawOpeningFeeParams {
min_fee_msat,
proportional,
valid_until: valid_until.clone().into(),
min_lifetime,
max_client_to_self_delay,
min_payment_size_msat,
max_payment_size_msat,
};

let promise_secret = [1u8; 32];
Expand All @@ -244,13 +256,17 @@ mod tests {
let valid_until = chrono::DateTime::parse_from_rfc3339("2035-05-20T08:30:45Z").unwrap();
let min_lifetime = 144;
let max_client_to_self_delay = 128;
let min_payment_size_msat = 1;
let max_payment_size_msat = 100_000_000;

let raw = RawOpeningFeeParams {
min_fee_msat,
proportional,
valid_until: valid_until.into(),
min_lifetime,
max_client_to_self_delay,
min_payment_size_msat,
max_payment_size_msat,
};

let promise_secret = [1u8; 32];
Expand All @@ -267,13 +283,17 @@ mod tests {
let valid_until = chrono::DateTime::parse_from_rfc3339("2035-05-20T08:30:45Z").unwrap();
let min_lifetime = 144;
let max_client_to_self_delay = 128;
let min_payment_size_msat = 1;
let max_payment_size_msat = 100_000_000;

let raw = RawOpeningFeeParams {
min_fee_msat,
proportional,
valid_until: valid_until.into(),
min_lifetime,
max_client_to_self_delay,
min_payment_size_msat,
max_payment_size_msat,
};

let promise_secret = [1u8; 32];
Expand All @@ -292,13 +312,17 @@ mod tests {
let valid_until = chrono::DateTime::parse_from_rfc3339("2023-05-20T08:30:45Z").unwrap();
let min_lifetime = 144;
let max_client_to_self_delay = 128;
let min_payment_size_msat = 1;
let max_payment_size_msat = 100_000_000;

let raw = RawOpeningFeeParams {
min_fee_msat,
proportional,
valid_until: valid_until.into(),
min_lifetime,
max_client_to_self_delay,
min_payment_size_msat,
max_payment_size_msat,
};

let promise_secret = [1u8; 32];
Expand Down
36 changes: 18 additions & 18 deletions src/lsps2/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ pub struct LSPS2ServiceConfig {
///
/// Note: If this changes then old promises given out will be considered invalid.
pub promise_secret: [u8; 32],
/// The minimum payment size you are willing to accept.
pub min_payment_size_msat: u64,
/// The maximum payment size you are willing to accept.
pub max_payment_size_msat: u64,
}

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
Expand All @@ -69,6 +65,8 @@ enum OutboundJITChannelState {
AwaitingPayment {
min_fee_msat: u64,
proportional_fee: u32,
min_payment_size_msat: u64,
max_payment_size_msat: u64,
htlcs: Vec<InterceptedHTLC>,
payment_size_msat: Option<u64>,
},
Expand All @@ -88,18 +86,20 @@ impl OutboundJITChannelState {
OutboundJITChannelState::AwaitingPayment {
min_fee_msat: opening_fee_params.min_fee_msat,
proportional_fee: opening_fee_params.proportional,
min_payment_size_msat: opening_fee_params.min_payment_size_msat,
max_payment_size_msat: opening_fee_params.max_payment_size_msat,
htlcs: vec![],
payment_size_msat,
}
}

fn htlc_intercepted(
&self, htlc: InterceptedHTLC, config: &LSPS2ServiceConfig,
) -> Result<Self, ChannelStateError> {
fn htlc_intercepted(&self, htlc: InterceptedHTLC) -> Result<Self, ChannelStateError> {
match self {
OutboundJITChannelState::AwaitingPayment {
htlcs,
payment_size_msat,
min_payment_size_msat,
max_payment_size_msat,
min_fee_msat,
proportional_fee,
} => {
Expand All @@ -122,14 +122,14 @@ impl OutboundJITChannelState {
(total_expected_outbound_amount_msat, false)
};

if expected_payment_size_msat < config.min_payment_size_msat
|| expected_payment_size_msat > config.max_payment_size_msat
if expected_payment_size_msat < *min_payment_size_msat
|| expected_payment_size_msat > *max_payment_size_msat
{
return Err(ChannelStateError(
format!("Payment size violates our limits: expected_payment_size_msat = {}, min_payment_size_msat = {}, max_payment_size_msat = {}",
expected_payment_size_msat,
config.min_payment_size_msat,
config.max_payment_size_msat
min_payment_size_msat,
max_payment_size_msat
)));
}

Expand Down Expand Up @@ -164,6 +164,8 @@ impl OutboundJITChannelState {
proportional_fee: *proportional_fee,
htlcs,
payment_size_msat: *payment_size_msat,
min_payment_size_msat: *min_payment_size_msat,
max_payment_size_msat: *max_payment_size_msat,
})
} else {
Err(ChannelStateError(
Expand Down Expand Up @@ -211,9 +213,9 @@ impl OutboundJITChannel {
}

fn htlc_intercepted(
&mut self, htlc: InterceptedHTLC, config: &LSPS2ServiceConfig,
&mut self, htlc: InterceptedHTLC,
) -> Result<Option<(u64, u64)>, LightningError> {
self.state = self.state.htlc_intercepted(htlc, config)?;
self.state = self.state.htlc_intercepted(htlc)?;

match &self.state {
OutboundJITChannelState::AwaitingPayment { .. } => {
Expand Down Expand Up @@ -370,8 +372,6 @@ where
param.into_opening_fee_params(&self.config.promise_secret)
})
.collect(),
min_payment_size_msat: self.config.min_payment_size_msat,
max_payment_size_msat: self.config.max_payment_size_msat,
});
self.enqueue_response(counterparty_node_id, request_id, response);
Ok(())
Expand Down Expand Up @@ -472,7 +472,7 @@ where
peer_state.outbound_channels_by_intercept_scid.get_mut(&intercept_scid)
{
let htlc = InterceptedHTLC { intercept_id, expected_outbound_amount_msat };
match jit_channel.htlc_intercepted(htlc, &self.config) {
match jit_channel.htlc_intercepted(htlc) {
Ok(Some((opening_fee_msat, amt_to_forward_msat))) => {
self.enqueue_event(Event::LSPS2Service(
LSPS2ServiceEvent::OpenChannel {
Expand Down Expand Up @@ -616,7 +616,7 @@ where
&self, request_id: RequestId, counterparty_node_id: &PublicKey, params: BuyRequest,
) -> Result<(), LightningError> {
if let Some(payment_size_msat) = params.payment_size_msat {
if payment_size_msat < self.config.min_payment_size_msat {
if payment_size_msat < params.opening_fee_params.min_payment_size_msat {
self.enqueue_response(
counterparty_node_id,
request_id,
Expand All @@ -633,7 +633,7 @@ where
});
}

if payment_size_msat > self.config.max_payment_size_msat {
if payment_size_msat > params.opening_fee_params.max_payment_size_msat {
self.enqueue_response(
counterparty_node_id,
request_id,
Expand Down
2 changes: 2 additions & 0 deletions src/lsps2/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pub fn is_valid_opening_fee_params(
hmac.input(fee_params.valid_until.to_rfc3339().as_bytes());
hmac.input(&fee_params.min_lifetime.to_be_bytes());
hmac.input(&fee_params.max_client_to_self_delay.to_be_bytes());
hmac.input(&fee_params.min_payment_size_msat.to_be_bytes());
hmac.input(&fee_params.max_payment_size_msat.to_be_bytes());
let promise_bytes = Hmac::from_engine(hmac).to_byte_array();
let promise = utils::hex_str(&promise_bytes[..]);
promise == fee_params.promise
Expand Down