Skip to content
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

Use consistent cltv_expiry_delta in ForwardTlvs #2831

Merged
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
18 changes: 15 additions & 3 deletions lightning/src/blinded_path/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,24 @@ pub struct PaymentConstraints {
pub htlc_minimum_msat: u64,
}

impl From<CounterpartyForwardingInfo> for PaymentRelay {
fn from(info: CounterpartyForwardingInfo) -> Self {
impl TryFrom<CounterpartyForwardingInfo> for PaymentRelay {
type Error = ();

fn try_from(info: CounterpartyForwardingInfo) -> Result<Self, ()> {
let CounterpartyForwardingInfo {
fee_base_msat, fee_proportional_millionths, cltv_expiry_delta
} = info;
Self { cltv_expiry_delta, fee_proportional_millionths, fee_base_msat }

// Avoid exposing esoteric CLTV expiry deltas
let cltv_expiry_delta = match cltv_expiry_delta {
0..=40 => 40,
41..=80 => 80,
81..=144 => 144,
145..=216 => 216,
_ => return Err(()),
};

Ok(Self { cltv_expiry_delta, fee_proportional_millionths, fee_base_msat })
Comment on lines +100 to +117
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error type for the TryFrom trait implementation is a unit type (). This provides no information about why the conversion failed. It would be more helpful to define a custom error type that can provide context on failure.

Additionally, the logic for adjusting cltv_expiry_delta is hard-coded with specific ranges and returns an error for any value not within those ranges. This could be problematic if the valid range of cltv_expiry_delta changes in the future or if there are legitimate cases where values outside these ranges should be allowed. Consider whether this logic could be made more flexible or if additional context should be provided for these constraints.

}
}

Expand Down
15 changes: 5 additions & 10 deletions lightning/src/routing/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,14 @@ impl<G: Deref<Target = NetworkGraph<L>> + Clone, L: Deref, S: Deref, SP: Sized,
None => return None,
};
let payment_relay: PaymentRelay = match details.counterparty.forwarding_info {
Some(forwarding_info) => forwarding_info.into(),
Some(forwarding_info) => match forwarding_info.try_into() {
Ok(payment_relay) => payment_relay,
Err(()) => return None,
},
None => return None,
};

// Avoid exposing esoteric CLTV expiry deltas
let cltv_expiry_delta = match payment_relay.cltv_expiry_delta {
0..=40 => 40u32,
41..=80 => 80u32,
81..=144 => 144u32,
145..=216 => 216u32,
_ => return None,
};

let cltv_expiry_delta = payment_relay.cltv_expiry_delta as u32;
let payment_constraints = PaymentConstraints {
max_cltv_expiry: tlvs.payment_constraints.max_cltv_expiry + cltv_expiry_delta,
htlc_minimum_msat: details.inbound_htlc_minimum_msat.unwrap_or(0),
Expand Down