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
Implement TryFrom #1007
Implement TryFrom #1007
Conversation
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 c898301
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.
LGTM c898301, but needs rebase
src/util/sighash.rs
Outdated
0x81 => AllPlusAnyoneCanPay, | ||
0x82 => NonePlusAnyoneCanPay, | ||
0x83 => SinglePlusAnyoneCanPay, | ||
0xFF => Reserved, |
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.
Opened a new issue about removing the variant 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.
Linking the issue here: #1030
af94774
to
602c17a
Compare
Changes in force push:
|
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 602c17a
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.
Would've acked but I'm not sure about sighash.
fn try_from(opcode: opcodes::All) -> Result<Self, Self::Error> { | ||
match opcode.to_u8() { | ||
0 => Ok(WitnessVersion::V0), | ||
version if version >= opcodes::all::OP_PUSHNUM_1.to_u8() && version <= opcodes::all::OP_PUSHNUM_16.to_u8() => |
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.
Doesn't clippy complain about this? I don't like the lint much anyway, just surprised.
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.
Super bizarre, it seems that to trigger the 'manual range' lint there has to be an =
sign. This does not trigger the lint
let version = 0u8;
if version > 1 && version < 10 {
in_range = true;
}
But this does (added =
):
let version = 0u8;
if version >= 1 && version < 10 {
in_range = true;
}
ec7f73b
to
553c4a2
Compare
Changes in force push:
Remaining open question: what to do about sighash? I do think this PR is a move in the correct direction, possibly we could tackle the naming of |
src/util/taproot.rs
Outdated
/// | ||
/// If inner proof length is more than [`TAPROOT_CONTROL_MAX_NODE_COUNT`] (128). | ||
fn try_from(v: &[sha256::Hash]) -> Result<Self, Self::Error> { | ||
let v = v.to_vec(); |
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 wasn't my point, the caller can already do this. :) My point was to check the length here so that allocation is avoided in the error case. If you want to avoid duplicating checking logic I have a trick for that:
// non-public
fn from_collection<T: AsRef<[sha256::Hash]> + Into<Vec<sha256::Hash>>>(collection: T) -> Result<Self, TaprootError> {
if collection.as_ref().len() > TAPROOT_CONTROL_MAX_NODE_COUNT {
Err(TaprootError::InvalidMerkleTreeDepth(collection.as_ref().len()))
} else {
Ok(TaprootMerkleBranch(collection.into()))
}
}
// just directly call `Self::from_collection(v)` in all `TryFrom` impls
You could also use macros for TryFrom
and also add Box<[sha256::Hash]>
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.
Nice trick :) Implemented as suggested, thanks.
553c4a2
to
6ef1ecc
Compare
Changes in force push: Amend the final commit adding a macro as suggested. |
Weird, the CI run is not showing? |
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 6ef1ecc
6ef1ecc
to
b162ea1
Compare
Changes in force push:
|
Intermediate commits seem not to compile now. |
We have a bunch of 'from' methods that are fallible; `TryFrom` became available in Rust 1.34 so we can use it now we have bumped our MSRV. Implement the various `WitnessVersion` from methods using `TryFrom` and deprecate the originals.
TryFrom` became available in Rust 1.34 so we can use it now we have bumped our MSRV. Implement `TryFrom<TaprootBuilder>` for `TapTree` and deprecate the `from_builder` method.
TryFrom` became available in Rust 1.34 so we can use it now we have bumped our MSRV. Implement `TryFrom<Key>` for `ProprietaryKey` and deprecate the `from_key` method.
TryFrom` became available in Rust 1.34 so we can use it now we have bumped our MSRV. Add a macro for implementing `TryFrom` for various lists of `sha256::Hash` types. Use the macro to for vec, slice, and boxed slice.
The `u8` parameter in the `SchnorrSighashType` constructor is a consensus valid `u8`. Re-name the constructor to make this explicit. Deprecate `from_u8` as is typical.
b162ea1
to
b29ff9b
Compare
Force push is rebase to pick up clippy fix from #1063, no changes. |
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 b29ff9b
(Having local compilation errors but just due to rust-bitcoin/rust-secp256k1#466 )
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 b29ff9b
b29ff9b Rename SchnorrSighashType::from_u8 -> from_consensus_u8 (Tobin C. Harding) af16286 Implement TryFrom sha256::Hash for TaprootMerkleBranch (Tobin C. Harding) 6b7b440 Implement TryFrom<Key> for ProprietaryKey (Tobin C. Harding) 5c49fe7 Implement TryFrom<TaprootBuilder> for TapTree (Tobin C. Harding) 632a5db Implement TryFrom for WitnessVersion (Tobin C. Harding) Pull request description: Audit the whole codebase checking for any method that is of the form `from_foo` where foo is not an interesting identifier (like 'consensus' and 'standard'). Implement `TryFrom` for any such methods, deprecating the original. Done as separate patches so any can be easily dropped if not liked. ACKs for top commit: apoelstra: ACK b29ff9b Kixunil: ACK b29ff9b Tree-SHA512: 40f1d96b505891080df1f7a9b3507979b0279a9e0f9d7cd32598bdc16c866785e6b13d5cb1face5ba50e3bc8484a5cd9c7f430d7abc86db9609962476dacd467
Audit the whole codebase checking for any method that is of the form
from_foo
where foo is not an interesting identifier (like 'consensus' and 'standard'). ImplementTryFrom
for any such methods, deprecating the original.Done as separate patches so any can be easily dropped if not liked.