diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2025-10-21 09:05:16 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2025-10-21 09:05:16 +0800 |
| commit | 796f1f2f607f457ca4cd834c39a49f0e346d8514 (patch) | |
| tree | 85356e7ea42041ac408c125d368bb51b7a5ccc81 /src/blocks.rs | |
| parent | 87814a5e33375a4118b75fce01f8a7c53ba39a37 (diff) | |
network: verify requests
Diffstat (limited to 'src/blocks.rs')
| -rw-r--r-- | src/blocks.rs | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/src/blocks.rs b/src/blocks.rs index 39abde2..9c52d71 100644 --- a/src/blocks.rs +++ b/src/blocks.rs @@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize}; use crate::{ZERO_PREFIX_AMOUNT, transaction::Transaction}; -#[derive(Deserialize, Serialize, Clone, Copy)] +#[derive(Deserialize, Serialize, Clone, Copy, PartialEq)] pub struct Block { pub prev_hash: u64, pub trans: Transaction, @@ -42,6 +42,19 @@ impl Block { unreachable!() } + pub fn verify_pow(&mut self, pow: u64) -> bool { + let mut hasher = DefaultHasher::new(); + (&self.prev_hash, &self.trans, pow).hash(&mut hasher); + hasher + .finish() + .to_string() + .to_string() + .starts_with( + &"0".repeat(ZERO_PREFIX_AMOUNT) + .to_string(), + ) + } + pub fn calc_set_pow(&mut self) { self.pow = Some(self.calc_pow()); } @@ -65,7 +78,7 @@ impl Hash for Block { } } -#[derive(Default, Deserialize, Serialize, Clone)] +#[derive(Default, Deserialize, Serialize, Clone, PartialEq)] pub struct BlockChain { blocks: Vec<Block>, } |
