Skip to content

Commit

Permalink
Implemented proof of work method in the block struct
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielgusn committed Apr 24, 2024
1 parent 25ebc4f commit ca50bc8
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 42 deletions.
65 changes: 49 additions & 16 deletions src/block/block.rs
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,
)
}
}
47 changes: 21 additions & 26 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
mod block;
mod transactions;
use block::block_header::BlockHeader;
use block::block::Block;
use chrono::Utc;
use mining_challenge::{read_mempool, read_tx_from_file};
use ring::digest::{Context, Digest, SHA256};
use sha2::digest::block_buffer::Block;
use std::fmt::Write;
// use hex_literal::hex;
use std::fs;
Expand All @@ -18,41 +18,36 @@ fn main() {

// let mut hasher = Sha256::new();
let first_block_header: BlockHeader = BlockHeader::new(String::from("00000000000000000000000000000000"), String::from("00000000000000000000000000000000"), Utc::now(), 128);
let second_block_header: BlockHeader = BlockHeader::new(first_block_header.get_block_header_sha256sum(), String::from("00000000000000000000000000000000"), Utc::now(), 256);
let block = Block::new(first_block_header, vec![]);

println!("=================================");
println!("{}", first_block_header);
println!("=================================");
println!("{}", second_block_header);
println!("=================================");
println!("1st Block Hash: {}", first_block_header.get_block_header_sha256sum());
println!("Size of Block Header: {}", std::mem::size_of::<BlockHeader>());
// println!("=================================");
// println!("{}", first_block_header);
// println!("=================================");
// println!("1st Block Hash: {}", first_block_header.get_block_header_sha256sum());
// println!("Size of Block Header: {}", std::mem::size_of::<BlockHeader>());

let mut block_vec: Vec<BlockHeader> = vec![];
let mut block_vec: Vec<Block> = vec![];

block_vec.push(first_block_header);
block_vec.push(block);

for i in 0..10{
println!("{}", i);
let block_header = BlockHeader::new(block_vec[i].get_block_header_sha256sum(), String::from("00000000000000000000000000000000"), Utc::now(), 0);
block_vec.push(block_header);

println!("Generating Block {}", i+1);

let block_header = BlockHeader::new(block_vec[i].block_header.get_block_header_sha256sum(), String::from("00000000000000000000000000000000"), Utc::now(), 0);

let new_block = Block::new(block_header, vec![]);
block_vec.push(new_block);
}

for block in &block_vec {

for mut block in block_vec {
println!("{}", block);
block.proof_of_work()
}

let mut nonce: u64 = 0_u64;

// for mut block in block_vec{
loop {
if block_vec[0].get_block_header_sha256sum() < block_vec[0].target_hash {
println!("Found the nonce for the target Hash! It is: {} and you can attach this block to the blockchain\n\t Block Hash is: {}", block_vec[0].nonce, block_vec[0].get_block_header_sha256sum());
break
}
println!("Not yet! ({})", block_vec[0].get_block_header_sha256sum());
block_vec[0].nonce += 1;
}
// for mut block in block_veu ec{

// }

// loop{
Expand Down

0 comments on commit ca50bc8

Please sign in to comment.