feat: data and hash time cost added to the chain

This commit is contained in:
Ahmet Kaan GÜMÜŞ 2024-05-22 04:23:43 +03:00
parent eca7a6e50a
commit 5111196588
3 changed files with 23 additions and 8 deletions

View file

@ -1,3 +1,5 @@
use std::time::{Duration, Instant};
use chrono::Utc; use chrono::Utc;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sha3::{Digest, Sha3_512}; use sha3::{Digest, Sha3_512};
@ -8,9 +10,11 @@ use crate::blockchain::BlockChain;
pub struct Block { pub struct Block {
pub index: u64, pub index: u64,
pub timestamp: u64, pub timestamp: u64,
pub data: String,
pub proof_of_work: u64, pub proof_of_work: u64,
pub previous_hash: String, pub previous_hash: String,
pub hash: String, pub hash: String,
pub hash_time_cost: Duration,
} }
impl Block { impl Block {
@ -24,13 +28,15 @@ impl Block {
format!("{:x}", hash) format!("{:x}", hash)
} }
pub fn new(index: u64, previous_hash: String) -> Self { pub fn new(index: u64, data: String, previous_hash: String, instant: Instant) -> Self {
Block { Block {
index, index,
timestamp: Utc::now().timestamp_millis() as u64, timestamp: Utc::now().timestamp_millis() as u64,
data,
proof_of_work: 0_u64, proof_of_work: 0_u64,
previous_hash, previous_hash,
hash: String::new(), hash: String::new(),
hash_time_cost: instant.elapsed(),
} }
} }

View file

@ -1,3 +1,5 @@
use std::time::{Duration, Instant};
use chrono::Utc; use chrono::Utc;
use crate::block::Block; use crate::block::Block;
@ -14,9 +16,11 @@ impl BlockChain {
let genesis_block = Block { let genesis_block = Block {
index: 0, index: 0,
timestamp: Utc::now().timestamp_millis() as u64, timestamp: Utc::now().timestamp_millis() as u64,
data: String::from("Tahinli"),
proof_of_work: 0_u64, proof_of_work: 0_u64,
previous_hash: String::new(), previous_hash: String::new(),
hash: String::new(), hash: String::new(),
hash_time_cost: Duration::from_secs(0),
}; };
let chain = vec![genesis_block.clone()]; let chain = vec![genesis_block.clone()];
@ -28,10 +32,12 @@ impl BlockChain {
} }
} }
pub fn add_block(&mut self) { pub fn add_block(&mut self, data: String, instant: Instant) {
let new_block = Block::new( let new_block = Block::new(
self.chain.len() as u64, self.chain.len() as u64,
data,
self.chain[&self.chain.len() - 1].hash.clone(), self.chain[&self.chain.len() - 1].hash.clone(),
instant,
) )
.mine(self.clone()); .mine(self.clone());
self.chain.push(new_block); self.chain.push(new_block);

View file

@ -8,14 +8,17 @@ fn main() {
let difficulty = 1; let difficulty = 1;
let mut blockchain = BlockChain::new(difficulty); let mut blockchain = BlockChain::new(difficulty);
let time = Instant::now(); let instant = Instant::now();
BlockChain::add_block(&mut blockchain); BlockChain::add_block(&mut blockchain, "T".to_string(), Instant::now());
BlockChain::add_block(&mut blockchain); BlockChain::add_block(&mut blockchain, "a".to_string(), Instant::now());
BlockChain::add_block(&mut blockchain); BlockChain::add_block(&mut blockchain, "h".to_string(), Instant::now());
BlockChain::add_block(&mut blockchain); BlockChain::add_block(&mut blockchain, "i".to_string(), Instant::now());
BlockChain::add_block(&mut blockchain, "n".to_string(), Instant::now());
BlockChain::add_block(&mut blockchain, "l".to_string(), Instant::now());
BlockChain::add_block(&mut blockchain, "i".to_string(), Instant::now());
println!( println!(
"\t ⛏️⛏️⛏️ | Mined | ⛏️⛏️⛏️\n\n\tElapsed: {:?}\n\n{:#?}", "\t ⛏️⛏️⛏️ | Mined | ⛏️⛏️⛏️\n\n\tElapsed: {:?}\n\n{:#?}",
time.elapsed(), instant.elapsed(),
blockchain blockchain
); );
} }