Skip to content

Commit

Permalink
Starting with implementation of sha256sum for block header and transa…
Browse files Browse the repository at this point in the history
…ctions
  • Loading branch information
gabrielgusn committed Apr 24, 2024
1 parent cd49954 commit f22304e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 26 deletions.
13 changes: 11 additions & 2 deletions src/block/block.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
#![allow(dead_code, unused)]

use super::block_header::BlockHeader;

use crate::transactions::tx::{Tx};

#[derive(Debug)]
struct Block{
block_header: BlockHeader,
// TO DO: still need to define how we will represent the transactions in the block
transactions: String
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();
}

return block_size;
}
}
25 changes: 13 additions & 12 deletions src/block/block_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use core::fmt;
// chrono::... is being used for the `timestamp` value in the header
use chrono::{DateTime, Utc};
use sha2::{Sha256, Digest};

/* As we use ::fmt, we need the Debug trait in the struct and it can be automatically
implemented using derive()
Expand All @@ -32,16 +33,6 @@ pub struct BlockHeader {

impl BlockHeader {
pub fn new(block_id: String, txs_merkle_root: String, timestamp: DateTime<Utc>, nonce: u32) -> Self {
// I did not find a better way of representing the block id and merkle root other than
// String for now, so it uses the following control flows to avoid having "hashes"
//different than 32 bytes.
if block_id.len() != 32 {
panic!("The block_id parameter needs to be a 32 bytes length string");
}
if txs_merkle_root.len() != 32 {
panic!("The txs_merkle_root parameter needs to be a 32 bytes length string");
}

Self {
block_id,
txs_merkle_root,
Expand All @@ -51,9 +42,19 @@ impl BlockHeader {
}
}

pub fn get_block_header_sha256sum() -> String{
String::from("Hello World")
pub fn get_block_header_sha256sum(&self) -> String{
let serialized_header = format!("{:?}", self);
let mut hasher = Sha256::new();

hasher.update(serialized_header.as_bytes());

let result = hasher.finalize();

let hash_string = result.iter().map(|byte| format!("{:02x}", byte)).collect::<String>();

return hash_string;
}

}

impl fmt::Display for BlockHeader {
Expand Down
10 changes: 1 addition & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@ pub fn read_mempool(path: &str){

let tx = read_tx_from_file("/home/gabriel/projects/bitcoin-mining-challenge/mempool/0.json");

println!("================================");
println!("Transaction size = {}", tx.get_tx_size_in_bits());
println!("Input size = {}", tx.get_tx_input_vec_size_in_bits());
println!("Output size = {}", tx.get_tx_output_vec_size_in_bits());
println!("================================");

tx.tx_input[0].get_tx_input_size_in_bits();

// for file in files{
// let file_path = path.to_string() + &file;
// // let file: File = File::create(&file_path).unwrap();
Expand All @@ -46,7 +38,7 @@ pub fn read_tx_from_file(file_path: &str) -> Tx {
file.read_to_string(&mut file_content).expect("File can not be read");
// let contents = fs::read_to_string(file_path)
// .expect("Error while reading file");
let error_in_file_message:String = String::from("Error while parsing json to Tx in file ") + file_path.clone();
let error_in_file_message:String = String::from("Error while parsing json to Tx in file ") + file_path;

let tx_in_json: Tx = serde_json::from_str(&file_content).expect(&error_in_file_message);
// .expect("Error parsing file content to JSON");
Expand Down
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use block::block_header::BlockHeader;
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 @@ -17,9 +18,14 @@ 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);

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>());

let data: &str = "texto 1234";
Expand Down
2 changes: 0 additions & 2 deletions src/transactions/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ impl Tx{

let hash_string = result.iter().map(|byte| format!("{:02x}", byte)).collect::<String>();

println!("{}", serialized_tx);

return hash_string;
}

Expand Down

0 comments on commit f22304e

Please sign in to comment.