Skip to content

Store a cached NodeId for each Peer #2022

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

Conversation

alecchendev
Copy link
Contributor

@alecchendev alecchendev commented Feb 8, 2023

Closes #2021 as a followup on #2016.

Converts Peer field their_node_id: Option<PublicKey> to Option<(PublicKey, NodeId)> to avoid redundantly serializing peer public keys when forwarding gossip messages.

Comment on lines 392 to 398
// When setting this field to a `Some(..)` value, use `Peer::set_their_node_id` to also set
// the following field `their_node_id_serialized`.
their_node_id: Option<PublicKey>,
// This field is a cached `NodeId` of `their_node_id` to avoid serializing peers' keys every
// time we forward gossip messages in `PeerManager`. It should be set anytime `their_node_id`
// gets set.
their_node_id_serialized: Option<NodeId>,
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a bit worried these could go out of sync, but doc comments which would at least appear in most author IDEs could help with that if there's no better way (which I can't think of right now). Unfortunately, single fields cannot be made externally immutable in Rust. I'm sure others might have some experience here. :)

Suggested change
// When setting this field to a `Some(..)` value, use `Peer::set_their_node_id` to also set
// the following field `their_node_id_serialized`.
their_node_id: Option<PublicKey>,
// This field is a cached `NodeId` of `their_node_id` to avoid serializing peers' keys every
// time we forward gossip messages in `PeerManager`. It should be set anytime `their_node_id`
// gets set.
their_node_id_serialized: Option<NodeId>,
/// NOTE: Do not modify this field directly. Use `Peer::set_their_node_id` instead.
their_node_id: Option<PublicKey>,
/// This field is a cached `NodeId` of `their_node_id` to avoid serializing peers' keys every
/// time we forward gossip messages in `PeerManager`.
///
/// NOTE: Do not modify this field directly. Use `Peer::set_their_node_id` instead.
their_node_id_serialized: Option<NodeId>,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh oops I meant to make them doc comments, thanks. And yea those were exactly my thoughts, at the moment I can't think of a way to cache without risking them going out of sync, hoping this would have the least friction for keeping them in sync.

Copy link
Contributor

Choose a reason for hiding this comment

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

We could make their_node_id a tuple (PublicKey, NodeId), that way you're forced to update them in tandem.

Copy link
Contributor

Choose a reason for hiding this comment

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

Aha! Simple :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't avoid the possibility of a mismatch but better than two fields.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh nice! Will update

@alecchendev alecchendev force-pushed the 2023-02-cache-peer-nodeid branch from b6b52a9 to 08e9bc3 Compare February 8, 2023 16:56
@codecov-commenter
Copy link

codecov-commenter commented Feb 8, 2023

Codecov Report

Base: 91.04% // Head: 91.06% // Increases project coverage by +0.02% 🎉

Coverage data is based on head (b6b52a9) compared to base (56146e7).
Patch coverage: 72.72% of modified lines in pull request are covered.

❗ Current head b6b52a9 differs from pull request most recent head 4c1055d. Consider uploading reports for the commit 4c1055d to get more accurate results

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2022      +/-   ##
==========================================
+ Coverage   91.04%   91.06%   +0.02%     
==========================================
  Files          99       99              
  Lines       51722    51727       +5     
  Branches    51722    51727       +5     
==========================================
+ Hits        47090    47107      +17     
+ Misses       4632     4620      -12     
Impacted Files Coverage Δ
lightning/src/ln/chan_utils.rs 93.57% <ø> (ø)
lightning/src/ln/peer_handler.rs 55.84% <72.72%> (+0.34%) ⬆️
lightning/src/util/events.rs 29.56% <0.00%> (-0.24%) ⬇️
lightning/src/ln/channelmanager.rs 87.43% <0.00%> (+0.02%) ⬆️
lightning/src/ln/functional_tests.rs 97.07% <0.00%> (+0.16%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

This is done to avoid redundantly serializing peer node
ids when forwarding gossip messages in
`PeerManager::forward_broadcast_msg`.
@alecchendev alecchendev force-pushed the 2023-02-cache-peer-nodeid branch from 08e9bc3 to 4c1055d Compare February 8, 2023 17:58
Copy link
Contributor

@dunxen dunxen left a comment

Choose a reason for hiding this comment

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

This LGTM. Nice one!

@TheBlueMatt TheBlueMatt merged commit 137b77c into lightningdevkit:main Feb 8, 2023
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.

Store a cached NodeId for each of our Peers in PeerManager.
5 participants