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

Onion messages v1 pre-refactor #1518

Merged

Conversation

valentinewallace
Copy link
Contributor

@valentinewallace valentinewallace commented Jun 4, 2022

Pre-factor for #1503. See individual commit messages.

@valentinewallace valentinewallace marked this pull request as ready for review June 4, 2022 22:29
@valentinewallace valentinewallace mentioned this pull request Jun 4, 2022
6 tasks
@codecov-commenter
Copy link

codecov-commenter commented Jun 4, 2022

Codecov Report

Merging #1518 (394bb26) into main (8e5cf75) will increase coverage by 1.26%.
The diff coverage is 95.90%.

❗ Current head 394bb26 differs from pull request most recent head ee805e9. Consider uploading reports for the commit ee805e9 to get more accurate results

@@            Coverage Diff             @@
##             main    #1518      +/-   ##
==========================================
+ Coverage   90.93%   92.19%   +1.26%     
==========================================
  Files          80       80              
  Lines       43386    52214    +8828     
  Branches    43386    52214    +8828     
==========================================
+ Hits        39451    48139    +8688     
- Misses       3935     4075     +140     
Impacted Files Coverage Δ
lightning/src/util/ser_macros.rs 89.13% <ø> (ø)
lightning/src/util/chacha20poly1305rfc.rs 96.22% <95.45%> (-1.74%) ⬇️
lightning/src/ln/channelmanager.rs 87.77% <100.00%> (+3.36%) ⬆️
lightning/src/ln/onion_utils.rs 95.45% <100.00%> (+0.07%) ⬆️
lightning/src/util/ser.rs 91.34% <100.00%> (+0.06%) ⬆️
lightning/src/util/events.rs 40.18% <0.00%> (-1.81%) ⬇️
lightning/src/chain/onchaintx.rs 93.98% <0.00%> (-0.93%) ⬇️
lightning/src/ln/features.rs 99.28% <0.00%> (-0.19%) ⬇️
lightning/src/ln/payment_tests.rs 99.31% <0.00%> (+0.06%) ⬆️
lightning-invoice/src/utils.rs 96.92% <0.00%> (+0.14%) ⬆️
... and 16 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 8e5cf75...ee805e9. Read the comment docs.

lightning/src/util/chacha20poly1305rfc.rs Outdated Show resolved Hide resolved
lightning/src/util/ser_macros.rs Outdated Show resolved Hide resolved
@valentinewallace valentinewallace force-pushed the 2022-06-OMs-prefactor branch 2 times, most recently from 29379ae to 2a58ae4 Compare June 14, 2022 20:37
lightning/src/util/chacha20poly1305rfc.rs Outdated Show resolved Hide resolved
lightning/src/util/chacha20poly1305rfc.rs Outdated Show resolved Hide resolved
lightning/src/util/chacha20poly1305rfc.rs Outdated Show resolved Hide resolved
lightning/src/util/chacha20poly1305rfc.rs Outdated Show resolved Hide resolved
lightning/src/util/chacha20poly1305rfc.rs Outdated Show resolved Hide resolved
@valentinewallace valentinewallace force-pushed the 2022-06-OMs-prefactor branch 4 times, most recently from 97de6f3 to 18c3649 Compare June 15, 2022 21:53
Copy link
Collaborator

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

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

Basically LGTM, some nits, a few wording comments, some left in only one place but apply in a few.

lightning/src/util/chacha20poly1305rfc.rs Show resolved Hide resolved
lightning/src/util/chacha20poly1305rfc.rs Outdated Show resolved Hide resolved
lightning/src/util/chacha20poly1305rfc.rs Outdated Show resolved Hide resolved
// complete.
fn write_all(&mut self, src: &[u8]) -> Result<(), io::Error> {
let mut src_idx = 0;
loop {
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: maybe ideally lets not rely on bytes_written to be 0 (and the write "succeeding") when we get to the end and check it as a loop condition?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure I understood this, plz check out latest version

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oops, instead of calling write outside the loop lets just check if src.len() - src_idx is > 0.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, instead of calling write outside the loop lets just check if src.len() - src_idx is > 0.

Done

}

/// Enables simultaneously writing and encrypting a byte stream into a Writer.
pub(crate) struct ChaChaPolyWriter<'a, W: Writer> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

does this need to be pub at all or is it just used by ChaChaPolyWriteAdapter?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops @TheBlueMatt I need to make this pub(crate) again for use in blinded route construction (the Read version can stay private): d74a26b#diff-42d0fc04e68d7ce058f818e06b0dddf39f2d0661f0e86c973a50c0ace454e8b1R182

lightning/src/util/ser.rs Outdated Show resolved Hide resolved
@@ -144,6 +147,9 @@ macro_rules! check_missing_tlv {
($last_seen_type: expr, $type: expr, $field: ident, ignorable) => {{
// no-op
}};
($last_seen_type: expr, $type: expr, $field: ident, (read_arg, $read_arg: expr)) => {{
Copy link
Collaborator

Choose a reason for hiding this comment

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

read_arg is pretty ambiguous can we say, like optional_explicit_length_with_args? I dunno, that's kinda verbose but read seems redundant.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fine with that. @jkczyz thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm... maybe we can make this less of a special case and instead as a variation of option that gives the trait as an ident macro param. Something like:

decode_tlv_stream!(&writer.0[..], {
    (1, read_adapter, option: (LengthReadableArgs, arg: rho)),
});

Syntax-wise, I think I suggested awhile ago we changed (default_value, $default: expr) to (default: $default: expr), which this would align better with. I guess the exact syntax isn't too important now.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe even drop the arg: token such that non-Args traits could be used without special casing. Don't have a strong opinion on that part.

Copy link
Contributor

@jkczyz jkczyz Jun 17, 2022

Choose a reason for hiding this comment

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

I had to move the ( outside of the option to work with the tt type in decode_tlv_stream, but this seems to work.

diff --git a/lightning/src/util/chacha20poly1305rfc.rs b/lightning/src/util/chacha20poly1305rfc.rs
index 3b2d2627..5a8727cc 100644
--- a/lightning/src/util/chacha20poly1305rfc.rs
+++ b/lightning/src/util/chacha20poly1305rfc.rs
@@ -393,7 +393,7 @@ mod tests {
                // Now deserialize the object back and make sure it matches the original.
                let mut read_adapter: Option<ChaChaPolyReadAdapter<TestWriteable>> = None;
                decode_tlv_stream!(&writer.0[..], {
-                       (1, read_adapter, (read_arg, rho)),
+                       (1, read_adapter, (option: LengthReadableArgs, rho)),
                });
                assert_eq!(writeable, read_adapter.unwrap().readable);
 
diff --git a/lightning/src/util/ser_macros.rs b/lightning/src/util/ser_macros.rs
index 35e4ecf6..816426b1 100644
--- a/lightning/src/util/ser_macros.rs
+++ b/lightning/src/util/ser_macros.rs
@@ -118,7 +118,7 @@ macro_rules! check_tlv_order {
        ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, ignorable) => {{
                // no-op
        }};
-       ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, (read_arg, $read_arg: expr)) => {{
+       ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, (option: $trait: ident $(, $read_arg: expr)?)) => {{
                // no-op
        }};
 }
@@ -147,7 +147,7 @@ macro_rules! check_missing_tlv {
        ($last_seen_type: expr, $type: expr, $field: ident, ignorable) => {{
                // no-op
        }};
-       ($last_seen_type: expr, $type: expr, $field: ident, (read_arg, $read_arg: expr)) => {{
+       ($last_seen_type: expr, $type: expr, $field: ident, (option: $trait: ident $(, $read_arg: expr)?)) => {{
                // no-op
        }};
 }
@@ -169,9 +169,8 @@ macro_rules! decode_tlv {
        ($reader: expr, $field: ident, ignorable) => {{
                $field = ser::MaybeReadable::read(&mut $reader)?;
        }};
-       // Read a ser::LengthReadableArgs
-       ($reader: expr, $field: ident, (read_arg, $read_arg: expr)) => {{
-               $field = Some(ser::LengthReadableArgs::read(&mut $reader, $read_arg)?);
+       ($reader: expr, $field: ident, (option: $trait: ident $(, $read_arg: expr)?)) => {{
+               $field = Some($trait::read(&mut $reader $(, $read_arg)*)?);
        }};
 }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks Jeff, added that! Sorry, I forgot to add a fixup commit :(

lightning/src/ln/onion_utils.rs Outdated Show resolved Hide resolved
lightning/src/util/chacha20poly1305rfc.rs Outdated Show resolved Hide resolved
Comment on lines 135 to 137
let mut write_buffer = [0; 8192];
let mut bytes_written = (&mut write_buffer[..]).write(&src[src_idx..]).expect("In-memory writes can't fail");
while bytes_written != 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Could make this a loop with a break and move the statements inside to avoid some duplication.

Copy link
Contributor Author

@valentinewallace valentinewallace Jun 17, 2022

Choose a reason for hiding this comment

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

Let me know what you think of the latest version. I implemented it per Matt's previous comment: #1518 (comment). FYI the first version did use a loop and a break.

check_object_read_write!(small_writeable);
}

fn do_chacha_stream_adapters_ser_macros() -> Result<(), DecodeError> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this be inlined in the test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem is the ser macros use ?, which can only be used in a method that returns a Result. Would you prefer a different way?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I see. Nah, this is fine as is then.

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.

Nicely, done! I like how these abstractions don't add complexity elsewhere. LGTM modulo a couple trivial comments.

lightning/src/util/chacha20poly1305rfc.rs Outdated Show resolved Hide resolved
lightning/src/util/ser_macros.rs Outdated Show resolved Hide resolved
To get the next hop's packet's pubkey. This will be used to DRY onion message
forwarding in the upcoming Onion Messages PR lightningdevkit#1503
Copy link
Collaborator

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

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

LGTM.

lightning/src/util/ser.rs Outdated Show resolved Hide resolved
lightning/src/util/ser.rs Outdated Show resolved Hide resolved
lightning/src/util/ser.rs Outdated Show resolved Hide resolved
@valentinewallace
Copy link
Contributor Author

valentinewallace commented Jun 21, 2022

Sorry had to force push again, I was missing one change I thought I added(?) [making ChaChaPolyWriter pub(crate)]

Edit: never mind, it doesn't need to be pub(crate), removed

In the upcoming onion messages PR, this will allow us to avoid encoding onion
message encrypted data into an intermediate Vec before encrypting it.  Instead
we encode and encrypt at the same time using this new ChaChaPolyWriteAdapter object.
jkczyz
jkczyz previously approved these changes Jun 21, 2022
In the upcoming onion messages PR, this will allow us to avoid decrypting onion
message encrypted data in an intermediate Vec before decoding it. Instead we
decrypt and decode it at the same time using this new ChaChaPolyReadAdapter object.

In doing so, we need to adapt the decode_tlv_stream macro such that it will
decode a LengthReadableArgs, which is a new trait as well. This trait is
necessary because ChaChaPoly needs to know the total length ahead of time to
separate out the tag at the end.
@TheBlueMatt TheBlueMatt merged commit 3676a05 into lightningdevkit:main Jun 21, 2022
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.

4 participants