-
Notifications
You must be signed in to change notification settings - Fork 5
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
Bitcoin Headerchain verification #259
Conversation
Codecov ReportAttention: Patch coverage is
@@ Coverage Diff @@
## master #259 +/- ##
==========================================
+ Coverage 58.47% 59.10% +0.63%
==========================================
Files 123 124 +1
Lines 13618 13546 -72
==========================================
+ Hits 7963 8007 +44
+ Misses 5655 5539 -116
|
57fa303
to
1b876da
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.
A bit scared by seemingly reimplementing Bitcoin validation logic here, would like it if we looked into being able to reuse the logic from the bitcoin-pow
crate or another one. There was another crate that I saw that had no-std versions of all of these, but I don't remember the name now.
Also the inconsistency between the names of the bitcoin-blockspace
and btc-headerchain
crate names is a bit weird, was there a logic to this?
/// Difficulty recalculation interval. | ||
/// On [MAINNET](bitcoin::consensus::params::MAINNET), it is around 2 weeks | ||
const POW_TARGET_TIMESPAN: u32 = 14 * 24 * 60 * 60; | ||
|
||
/// Expected amount of time to mine one block. | ||
/// On [MAINNET](bitcoin::consensus::params::MAINNET), it is around 10 minutes | ||
const POW_TARGET_SPACING: u32 = 10 * 60; | ||
|
||
/// No of blocks after which the difficulty is adjusted. | ||
/// [bitcoin::consensus::params::Params::difficulty_adjustment_interval]. | ||
const DIFFICULTY_ADJUSTMENT_INTERVAL: u32 = POW_TARGET_TIMESPAN / POW_TARGET_SPACING; |
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 should all be configurable for whichever Bitcoin chain we're using, perhaps again with env!
.
|
||
/// Total accumulated [difficulty](bitcoin::pow::Target::difficulty_float) | ||
/// TODO: check if using [this](bitcoin::pow::Target::difficulty) makes more sense | ||
pub total_accumulated_pow: f64, |
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.
🚩
Do the other Rust Bitcoin crates used f64
? We so want to avoid using floats if possible, this isn't natively supported in the riscv arch that r0 or sp1 supports so this would be a huge prover cost to use if it's being emulated by the codegen. Plus floats are screwy in general.
It looks like this isn't being used, what's it for?
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.
total_accumulated_pow
is used as:
L148: self.total_accumulated_pow += header.difficulty_float();
This will later used in the Challenge-Response game to determine the right fork of the bitcoin. The chain with highest accumulated PoW is considered valid.
f64
was used because rust-bitcoin
already had a function internally to convert difficulty to f64 and this seems to be how the difficulty was displayed in the UI.
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.
Maybe add a prefix to this like the other one so it's clear what it's for. Was this extracted with a tool? If it's something bespoke then add it to a new contrib
directory.
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.
Sure I'll attach the prefix! I wrote a simple binary to fetch the block headers.
I looked into rust-bitcoin, which seems to have the logic but is was not yet released. I didn't look into other crates for this. Will checkout bitcoin-pow. Edit: bitcoin-pow impl requires BlockIndex which has a lot of redundant fields. Thanks for pointing the inconsistency out. |
I will try to resolve all the comments here in PR #268 |
Description
Implements code for block header verification.
The chain of block headers provides everything a light client needs to validate the proof of work and the correct arrangement of blocks in the chain. When receiving a block header the light client checks it against Bitcoin's consensus rules:
4.The correct target is encoded in the block. In case a retarget happened the new target was correctly derived from the epoch timestamps.
While rules 1 and 2 directly enforce the correct formation and mining of the chain, rules 3 and 4 are required so that no one can adjust the target to their own will. Otherwise, they were able to mine new blocks at a faster rate than honest nodes by increasing the target (which can be done by decreasing the epoch's timestamp delta) or they could decrease the target to make it artificially harder to mine blocks.
Type of Change
Checklist
Related Issues