-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented proof of work method in the block struct
- Loading branch information
1 parent
25ebc4f
commit ca50bc8
Showing
2 changed files
with
70 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,59 @@ | ||
#![allow(dead_code, unused)] | ||
|
||
use super::block_header::BlockHeader; | ||
use crate::transactions::tx::{Tx}; | ||
|
||
use super::block_header::{self, BlockHeader}; | ||
use crate::transactions::{self, tx::Tx}; | ||
use core::fmt; | ||
#[derive(Debug)] | ||
struct Block{ | ||
block_header: BlockHeader, | ||
pub struct Block{ | ||
pub block_header: BlockHeader, | ||
// TO DO: still need to define how we will represent the transactions in the block | ||
transactions: Vec<Tx>, | ||
pub transactions: Vec<Tx>, | ||
} | ||
|
||
impl Block { | ||
fn get_block_header_hash() -> String { | ||
String::from("Hello World!") | ||
} | ||
|
||
pub fn get_block_size(&self) -> u64 { | ||
let mut block_size: u64 = 0; | ||
for tx in &self.transactions{ | ||
block_size += tx.get_tx_size_in_bits(); | ||
|
||
pub fn new(block_header: BlockHeader, transactions: Vec<Tx>) -> Self { | ||
Self{ | ||
block_header, | ||
transactions, | ||
} | ||
} | ||
|
||
fn get_block_header_hash() -> String { | ||
String::from("Hello World!") | ||
} | ||
|
||
pub fn get_block_size(&self) -> u64 { | ||
let mut block_size: u64 = 0; | ||
for tx in &self.transactions{ | ||
block_size += tx.get_tx_size_in_bits(); | ||
} | ||
|
||
return block_size; | ||
} | ||
|
||
pub fn proof_of_work(&mut self){ | ||
loop { | ||
if self.block_header.get_block_header_sha256sum() < self.block_header.target_hash { | ||
println!("\tFound the nonce for the target Hash! It is: {} and you can attach this block to the blockchain\n\t Block Hash is: {}", self.block_header.nonce, self.block_header.get_block_header_sha256sum()); | ||
break | ||
} | ||
// println!("Not yet! ({})", self.block_header.get_block_header_sha256sum()); | ||
self.block_header.nonce += 1; | ||
} | ||
} | ||
} | ||
|
||
return block_size; | ||
} | ||
impl fmt::Display for Block { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!( | ||
f, | ||
"Block ID: {}\nMerkle Root: {}\nTimestamp: {}\nNonce: {}\n\tTransactions: {:?}", | ||
self.block_header.block_id, | ||
self.block_header.txs_merkle_root, | ||
self.block_header.timestamp, | ||
self.block_header.nonce, | ||
self.transactions, | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters