Skip to content

Improve witness unit tests#3409

Merged
apoelstra merged 5 commits intorust-bitcoin:masterfrom
tcharding:09-24-witness-refactor-tests
Sep 30, 2024
Merged

Improve witness unit tests#3409
apoelstra merged 5 commits intorust-bitcoin:masterfrom
tcharding:09-24-witness-refactor-tests

Conversation

@tcharding
Copy link
Copy Markdown
Member

In preparation for moving the Witness type over to primitives refactor and improve all the unit tests that will be moved, do not touch the ones that will stay behind.

The first five patches are from #3406, the last is just a re-name of the test function I tried to refactor in ac6fe3a

The current unit test is incorrect, the indices field of a witness with
a single element starts at 1 because 0 is encode as a single
byte (compact encoded integer).

Fix the debug test and add a test that pushes an empty slice.
@github-actions github-actions bot added the C-bitcoin PRs modifying the bitcoin crate label Sep 25, 2024
@tcharding tcharding force-pushed the 09-24-witness-refactor-tests branch from 6d48eaa to 42a962e Compare September 25, 2024 01:56
@apoelstra
Copy link
Copy Markdown
Member

I think the CI failure is a bug that the new tests expose, though I can't tell what exactly it is.

It may be related to our use of to_ne_bytes and from_ne_bytes, given that it triggers only on the big-endian test system. But in reading the code I can't tell exactly what's wrong here.

@apoelstra
Copy link
Copy Markdown
Member

code review ACK 42a962ee082778b781154885bd04796642e726de but we need to address this failure before we can merge this.

@tcharding
Copy link
Copy Markdown
Member Author

tcharding commented Sep 25, 2024

Mad, this might be a bug. In Encodable for Witness we are use to_ne_bytes (via encode_cursor), surely this is incorrect, a consensus serialization should be able to be passed between machines without relying on the endianness of the machine, right? Funny that I noticed to_ne_bytes in the unit tests but not in the codebase.

EDIT: I have a nagging feeling that we have discussed this before in the past.

@tcharding
Copy link
Copy Markdown
Member Author

Related comment you made (before my time): https://github.com/rust-bitcoin/rust-bitcoin/pull/627/files#r660852407

@apoelstra
Copy link
Copy Markdown
Member

@tcharding we don't ever use that in the consensus encoding though. The consensus encoding uses varints. AFAICT we symmetrically use encode_cursor and decode_cursor, which in turn use to_ne_bytes and from_ne_bytes. So the endianness-specificness shouldn't be visible.

@apoelstra
Copy link
Copy Markdown
Member

I don't think the comment you linked is related to this. That was about using swap_bytes to go between BE and LE. This is about using ne_bytes to "cast" 4 bytes to a u32, in a way that shouldn't be visible to consumers.

@tcharding
Copy link
Copy Markdown
Member Author

Ah you are right we are only encoding the index in the index area of content, no drama - my bad.

Make an effort to clean up the `Witness::push` unit test. This patch
dose not change the test, it only attempts to make it easier to read.
Make an effort to clean up the encoding unit test, by doing:

- Remove element accessor assertions (tested already above)
- Add roundtrip encoding assertion
In preparation for moving unit tests to `primitives` give the serde
tests some love by doing:

- Split them up to do one thing only
- Round trip arbitrary witness
- Use better names
Rust convention is to not use `test_` prefix on unit tests. Also this
unit test is testing that the `ExactSizedIterator` trait is implemented
and working.

Re-name unit test to `exact_sized_iterator`.
@tcharding tcharding force-pushed the 09-24-witness-refactor-tests branch from 42a962e to 5fab6b1 Compare September 26, 2024 05:32
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 5fab6b1 successfully ran local tests

@tcharding
Copy link
Copy Markdown
Member Author

Merge please. This is holding up more primitives work, onwards and upwards!

@apoelstra apoelstra merged commit 7379eba into rust-bitcoin:master Sep 30, 2024
@tcharding tcharding deleted the 09-24-witness-refactor-tests branch October 14, 2024 22:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-bitcoin PRs modifying the bitcoin crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants