diff --git a/mine-your-first-block/src/main.rs b/mine-your-first-block/src/main.rs index 5e32378..f01b84d 100644 --- a/mine-your-first-block/src/main.rs +++ b/mine-your-first-block/src/main.rs @@ -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; @@ -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 = Vec::new(); let mut fees: Vec = Vec::new(); @@ -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); @@ -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(); @@ -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; - } } } \ No newline at end of file