Skip to content

Commit

Permalink
feat: add checked_sub (#6628)
Browse files Browse the repository at this point in the history
Description
---
Add checked_sub implementation for accumulated difficulty struct. 

Motivation and Context
---
In a normal block chain world, you would never subtract difficulty, but
in P2pool you only count the last X blocks. This means that when a block
falls away and is not used anymore, we need to subtract its difficulty.
  • Loading branch information
SWvheerden authored Oct 14, 2024
1 parent 186d0d5 commit 2d0ccb1
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions base_layer/core/src/proof_of_work/accumulated_difficulty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ impl AccumulatedDifficulty {
self.0.checked_add(u128::from(d.as_u64())).map(AccumulatedDifficulty)
}

pub fn checked_sub_difficulty(&self, d: Difficulty) -> Option<AccumulatedDifficulty> {
self.0.checked_sub(u128::from(d.as_u64())).map(AccumulatedDifficulty)
}

pub fn to_be_bytes(&self) -> Vec<u8> {
self.0.to_be_bytes().to_vec()
}
Expand Down

0 comments on commit 2d0ccb1

Please sign in to comment.