Skip to content

Add AuxHtlcValidator#10434

Merged
ziggie1984 merged 4 commits intolightningnetwork:masterfrom
GeorgeTsagk:extra-aux-band-check
Mar 5, 2026
Merged

Add AuxHtlcValidator#10434
ziggie1984 merged 4 commits intolightningnetwork:masterfrom
GeorgeTsagk:extra-aux-band-check

Conversation

@GeorgeTsagk
Copy link
Collaborator

Description

Updates the lightning channel to query the TrafficShaper bandwidth once more before adding the HTLC to the channel state. During pathfinding, the reported payment bandwidth could be stale, as it may have not accounted for HTLCs that have not yet been added to the channel state (i.e the aux htlc view).

By querying the aux bandwidth once more, right before the HTLC is added to the channel state, we ensure that no race condition can lead to unexpected failures due to insufficient balance.

@GeorgeTsagk GeorgeTsagk self-assigned this Dec 8, 2025
@gemini-code-assist
Copy link

Summary of Changes

Hello @GeorgeTsagk, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a robust mechanism to enhance the reliability of HTLC additions within Lightning channels. It addresses a potential race condition where payment bandwidth calculations could become outdated by implementing a new AuxHtlcValidator. This validator performs a final, real-time check against the channel's most current state, leveraging the TrafficShaper to ensure that HTLCs are only processed when adequate balance is confirmed, thereby preventing unexpected failures due to insufficient funds.

Highlights

  • New HTLC Validation Mechanism: Introduced an AuxHtlcValidator interface and associated functionality within the lnwallet package. This allows for an additional, custom validation step for HTLCs just before they are added to the channel state.
  • Race Condition Mitigation: The primary goal of this change is to prevent race conditions where the reported payment bandwidth might be stale. By performing a final validation check against the most up-to-date channel state, including pending HTLCs, it ensures that HTLCs are only added if sufficient balance is truly available.
  • Traffic Shaper Integration: The peer/brontide.go component now integrates with this new validation mechanism. It creates an AuxHtlcValidator that queries the TrafficShaper's PaymentBandwidth method, providing the most current channel state and HTLC details for a precise bandwidth check.
  • Configurable Validation: The AuxHtlcValidator is an optional component, configured via ChannelOpts, allowing for flexible integration with external systems like the TrafficShaper without altering core channel logic.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces an AuxHtlcValidator to perform a final bandwidth check right before an HTLC is added to the channel state. This is a solid approach to prevent race conditions arising from stale bandwidth information during pathfinding. The implementation is clean and integrates well with the existing channel logic. My main feedback is the lack of unit tests for this new validation logic, which would be beneficial to ensure its correctness and cover edge cases. I've also included one minor suggestion for code simplification.

peer/brontide.go Outdated
Comment on lines +5489 to +5494
peerBytes := p.IdentityKey().SerializeCompressed()
peer, err := route.NewVertexFromBytes(peerBytes)
if err != nil {
return fmt.Errorf("failed to create vertex from peer "+
"pub key: %w", err)
}

Choose a reason for hiding this comment

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

medium

This block can be simplified by using route.NewVertex. The current implementation serializes the public key to bytes, then route.NewVertexFromBytes parses it back to a public key for validation before converting it to a route.Vertex. Since p.IdentityKey() is guaranteed to return a valid *btcec.PublicKey, we can use route.NewVertex directly. This is slightly more efficient and makes the code cleaner by removing the unnecessary error handling.

Suggested change
peerBytes := p.IdentityKey().SerializeCompressed()
peer, err := route.NewVertexFromBytes(peerBytes)
if err != nil {
return fmt.Errorf("failed to create vertex from peer "+
"pub key: %w", err)
}
peer := route.NewVertex(p.IdentityKey())

Copy link
Member

@Roasbeef Roasbeef left a comment

Choose a reason for hiding this comment

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

My gut reaction: can we get rid of some of the extra calls that validate this elsewhere (eg: switch method calls into the link to check if an HTLC is ready for transit) if we're adding this additional layer of protection?

@GeorgeTsagk
Copy link
Collaborator Author

GeorgeTsagk commented Dec 15, 2025

My gut reaction: can we get rid of some of the extra calls that validate this elsewhere (eg: switch method calls into the link to check if an HTLC is ready for transit) if we're adding this additional layer of protection?

We definitely need more than 1 call sites per operation (forward / payment) as we need to first quickly gauge if enough funds exist to go ahead with the operation, then verify things one last time before committing it to the channel.

The aux bandwidth calls though seem to be a bit intertwined. By adding the AuxHtlcValidator we end up calling the PaymentBandwidth a total of 6 times (previous was 4). We definitely need to trim some of those call sites.

Will update PR soon

Copy link
Collaborator

@ziggie1984 ziggie1984 left a comment

Choose a reason for hiding this comment

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

Interesting approach, this solves all your flakes on the tap side ?

@jtobin
Copy link
Collaborator

jtobin commented Jan 26, 2026

@Roasbeef @ziggie1984 Would be good to prio review on this after the last round of fixes. This should help fix some recurrent itest flakes we get pummelled by in tapd.

return fn.Map(v.Updates.Remote, newAuxHtlcDescriptor)
}

// FetchLatestAuxHTLCView returns the latest HTLC view of the lightning channel
Copy link
Collaborator

Choose a reason for hiding this comment

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

is this only returning the Taproot Assets HTLC or all HTLCs of a channel ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

all HTLCs of the channel, wrapped inside the AuxHtlcDescriptor

Copy link
Collaborator

Choose a reason for hiding this comment

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

could you explain why we need to fetch the none AssetHTLCs as well tho, how are they treated in the limit calculation because they do not represent any assets or are they then just skipped in the taproot assests evaluation ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

All HTLCs are included because ComputeView (in tapchannel/commitment.go) needs the full picture. It iterates through all updates and distinguishes asset vs non-asset HTLCs. Non-asset HTLCs are skipped for the asset balance calculation and placed into a separate nonAssetView (used downstream for non-asset allocations). Only HTLCs with asset custom records affect the computed local asset balance.

@ziggie1984
Copy link
Collaborator

@claude review this

@claude
Copy link

claude bot commented Feb 11, 2026

Claude Code is working…

I'll analyze this and get back to you.

View job run

Copy link
Collaborator

@ziggie1984 ziggie1984 left a comment

Choose a reason for hiding this comment

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

Could you explain to me why we have the

// opts is the set of options that channel was initialized with.
	opts *channelOpts`

where the new auxHtlcValidator lives and in

LightningChannel

we have like auxResolver or auxSigner on a different level, did we just change our design and now try to move everything into channelOpts or is there another reason.

@GeorgeTsagk
Copy link
Collaborator Author

GeorgeTsagk commented Feb 19, 2026

Could you explain to me why we have the

// opts is the set of options that channel was initialized with.
	opts *channelOpts`

where the new auxHtlcValidator lives and in

LightningChannel

we have like auxResolver or auxSigner on a different level, did we just change our design and now try to move everything into channelOpts or is there another reason.

The auxSigner and auxResolver were added as top-level fields on LightningChannel because they're referenced throughout the channel code. They're passed to external functions like NewBreachRetribution and NewUnilateralCloseSummary which receive them as parameters rather than having access to the channel's opts.

The auxHtlcValidator on the other hand is only used internally within addHTLC, so it doesn't need to be elevated to a top-level field and can stay in channelOpts where it's accessed via lc.opts.auxHtlcValidator. Just a matter of scope/accessibility.

@lightninglabs-deploy lightninglabs-deploy added the severity-critical Requires expert review - security/consensus critical label Feb 19, 2026
@lightninglabs-deploy lightninglabs-deploy added severity-critical Requires expert review - security/consensus critical and removed severity-critical Requires expert review - security/consensus critical labels Feb 19, 2026
Copy link
Collaborator

@ziggie1984 ziggie1984 left a comment

Choose a reason for hiding this comment

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

LGTM, nice work 🎉

@lightninglabs-deploy
Copy link
Collaborator

@Roasbeef: review reminder
@GeorgeTsagk, remember to re-request review from reviewers when ready

Copy link
Member

@Roasbeef Roasbeef left a comment

Choose a reason for hiding this comment

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

LGTM 🫐

peer/brontide.go Outdated
// The linkBandwidth is provided by the channel and represents
// the current available balance, which is used by the traffic
// shaper to ensure we don't dip below channel reserves.
bandwidth, err := ts.PaymentBandwidth(
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps we should add a context.Context here? Then it can exit out safely if this hangs for w/e reason.

Non-blocking.

Copy link
Collaborator Author

@GeorgeTsagk GeorgeTsagk Mar 4, 2026

Choose a reason for hiding this comment

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

Since you marked this as non-blocking: Let's leave it as is for the scope of this PR.

I'd be very happy to have a follow up switching all the aux interfaces to using the actor-ish model (with all hooks returning fn.Result immediately). In that version I think context is unnecessary.

@github-project-automation github-project-automation bot moved this from 🆕 New to 👀 In review in Taproot-Assets Project Board Mar 4, 2026
Previously we'd perform aux bandwidth checks during path finding. This
could lead to issues where multiple HTLCs where querying the same
bandwidth but were not accounting for each other before being added to
the commitment log. We now add a new validator function that will serve
as the last point of checks before adding the HTLC to the commitment.

During path finding HTLCs could query channel bandwidth asynchronously.
At this new call site all HTLCs that are about to be added to the
channel have been organised in sequence, so it's safe to query bandwdith
again at this point as we're getting the actual up-to-date values.

We remove the aux bandwidth check from the helper canSendHtlc, which was
called from CheckHTLCTransit and CheckHTLCForward (both are methods of
the htlcswitch).

For forwards we now fail at the link level, following the introduction
of the AuxHtlcValidator.

For payments, we now may fail either at the pathfinding level, or at the
link level. The htlcswitch may no longer fail for aux bandwidth checks.

Finally, when fetching the latest htlc view (for bandwidth checks during
pathfinding) we'd silently set the nextHeight of the view to the default
zero value. We now make sure to set it to the correct nextHeight value.
When instantiating the lightning channel we now pass in the created HTLC
validator. This validator simply performs a bandwidth check and errors
out if that is insufficient.
@GeorgeTsagk GeorgeTsagk force-pushed the extra-aux-band-check branch from b4e95a1 to defb09e Compare March 4, 2026 19:13
@lightninglabs-deploy lightninglabs-deploy added severity-critical Requires expert review - security/consensus critical and removed severity-critical Requires expert review - security/consensus critical labels Mar 4, 2026
@lightninglabs-deploy
Copy link
Collaborator

🔴 PR Severity: CRITICAL

Automated classification | 3 files | 232 lines changed

🔴 Critical (3 files)
  • lnwallet/aux_test_utils.go - lnwallet/* package (wallet operations, channel funding, signing, commitment transactions)
  • lnwallet/channel.go - lnwallet/* package (wallet operations, channel funding, signing, commitment transactions)
  • peer/brontide.go - peer/* package (encrypted peer connections, Noise protocol)

Analysis

This PR touches two distinct CRITICAL packages: lnwallet/ and peer/. Changes to lnwallet/channel.go affect core channel state and commitment transaction logic, while peer/brontide.go modifies encrypted peer connection handling — both are highly security-sensitive code paths. The PR adds 89 lines to channel.go and 110 lines to brontide.go, representing non-trivial additions to the most sensitive parts of the codebase.

Expert review is warranted given the combination of wallet/channel logic changes alongside peer connection changes.


To override, add a severity-override-{critical,high,medium,low} label.

We add this constructor for an AuxHtlcDescriptor that allows setting
some of the internal fields. This is useful for testing purposes for
code external to this package that may need to extensively test the
AuxHtlcView.
@GeorgeTsagk GeorgeTsagk force-pushed the extra-aux-band-check branch from defb09e to 8d30e7d Compare March 4, 2026 19:19
@ziggie1984 ziggie1984 merged commit 7c38c1e into lightningnetwork:master Mar 5, 2026
70 of 79 checks passed
@github-project-automation github-project-automation bot moved this from 👀 In review to ✅ Done in Taproot-Assets Project Board Mar 5, 2026
@github-project-automation github-project-automation bot moved this from In review to Done in v0.21 Mar 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-changelog severity-critical Requires expert review - security/consensus critical

Projects

Status: Done

6 participants