-
Notifications
You must be signed in to change notification settings - Fork 142
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
Add Hash256 associated type #434
Conversation
@sipa in your miniscript implementation, do you serialize hashes (e.g. in |
@apoelstra They're treated as byte arrays, so the order of 2-character-hex units is the same as the order of the bytes as they appear in the script. |
Perfect, thank you! |
Done reviewing 4ffabb7. ACK except for the one nit. Arguably this should be two PRs -- the hash256 stuff in the first two commits looks unobjectionable (and it's not even breaking, since amusingly we previously had a shitload of manual byte-reversing logic..). The second two are potentially more controversial, since we introduce a macro-based API to simulate a trait hierarchy. Given the limitations of Rust's type system, I think this is the right approach, but it's definitely not a great user experience to need to know about these macros.... I guess we will just have to be careful to document them well. nit: there are a typos in the last two commit messages |
sorry, tried to click "comment" while scratching my arm and the mouse moved. |
@apoelstra split into two PRs.
I will document them more carefully in the upcoming PR and will add a macro exporting. My plan is to add an example using the API after we are merged all the associated fields (ripemd160/hash160/older/after). The users don't have to know about them, they still implement all of the methods, but it's annoying. |
The nightly formatter had configuration errors that were fixed in the first commit. |
@tcharding, it occurs to me that if We can give it a try for a few more weeks to test the stability. If it breaks again, we should consider moving back to the stable version. |
74392eb
to
b9f7a63
Compare
The reason we moved to nightly was that many of the config options are not available on stable. With the current contention over using rustfmt in rust-bitcoin we need the nightly config options if we are to have any hope of enabling rustfmt there. I do not think that 'breaking' is the correct term, rustfmt nighltly changes and our config file needed updating. I have two suggestions
I like (1) because I can do it as part of my routine work and then others are not inconvenienced by rustfmt. Devs could see the CI verison used and use the same one locally (if I can figure out an easy way to do so).
I think I answered this above, are you open to the suggestions? Sorry that |
Thanks. I like (1) too. We are in agreement with the next steps. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be rebased now that #424 has merged (the first 5 patches of this PR).
I'd prefer to see the rustfmt stuff as a separate PR so other open PRs could use it, I imagine everyone is going to have red in the CI pipeline because of this but its not that big a deal.
High level comment: seems like this DISPLAY_BACKWARDS
const makes the types in bitcoin_hashes
less usable. Perhaps it should be a field on the type so users can control the direction of the output? I had to hack around it once recently also.
src/miniscript/hash256.rs
Outdated
feature = "schemars", | ||
schemars(schema_with = "crate::util::json_hex_string::len_32") | ||
)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't have schemars
dependency, did you mean to add it? Will need the module in bitcoin_hashes/src/util.rs
as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a fail in the tooling, compiler does not flag this and neither does clippy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, embarrassing copy-paste error here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lol, I should've also flagged this in review rather than shrugging "I guess we capitulated about supporting schemars at some point"
src/lib.rs
Outdated
@@ -159,13 +159,18 @@ pub trait MiniscriptKey: Clone + Eq + Ord + fmt::Debug + fmt::Display + hash::Ha | |||
/// Used in the sha256 fragment | |||
type Sha256: Clone + Eq + Ord + fmt::Display + fmt::Debug + hash::Hash; | |||
|
|||
/// The associated [`hash256::Hash`] type for this [`MiniscriptKey`] type. | |||
/// Used in the sha256 fragment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// Used in the sha256 fragment | |
/// The associated [`hash256::Hash`] for this `MiniscriptKey`, used in the hash256 fragment. |
If this form is agreeably, then could use it for the comment on Sha256
also.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
@@ -43,6 +43,15 @@ impl Translator<String, bitcoin::PublicKey, ()> for StrKeyTranslator { | |||
.unwrap(); | |||
Ok(hash) | |||
} | |||
|
|||
fn hash256(&mut self, _hash256: &String) -> Result<hash256::Hash, ()> { | |||
// hard coded value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment adds no value, we can see its a hard coded value. Perhaps comment why this value is used or don't comment at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing the comment
src/miniscript/astelem.rs
Outdated
x.reverse(); | ||
write!(f, "hash256({})", sha256d::Hash::from_inner(x)) | ||
} | ||
Terminal::Hash256(ref h) => write!(f, "hash256({})", h), // reverse would handled correctly by rust-bitcoin |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is incorrect, right? We are handling the display here in this lib now, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, this comment is incorrect. Thanks for catching this!
src/miniscript/astelem.rs
Outdated
x.reverse(); | ||
write!(f, "hash256({})", sha256d::Hash::from_inner(x)) | ||
} | ||
Terminal::Hash256(ref h) => write!(f, "hash256({})", h), // reverse correctly handled upstream |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And this one?
/// The associated [`hash256::Hash`] type for this [`MiniscriptKey`] type. | ||
/// Used in the sha256 fragment | ||
type Hash256: Clone + Eq + Ord + fmt::Display + fmt::Debug + hash::Hash; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
re: Hash
, Sha256
, and Hash256
From the docs on the trait its not really clear, at least to me, what is the relation between these three types. Two of them are named after fragments but one is not and they are all 'hashes' but there are other hash fragments as well. Can be add some documentation to try and help devs (cough me) to understand this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, this is indeed clarification now that we have multiple hashes. I will add some clarification here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sha256
and Hash256
are two of the five hash types supported by Script (the others are Sha1
(not supported by miniscript), Ripemd160
and Hash160
). Meanwhile Hash
is a trait from bitcoin_hashes which abstracts over all of these and allows feeding data into it.
(IIRC Hash
is basically just a copy of io::Write
but io::Write
is std-only, for unclear reasons.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@apoelstra , I think the Hash
is @tcharding is talking about here is MiniscirptKey::Hash which is different from bitcoin_hashes::Hash
.
The Hash
represents the associated type corresponding to PkH fragment. Perhaps, with multiple hashes in the picture and considering #431 we should rename that to RawPkHash
?
I think we should re-evaluate whether we even need a Pk::Hash type anymore. That can be useful for abstractions over RawPkH in the future when we have partial descriptors.
We can consider renaming that as a part of a separate PR.
src/miniscript/hash256.rs
Outdated
//! Functionality similar to `sha256d` Hash type in bitcoin, but the FromStr | ||
//! `Display` are *NOT* REVERSE. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
//! Functionality similar to `sha256d` Hash type in bitcoin, but the FromStr | |
//! `Display` are *NOT* REVERSE. | |
//! This module is _identical_ in functionality to the `bitcoin_hashes::sha256d` hash type | |
//! but the `FromHex` and `ToHex` implementations use `DISPLAY_BACKWARDS = false`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed and also added a comment about FromStr
and Display
Hey man, I'm not sure what the force push means, did you think this is ready for re-review? The schemars stuff is still in there. |
f9f3e56
to
3685071
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry about the previous push. This is ready for review now @tcharding
/// The associated [`hash256::Hash`] type for this [`MiniscriptKey`] type. | ||
/// Used in the sha256 fragment | ||
type Hash256: Clone + Eq + Ord + fmt::Display + fmt::Debug + hash::Hash; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@apoelstra , I think the Hash
is @tcharding is talking about here is MiniscirptKey::Hash which is different from bitcoin_hashes::Hash
.
The Hash
represents the associated type corresponding to PkH fragment. Perhaps, with multiple hashes in the picture and considering #431 we should rename that to RawPkHash
?
I think we should re-evaluate whether we even need a Pk::Hash type anymore. That can be useful for abstractions over RawPkH in the future when we have partial descriptors.
We can consider renaming that as a part of a separate PR.
@@ -43,6 +43,15 @@ impl Translator<String, bitcoin::PublicKey, ()> for StrKeyTranslator { | |||
.unwrap(); | |||
Ok(hash) | |||
} | |||
|
|||
fn hash256(&mut self, _hash256: &String) -> Result<hash256::Hash, ()> { | |||
// hard coded value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing the comment
@apoelstra, let's try to merge #431 before this. |
Yeah, renaming |
@apoelstra, we could also remove the But on the other hand, we already have the code. So part of me wants to keep it in case we need it in the future after partial descriptors are officially supported. |
Yeah, let's keep it for now. |
@tcharding , @apoelstra , ready for review again. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK e49b6d8
Rebased after #417 . |
Another problem that I am noting with merging the imports is that there are always conflicts when merging PRs that touch the same file. PR1: use crate::{A, B, SomethingFromPR1, T, U}; PR2: use crate::{A, AnotherThingFromPR2, B, T, U}; These two PRs would conflict. While previously it were no conflicts use crate::SomethingFromPR1; use crate::AnotherThingFromPR2; |
Yes you are correct. Its a trade off right, the more you merge the imports (not at all, merge last module, merge crate Although my view was formed in projects that do not use the '2 explicit acks to merge' policy so perhaps the additional merge conflicts are not good in the rust-bitcoin stack? |
I tend to agree. I think we should have this in the rust-bitcoin/rust-bitcoin.
Let us keep the merging import feature in rust-miniscript for a few more weeks and learn a few more things. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 9d83a8f
Implement a new hash type that does not displat/fromstr in backwards direction. We need support for support for double hashing, but if we use upstream sha256d::Hash, our FromString/Display requirements break
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 905c5dc
DISPLAY_BACKWARD
. Should we make this type upstream in bitcoin-hashes?