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

derive Hash for Invoice #1575

Merged
merged 1 commit into from Aug 19, 2022
Merged

Conversation

NicolaLS
Copy link
Contributor

It would be nice to have Hash derived for Invoice. The problem was that RecoverableSignature does not implement Hash but I also opened a PR there so the 'by hand' implementation for InvoiceSignature could be removed.

Comment on lines 1422 to 1424
state.write(&sig);
state.write_i32(recovery_id);
Copy link
Contributor Author

@NicolaLS NicolaLS Jun 27, 2022

Choose a reason for hiding this comment

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

I'm not sure if it is ok to do it like this. In secp256k1 they already have a Hash impl for the contained type in ffi pub struct RecoverableSignature(ffi::RecoverableSignature); but they don't derive Hash on that. If ffi was public the correct way would be (&self.0.0[..]).hash(state) but it is not possible

Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we just wait for rust-bitcoin/rust-secp256k1#462 then? Otherwise let's at least leave a TODO to note we should drop the manual form here, but this is correct.

@codecov-commenter
Copy link

codecov-commenter commented Jun 27, 2022

Codecov Report

Merging #1575 (8a44b49) into main (79e2af9) will decrease coverage by 0.03%.
The diff coverage is 85.71%.

❗ Current head 8a44b49 differs from pull request most recent head 46b46e7. Consider uploading reports for the commit 46b46e7 to get more accurate results

@@            Coverage Diff             @@
##             main    #1575      +/-   ##
==========================================
- Coverage   91.04%   91.00%   -0.04%     
==========================================
  Files          80       80              
  Lines       44072    44072              
  Branches    44072    44072              
==========================================
- Hits        40126    40109      -17     
- Misses       3946     3963      +17     
Impacted Files Coverage Δ
lightning-invoice/src/lib.rs 87.39% <85.71%> (ø)
lightning/src/util/events.rs 41.66% <0.00%> (-0.33%) ⬇️
lightning/src/ln/functional_tests.rs 96.98% <0.00%> (-0.24%) ⬇️

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 79e2af9...46b46e7. Read the comment docs.

TheBlueMatt
TheBlueMatt previously approved these changes Jun 27, 2022
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.

Makes sense

@NicolaLS NicolaLS marked this pull request as ready for review June 27, 2022 14:56
@@ -1415,6 +1415,15 @@ impl Deref for SignedRawInvoice {
}
}

impl core::hash::Hash for InvoiceSignature {
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like clippy doesn't like this

error: you are implementing `Hash` explicitly but have derived `PartialEq`
    --> lightning-invoice/src/lib.rs:1418:1
     |
1418 | / impl core::hash::Hash for InvoiceSignature {
1419 | |     fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
1420 | |         let (recovery_id, sig) = self.0.serialize_compact();
1421 | |         let recovery_id = recovery_id.to_i32();
...    |
1424 | |     }
1425 | | }
     | |_^
     |
     = note: `#[deny(clippy::derive_hash_xor_eq)]` on by default
note: `PartialEq` implemented here
    --> lightning-invoice/src/lib.rs:456:28
     |
456  | #[derive(Clone, Debug, Eq, PartialEq)]
     |                            ^^^^^^^^^
     = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#derive_hash_xor_eq
     = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Clippy is dumb. But, also, we could just wait for #1575 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes it's no hurry so do I close the PR and open another if this is ready or should I leave it open ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Up to you, you're welcome to leave it open and I'll just add a "waiting on secp bump" tag

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok I'll leave it open, thank you

Choose a reason for hiding this comment

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

I'd argue clippy isn't dumb here. The contract of Hash states that hashes have to be equal if Eq returns equal for the hashed objects. With this implementation we'd rely on libsecp256k1 to not use the higher bits of the recovery id byte for other things for that contract to hold. Related to rust-bitcoin/rust-secp256k1#463. The Hash impl from secp256k1 would be guaranteed to be at least internally consistent (just not between versions I guess).

@NicolaLS
Copy link
Contributor Author

NicolaLS commented Jul 9, 2022

I think it's ready now @TheBlueMatt

@NicolaLS
Copy link
Contributor Author

NicolaLS commented Jul 9, 2022

I think it's ready now @TheBlueMatt

Oh wait it's until ldk updates to the version, not only when it's released so nvm

@TheBlueMatt
Copy link
Collaborator

Indeed, now blocked on rust-bitcoin/rust-bitcoin#1066

@dunxen
Copy link
Contributor

dunxen commented Aug 19, 2022

@NicolaLS, the upstream PRs are in and I see rust-bitcoin 0.29 does include rust-bitcoin/rust-bitcoin#1066. So looks like we're unblocked completely now :)

@dunxen dunxen removed the blocked on rust-bitcoin Blocked until we update rust-bitcoin/etc label Aug 19, 2022
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.

Thanks, pretty neat now!

@NicolaLS
Copy link
Contributor Author

Thanks, pretty neat now!

Is it a problem that some CI failed ?

@dunxen
Copy link
Contributor

dunxen commented Aug 19, 2022

Is it a problem that some CI failed ?

Those are unrelated to the project. It’s a crates registry issue (I think).

@TheBlueMatt TheBlueMatt merged commit 49a0233 into lightningdevkit:main Aug 19, 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.

None yet

6 participants