From 2d0ccb121bb13990cac422fe72945cced06c1f74 Mon Sep 17 00:00:00 2001 From: SW van Heerden Date: Mon, 14 Oct 2024 18:47:25 +0200 Subject: [PATCH] feat: add checked_sub (#6628) 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. --- base_layer/core/src/proof_of_work/accumulated_difficulty.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/base_layer/core/src/proof_of_work/accumulated_difficulty.rs b/base_layer/core/src/proof_of_work/accumulated_difficulty.rs index cd5646590b..6dcc2acd1f 100644 --- a/base_layer/core/src/proof_of_work/accumulated_difficulty.rs +++ b/base_layer/core/src/proof_of_work/accumulated_difficulty.rs @@ -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 { + self.0.checked_sub(u128::from(d.as_u64())).map(AccumulatedDifficulty) + } + pub fn to_be_bytes(&self) -> Vec { self.0.to_be_bytes().to_vec() }