-
Notifications
You must be signed in to change notification settings - Fork 878
Introduce and use test_hex_unwrap
macro in internals
#4250
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
Conversation
This will make #4249 less noisy. |
813a170
to
c3b0e79
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.
Unless I'm missing something, all of these can, and probably should, use hex_lit
.
Yeah, would prefer to use |
c3b0e79
to
9fdb191
Compare
Not all call sites use string literals. Now uses |
What is going on here, the dev-dependency seems not to be available in bench code? |
base58/Cargo.toml
Outdated
@@ -23,6 +23,7 @@ internals = { package = "bitcoin-internals", version = "0.4.0" } | |||
|
|||
[dev-dependencies] | |||
hex = { package = "hex-conservative", version = "0.3.0", default-features = false, features = ["alloc"] } | |||
internals = { package = "bitcoin-internals", version = "0.4.0", features = ["hex"] } |
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.
Looks like this needs hex_lit
dependency instead.
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.
Woops, I tried that before I worked out to throw .to_vec()
everywhere. Done as suggested.
bitcoin/src/blockdata/witness.rs
Outdated
// annex starting with 0x50 causes the branching logic. | ||
let annex = hex!("50"); | ||
let annex = hex!("50").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.
Why are these needed?
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 think the root cause is that we don't implement Encodable
for slices.
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.
Now that I looked below, it only needs slices.
internals/src/macros.rs
Outdated
#[cfg(feature = "hex")] | ||
mod tests { | ||
#[test] | ||
fn parse_hex_into_vector() { |
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.
Is this still needed?
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.
The test you mean or the macro itself? The macro is used and I'd say if we use the macro then we keep the test 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.
I meant the macro, yes. Interesting that it's used - we have dynamic test data somewhere.
9fdb191
to
fc334a1
Compare
CI failure is real. |
In fc334a1: :/ there are a few points of messiness here:
Only the third point is something that I'd like changed about this PR. |
Do you mean Honestly, I think
That's not |
fc334a1
to
382ed03
Compare
89a6596
to
fa8e588
Compare
Mainly I just called |
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.
IDK, so many call to to_vec
where slices can be used is somewhat confusing.
bitcoin/src/blockdata/transaction.rs
Outdated
|
||
const SOME_TX: &str = "0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000"; | ||
|
||
#[bench] | ||
pub fn bench_transaction_size(bh: &mut Bencher) { | ||
let raw_tx = hex!(SOME_TX); | ||
let raw_tx = Vec::from_hex(SOME_TX).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.
This also shouldn't need Vec
, might need cast to &[u8]
below.
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.
Added a patch up front that cleans up the bench code in transaction
.
let el_0 = hex!("03d2e15674941bad4a996372cb87e1856d3652606d98562fe39c5e9e7e413f2105"); | ||
let el_1 = hex!("000000"); | ||
let el_0 = hex!("03d2e15674941bad4a996372cb87e1856d3652606d98562fe39c5e9e7e413f2105").to_vec(); | ||
let el_1 = hex!("000000").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.
These could've been just slices.
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 now see that for some reason this tests that Vec<Vec<u8>>
serialization is the same as Witness
. Do we actually need this? @apoelstra I think since we added Witness
Vec<Vec<u8>>
serialization is no longer relevant.
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 now see that for some reason this tests that
Vec<Vec<u8>>
serialization is the same asWitness
.
Well, because that's how the serialization of Witness
is defined.
Maybe we can drop the encoding impls for Vec<Vec<u8>>
, though it's a single line of code and isn't hurting anything and makes this test easy to write -- although it should probably be an arbitrary/kani test rather than a fixed-vector one.
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.
Well, because that's how the serialization of
Witness
is defined.
My idea is to move the entire thing to Witness
- indeed, removing Vec<Vec<u8>>
.
isn't hurting anything
If any other future soft fork adds a structure that's similar to witness but encoded differently it'll be a mess. I would even argue it already has something similar - taproot merkle branch. Though we have a type that's so much more convenient nobody will ever convert it so it's not a problem now. Note that I don't object to a function capable of serializing Vec<Vec<u8>>
existing, just to it being the trait impl.
And yes, I think we should fuzz it.
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 don't think that taproot merkle branches are similar at all to a Vec<Vec>. They are similar to a Vec<[u8; 32]>, and without looking I can't say whether they are serialized like that or like a Vec.
But regardless, it's a distinct object which has its own distinct type, and the "raw type" that shares its serialization means little more than a mnemonic for remembering/implementing/testing the serialization.
I don't really mind removing Vec<Vec>, but I really don't think it hurts anything.
bitcoin/src/blockdata/witness.rs
Outdated
// annex starting with 0x50 causes the branching logic. | ||
let annex = hex!("50"); | ||
let annex = hex!("50").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.
Again, could've been slices.
bitcoin/src/blockdata/witness.rs
Outdated
// annex starting with 0x50 causes the branching logic. | ||
let annex = hex!("50"); | ||
let annex = hex!("50").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.
Now that I looked below, it only needs slices.
bitcoin/src/blockdata/witness.rs
Outdated
// annex starting with 0x50 causes the branching logic. | ||
let annex = hex!("50"); | ||
let annex = hex!("50").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.
Slices again.
bitcoin/src/psbt/mod.rs
Outdated
hex!("3045022100d12b852d85dcd961d2f5f4ab660654df6eedcc794c0c33ce5cc309ffb5fce58d022067338a8e0e1725c197fb1a88af59f51e44e4255b20167c8684031c05d1f2592a01"), | ||
hex!("0223b72beef0965d10be0778efecd61fcac6f79a4ea169393380734464f84f2ab3"), | ||
hex!("3045022100d12b852d85dcd961d2f5f4ab660654df6eedcc794c0c33ce5cc309ffb5fce58d022067338a8e0e1725c197fb1a88af59f51e44e4255b20167c8684031c05d1f2592a01").to_vec(), | ||
hex!("0223b72beef0965d10be0778efecd61fcac6f79a4ea169393380734464f84f2ab3").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.
Again, slices on all Witness
constructions above.
I'm leaving all the witness stuff as is. The macro being replaced produced a |
The reason is that the test is not supposed to test if vec works in particular (quite the opposite, a test should make sure non-vecs are usable). I know that historically we had I feel like the cleanup should go before this PR, not after. I can send it if you're fine with waiting. |
Thanks, go for it. |
fa8e588
to
7560f0f
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.
Looks much better now.
@@ -2013,10 +2007,18 @@ mod benches { | |||
|
|||
#[bench] | |||
pub fn bench_transaction_deserialize(bh: &mut Bencher) { | |||
let raw_tx = hex!(SOME_TX); | |||
let raw_tx = <Vec<u8> as hex::FromHex>::from_hex(SOME_TX).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.
I don't understand this change. I think it's equivalent to the macro and should work equally well with hex_lit
.
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.
hex_lit
doesn't work in bench code for some reason. Seems the dev-dependecies aren't available.
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, whaat?! OK then, let's keep it this way. A comment if you can be bothered would be nice.
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 suspect this would be fixed with criterion. We should probably have a "switch to criterion" tracking issue so that we notice when we hit a critical mass of pain with the built-in benchmarks and feel like doing the work to switch.
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 suspect this would be fixed with criterion. We should probably have a "switch to criterion" tracking issue so that we notice when we hit a critical mass of pain with the built-in benchmarks and feel like doing the work to switch.
Added to #3333
84bee2f Simplify `Witness` construction in tests (Martin Habovstiak) 3551ec2 Don't access internalls of `Witness` in tests (Martin Habovstiak) c807836 Impl `PartialEq` between `Witness` and containers (Martin Habovstiak) 587a66d Add a bunch of missing conversions for `Witness` (Martin Habovstiak) Pull request description: This is supposed to go in front of #4250 `Witness` lacked a bunch of APIs that were making it harder to use and test, so this also adds them in addition to cleaning up tests. (I only realized they are missing when I tried to clean up tests and got a bunch of errors.) ACKs for top commit: tcharding: ACK 84bee2f apoelstra: ACK 84bee2f; successfully ran local tests Tree-SHA512: 7973f2a56b070babba7b4c632f45858154ccd00f8e77956ad2d28cb66e1fd18ff60d92c031ba3b76d0958e4acd34adfca10607fa26ec569dfd52ba1c1e2c79eb
We can use `deserialize_hex` when outside of the actual benchmark code to simplify the functions. Also add an additional test that benchmarks `deserialize_hex`.
We have the `hex_lit` dependency for converting a hex string literal to an array. Currently we have a `test_hex_unwrap` macro in the `hex v0.3.0` release but not on either `master` or the upcoming `v1.0.0-alpha.0` release. This is making PRs around releasing and depending on the release more noisy than required. Use `hex_lit::hex` where possible (often needing an additional call to `to_vec()`) and where not possible use `Vec::from_hex`.
7560f0f
to
d6296cd
Compare
Rebased and added a comment to the bench usage of |
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 d6296cd; successfully ran local tests
|
||
let mut want_witness = Witness::default(); | ||
want_witness.push(&el_0); | ||
want_witness.push(&el_1); | ||
|
||
let vec = vec![el_0.clone(), el_1.clone()]; | ||
let vec = vec![el_0, el_1]; |
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 later used in a function let got_witness = Witness::from_slice(&vec);
. Can't this just be a slice the begin with?
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.
If we need to rebase this PR we could fix this. I'm a little surprised the clippy "unnecesary vec" lint didn't notice it.
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 d6296cd
We have the
hex_lit
dependency for converting a hex string literal to an array.Currently we have a
test_hex_unwrap
macro in thehex v0.3.0
release but not on eithermaster
or the upcoming
v1.0.0-alpha.0
release. This is making PRs around releasing and depending on therelease more noisy than required.
Introduce a
test_hex_unwrap
macro in internals for usage when the input is not a string literal.Use
hex_lit::hex
where possible (often needing an additional call to