Skip to content

Remove to/from/as_raw_hash functions#2981

Merged
apoelstra merged 1 commit intorust-bitcoin:masterfrom
tcharding:07-08-rm-raw-hash-functions
Jul 15, 2024
Merged

Remove to/from/as_raw_hash functions#2981
apoelstra merged 1 commit intorust-bitcoin:masterfrom
tcharding:07-08-rm-raw-hash-functions

Conversation

@tcharding
Copy link
Copy Markdown
Member

@tcharding tcharding commented Jul 8, 2024

In an effort to shrink the API of hashes remove the from_raw_hash, to_raw_hash, and as_raw_hash inherent functions from types created with the hash_newtype macro.

There are a few reasons why this is favourable:

  • It allows stable crates to use the macro and not expose unstable hashes types in their API.
  • It makes types created with the macro less "general" in the sense that its more obscure to just hash any data into them. This allows us to write cleaner APIs in rust-bitcoin.

@github-actions github-actions bot added C-bitcoin PRs modifying the bitcoin crate C-hashes PRs modifying the hashes crate labels Jul 8, 2024
@tcharding tcharding mentioned this pull request Jul 8, 2024
6 tasks
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I don't think this helps anything. AsRef impl on Txid/Wtxid should stay but it's true that this method has weak API. I think it'd be more appropriate to have a trait for that that's only implemented for the two expected ids.

// please suggest a better name
pub trait AsTransactionIdentifier: sealed::Sealed {
    fn as_identifier(&self) -> &[u8; 32];
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If we add such a trait we should also add it as a bound on bitcoin::merkle_node::MerkleNode::Leaf since the MerkleNode trait is currently unsealed and has no bounds on its Leaf type, but it is also intended to only be about wtxid and txid.

Interesting that there are multiple places where we want to be generic over those two types..

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

#2987, a sexy little PR if I don't say so myself. Good ideas crew.

Copy link
Copy Markdown
Collaborator

@Kixunil Kixunil Jul 8, 2024

Choose a reason for hiding this comment

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

I agree with the change but not justification. I think casting raw hashes is perfectly fine. The reason to remove it is to help getting 1.0 out sooner. Hopefully one day we can add it back. At least getters.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Personally I think that casting raw hashes should be done by to_byte_array and from_byte_array and we shouldn't have blessed "raw hash" versions.

But sure, let's default to not having them, and it's easy to add later if we agree that we should.

@tcharding tcharding marked this pull request as draft July 9, 2024 02:28
@tcharding
Copy link
Copy Markdown
Member Author

Note to self, wait till #2987 resolves then pick this back up.

@tcharding tcharding force-pushed the 07-08-rm-raw-hash-functions branch from fa0eca8 to b48d780 Compare July 12, 2024 04:38
@tcharding tcharding marked this pull request as ready for review July 12, 2024 04:38
@github-actions github-actions bot removed the C-bitcoin PRs modifying the bitcoin crate label Jul 12, 2024
Copy link
Copy Markdown
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 b48d7806a963a696ca28552d819266a7c4775087

@Kixunil
Copy link
Copy Markdown
Collaborator

Kixunil commented Jul 12, 2024

ACK the code but I'd prefer to modify the commit message to not be misleading.

@tcharding tcharding force-pushed the 07-08-rm-raw-hash-functions branch from b48d780 to a764d6f Compare July 12, 2024 22:13
@tcharding
Copy link
Copy Markdown
Member Author

tcharding commented Jul 12, 2024

ACK the code but I'd prefer to modify the commit message to not be misleading.

I went to do this but I don't see which bit is you think is misleading sorry. A added as to the list of functions in the commit brief log and mentioned the unit test - but I don't think this is what you meant. Can you check and tell me please?

EDIT: Oh did you mean the PR description? I changed it to match the commit log.

@tcharding
Copy link
Copy Markdown
Member Author

Force push is changes to the commit log only, no code changes.

@Kixunil
Copy link
Copy Markdown
Collaborator

Kixunil commented Jul 13, 2024

What I found misleading is the claim that this has anything to do with hash newtypes not being general hashes. A Txid is a sha256 and should be convertible to general hash directly. Just like NonZeroUsize is a usize and can be converted to it.

The reason to still remove this is to not produce unstable API from the hash_newtype macro while hashes is unstable because crates that want to be stable soon need to use the macro.

So the message should state this.

@apoelstra
Copy link
Copy Markdown
Member

I could be convinced either way (and it's fine to change the commit message to say "we're doing it to shrink the API to things we agree on", but I also had no idea what you meant about the original message being misleading. There is a comment thread above where you stated your case and I disagreed and there is no clear conclusion. And then the request to change the message was in a separate thread so it wasn't clear it was related.

I'm often guilty of this too -- but it's worth the effort to slow down and make your position clear rather than saying things are "wrong" in an unspecified way.

@Kixunil
Copy link
Copy Markdown
Collaborator

Kixunil commented Jul 13, 2024

I guess the confusion often stems from GH not allowing commenting on commit messages. Another thing is that this PR is weird because we all want it for completely different reasons and simultaneously I think the reason stated in the message doesn't make sense.

@tcharding
Copy link
Copy Markdown
Member Author

tcharding commented Jul 13, 2024

this PR is weird because we all want it for completely different reasons

lol, now that is rough consensus

In an effort to shrink the API of `hashes` remove the `from_raw_hash`,
`to_raw_hash`, and `as_raw_hash` inherent functions from types created
with the `hash_newtype` macro.

There are a few reasons why this is favourable:

- It allows stable crates to use the macro and not expose unstable
`hashes` types in their API.
- It makes types created with the macro less "general" in the sense that
its more obscure to just hash any data into them. This allows us to
write cleaner APIs in `rust-bitcoin`.
@tcharding tcharding force-pushed the 07-08-rm-raw-hash-functions branch from a764d6f to 2b56f76 Compare July 13, 2024 19:06
@tcharding
Copy link
Copy Markdown
Member Author

Force push is commit log change (copied to PR description) only, no code changes.

Copy link
Copy Markdown
Collaborator

@Kixunil Kixunil left a comment

Choose a reason for hiding this comment

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

ACK 2b56f76

Copy link
Copy Markdown
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 2b56f76

@apoelstra apoelstra merged commit c50796c into rust-bitcoin:master Jul 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-hashes PRs modifying the hashes crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants