Skip to content

Commit

Permalink
Adding some comments to the block_header module and changing the data…
Browse files Browse the repository at this point in the history
… type of the sha256 variables
  • Loading branch information
gabrielgusn committed Apr 15, 2024
1 parent cd2fbb8 commit bfbf8b7
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/block/block_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,21 @@ use chrono::{DateTime, Utc};
*/
#[derive(Debug)]
pub struct BlockHeader {
pub block_id: String,
pub txs_merkle_root: String,
// block_id is intended to store the sha256 hash of the previous block's header, as in bitcoin
pub block_id: String, //[u8; 32],
// the block's transactions merkle root, intended also to store a sha256 hash
// both the block_id and txs_merkle_root content is garanteed to store a 32 bytes value
// in the "new()" method
pub txs_merkle_root: String, //[u8; 32],
// intended to include information about the time the block was mined
pub timestamp: DateTime<Utc>,
pub nonce: u32
pub target_hash: String, //[u8; 32],
// value to be adjusted in order to find the pre image for the sha256 hash below the target
pub nonce: u32,
}

impl BlockHeader {
pub fn new(block_id: String, txs_merkle_root: String, timestamp: DateTime<Utc>, nonce: u32) -> 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.
Expand All @@ -35,13 +42,18 @@ impl BlockHeader {
panic!("The txs_merkle_root parameter needs to be a 32 bytes length string");
}

BlockHeader {
Self {
block_id,
txs_merkle_root,
txs_merkle_root,
target_hash: String::from("0000ffff00000000000000000000000000000000000000000000000000000000"),
timestamp,
nonce
}
}

pub fn get_block_header_sha256sum() -> String{
String::from("Hello World")
}
}

impl fmt::Display for BlockHeader {
Expand Down

0 comments on commit bfbf8b7

Please sign in to comment.