Skip to content

Simplify Message Serialization and Parse TLV Suffix #1068

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

Merged
merged 4 commits into from
Sep 18, 2021

Conversation

TheBlueMatt
Copy link
Collaborator

See individual commits

@codecov
Copy link

codecov bot commented Sep 4, 2021

Codecov Report

Merging #1068 (41cf0c2) into main (4c4d99b) will increase coverage by 1.52%.
The diff coverage is 53.76%.

❗ Current head 41cf0c2 differs from pull request most recent head 997dc5f. Consider uploading reports for the commit 997dc5f to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1068      +/-   ##
==========================================
+ Coverage   90.87%   92.39%   +1.52%     
==========================================
  Files          65       65              
  Lines       33229    43459   +10230     
==========================================
+ Hits        30196    40153    +9957     
- Misses       3033     3306     +273     
Impacted Files Coverage Δ
lightning/src/chain/transaction.rs 100.00% <ø> (+4.16%) ⬆️
lightning/src/util/ser.rs 93.95% <ø> (+2.26%) ⬆️
lightning/src/util/test_utils.rs 83.90% <ø> (+1.56%) ⬆️
lightning/src/ln/channelmanager.rs 88.15% <29.41%> (+2.30%) ⬆️
lightning/src/ln/msgs.rs 88.05% <75.86%> (-1.06%) ⬇️
lightning/src/ln/features.rs 99.43% <100.00%> (-0.01%) ⬇️
lightning/src/ln/peer_handler.rs 49.27% <100.00%> (+3.48%) ⬆️
lightning/src/util/ser_macros.rs 86.30% <100.00%> (-1.60%) ⬇️
lightning/src/util/enforcing_trait_impls.rs 88.97% <0.00%> (-0.31%) ⬇️
... and 24 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4c4d99b...997dc5f. Read the comment docs.

@TheBlueMatt TheBlueMatt force-pushed the 2021-09-ser-cleanup branch 5 times, most recently from 678c3b7 to 030c25e Compare September 4, 2021 19:24
Copy link
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.

Nice cleanup! Minor comments, otherwise ACK

Copy link
Contributor

@jkczyz jkczyz left a comment

Choose a reason for hiding this comment

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

FWIW, commit 27c6ee2 refers to "previous commit" but the commit in question is a few commits back.

@TheBlueMatt
Copy link
Collaborator Author

Reordered the commits a bit and updated writer docs. Note that at this point we should probably remove writer, but I'll leave that as a followup huge-diff change.

In order to avoid significant malloc traffic, messages previously
explicitly stated their serialized length allowing for Vec
preallocation during the message serialization pipeline. This added
some amount of complexity in the serialization code, but did avoid
some realloc() calls.

Instead, here, we drop all the complexity in favor of a fixed 2KiB
buffer for all message serialization. This should not only be
simpler with a similar reduction in realloc() traffic, but also
may reduce heap fragmentation by allocating identically-sized
buffers more often.
Going forward, all lightning messages have a TLV stream suffix,
allowing new fields to be added as needed. In the P2P protocol,
messages have an explicit length, so there is no implied length in
the TLV stream itself. HTLCFailureMsg enum variants have messages
in them, but without a size prefix or any explicit end. Thus, if a
HTLCFailureMsg is read as a part of a ChannelManager, with a TLV
stream at the end, there is no way to differentiate between the end
of the message and the next field(s) in the ChannelManager.

Here we add two new variant values for HTLCFailureMsg variants in
the read path, allowing us to switch to the new values if/when we
add new TLV fields in UpdateFailHTLC or UpdateFailMalformedHTLC so
that older versions can still read the new TLV fields.
The network serialization format for all messages was changed some
time ago to include a TLV suffix for all messages, however we never
bothered to implement it as there isn't a lot of use validating a
TLV stream with nothing to do with it. However, messages are
increasingly utilizing the TLV suffix feature, and there are some
compatibility concerns with messages written as a part of other
structs having their format changed (see previous commit).

Thus, here we go ahead and convert most message serialization to a
new macro which includes a TLV suffix after a series of fields,
simplifying several serialization implementations in the process.
@TheBlueMatt
Copy link
Collaborator Author

Squashed with no further changes, will land after CI.

$ git diff-tree -U1 41cf0c2e 997dc5f5
$

@TheBlueMatt TheBlueMatt merged commit 801d6e5 into lightningdevkit:main Sep 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants