-
Notifications
You must be signed in to change notification settings - Fork 623
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
Modify from_script functions in address.rs to return result #1038
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.
Good work, I left a few suggestions.
On commit descriptions and titles: the canonical blog post that folks always link to is: https://cbea.ms/git-commit/, IMO you should definitely have read this post once in your life. The better you do all the auxillary things around making code changes the better response you will get from reviewers and the more chance your changes will have of getting merged.
Of note, this PR is not a refactoring - it makes logic changes.
src/util/address.rs
Outdated
@@ -81,7 +81,9 @@ pub enum Error { | |||
/// An uncompressed pubkey was used where it is not allowed. | |||
UncompressedPubkey, | |||
/// Address size more than 520 bytes is not allowed. | |||
ExcessiveScriptSize | |||
ExcessiveScriptSize, | |||
/// Script which is not a p2pkh, p2sh or witness program. |
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.
/// Script which is not a p2pkh, p2sh or witness program. | |
/// Script is not a p2pkh, p2sh, or witness program. |
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.
Thanks @tcharding for your review and feedback!
The link was super helpful and applied the takeaways from it to this PR. Kindly let me know how it is right now and where it could be further improved.
src/util/address.rs
Outdated
@@ -98,6 +100,7 @@ impl fmt::Display for Error { | |||
Error::InvalidSegwitV0ProgramLength(l) => write!(f, "a v0 witness program must be either of length 20 or 32 bytes: length={}", l), | |||
Error::UncompressedPubkey => write!(f, "an uncompressed pubkey was used where it is not allowed"), | |||
Error::ExcessiveScriptSize => write!(f, "Script size exceed 520 bytes"), | |||
Error::UnrecognizedScript => write!(f, "Script is not a p2pk, p2sh or witness program") |
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.
Same as above. For the record, error messages should start with a lower case letter because they are often combined with other error messages e.g., "erorr one: error two: error three".
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.
Adding an extra commit to update the error message for Error::ExcessiveScriptSize.
src/util/address.rs
Outdated
@@ -119,7 +122,8 @@ impl std::error::Error for Error { | |||
| InvalidWitnessProgramLength(_) | |||
| InvalidSegwitV0ProgramLength(_) | |||
| UncompressedPubkey | |||
| ExcessiveScriptSize => None, | |||
| ExcessiveScriptSize |
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 line has trailing whitespace. You should be able to configure what ever system you use to view diffs before pushing them (editor or shell) to highlight trailing white space. In case its useful, I personally view diffs using
git-log-patch () {
local commit
if (( $# < 1 ))
then
commit="HEAD~"
else
commit="$1"
fi
git log --color=always --patch --reverse "${commit}".. | vimpager
}
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.
Along with a shell alias :)
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.
@tcharding Hey, this is good. Thanks a lot! Though I modified it to (~/.gitconfig
):
[alias]
check = "!f() { git log --color=always --patch --reverse "${1:-HEAD~}"..; }; f"
That pager seems unnecessary BTW, git log
pages by default(?) when run on interactive shell.
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.
Oh nice, I started using vimpager
because I was loosing colours somewhere but seems you are correct. I can stop using it, cheers.
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.
Thanks for this snippet @dpc and @tcharding ! TIL.
src/util/address.rs
Outdated
@@ -397,11 +401,11 @@ impl Payload { | |||
Payload::ScriptHash(ScriptHash::from_inner(hash_inner)) | |||
} else if script.is_witness_program() { | |||
Payload::WitnessProgram { | |||
version: WitnessVersion::from_opcode(opcodes::All::from(script[0])).ok()?, | |||
version: WitnessVersion::from_opcode(opcodes::All::from(script[0])).ok().unwrap(), |
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.
Typically we never want to use unwrap
in non-test code because it panics. If the code path can never be hit use expect
and explain why it cannot panic in the argument string.
(I'm intentionally not putting a suggestion for a better way to do this so you can problem solve it yourself :)
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.
Ahh, makes sense!
Kindly let me know if the current change looks good.
src/util/address.rs
Outdated
@@ -98,6 +100,7 @@ impl fmt::Display for Error { | |||
Error::InvalidSegwitV0ProgramLength(l) => write!(f, "a v0 witness program must be either of length 20 or 32 bytes: length={}", l), | |||
Error::UncompressedPubkey => write!(f, "an uncompressed pubkey was used where it is not allowed"), | |||
Error::ExcessiveScriptSize => write!(f, "Script size exceed 520 bytes"), | |||
Error::UnrecognizedScript => write!(f, "Script is not a p2pk, p2sh or witness program") |
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.
I believe this should be p2pkh not p2pk.
Error::UnrecognizedScript => write!(f, "Script is not a p2pk, p2sh or witness program") | |
Error::UnrecognizedScript => write!(f, "Script is not a p2pkh, p2sh or witness program") |
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.
Ahh you're right! Thanks for pointing it out, I need more coffee 😄
ACK 0939ef4 except for all the nits that others have pointed out :) |
@@ -397,11 +401,11 @@ impl Payload { | |||
Payload::ScriptHash(ScriptHash::from_inner(hash_inner)) | |||
} else if script.is_witness_program() { | |||
Payload::WitnessProgram { |
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.
From #1022 (comment), would it benefit to return an error indicating incorrect segwit v0 program length here?
Then we can remove the explicit check in Address::from_script()
since it would be done 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.
Good point. That way, we can reduce the number of calls to script.is_witness_program()
for correct scripts.
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 2a4f439
Modify from_script functions to return result instead of option so that, in case of errors, there is more information on what went wrong. Resolves: rust-bitcoin#1022
Changes done in the 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 66e852c. LGTM
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 66e852c
Hey @Eunoia1729, just to let you know; in That was long winded way of saying that you should only typically rebase a PR if there are merge conflicts :) |
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 2a4f439
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 66e852c
…rogramLength and add test 24fdb53 Fix incorrect argument passed to Error::InvalidSegwitV0ProgramLength (eunoia_1729) Pull request description: See also: #995, #1038 ACKs for top commit: sanket1729: utACK 24fdb53. Nice catch apoelstra: ACK 24fdb53 tcharding: ACK 24fdb53 Tree-SHA512: ced78b69054ec81431399a853291c7bad5b1a49d6683b1ac153a0f1449935bb5d75a31e3d86160602064530959a2ddc3c59a2a2ca268252c42a6805253ead9d0
… address.rs to return result 66e852c Update format of ExcessiveScriptSize error message (eunoia_1729) 89bd4b6 Modify from_script functions in address.rs to return result (eunoia_1729) Pull request description: Modify from_script functions to return result instead of option so that, in case of errors, there is more information on what went wrong. Resolves: #1022 ACKs for top commit: sanket1729: ACK 66e852c. LGTM tcharding: ACK 66e852c apoelstra: ACK 66e852c Tree-SHA512: 0d9529aee0a5459351bed2cc8b2c5571736d3293e2931c43d98f53330e9ac5f3d998a19da2b4575af0a3c1c4dcfd5a24c8813390bf6f5492a689c36ebb9cb426
Modify from_script functions to return result instead of option so that, in case of errors, there is more
information on what went wrong.
Resolves: #1022