Skip to content

Commit

Permalink
My last block said Block does not meet target difficulty so i edited …
Browse files Browse the repository at this point in the history
…main to verify the block_hash isn't > that target
  • Loading branch information
slanesuke committed Apr 12, 2024
1 parent 679074a commit bd09d78
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions mine-your-first-block/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -881,9 +881,6 @@ fn calculate_transaction_weight(tx: &Transaction) -> u64 {
}

fn main() {
// Clear output filee
std::fs::write("../output.txt", "").unwrap();

let mempool_path = "../mempool";

// Initialize nonce value;
Expand All @@ -892,9 +889,6 @@ fn main() {
// Get the valid txs from the mempool
let mut valid_tx = process_mempool(mempool_path).unwrap();

// Convert the valid txs into a vec of txids
//let valid_txs = valid_txs_to_vec(valid_tx);

// Calculate the total fees and get the txids
let mut valid_txids: Vec<String> = Vec::new();
let mut fees: Vec<u64> = Vec::new();
Expand Down Expand Up @@ -925,15 +919,15 @@ fn main() {
// Start Mining!
loop {
// Get the block header and serialize it
let block_header = construct_block_header(valid_txids.clone(), nonce);
let block_header = construct_block_header(sorted_txids.clone(), nonce);

let serialized_block_header = serialize_block_header(&block_header);

// Calculate the hash of the block header
let hash = calculate_hash(serialized_block_header.clone());
//println!("Nonce {}, Hash{}", nonce, hash);
let block_hash = calculate_hash(serialized_block_header.clone());

// Check if the hash meets the target
if hash_meets_difficulty_target(&hash) {
if hash_meets_difficulty_target(&block_hash) {

// Generate coinbase tx
let coinbase_tx = create_coinbase_tx(total_fees);
Expand All @@ -942,6 +936,9 @@ fn main() {
// coinbase txid
let coinbase_txid = double_sha256(serialized_cb_tx.as_bytes().to_vec());

// Clear the output file
fs::write("../output.txt", "").unwrap();

// Write the block header, coinbase tx, and txids to the output file
//append_to_file("../output.txt", &hash).unwrap();
append_to_file("../output.txt", &hex::encode(serialized_block_header)).unwrap();
Expand All @@ -955,12 +952,9 @@ fn main() {
append_to_file("../output.txt", txid).unwrap();
}
break;
} else {
nonce += 1;
}
nonce += 1;

if nonce == 0 {
println!("Exhausted all nonce. None were valid.");
break;
}
}
}

0 comments on commit bd09d78

Please sign in to comment.