Skip to content

[1/2] lnwire: fix encoding customized TLV records #10072

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

yyforyongyu
Copy link
Member

Depending on #10027, this PR fixes the wrong encoding used in the extra data field. Followup PRs will be created to fix the rest of the msgs. The msgs in this PR are not used in production yet, and for other msgs, it's likely the error wasn't caught due to there's no interoperability test yet.

The commits are structured as follows,

  1. A commit that adds a unit test for the encoding/decoding - we check that the raw bytes will be decoded and encoded into the same bytes.
  2. A fix in the actual encoding/decoding methods.

@yyforyongyu yyforyongyu added this to the v0.20.0 milestone Jul 14, 2025
@yyforyongyu yyforyongyu self-assigned this Jul 14, 2025
@yyforyongyu yyforyongyu added wire protocol Encoding of messages for the communication between nodes bug fix size/micro small bug fix or feature, less than 15 mins of review, less than 250 labels Jul 14, 2025
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.

Summary of Changes

Hello @yyforyongyu, 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 addresses a critical bug in the lnwire package where customized TLV records, particularly those in the 'extra data' fields of certain messages, were being incorrectly encoded. The changes involve a significant refactoring of the TLV handling logic, introducing a standardized approach for parsing and encoding these records. This ensures that all TLV data, whether known or unknown, is processed correctly, improving the reliability and forward compatibility of Lightning Network messages. Extensive new tests have been added to validate these fixes.

Highlights

  • TLV Encoding Fix: Implemented a fix for incorrect encoding of customized TLV (Type-Length-Value) records within the 'extra data' field of various Lightning Network messages. This ensures that both known and unknown TLV fields are correctly handled during serialization and deserialization.
  • Standardized TLV Parsing: Introduced a new helper function, ParseAndExtractExtraData, which centralizes the logic for separating known TLV records from arbitrary 'extra data' within a message's TLV stream. This promotes consistent and robust parsing across the lnwire package.
  • TLV Type Correction: Corrected the TLV type for DALocalMusig2Pubnonce in the DynAck message from 0 to 14. This resolves a potential conflict with other TLV types and aligns with standard practices for assigning even-numbered types to optional fields.
  • Enhanced Test Coverage: Added comprehensive unit tests for the encode/decode functionality of several lnwire messages, including AnnounceSignatures1/2, ChannelAnnouncement1/2, ChannelUpdate2, DynAck, DynCommit, and DynPropose. These tests verify byte-for-byte fidelity after a full encode-decode roundtrip, ensuring the correctness of the TLV handling.
  • Type Refinements: Updated the TLV record types for DustLimit and ChannelReserve in dynamic commitment messages (DynPropose, DynCommit) to use tlv.BigSizeT for btcutil.Amount, providing a more appropriate and flexible encoding for these values.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

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 issue 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 is currently in preview and 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 to provide feedback.

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 a fix for encoding customized TLV records in lnwire, specifically addressing an issue where known TLV fields were incorrectly encoded into the ExtraData field of messages. The changes include adding unit tests and refactoring encoding/decoding methods to ensure correct serialization and preservation of unknown TLV fields. The code adheres to the LND Style Guide.

Fix the test for `ChannelUpdate2` and `ChannelAnnouncement2`.
This is used to create unknown random records by specifying the max
known record type number.
This commit adds unit tests for `AnnounceSignatures1`,
`AnnounceSignatures2`, and `ChannelAnnouncement1`. This further assures
all the encoding and decoding methods are behaved as expected.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug fix size/micro small bug fix or feature, less than 15 mins of review, less than 250 wire protocol Encoding of messages for the communication between nodes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant