Skip to content

trampoline: accumulate inbound trampoline htlcs#4493

Open
carlaKC wants to merge 14 commits intolightningdevkit:mainfrom
carlaKC:2299-mpp-accumulation
Open

trampoline: accumulate inbound trampoline htlcs#4493
carlaKC wants to merge 14 commits intolightningdevkit:mainfrom
carlaKC:2299-mpp-accumulation

Conversation

@carlaKC
Copy link
Copy Markdown
Contributor

@carlaKC carlaKC commented Mar 18, 2026

This PR handles accumulation of inbound MPP trampoline parts, including handling of timeout and MPP validation. When all parts are successfully accumulated, we'll fail the MPP set backwards as we do not yet have support for outbound dispatch.

It does not include:

  • Handling trampoline replays / reload from disk (we currently refuse to read HTLCSource::TrampolineForward to prevent downgrade with trampoline in flight).
  • Interception of trampoline forwards, which I think we should add a separate flag for because it's difficult to map to our existing structure when we don't know the outbound channel at time of interception.

@ldk-reviews-bot
Copy link
Copy Markdown

ldk-reviews-bot commented Mar 18, 2026

👋 I see @valentinewallace was un-assigned.
If you'd like another reviewer assignment, please click here.

Comment thread lightning/src/events/mod.rs Outdated
@carlaKC carlaKC force-pushed the 2299-mpp-accumulation branch 2 times, most recently from 2f01cdc to 9d17783 Compare March 18, 2026 17:53
@valentinewallace
Copy link
Copy Markdown
Contributor

I find it easier to be confident in smaller PRs, so happy to see this broken up as mentioned on the dev call!

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 24, 2026

Codecov Report

❌ Patch coverage is 90.00000% with 29 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.29%. Comparing base (0c7e6e7) to head (b838480).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
lightning/src/ln/channelmanager.rs 93.20% 15 Missing and 2 partials ⚠️
lightning/src/ln/onion_payment.rs 73.07% 7 Missing ⚠️
lightning/src/ln/onion_utils.rs 64.28% 3 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4493      +/-   ##
==========================================
+ Coverage   87.16%   87.29%   +0.12%     
==========================================
  Files         161      161              
  Lines      109251   109496     +245     
  Branches   109251   109496     +245     
==========================================
+ Hits        95230    95580     +350     
+ Misses      11547    11435     -112     
- Partials     2474     2481       +7     
Flag Coverage Δ
fuzzing-fake-hashes 31.11% <0.77%> (-0.04%) ⬇️
fuzzing-real-hashes 22.86% <5.81%> (-0.07%) ⬇️
tests 86.35% <90.00%> (+0.12%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@carlaKC carlaKC removed this from Weekly Goals Mar 26, 2026
@valentinewallace
Copy link
Copy Markdown
Contributor

Can you let me know your thoughts on the top two claude'd commits here? https://github.com/valentinewallace/rust-lightning/tree/2026-03-mpp-accumulation-wip

I think I prefer not entirely repurposing the existing claimable structs for trampoline. The top commit is pretty large though, admittedly, though it's super mechanical. The nice part is that there isn't a need to add the PaymentPurpose::Trampoline, or have fields that don't apply present in either claimable HTLCs or trampoline HTLCs, such as the skimmed fee.

@carlaKC
Copy link
Copy Markdown
Contributor Author

carlaKC commented Mar 26, 2026

Can you let me know your thoughts on the top two claude'd commits here? https://github.com/valentinewallace/rust-lightning/tree/2026-03-mpp-accumulation-wip
I think I prefer not entirely repurposing the existing claimable structs for trampoline. The top commit is pretty large though, admittedly, though it's super mechanical.

Nice cleanup! Didn't think that repurposing was too bad because it's relatively contained, but def nice to not need an unused PaymentPurpose/few fields. Will incorporate in the prefactor 👍

@carlaKC carlaKC force-pushed the 2299-mpp-accumulation branch 3 times, most recently from 2a44215 to 86256af Compare April 7, 2026 17:21
@carlaKC carlaKC self-assigned this Apr 9, 2026
@carlaKC carlaKC force-pushed the 2299-mpp-accumulation branch 5 times, most recently from 3b411ba to 54d0f2b Compare April 14, 2026 16:15
@carlaKC carlaKC marked this pull request as ready for review April 14, 2026 17:57
Comment thread lightning/src/ln/channelmanager.rs Outdated
Comment on lines +8510 to +8512
// TODO: add restriction to specification that trampoline should be consistent across
// MPP parts? Currently, we'll accept a MPP trampoline payments that specify different
// next_node_id destinations (just forwarding to the last one that arrives).
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This TODO has security implications worth calling out: if MPP parts carry different next_hop_info (onion packet, amount, cltv, next_node_id), only the last-arriving part's values are used for fee/cltv validation and forwarding. A malicious sender could exploit this by sending one part with legitimate values (to pass initial checks) and a final part with different values.

Since forwarding isn't implemented yet, this isn't exploitable today, but when it is, this needs to be addressed — either by requiring consistency across parts or by using the first part's values.

Comment on lines +192 to +193
let (next_hop_amount, next_hop_cltv) = check_blinded_forward(
outer_hop_data.multipath_trampoline_data.as_ref().map(|f| f.total_msat).unwrap_or(msg.amount_msat), msg.cltv_expiry, &next_trampoline_hop_data.payment_relay, &next_trampoline_hop_data.payment_constraints, &next_trampoline_hop_data.features
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This changes the input to check_blinded_forward from msg.amount_msat (single HTLC amount) to the MPP total_msat. This is significant: the fee computation in amt_to_forward_msat and the check_blinded_payment_constraints (including htlc_minimum_msat check) now operate on the total MPP amount rather than the per-HTLC amount.

For the fee computation, this is correct — the blinded relay parameters are designed to be applied to the total amount, not per-part. But check_blinded_payment_constraints at line 69 of this file calls check_blinded_payment_constraints(inbound_amt_msat, ...) which checks against htlc_minimum_msat. Using the total here means a per-HTLC amount below htlc_minimum_msat would still pass if the total is above it. Is that the intended behavior for trampoline MPP?

If multipath_trampoline_data is None, this falls back to msg.amount_msat which is the per-HTLC amount (non-MPP case) — that's correct.

Comment thread lightning/src/ln/channelmanager.rs
@ldk-claude-review-bot
Copy link
Copy Markdown
Collaborator

ldk-claude-review-bot commented Apr 14, 2026

I've completed a thorough review of every changed file and hunk in this PR. My prior review comments already cover the significant issues. I found no new bugs or security problems beyond those already flagged.

Review Summary

No new issues found beyond those already flagged in the prior review pass. The trampoline MPP accumulation logic is well-structured, correctly reuses check_incoming_mpp_part, and properly handles timeout (both timer-tick and on-chain) paths.

Previously flagged issues (still applicable):

  1. lightning/src/ln/channelmanager.rs:8576as u64 truncation of proportional fee could silently understate the fee with pathological inputs.
  2. lightning/src/ln/channelmanager.rs:8528 — Inconsistent next_hop_info across MPP parts (documented TODO, not exploitable until forwarding is implemented).
  3. lightning/src/ln/onion_payment.rs:193check_blinded_forward now uses MPP total_msat instead of per-HTLC amount, changing htlc_minimum_msat semantics from per-part to per-aggregate.
  4. lightning/src/ln/channelmanager.rs:8585 — Already addressed with checked_add in current code.

Verified areas (no issues):

  • Error double-encryption (onion_utils.rs:2131-2136): Both Reason and LightningError variants correctly apply trampoline layer first, then outer layer.
  • HTLCPreviousHopData population: htlc_previous_hop_data() correctly extracts trampoline_shared_secret from TrampolineForward routing and includes it per-hop for MPP failure handling.
  • Lock ordering: awaiting_trampoline_forwards is released before fail_htlc_backwards_internal runs in all timeout/failure paths.
  • Serialization: New required TLV fields (12, 14) on PendingHTLCRouting::TrampolineForward are safe since trampoline forwards never reach forward_htlcs in production builds. HTLCSource::TrampolineForward is write-only (reading blocked at variant 2).
  • Timeout logic: Both timer_tick_occurred and best_block_connected correctly drain all MPP parts when any part times out, using the appropriate failure reason (MPPTimeout vs CLTVExpiryTooSoon).
  • Outer-hop amount change: Using outer_hop_data.amt_to_forward instead of inner trampoline amounts for NextPacketDetails is correct — it detects fee/CLTV skimming by the forwarding node, while inner amounts are checked later in handle_trampoline_htlc.

@ldk-reviews-bot
Copy link
Copy Markdown

🔔 1st Reminder

Hey @valentinewallace! This PR has been waiting for your review.
Please take a look when you have a chance. If you're unable to review, please let us know so we can find another reviewer.

@carlaKC carlaKC removed the request for review from valentinewallace April 17, 2026 13:26
@carlaKC carlaKC force-pushed the 2299-mpp-accumulation branch from 54d0f2b to 7e428bd Compare May 4, 2026 12:40
carlaKC added 14 commits May 4, 2026 08:41
We don't need to track a single trampoline secret in our HTLCSource
because this is already tracked in each of our previous hops contained
in the source. This field was unnecessarily added under the belief that
each inner trampoline onion we receive for inbound MPP trampoline would
have the same session key.

It can be removed with breaking changes to persistence because we
currently refuse to decode trampoline forwards, and will not read
HTLCSource::Trampoline to prevent downgrades.
When we receive a trampoline forward, we need to wait for MPP parts to
arrive at our node before we can forward the outgoing payment onwards.
This commit threads this information through to our pending htlc struct
which we'll use to validate the parts we receive.
For regular blinded forwards, it's okay to use the amount in our
update_add_htlc to calculate the amount that we need to foward onwards
because we're only expecting on HTLC in and one HTLC out.

For blinded trampoline forwards, it's possible that we have multiple
incoming HTLCs that need to accumulate at our node that make our total
incoming amount from which we'll calculate the amount that we need to
forward onwards to the next trampoline. This commit updates our next
trampoline amount calculation to use the total intended incoming amount
for the payment so we can correctly calculate our next trampoline's
amount.

`decode_incoming_update_add_htlc_onion` is left unchanged because
the call to `check_blinded` will be removed in upcoming commits.
When we are a trampoline node receiving an incoming HTLC, we need access
to our outer onion's amount_to_forward to check that we have been
forwarded the correct amount. We can't use the amount in the inner
onion, because that contains our fee budget - somebody could forward us
less than we were intended to receive, and provided it is within the
trampoline fee budget we wouldn't know.

In this commit we set our outer onion values in PendingHTLCInfo to
perform this validation properly. In the commit that follows, we'll
start tracking our expected trampoline values in trampoline-specific
routing info.
When we're forwarding a trampoline payment, we need to remember the
amount and CLTV that the next trampoline is expecting.
When we receive trampoline payments, we first want to validate the
values in our outer onion to ensure that we've been given the amount/
expiry that the sender was intending us to receive to make sure that
forwarding nodes haven't sent us less than they should.
When we are a trampoline router, we need to accumulate incoming HTLCs
(if MPP is used) before forwarding the trampoline-routed outgoing
HTLC(s). This commit adds a new map in channel manager, and mimics the
handling done for claimable_payments.

We will rely on our pending_outbound_payments (which will contain a
payment for trampoline forwards) for completing MPP claims,
not want to surface `PaymentClaimable` events for trampoline,
so do not need to have pending_claiming_payments like we have for MPP
receives.

This map is not persisted, as we're currently working on refactoring
restart logic to depend on channel monitors. We should not use this
accumulation map in production yet, as we can hit a force close if:
- We are used as a trampoline, despite not supporting the feature
- A trampoline MPP part arrives and is committed to the inbound channel
  and added to `awaiting_trampoline_forwards`
- We restart and the MPP part is not re-added to
  `awaiting_trampoline_forwards`

In this scenario, we will not hit our MPP timeout logic for this HTLC
because we have "forgotten" about it. It will be up to our counterparty
to force close the channel on us, because we're not failing it back
after we hit MPP timeout. Likewise, even if other MPP parts arrive,
we won't consider the inbound accumulation to be complete so we'll fail
them back but forget about the HTLC that came before the restart.

We currently reject trampoline HTLCs earlier in the lifecycle, so we
are not at risk of producing a state that could trigger such a force
close. In the commits that follow, we'll allow forwarding of
trampoline HTLC for tests so that we can start to cover this code.
Add our MPP accumulation logic for trampoline payments, but reject
them when they fully arrive. This allows us to test parts of our
trampoline flow without fully implementing outbound dispatch.

This commit keeps the same first_claimable_htlc debug_assert behavior
as MPP claims, asserting that we do not fail our
check_claimable_incoming_htlc merge for the first HTLC that we add to a
set. This assert can only be hit if our first part exceeds the
`MAX_VALUE_MSAT`, which should not be hit because we check individual
amounts elsewhere in the codebase (the check exists to check that
multiple parts combined don't hit this overflow).
If we're a trampoline node and received an error from downstream that
we can't fully decrypt, we want to double-wrap it for the original
sender. Previously not implemented because we'd only focused on
receives, where there's no possibility of a downstream error.

While proper error handling will be added in a followup, we add the
bare minimum required here for testing.
While proper error handling will be added in a followup, we add the
bare minimum required here for testing.

Note that we intentionally keep the behavior of not setting
`payment_failed_permanently` for local failures because we can possibly
retry it because we're the sender as a trampoline forwarder.

For example, a local ChannelClosed error is considered to be permanent,
but we can still retry along another channel.
We can't perform proper validation because we don't know the outgoing
channel id until we forward the HTLC, so we just perform a basic CLTV
check.

We don't yet have proper handling of trampoline forwards on restart, so
we only enable this in our tests.
@carlaKC carlaKC force-pushed the 2299-mpp-accumulation branch from 7e428bd to b838480 Compare May 4, 2026 12:42
Copy link
Copy Markdown
Contributor

@valentinewallace valentinewallace left a comment

Choose a reason for hiding this comment

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

Still making my way through but generally looks quite good! 🥇

Comment thread lightning/src/ln/onion_payment.rs Outdated
}
}
RoutingInfo::Trampoline { next_trampoline, new_packet_bytes, next_hop_hmac, shared_secret, current_path_key } => {
RoutingInfo::Trampoline { next_trampoline, new_packet_bytes, next_hop_hmac, shared_secret, current_path_key, incoming_multipath_data: multipath_trampoline_data } => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: no need to rebind the field here

Comment on lines +8453 to +8455
// Handles the addition of a HTLC associated with a trampoline forward that we need to accumulate
// on the incoming link before forwarding onwards. If the HTLC is failed, it returns the source
// and error that should be used to fail the HTLC(s) back.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: doc comment

Comment on lines +8472 to +8475
// TODO: add restriction to specification that trampoline should be consistent across
// MPP parts? Currently, we'll accept a MPP trampoline payments that specify different
// next_node_id destinations (just forwarding to the last one that arrives).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Adding a restriction in the spec makes sense to me!

Comment on lines +8574 to +8577
let proportional_fee = (forwarding_fee_proportional_millionths as u128
* next_hop_info.amount_msat as u128
/ 1_000_000) as u64;
let our_forwarding_fee_msat = proportional_fee + forwarding_fee_base_msat as u64;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we use router's compute_fees or compute_fees_saturating?

Comment on lines +8574 to +8576
let proportional_fee = (forwarding_fee_proportional_millionths as u128
* next_hop_info.amount_msat as u128
/ 1_000_000) as u64;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wasn't sure about this at first because for normal forwards we calculate the proportional fee based on the amount we're relaying to the direct next hop, whereas in this case we calculate it on the next trampoline hop's amount (i.e. there may be hops in between). It looks like this is how eclair does it too, though, and maybe it isn't possible to do any other way? Could possibly use a comment.

for (i, path) in route.paths.iter().enumerate() {
nodes[0]
.node
.test_send_payment_along_path(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Generally preferred to use the public API in tests. I clauded this patch but haven't vetted it much, let me know your thoughts: valentinewallace@79a0771

pub(super) fn decode_onion_failure<T: secp256k1::Signing, L: Logger>(
&self, secp_ctx: &Secp256k1<T>, logger: &L, htlc_source: &HTLCSource,
) -> DecodedOnionFailure {
macro_rules! decoded_onion_failure {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks like this can be a closure, which avoids the somewhat awkward parenthesis around the scid option below: valentinewallace@00b24b3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

4 participants