Skip to content
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

Peer Storage Feature – Part 2 #3623

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

adi2011
Copy link

@adi2011 adi2011 commented Feb 26, 2025

This is the second PR in the peer storage feature series.

Key Changes

  • Enable ChainMonitor to send peer storage to all channel partners whenever a new block is mined.
  • Add get_peer_storage_key() to NodeSigner.
  • Implement decryption logic for peer storage in handle_peer_storage_retrieval().

In the next one, I will add serialisation logic for ChannelMonitors inside peer storage.

@adi2011 adi2011 force-pushed the peer-storage/encrypt-decrypt branch 3 times, most recently from d8df7cf to 361738d Compare February 27, 2025 03:31
@adi2011 adi2011 marked this pull request as ready for review February 27, 2025 07:46
@tnull
Copy link
Contributor

tnull commented Feb 27, 2025

Seems this isn't properly rebased, hence CI is failing?

@adi2011 adi2011 force-pushed the peer-storage/encrypt-decrypt branch from 361738d to a1006e0 Compare February 27, 2025 11:25
@adi2011
Copy link
Author

adi2011 commented Feb 27, 2025

Seems this isn't properly rebased, hence CI is failing?

I think #3626 will fix this.

@tnull
Copy link
Contributor

tnull commented Feb 27, 2025

Seems this isn't properly rebased, hence CI is failing?

I think #3626 will fix this.

No, it fails with:

src/ln/our_peer_storage.rs - ln::our_peer_storage::OurPeerStorage (line 48) stdout ----
error[E0433]: failed to resolve: use of undeclared type `OurPeerStorage`
 --> src/ln/our_peer_storage.rs:49:28
  |
3 | let mut our_peer_storage = OurPeerStorage::new();
  |                            ^^^^^^^^^^^^^^ not found in this scope
  |
help: consider importing this struct
  |
2 | use lightning::ln::our_peer_storage::OurPeerStorage;
  |

error[E0433]: failed to resolve: use of undeclared type `OurPeerStorage`
 --> src/ln/our_peer_storage.rs:54:1
  |
8 | OurPeerStorage::decrypt_our_peer_storage(&mut decrypted, &encrypted).unwrap();
  | ^^^^^^^^^^^^^^ not found in this scope
  |
help: consider importing this struct
  |
2 | use lightning::ln::our_peer_storage::OurPeerStorage;
  |

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0433`.
Couldn't compile the test.

failures:
    src/ln/our_peer_storage.rs - ln::our_peer_storage::OurPeerStorage (line 48)

test result: FAILED. 28 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 3.00s

error: test failed, to rerun pass '--doc'

@adi2011
Copy link
Author

adi2011 commented Feb 27, 2025

That is because it’s trying to compile the commented code in the documentation, I will fix it in a bit.

@adi2011 adi2011 force-pushed the peer-storage/encrypt-decrypt branch 2 times, most recently from ff7fec7 to 465da4a Compare February 27, 2025 12:09
@jkczyz jkczyz added the weekly goal Someone wants to land this this week label Feb 27, 2025
@adi2011 adi2011 force-pushed the peer-storage/encrypt-decrypt branch from 465da4a to 4f29813 Compare February 28, 2025 04:49
Aditya Sharma and others added 6 commits February 28, 2025 10:20
Add get_peer_storage_key method to derive a 32-byte encryption key for securing Peer Storage.
This method utilizes HKDF with the node's secret key as input and a fixed info string to generate the encryption key.

 - Add 'get_peer_storage_key' to NodeSigner.
 - Implement 'get_peer_storage_key' for KeysManager & PhantomKeysManager.
Introduce the OurPeerStorage struct to manage serialized channel data for peer storage backups.
This struct facilitates the distribution of peer storage to channel partners and includes
versioning and timestamping for comparison between retrieved peer storage instances.

 - Add the OurPeerStorage struct with fields for version, timestamp, and serialized channel data (ser_channels).
 - Implement methods to encrypt and decrypt peer storage securely.
 - Add functionality to update channel data within OurPeerStorage.
To enable ChainMonitor sending peer storage to channel partners whenever a new block is added,
a new message handler, `SendingOnlyMessageHandler`, has been introduced.
This allows the `ChainMonitor` to handle the peer storage distribution.

Key changes:
 - Introduce the SendingOnlyMessageHandler trait to facilitate peer storage distribution.
 - Add SendingOnlyMessageHandler into the MessageHandler.
 - Implement SendingOnlyMessageHandler for ChainMonitor and IgnoringMessageHandler.
 - Process SendingOnlyMessageHandler events inside process_events().
Everytime a new block is added we send PeerStorage to all of our channel partner.

 - Add our_peer_storage and our_peerstorage_encryption_key to ChainMonitor
 - Write send_peer_storage() and send it to all channel partners whenever a new block is added
Ensure ChannelManager properly handles peer_storage_retrieval.

 - Write internal_peer_storage_retreival to verify if we recv correct peer storage.
 - Send error if we get invalid peer_storage data.
Ensure that we correctly handle the sendpeerstorage message event from chainmonitor
and process it through channelmonitor.

Key Changes:
- Retrieve sendpeerstorage message event from chainmonitor for both nodes.
- Handle peer storage messages exchanged between nodes and verify correct decryption.
@adi2011 adi2011 force-pushed the peer-storage/encrypt-decrypt branch from 4f29813 to 2113034 Compare February 28, 2025 04:51
Copy link
Contributor

@tnull tnull left a comment

Choose a reason for hiding this comment

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

Unfortunately CI is still failing as the code doesn't work in no_std.

use crate::ln::msgs::DecodeError;

/// [`OurPeerStorage`] is used to store channel information that allows for the creation of a
/// PeerStorage backup. It includes versioning and timestamping for comparison between
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: IIRC, we settled on snake_case here?

Suggested change
/// PeerStorage backup. It includes versioning and timestamping for comparison between
/// `peer_storage` backup. It includes versioning and timestamping for comparison between

Copy link
Author

Choose a reason for hiding this comment

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

Sorry, my bad. Fixed!

impl OurPeerStorage {
/// Returns a [`OurPeerStorage`] with version 1 and current timestamp.
pub fn new() -> Self {
let duration_since_epoch = std::time::SystemTime::now()
Copy link
Contributor

Choose a reason for hiding this comment

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

As LDK also supports no_std, we need to make this code work in no_std environments. As accessing time is only available in std, it means we either need to find a way to have the user provide the timestamp themselves, or feature-gate the peer storage to only work on std.

That said, are we positive that we need a timestamp in the first place?

Copy link
Author

Choose a reason for hiding this comment

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

No, I have added a timestamp so that if we receive multiple peer storage instances, we can select the latest one. I was thinking of replacing the timestamp with block height...

@adi2011 adi2011 requested a review from tnull March 1, 2025 07:08
@adi2011
Copy link
Author

adi2011 commented Mar 1, 2025

Thanks for the review @tnull.

Fixed the CI :)

Copy link

codecov bot commented Mar 1, 2025

Codecov Report

Attention: Patch coverage is 79.91071% with 45 lines in your changes missing coverage. Please review.

Project coverage is 89.98%. Comparing base (eaeed77) to head (4759f21).
Report is 23 commits behind head on main.

Files with missing lines Patch % Lines
lightning/src/ln/channelmanager.rs 75.38% 15 Missing and 1 partial ⚠️
lightning/src/ln/our_peer_storage.rs 80.95% 8 Missing and 4 partials ⚠️
lightning/src/util/test_utils.rs 70.00% 9 Missing ⚠️
lightning/src/ln/peer_handler.rs 63.15% 6 Missing and 1 partial ⚠️
lightning/src/ln/blinded_payment_tests.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3623      +/-   ##
==========================================
+ Coverage   89.16%   89.98%   +0.82%     
==========================================
  Files         152      153       +1     
  Lines      118791   124698    +5907     
  Branches   118791   124698    +5907     
==========================================
+ Hits       105921   112213    +6292     
+ Misses      10312     9992     -320     
+ Partials     2558     2493      -65     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
weekly goal Someone wants to land this this week
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants