From 006fe61b5ca5bb879c25427d535897c913675078 Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Sun, 11 Aug 2024 14:42:52 +0700 Subject: [PATCH] Update block.rs --- pi-nexus-blockchain/src/block.rs | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/pi-nexus-blockchain/src/block.rs b/pi-nexus-blockchain/src/block.rs index 654a2d383..b1877faf8 100644 --- a/pi-nexus-blockchain/src/block.rs +++ b/pi-nexus-blockchain/src/block.rs @@ -1,19 +1,25 @@ -// block.rs (updated) +// block.rs (update) -// ... +use crate::transaction::Transaction; -impl BlockHeader { - // ... +pub struct Block { + pub index: u64, + pub previous_hash: String, + pub timestamp: u64, + pub transactions: Vec, + pub hash: String, +} - pub fn mine(&mut self, difficulty_target: u64) { - let mut nonce = 0; - loop { - self.nonce = nonce; - let hash = self.hash(); - if hash.starts_with(&format!("{:0>64}", difficulty_target)) { - break; - } - nonce += 1; +impl Block { + pub fn new(index: u64, previous_hash: &str, transactions: Vec) -> Self { + let timestamp = crate::utils::get_current_timestamp(); + let hash = crate::utils::calculate_block_hash(index, previous_hash, &transactions, timestamp); + Block { + index, + previous_hash: previous_hash.to_string(), + timestamp, + transactions, + hash, } } }