Commit Graph

18 Commits

Author SHA1 Message Date
junderw 4924148dc6
Swap out `Work::log2` implementation for `U256::to_f64` 2023-03-17 14:10:00 -07:00
junderw 2158f88f1d
Add a method to `pow::Target` for returning difficulty as an f64.
This adds a conversion function to U256 to get an f64. We use the method shown in the following blog post. https://blog.m-ou.se/floats/
Target::MAX was converted to a f64 and set as a const that is verified in a unit test.
2023-03-10 07:54:36 -07:00
Tobin C. Harding a11cf07501
Run the formatter
Various formatting issues have crept into the codebase because we do not
run the formatter in CI.

In preparation for enabling formatting checks in CI run `cargo +nightly
fmt` to fix current formatting issues. No changes other than those
create by the formatter.
2023-03-06 10:22:29 +11:00
Tobin C. Harding 161273b209
Re-name hash inner/byte methods
Currently we have an associated type on hash types `Inner` with
accompanying methods `into_inner`, `from_inner`, `as_inner`. Also, we
provide a way to create new wrapped hash types. The use of 'inner'
becomes ambiguous with the addition of wrapped types because the inner
could be the inner hash type or the `Inner` byte array of the inner
wrapped hash type.

In an effort to make the API more clear and uniform do the following:

- Rename `Inner` -> `Bytes`
- Rename `*_inner` -> `*_byte_array`
- Rename the inner hash to/from methods to `*_raw_hash`

Correct method prefix `into_` -> `to_` because theses methods convert
owned `Copy` types.

Add the trait Bound `Copy` to the `Bytes` type because we rely on this
trait bound for the conversion methods to be correctly named according
to convention.

Because of the dependency hole created by `secp256k1` this patch changes
the secp dependency to a git tag dependency that includes changes to the
hashes calls required so that we can get green lights on CI in this
repo.
2023-02-27 14:23:58 +11:00
Tobin C. Harding dd316e4d14
pow: Remove Mul/Div by arbitrary integer types
When we added `Target` and `Work` types we implemented multiplication
and division by anything `Into<u64>`, this is not typically done in the
Rust stdlib and also is semantically incorrect for the types.

Remove `Mul` and `Div` impls from `Target` and `Work`. Also remove
`Mul<T>` for `T: Into<u64>` from the private `U256` type.
2023-02-14 11:50:46 +11:00
Tobin C. Harding 7dde3b3b22 Make max/min_value functions const
The `max_value` and `min_value` functions only exist to be
compatible/uniform with Rust 1.41.1 they will never change and they just
return a constant value. They can therefore be made const functions.
2023-01-31 08:35:32 +11:00
Tobin C. Harding 308c727c82
pow: Add more mutation testing
Recently we introduced some mutation testing to the `pow` module but
testing is never done - add more `mutate` attributes and add unit tests
to ensure all mutants are killed.

Of note, the `from_compact` and `to_compact_lossy` functions are not
done, doing so results in a bunch of surviving mutants.
2023-01-13 08:23:14 +11:00
Tobin C. Harding 948b04927d
Remove unnecessary parenthesis
Plain integer does not require parenthesis, remove them.
2023-01-13 08:22:45 +11:00
Tobin C. Harding c3021d852a
Fix typo in code comment
Fix a trivial grammatical mistake in code comment.
2023-01-13 08:21:34 +11:00
Martin Habovstiak 1b0988833a Remove `ToHex`
The `ToHex` trait was replaced by either simple `Display`/`LowerHex`
where appropriate or `DisplayHex` from `bitcoin_internals` which is
faster.

This change replaces the usages and removes the trait.
2023-01-07 19:50:03 +01:00
Tobin C. Harding cf9733d678 Verify and fix mul_u64
Add kani verification for `U256::mul_u64`, doing so uncovered a bug in
the current implementation due to overflow.

Re-write the `mul_u64` method.

Props to Elichai for the algorithm.

Co-authored-by: Elichai Turkel <elichai.turkel@gmail.com>
2022-12-30 09:39:41 +11:00
Tobin C. Harding 2e79a0bdc4 Introduce mutation testing
Introduce mutation testing by way of mutagen [0]

- Conditionally add the dev-dependency `mutagen` (using `do_mutate`
flag)

 This flag is not very well named but `mutagen` and `mutate` are already
 taken?

- Mutate all methods of the `U256` struct that do not require additional
  unit tests.

 Uses `cfg(all(test, do_mutate), mutate)` - I cannot workout why we need
 to check on `test` as well i.e., I don't understand why we cannot use
 `cfg(do_mutate, mutate)`?

With this applied test can be run as usual with a stable toolchain. To
run mutagen we use `RUSTFLAGS='--cfg=do_mutate' cargo +nightly mutagen`.

[0] https://github.com/llogiq/mutagen
2022-12-23 08:23:49 +11:00
connormullett e00dfa9806 impl FromHexStr for structs with single u32 member
Adds new module `string` to be later converted to its own
crate. The module currently contains the FromHexStr trait and an error
type to be used for implementing hex parsing on types. This change
also adds implementations of FromHexStr for types with a single u32
member such as `Sequence(pub u32)`. All structs that match the
following regex have been given this implementation
`\(u32\)` and `\(pub u32\)`. All implementations have associated
unit tests matching all possible cases. NonStandardSighashType has
been ommitted from this change as it is an error and should not be
constructed using the methods added in this change.

Adds parse::hex_u32 for future use to be made generic to allow
different sizes of integers to be parsed from hex strings.

The error type FromHexError implements required traits such as
Display and std::error::Error
2022-12-11 00:01:58 -05:00
Jiri Jakes df90c50242 Add log2 to Work
Bitcoin Core displays log2 of chain work in certain situations. This
new log2 method returns equivalent value.
2022-12-03 17:24:41 +01:00
Tobin C. Harding 7e146ede96 Make types in block module more terse
Currently the types in the block module have longer names than
necessary, "header" and "version" identifiers contain the word "block",
this is unnecessary because we can write `block::Header` instead of
`BlockHeader` when context is required. This allows us to use the naked
type `Header` inside the `block` module with no loss of clarity.

We are stuck with `BlockHash` because the type is defined along with all
the other hash types in `hash_types`, leave it as is for now but
re-export it from the `block` module to assist in putting types that are
used together in scope in the same place, making import statements more
ergonomic.
2022-11-06 06:54:12 +11:00
Tobin C. Harding 624cda07b3 Remove unnecessary casts
Clippy emits a bunch of warnings of form:

 warning: casting to the same type is unnecessary ...

As suggested, remove the unnecessary casts.
2022-11-04 11:44:23 +11:00
Tobin C. Harding 02a2b43b2b Remove Default impl for Target and Work
A zero default value for `Target` and `Work` has no significance and/or
usecase, remove the derived `Default` implementation.

Includes making `Target::ZERO` public.
2022-09-30 12:02:06 +10:00
Tobin C. Harding cb9893c4a9 Add Target and Difficulty types
Currently we use the `Uint256` type to represent two proof of work
integers, namely target and difficulty (work).

It would be nice to not have a public integer type that is not fully
implemented (i.e., does not implement arithmetic etc as do integer types
in stdlib). Instead of implementing all the stdlib functions we can
instead add two new wrapper types, since these are not general purpose
integers they do not need to implement anything we do not need to use.

- Add a `pow` module.
- Put a modified version of `Uint256` to `pow`.
- Add two new wrapper types `Target` and `Difficulty`.
- Only implement methods that we use on each type.

Note this patch does not remove the original `Uint256`, that will be
done as a separate patch.
2022-09-28 04:16:59 +10:00