Skip to content

Commit

Permalink
Refactor code with cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
slanesuke committed Sep 27, 2024
1 parent ca83558 commit 27fed1e
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 129 deletions.
Binary file added mine-your-first-block/.DS_Store
Binary file not shown.
6 changes: 3 additions & 3 deletions mine-your-first-block/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn create_coinbase_tx(total_tx_fee: u64, witness_root_vec: Vec<String>) -> T
coinbase_tx.version = 0;

// The txid variable is the witness data also
let txid= "0000000000000000000000000000000000000000000000000000000000000000".to_string();
let txid = "0000000000000000000000000000000000000000000000000000000000000000".to_string();

// Initialize the input for the coinbase transaction
coinbase_tx.vin.push(Vin {
Expand Down Expand Up @@ -65,7 +65,7 @@ pub fn create_coinbase_tx(total_tx_fee: u64, witness_root_vec: Vec<String>) -> T

// Double hash the witness commitment, then format it for the scriptpubkey
let wtxid_items_bytes = hex::decode(concant_items).unwrap();
let wtxid_commitment_test = double_sha256(wtxid_items_bytes);
let wtxid_commitment_test = double_sha256(wtxid_items_bytes);
let wtxid_commitment = hex::encode(wtxid_commitment_test);
let scriptpubkey_for_wtxid_test = format!("6a24aa21a9ed{}", wtxid_commitment);

Expand All @@ -88,7 +88,7 @@ pub fn construct_block_header(nonce: u32, merkle_root: String) -> BlockHeader {
// The function takes a nonce and merkle root as input and returns a block header struct

// Initialize the block header
let mut block_header = BlockHeader{
let mut block_header = BlockHeader {
version: 0x20000000,
prev_block_hash: "".to_string(),
merkle_root: merkle_root.to_string(),
Expand Down
31 changes: 18 additions & 13 deletions mine-your-first-block/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
mod block;
mod transactions;
mod validation;
mod utils;
mod block;
use transactions::*;
use validation::*;
use utils::*;
mod validation;
use block::*;
use itertools::Itertools;
use transactions::*;
use utils::*;
use validation::*;

/// The main driver function that mines the block
fn main() {
Expand All @@ -27,15 +27,16 @@ fn main() {
let mut total_fees = 0u64;

// Sort transactions by fee in descending order before processing
let sorted_valid_txs: Vec<_> = valid_txs.iter()
let sorted_valid_txs: Vec<_> = valid_txs
.iter()
.sorted_by(|a, b| b.fee.cmp(&a.fee))
.collect();

// Select transactions to include in the block based on sorted order and weight
for tx in sorted_valid_txs {
let tx_weight = calculate_transaction_weight(&tx.transaction);
if total_weight + tx_weight > max_block_weight {
break; // Stop if adding this transaction would exceed the max block weight
break; // Stop if adding this transaction would exceed the max block weight
}
block_txs.push(tx.clone()); // Add the transaction to the block
total_weight += tx_weight; // Add the weight to the total weight
Expand All @@ -47,17 +48,18 @@ fn main() {

// Get the wtxids for the witness root
// Initialize the wtxids with the coinbase transaction
let mut wtx_ids_for_witness_root = vec!["0000000000000000000000000000000000000000000000000000000000000000".to_string()];
let mut wtx_ids_for_witness_root =
vec!["0000000000000000000000000000000000000000000000000000000000000000".to_string()];

// Collect wtxids for witness root
for tx in &block_txs {
// If the transaction is p2wpkh, use the wtxid, otherwise use the txid
if tx.is_p2wpkh {
if let Some(ref wtxid) = tx.wtxid {
wtx_ids_for_witness_root.push(wtxid.clone()); // Collect wtxid if valid
wtx_ids_for_witness_root.push(wtxid.clone()); // Collect wtxid if valid
}
} else {
wtx_ids_for_witness_root.push(tx.txid.clone()); // Collect txid if not p2wpkh
wtx_ids_for_witness_root.push(tx.txid.clone()); // Collect txid if not p2wpkh
}
}

Expand Down Expand Up @@ -85,7 +87,10 @@ fn main() {

// Use block_txs to generate Merkle root
// Txids_for_merkle is a vector of txids for the merkle root
let txids_for_merkle = block_txs.iter().map(|tx| tx.txid.clone()).collect::<Vec<_>>();
let txids_for_merkle = block_txs
.iter()
.map(|tx| tx.txid.clone())
.collect::<Vec<_>>();
let merkle_root = get_merkle_root(txids_for_merkle.clone());

// Start Mining!
Expand All @@ -95,7 +100,7 @@ fn main() {
let serialized_block_header = serialize_block_header(&block_header);

// Calculate the hash of the block header
let block_hash = double_sha256(serialized_block_header.clone());
let block_hash = double_sha256(serialized_block_header.clone());
let block_hash = reverse_bytes(block_hash.to_vec());

// Check if the hash meets the target
Expand All @@ -108,4 +113,4 @@ fn main() {
nonce += 1;
}
}
}
}
4 changes: 2 additions & 2 deletions mine-your-first-block/src/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,5 @@ pub struct BlockHeader {
pub merkle_root: String,
pub timestamp: u32,
pub bits: u32,
pub nonce: u32
}
pub nonce: u32,
}
Loading

0 comments on commit 27fed1e

Please sign in to comment.