From 7144bc2f348b0eb4e2f88438d36868ecd08b5232 Mon Sep 17 00:00:00 2001 From: Robert Kornacki <11645932+robkorn@users.noreply.github.com> Date: Mon, 22 Jul 2019 14:14:47 -0400 Subject: [PATCH] Implemented PoW validity checking & test --- src/proof_of_work.rs | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/proof_of_work.rs b/src/proof_of_work.rs index 09c40f8..183a6e8 100644 --- a/src/proof_of_work.rs +++ b/src/proof_of_work.rs @@ -61,6 +61,13 @@ impl PoW { pub fn calculate_serialized(&self, target: &[u8]) -> u128 { score(Sha256::new().chain(SALT).chain(target), self.nonce) } + + pub fn is_valid_proof(&self, t: &T) -> bool { + if self.result == self.calculate(t).unwrap() { + return true; + } + return false; + } } fn score(prefix_sha: Sha256, nonce: u128) -> u128 { @@ -94,18 +101,29 @@ mod test { #[test] fn base_functionality() { // Let's prove we did work targeting a phrase. - let phrase = b"Corver bandar palladianism retroform.".to_vec(); + let phrase = b"Ex nihilo nihil fit.".to_vec(); let pw = PoW::prove_work(&phrase, DIFFICULTY).unwrap(); assert!(pw.calculate(&phrase).unwrap() >= DIFFICULTY); + assert!(pw.is_valid_proof(&phrase)); } #[test] fn double_pow() { - let phrase = "Corver bandar palladianism retroform.".to_owned(); - let PoW = PoW::prove_work(&phrase, DIFFICULTY).unwrap(); - let PoWPoW: PoW> = PoW::prove_work(&PoW, DIFFICULTY).unwrap(); - assert!(PoW.calculate(&phrase).unwrap() >= DIFFICULTY); - assert!(PoWPoW.calculate(&PoW).unwrap() >= DIFFICULTY); + let phrase = "Ex nihilo nihil fit.".to_owned(); + let pw = PoW::prove_work(&phrase, DIFFICULTY).unwrap(); + let pwpw: PoW> = PoW::prove_work(&pw, DIFFICULTY).unwrap(); + assert!(pw.calculate(&phrase).unwrap() >= DIFFICULTY); + assert!(pwpw.calculate(&pw).unwrap() >= DIFFICULTY); + } + + #[test] + fn is_not_valid_proof() { + let phrase = "Ex nihilo nihil fit.".to_owned(); + let phrase2 = "Omne quod movetur ab alio movetur.".to_owned(); + let pw = PoW::prove_work(&phrase, DIFFICULTY).unwrap(); + let pw2 = PoW::prove_work(&phrase2, DIFFICULTY).unwrap(); + assert!(!pw.is_valid_proof(&phrase2)); + assert!(!pw2.is_valid_proof(&phrase)); } #[test]