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

Improve Message constructors #644

Merged
merged 1 commit into from
Aug 10, 2023

Conversation

tcharding
Copy link
Member

@tcharding tcharding commented Aug 9, 2023

Observe:

  • The word "hash" can be a verb or a noun, its usage in function names is therefore at times ambiguous.
  • The function name from_slice gives no indication as to what the slice input is.

Improve Message constructors by doing:

  • Add a constructor Message::from_digest that takes a 32 byte array as input.
  • Rename Message::from_slice to Message::from_digest_slice (deprecate from_slice and add from_digest_slice)
  • Improve the docs while we are at it.

Note

The original PR conflate 2 separate issues, the Message constructor naming clarity issue and the upgrade difficulty issue, PR is now only a solution to the first. The second will be done as a separate PR.

@tcharding
Copy link
Member Author

tcharding commented Aug 9, 2023

With this PR we can use Message::from_hash(sighash.to_byte_array()) in rust-bitcoin instead of relying on the ThirtyTwoByteHash trait, then we can have two different versions of hashes at the same time and BOOM! no more dependency hole.

@apoelstra
Copy link
Member

A few thoughts:

  • ACK introducing the from_hash constructor.
  • ACK renaming from_slice... though we should just rename it from_hash_slice and provide a giant warning. No need to call it insecure. It's not just used for testing; there are lots of contexts where you obtain a hash in the form of a slice rather than array.
  • I would still like to keep (and use) the ThirtyTwoByteHash trait. But you suggested in another thread that we could try moving that trait to bitcoin_hashes.
  • Finally, I think we should semver-trick the trait so that it's the same trait in every version of bitcoin_hashes. That might fix our upgrade grief? Similarly we could make the bitcoin_hashes dep in this crate be a range of major versions.

@tcharding tcharding changed the title Add additional Message constructors Improve Message constructors Aug 9, 2023
Observe:

- The word "hash" can be a verb or a noun, its usage in function names
  is therefore at times ambiguous.
- The function name `from_slice` gives no indication as to what the
  slice input is.

Improve Message constructors by doing:

- Add a constructor `Message::from_digest` that takes a 32 byte array as
  input.
- Rename `Message::from_slice` to `Message::from_digest_slice`
  (deprecate `from_slice` and add `from_digest_slice`)
- Improve the docs while we are at it.
@tcharding
Copy link
Member Author

PR now does the first two points you suggest, will play with second two in a separate PR.

@apoelstra
Copy link
Member

Looks good! I have a mild preference to use hash rather than digest but maybe this is the right approach, since hash is a pretty overloaded word already..

Copy link
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

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

ACK cd40ae7

@apoelstra apoelstra merged commit 14e8218 into rust-bitcoin:master Aug 10, 2023
22 of 24 checks passed
@tcharding
Copy link
Member Author

tcharding commented Aug 10, 2023

Looks good! I have a mild preference to use hash rather than digest but maybe this is the right approach, since hash is a pretty overloaded word already..

I got worried over night thinking about this because it introduces non-uniformity "why hash here and digest there?" since we barely use the word "digest" anywhere. I've been down this path before in years gone past and its a bit like pushing against the whole world. The "hash is a verb and noun" thing is a real problem though.

@tcharding tcharding deleted the 08-09-message-from branch August 10, 2023 23:30
@Kixunil
Copy link
Collaborator

Kixunil commented Oct 14, 2023

Regarding ThirtyTwoByteHash, if we truly want to avoid the hole we should just put it into a completely separate crate bitcoin_32b_hash which would be nearly instantly stable and then depend on that from both hashes and secp256k1 instead. It's the most principled approach, although I can see some people won't like more dependency lines.

@apoelstra
Copy link
Member

Aside from the "more dependency lines" issue (and IMO I would rather deal with the hole than have a more complicated dep tree), I think we considered this and had some issue with the orphan rules and blanket impls. But I don't remember specifically where.

@Kixunil
Copy link
Collaborator

Kixunil commented Oct 14, 2023

We could have the dep tree complicated only until stabilization and then incur one last break. I'm willing to look at the orphan problem if this is acceptable. At the very least From<T> should be implementable for our type.

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.

3 participants