Skip to content

Commit

Permalink
fix: Use same work calculation as Bitcoin
Browse files Browse the repository at this point in the history
  • Loading branch information
reednaa committed Oct 28, 2024
1 parent 874920a commit cdb5905
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/BtcPrism.sol
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,12 @@ contract BtcPrism is IBtcPrism {
unchecked {

uint256 target = periodToTarget[period];
uint256 workPerBlock = (2**256 - 1) / target;
// unchecked: The maximum target is around 2^224 < 2**256
// ~target / (target + 1) is type(uint256).max if target == 0
// but target > 1 => ~target / (target + 1) < type(uint256).max
// => ~target / (target + 1) + 1 <= type(uint256).max
// Target >= 1 is checked on the bitcoin level.
uint256 workPerBlock = ~target / (target + 1) + 1;

// unchecked: period is not raw from input but parsed as newHeight/2016.
// as such, we can multiply it by 2016.
Expand Down

0 comments on commit cdb5905

Please sign in to comment.