From b311e966032b95a9448a58d9c5f4a0a29ce38694 Mon Sep 17 00:00:00 2001 From: yancy Date: Wed, 8 Mar 2023 12:07:18 +0100 Subject: [PATCH] Fix weight subtract bug --- bitcoin/src/blockdata/weight.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/bitcoin/src/blockdata/weight.rs b/bitcoin/src/blockdata/weight.rs index 7bccaf2c..af45e657 100644 --- a/bitcoin/src/blockdata/weight.rs +++ b/bitcoin/src/blockdata/weight.rs @@ -86,7 +86,7 @@ impl Weight { /// /// Computes `self - rhs` returning `None` if overflow occurred. pub fn checked_sub(self, rhs: Self) -> Option { - self.0.checked_add(rhs.0).map(Self) + self.0.checked_sub(rhs.0).map(Self) } /// Checked multiplication. @@ -115,6 +115,20 @@ impl fmt::Display for Weight { } } +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn checked_sub_test(){ + let result = Weight(1).checked_sub(Weight(1)).expect("expected weight unit"); + assert_eq!(Weight::ZERO, result); + + let result = Weight::MIN.checked_sub(Weight(1)); + assert_eq!(None, result); + } +} + impl From for u64 { fn from(value: Weight) -> Self { value.to_wu()