Skip to content

TLV-ize enum serialization and a few additional structs #935

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 14 commits into from
Jun 1, 2021

Conversation

TheBlueMatt
Copy link
Collaborator

Nothing super exciting here, just more TLVs inside enum variants with a new macro.

@TheBlueMatt TheBlueMatt force-pushed the 2021-05-enum-tlvs branch 3 times, most recently from 6d08b25 to f546dcc Compare May 31, 2021 17:57
@codecov
Copy link

codecov bot commented May 31, 2021

Codecov Report

Merging #935 (fd7845e) into main (77bdc32) will increase coverage by 0.04%.
The diff coverage is 66.66%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #935      +/-   ##
==========================================
+ Coverage   90.56%   90.61%   +0.04%     
==========================================
  Files          60       60              
  Lines       30698    30393     -305     
==========================================
- Hits        27803    27540     -263     
+ Misses       2895     2853      -42     
Impacted Files Coverage Δ
lightning/src/chain/transaction.rs 95.83% <ø> (-0.60%) ⬇️
lightning/src/ln/chan_utils.rs 97.43% <ø> (+0.07%) ⬆️
lightning/src/util/events.rs 17.29% <15.38%> (-3.07%) ⬇️
lightning/src/ln/channelmanager.rs 84.03% <50.00%> (+0.88%) ⬆️
lightning/src/routing/router.rs 95.94% <57.14%> (-0.12%) ⬇️
lightning/src/chain/channelmonitor.rs 90.74% <84.21%> (-0.31%) ⬇️
lightning/src/util/ser_macros.rs 96.74% <86.66%> (-0.99%) ⬇️
lightning/src/chain/keysinterface.rs 95.41% <100.00%> (+0.44%) ⬆️
lightning/src/chain/onchaintx.rs 94.14% <100.00%> (+<0.01%) ⬆️
lightning/src/chain/package.rs 92.29% <100.00%> (-0.23%) ⬇️
... and 5 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 77bdc32...fd7845e. Read the comment docs.

@TheBlueMatt
Copy link
Collaborator Author

CI fail is just the route graph test needs to be updated.

payment_secret.write(writer)?;
amt.write(writer)?;
user_payment_id.write(writer)?;
write_tlv_fields!(writer, {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not use _impl_writeable_tlv_enum for Event?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Two reasons: (a), which we could change, is we don't write out the time_forwardable field in PendingHTLCsForwardable and (b) We don't write out (well, actually, read) the FundingGenerationReady event.

Copy link
Contributor

Choose a reason for hiding this comment

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

For (b), should this be the responsibility of the caller to determine if an Event should not be serialized? Then Event wouldn't need a custom implementation.

Copy link
Contributor

Choose a reason for hiding this comment

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

Though I guess that may just trickle up another level, so maybe not worth it. :/

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Right, in principle maybe we could (serialization is ultimately exposed to the user) and move the logic up to the ChannelManager? I think we can do it as a followup, though, as the actual on-disk format should be the same (with just extra variants).

/// variants stored directly.
/// The format is, for example
/// impl_writeable_tlv_based_enum!(EnumName,
/// (0, VariantName) => { (0, variant_field)}, {(1, variant_optional_field)}, {};
Copy link
Contributor

Choose a reason for hiding this comment

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

For variant_optional_field, is this odd because it is optional? (i.e., an Option field may be even or odd but is typically odd)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As an example, yes, I assume future-added optional fields will be odd (ie "if you dont know how to read this, ignore it"), but there's no written rule - the Option serialization section means it may or may not be there, but is separate from the "should old versions ignore or consider the object invalid" which is controlled by odd-or-even.

{$(($reqtype: expr, $reqfield: ident)),* $(,)*},
{$(($type: expr, $field: ident)),* $(,)*},
{$(($vectype: expr, $vecfield: ident)),* $(,)*}
),* $(,)*;
Copy link
Contributor

Choose a reason for hiding this comment

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

Regarding the separators, is there a need for trailing separators and also possibly different separators like semicolon? It's been a while, but when I wrote the macros for features I was able to separate using | but without needing to have one as a trailing separator.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The optional trailing separators are so that you can write the list with trailing commas, ie without them you wouldn't be able to say (0, A) => {}, {}, {}, (2, B) => {}, {}, {}, } which is very natural when they're on separate lines, not that its a big deal either way. I'm ok swapping ; for | but I'm not sure that's really any more natural?

Copy link
Contributor

Choose a reason for hiding this comment

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

Not suggesting to change the character. Just pointing out an example, but I found it a little odd that the three {} are not enclosed by another set of braces, which I think would allow using a comma instead? But maybe too verbose.

As discussed offline, an alternative we may consider is doing a syntax similar to protocol buffers, which would be more explicit, e.g.

optional foo = 1;
required bar = 2;
repeated baz = 4;

But no need to change in this PR if we'd prefer that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hmm, yea, I think that would be cool, but then we'd want to do it for write_tlv_fields!() as well, which is a much larger change.

@TheBlueMatt TheBlueMatt added this to the 0.0.15 milestone Jun 1, 2021
@TheBlueMatt
Copy link
Collaborator Author

(trivially) rebased after merge of #933.

},
&Event::PendingHTLCsForwardable { time_forwardable: _ } => {
4u8.write(writer)?;
write_tlv_fields!(writer, {}, {});
Copy link
Contributor

Choose a reason for hiding this comment

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

Checking my understanding: will this just write the fact that there's length = 0 bytes to read?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes.

@TheBlueMatt TheBlueMatt merged commit 34bed96 into lightningdevkit:main Jun 1, 2021
TheBlueMatt added a commit that referenced this pull request Jun 1, 2021
TLV-ize enum serialization and a few additional structs
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