From df3af977e0310c4a28c1fed2125b8bf41adf0755 Mon Sep 17 00:00:00 2001 From: Robert Kornacki <11645932+robkorn@users.noreply.github.com> Date: Tue, 23 Jul 2019 12:08:19 -0400 Subject: [PATCH] Added is_sufficient_difficulty fn & tests. --- src/lib.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0c312f8..6d92b18 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -66,6 +66,11 @@ impl PoW { } return false; } + + /// Checks if the PoW result is of sufficient difficulty + pub fn is_sufficient_difficulty(&self, target_diff: u128) -> bool { + return self.result >= target_diff; + } } fn score(prefix_sha: Sha256, nonce: u128) -> u128 { @@ -104,6 +109,7 @@ mod test { assert!(pw.calculate(&phrase).unwrap() >= DIFFICULTY); assert!(pw.result >= DIFFICULTY); assert!(pw.is_valid_proof(&phrase)); + assert!(pw.is_sufficient_difficulty(DIFFICULTY)); } #[test] @@ -113,8 +119,8 @@ mod test { let pwpw: PoW> = PoW::prove_work(&pw, DIFFICULTY).unwrap(); assert!(pw.calculate(&phrase).unwrap() >= DIFFICULTY); assert!(pwpw.calculate(&pw).unwrap() >= DIFFICULTY); - assert!(pw.result >= DIFFICULTY); - assert!(pwpw.result >= DIFFICULTY); + assert!(pw.is_sufficient_difficulty(DIFFICULTY)); + assert!(pwpw.is_sufficient_difficulty(DIFFICULTY)); assert!(pw.is_valid_proof(&phrase)); assert!(pwpw.is_valid_proof(&pw)); }