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

Fix ChannelType and Script decodings in TLVs #30

Merged
merged 8 commits into from
Mar 14, 2023
Merged

Conversation

dr-orlovsky
Copy link
Contributor

Supersedes #27

@crisdut can you pls add your tests here and see would they pass? If yes, we will merge this one instead of #27

@dr-orlovsky dr-orlovsky added the bug Something isn't working label Mar 11, 2023
@dr-orlovsky dr-orlovsky added this to the v0.9.x milestone Mar 11, 2023
@dr-orlovsky dr-orlovsky requested a review from crisdut March 11, 2023 11:30
@codecov-commenter
Copy link

Codecov Report

Merging #30 (2d7c0f6) into master (d11a1e4) will decrease coverage by 0.1%.
The diff coverage is 0.0%.

📣 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

@@           Coverage Diff            @@
##           master     #30     +/-   ##
========================================
- Coverage    35.1%   35.0%   -0.1%     
========================================
  Files          17      17             
  Lines        1878    1881      +3     
========================================
  Hits          659     659             
- Misses       1219    1222      +3     
Impacted Files Coverage Δ
lnp2p/src/bolt/bolt2.rs 9.8% <0.0%> (-0.1%) ⬇️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link
Member

@crisdut crisdut left a comment

Choose a reason for hiding this comment

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

I tried run the tests, all tests failed.

I listed the reasons bellow:

  • The TLV field shutdown_scriptpubkey causes a error when is empty. But this error is not related with this fix. I opened this issue.
  • The ChannelType ::lightning_deserialize does not handle the byte array correctly (see details bellow).

return Err(lightning_encoding::Error::DataIntegrityError(s!(
"non-minimal channel type encoding"
)));
} else if flags.as_inner() == &[] as &[u8] {
Copy link
Member

Choose a reason for hiding this comment

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

I think this doesn't seem right because if channel-type was not defined, FlagVec::lightning_deserialize (see here) returns FlagVec::default() (vec![0u8; 1]) instead of an empty vector.

Suggested change
} else if flags.as_inner() == &[] as &[u8] {
} else if flags == FlagVec::new() {

ChannelType::try_from(flags)
}
fn lightning_deserialize(data: impl AsRef<[u8]>) -> Result<Self, Error> {
let flags = FlagVec::lightning_deserialize(data)?;
Copy link
Member

@crisdut crisdut Mar 11, 2023

Choose a reason for hiding this comment

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

This method still needs to be completed.

To clarify the problem, I wrote the test bellow:

    #[test]
    fn decode_static_remotekey_channel_type() {
        // Byte array expected by ChannelType::lightning_deserialize method
        let bytes = [0u8, 2u8, 0u8, 16u8];
        // That's Working!
        let msg = ChannelType::lightning_deserialize(&bytes);
        assert_eq!(true, msg.is_ok());


        // The byte array received by `ChannelType::lightning_deserialize` when 
        // we try decode open_channel message sent by clightning
        let bytes = [16u8, 0u8];
        // Fail!
        let msg = ChannelType::lightning_deserialize(&bytes);
        assert_eq!(true, msg.is_ok())
    }

As we can see above, the ChannelType::lightning_deserialize method receives an unexpected byte array. But I don't know if the problem is TLV decode or ChannelType itself.

@dr-orlovsky
Copy link
Contributor Author

dr-orlovsky commented Mar 14, 2023

@crisdut I figured this thing out. If ends up with this story:

So I had to update encoding of all types which appear only in TLV not to store the length value and rely on TLV BigSize, while the other types which do not appear in TLV are encoded with 2-byte size prefix.

I published v0.9.2 with all these changes

@dr-orlovsky dr-orlovsky changed the title Fix ChannelType decoding for an empty size Fix ChannelType and Script decodings in TLVs Mar 14, 2023
@dr-orlovsky dr-orlovsky merged commit b35c2e9 into master Mar 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants