Conversation
434c92d to
c1ad58e
Compare
vincenzopalazzo
left a comment
There was a problem hiding this comment.
I added a couple of comments, but the code looks good to me
|
I just realized there's an alternative approach to this that bypasses the pub struct BlockHeaderValidator {
// ...
}
impl BlockHeaderValidator {
/// Creates the validator using genesis block of the network
pub fn new(network: Network) -> Self { /* ... */ }
/// Creates the validator for specific network starting from given block header
pub fn from_params(first_header: &Header, params: Params) -> Self { /* ... */ }
/// Tries to advance the validation by attaching the provided header.
///
/// On success the new header is attached, on error the state is unchanged.
pub fn next_header(&mut self, header: &Header) -> Result<(), ValidationError> { /* ... */ }
}Then not only would downstream users not have to implement retargeting but also avoid most of the validation work. The only thing I'm unsure about is how to handle reorgs. Maybe clone the validator and verify each branch in parallel? |
c1ad58e to
f9e5491
Compare
@Kixunil can we just do it as is for now so we can get the backport done and then look into your suggestion after that? |
|
cc @vincenzopalazzo because I wrote your name in the PR description without an |
vincenzopalazzo
left a comment
There was a problem hiding this comment.
Ok the code is fine for me, and thanks to allow more basic operation of some basic rust-bitcoin types
However, the 1.48 is complaining
Checking secp256k1 v0.28.0
error[E0599]: no method named `saturating_div` found for type `u32` in the current scope
--> bitcoin/src/blockdata/locktime/absolute.rs:525:28
|
525 | Some(Height(self.0.saturating_div(rhs)))
| ^^^^^^^^^^^^^^ help: there is an associated function with a similar name: `saturating_add`
error[E0658]: use of unstable library feature 'clamp'
--> bitcoin/src/pow.rs:1009:29
|
1009 | let timespan = timespan.clamp(min_timespan, max_timespan);
| ^^^^^
|
= note: see issue #44095 <https://github.com/rust-lang/rust/issues/44095> for more information
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0599, E0658.
For more information about an error, try `rustc --explain E0599`.
error: could not compile `bitcoin`
To learn more, run the command again with --verbose.
warning: build failed, waiting for other jobs to finish...
error[E0599]: no method named `saturating_div` found for type `u32` in the current scope
--> bitcoin/src/blockdata/locktime/absolute.rs:525:28
|
525 | Some(Height(self.0.saturating_div(rhs)))
| ^^^^^^^^^^^^^^ help: there is an associated function with a similar name: `saturating_add`
error[E0658]: use of unstable library feature 'clamp'
--> bitcoin/src/pow.rs:1009:29
|
1009 | let timespan = timespan.clamp(min_timespan, max_timespan);
| ^^^^^
|
= note: see issue #44095 <https://github.com/rust-lang/rust/issues/44095> for more information
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0599, E0658.
For more information about an error, try `rustc --explain E0599`.
error: build failed
I am worried if there is really someone that it is using this version anymore? As a rust compiler contributor I do not feel that keep an older version of rustic is a good way these days. However, I can miss some internal usage that I am not aware of
|
@tcharding I'm not happy about releasing things we intend to break soon but if it's blocking someone that's acceptable to me. |
|
@Kixunil well, we have released broken code (or at least, code which is missing functionality in the last release, and which wasn't missing that functionality before). So the "don't release APIs we intend to replace" ship has sailed and we might as well patch it up so that it's usable. Glad it's acceptable to you. |
|
@apoelstra I mean I would've preferred to do it right if we have an idea what the right way is but if it takes much longer and is urgent then I'm fine. |
|
The backport at least is urgent. Arguably we don't need to do the fix on master. But I don't like the idea of backporting a change that doesn't also appear on master. |
I agree. |
Turns out LDK may not actually need this, read the message yesterday. I want to dig into it today personally to work out exactly what we are doing re backporting for 0.30 and 0.31, |
To improve ergonomics it would be good to support a subset of integer
operations that make sense for the two locktime heights.
- Add `checked_*` and `saturating_*` functions for add, sub, div, mul
- Add functions to increment (add 1) and decrement (sub 1) for each
`Height` type. Call through to the respective `saturating_` function.
- Implement `ops::*` traits, `Add`, `Mul`, `Sub`, `Div` (as well as
`AddAssign` and friends).
It makes sense to add a number of blocks onto both absolute and relative
heights so we implement, for example:
- `ops::Add<u32> for Height`
- `ops::Add<Height> for u32`
- `ops::AddAssign<u32> for Height`
It also makes sense to add a `relative::Height` to an `absolute::Height`
but not two absolute heights together. so we implement:
- relative height:
- `ops::Add for Height`
- `ops::AddAssign for Height`
- absolute height:
- `ops::Add<relative::Height> for Height`
- `ops::AddAssign<relative::Height> for Height`
Same for other operations.
We can implement `Add<u16>` and since the max value of
`relative::Height` and `u16` is the same we get the same unsurprising
behavior in relation to overflow and panic.
For the `absolute` type `absolute::Height::MAX` is less that the inner
ints max value.
As suggested here:
rust-bitcoin#1639 (comment)
implement `ops` traits by unconditionally panicing if overflow occurs.
Done as part of rust-bitcoin#1639 and also to support the calculate next target stuff.
Add two functions to the `pow` module for calculating the next target. These functions are based on Bitcoin Core functions in `src/pow.cpp`: - GetNextWorkRequired - CalculateNextWorkRequired
f9e5491 to
39721b5
Compare
|
Yeah, they don't need it but it'd still be nice to have eventually. I'm completely fine with de-prioritizing. |
|
There is any update on this? I am kindly stuck in updating the recent bitcoin version on Nakamoto and this is blocking several projects like bdk and liana Also if it is not a priority for the team, @tcharding could you give me some estimation? otherwise, I will resurrect my work on it |
|
@vincenzopalazzo IIUC the recently released version with methods to compute min and max target fix lightning at least, do the other crates need something more? If yes, then I'm in favor of bumping priority here. |
Que? |
|
This is just blocked by the required brain power to do #2181, if you need it I can prioritise that one. |
|
|
||
| let adjustment_interval = params.difficulty_adjustment_interval() as u32; | ||
| // Go back by what we want to be 14 days worth of blocks | ||
| let height_first = current_height.saturating_sub(adjustment_interval - 1); |
There was a problem hiding this comment.
This line here hides the off-by-one error mentioned in #2740 (I think).
47dc4a3 feat(pow): add difficulty adjustment calculation (Rob N) Pull request description: Hi, I hit a roadblock with the current `pow` API. As far as I can tell, the only workaround to calculate the next work required similar to `bitcoin/src/pow.cpp` is to use a general big integer library, convert the `Target` to bytes, do the math, and convert back to `Target` from bytes. I have also been working with [Floresta](https://github.com/Davidson-Souza/Floresta/tree/780ea8d0b024eb69ccb723ccce9b4e703b143bfd) and their [solution](https://github.com/Davidson-Souza/Floresta/blob/780ea8d0b024eb69ccb723ccce9b4e703b143bfd/crates/floresta-chain/src/pruned_utreexo/consensus.rs#L187) was to fork off and exposed the `U256` struct publicly on their branch. I think these home brewed difficulty adjustment solutions will continually pop up, so I created a `from_next_work_required` method to return a `Target`. My work veers significantly from #2180, as I only provided a single method to do so, without further guidance on when exactly this retarget occurs. I am happy to add tests once I get further direction from maintainers if this as a likelihood of being accepted or not. Thanks. ACKs for top commit: tcharding: ACK 47dc4a3 apoelstra: ACK 47dc4a3 used range-diff Tree-SHA512: 6d627ce698361afed61c8f2a12a1a96371a7a93118e08a91dae250de4f23d65c615d2654d37d2699c88b7c22f6e4bc2a1195f963c15512d7c0d041498f02dc41
|
Replaced by #2740 |
Draft because patch 1 was pulled out into #2181 - still to be resolved.
This PR is taken over from #2090 with permission of vincenzopalazzo [0]
[0] no mention so as not to spam notifications as this progresses.
Fix: #1289