8000 Add tests based on mutagen outputs by elichai · Pull Request #399 · rust-bitcoin/rust-bitcoin · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add tests based on mutagen outputs #399

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

Merged
merged 6 commits into from
Feb 24, 2020

Conversation

elichai
Copy link
Member
@elichai elichai commented Jan 23, 2020

Hi,
I used mutagen(https://github.com/llogiq/mutagen) to find places where our test don't quite check (even if they "cover" as in code coverage).
and wrote more tests to try and cover the edge cases it found.

I didn't managed to run it on all of our code because it's a lot of work so I decided to do it gradually.

For actual CI inclusion I'll wait and see how Matt will do it in lightningdevkit/rust-lightning#453
And also: llogiq/mutagen#156 llogiq/mutagen#154

I also found some bugs while writing the tests and fixed them.

@@ -131,7 +131,7 @@ pub fn from(data: &str) -> Result<Vec<u8>, Error> {
// Build in base 256
for d58 in data.bytes() {
// Compute "X = X * 58 + next_digit" in base 256
if d58 as usize > BASE58_DIGITS.len() {
if d58 as usize >= BASE58_DIGITS.len() {
Copy link
Member Author
@elichai elichai Jan 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the right logic because we later do BASE58_DIGITS[d58] which would panic if d58==BASE58_DIGITS.len()
But this can't really happen in practice, because BASE58_DIGITS.len() == 128 and data is &str meaning it only contains valid UTF-8. and UTF-8 don't have any valid byte starting with 128.
(it's either a single byte < 128 or multiple bytes with the first one > 128)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch

Copy link
Collaborator
@stevenroose stevenroose left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks you so much for all the testing on Amount! I'm amazed that you only found a single small bug!

d.read_slice(&mut ret)?;
Ok(ret)
}
}

impl Encodable for Box<[u8]> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this one even used anywhere?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. pub struct Script(Box<[u8]>); so all operations on script.0 are via the Box impl

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fairness we might want to just do this impl directly on Script. I think I did it on Box<[u8]> so that all this mostly-boilerplate code would be in encode.rs (which is full of boilerplate code) rather than in script.rs (which is mostly actual functionality). But this isn't a strong argument.

Copy link
Member Author
@elichai elichai Jan 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already reverted this change back
removing this and implementing on Script can be done in a separate PR.

test_varint_len(VarInt(u64::max_value()), 9);
}

fn test_varint_len(varint: VarInt, expected: usize) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: declare methods before they're used

test_len_is_max_vec::<u64>();
}

fn test_len_is_max_vec<T>() where Vec<T>: Decodable, T: fmt::Debug {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: declare methods before they're used

@@ -668,7 +668,11 @@ impl SignedAmount {
///
/// Does not include the denomination.
pub fn fmt_value_in(&self, f: &mut fmt::Write, denom: Denomination) -> fmt::Result {
fmt_satoshi_in(self.as_sat().abs() as u64, self.is_negative(), f, denom)
let sats = self.as_sat().checked_abs().map(|a: i64| a as u64).unwrap_or_else(|| {
// We could also hard code this into `9223372036854775808`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that we only expect this to happen for a single value of self.as_sat(), should we not better assert it, return the fixed value and have the calculation commented as documentation?

@codecov-io
Copy link
codecov-io commented Jan 24, 2020

Codecov Report

Merging #399 into master will decrease coverage by 0.16%.
The diff coverage is 97.98%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #399      +/-   ##
==========================================
- Coverage   85.98%   85.81%   -0.17%     
==========================================
  Files          40       40              
  Lines        8083     7461     -622     
==========================================
- Hits         6950     6403     -547     
+ Misses       1133     1058      -75
Impacted Files Coverage Δ
src/util/base58.rs 83.78% <100%> (+1.61%) ⬆️
src/consensus/encode.rs 90% <100%> (+4.31%) ⬆️
src/util/amount.rs 88.99% <94%> (+2.2%) ⬆️
src/util/psbt/mod.rs 86.06% <0%> (-3.71%) ⬇️
src/blockdata/script.rs 78.55% <0%> (-2.6%) ⬇️
src/util/address.rs 86.31% <0%> (-1.57%) ⬇️
src/util/psbt/map/output.rs 62.16% <0%> (-1%) ⬇️
src/util/uint.rs 83.65% <0%> (-0.75%) ⬇️
src/blockdata/transaction.rs 93.92% <0%> (-0.74%) ⬇️
src/util/contracthash.rs 78.5% <0%> (-0.53%) ⬇️
... and 20 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1b946b0...07b30c7. Read the comment docs.

@stevenroose
Copy link
Collaborator

Needs rebase.

@elichai elichai force-pushed the 2020-01-tests branch 2 times, most recently from 83caf7d to 2b24d7a Compare January 26, 2020 11:31
stevenroose
stevenroose previously approved these changes Jan 26, 2020
@elichai elichai requested a review from stevenroose February 24, 2020 15:07
@stevenroose stevenroose merged commit 9cff794 into rust-bitcoin:master Feb 24, 2020
@elichai elichai deleted the 2020-01-tests branch February 25, 2020 10:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0