Skip to content

Commit

Permalink
commenting and edited a reverse fn to clean things up
Browse files Browse the repository at this point in the history
  • Loading branch information
slanesuke committed Apr 30, 2024
1 parent fae1d53 commit 7ce1baf
Show file tree
Hide file tree
Showing 2 changed files with 3,404 additions and 14 deletions.
38 changes: 24 additions & 14 deletions mine-your-first-block/src/block.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::time::{SystemTime, UNIX_EPOCH};
extern crate secp256k1;
use crate::transactions::{BlockHeader, Prevout, Transaction, Vin, Vout};
use crate::utils::{double_sha256, get_merkle_root};
use crate::utils::{double_sha256, get_merkle_root, reverse_bytes};

/// This function will return the coinbase transaction
///
pub fn create_coinbase_tx(total_tx_fee: u64, witness_root_vec: Vec<String>) -> Transaction {
// The function has total fees and a vec of wtxids as the input and returns a coinbase transaction
// Create a coinbase transaction and return it
let mut coinbase_tx = Transaction {
version: 0,
Expand All @@ -14,7 +16,6 @@ pub fn create_coinbase_tx(total_tx_fee: u64, witness_root_vec: Vec<String>) -> T
sighash: None,
};


// The block subsidy is 6.25 btc plus the fees from the transactions
let block_sub_plus_fees: u64 = 625000000 + total_tx_fee;

Expand All @@ -27,9 +28,10 @@ pub fn create_coinbase_tx(total_tx_fee: u64, witness_root_vec: Vec<String>) -> T
// version is 4 bytes lil endian 00000000
coinbase_tx.version = 0;

// witness data also
// the txid variable is the witness data also
let txid= "0000000000000000000000000000000000000000000000000000000000000000".to_string();
// input count is 1 byte 01

// Initialize the input for the coinbase transaction
coinbase_tx.vin.push(Vin {
txid: txid.clone(),
vout: 0xffffffff,
Expand All @@ -47,7 +49,7 @@ pub fn create_coinbase_tx(total_tx_fee: u64, witness_root_vec: Vec<String>) -> T
sequence: 0xffffffff,
});

// Output count is 1 byte 01
// Initialize the first output for the coinbase transaction
coinbase_tx.vout.push(Vout {
scriptpubkey,
scriptpubkey_asm: "OP_DUP OP_HASH160 OP_PUSHBYTES_20 06f1b66fd59a34755c37a8f701f43e937cdbeb13 OP_EQUALVERIFY OP_CHECKSIG".to_string(),
Expand All @@ -56,31 +58,37 @@ pub fn create_coinbase_tx(total_tx_fee: u64, witness_root_vec: Vec<String>) -> T
value: block_sub_plus_fees,
});


// Output count 2 for the wtxid stuff
// the witness root hash gets hashed with the witness reserve value and put into
// the scriptpubkey of the second output
// Get the witness root hash
let witness_root_hash = get_merkle_root(witness_root_vec);
// the txid below is the witness reserve value. It's just a 32 byte string of numbers
let concant_items = format!("{}{}", witness_root_hash, txid);

// Format the witness root hash and txid for the witness commitment
let concant_items = format!("{}{}", witness_root_hash, txid); // Remember txid is the witness reserve value

// 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 = hex::encode(wtxid_commitment_test);
let scriptpubkey_for_wtxid_test = format!("6a24aa21a9ed{}", wtxid_commitment);

// Construct the second output for the coinbase transaction
coinbase_tx.vout.push(Vout {
scriptpubkey: scriptpubkey_for_wtxid_test,
scriptpubkey_asm: "OP_RETURN OP_PUSHBYTES_36 aa21a9ed".to_string() + &wtxid_commitment,
scriptpubkey_type: "op_return".to_string(),
scriptpubkey_address: None,
value: 0,
});

// Return the coinbase transaction
coinbase_tx
}

/// This function creates the block header struct
pub fn construct_block_header(nonce: u32, merkle_root: String) -> BlockHeader {
// Construct the block header
// 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{
version: 0x20000000,
prev_block_hash: "".to_string(),
Expand All @@ -90,10 +98,12 @@ pub fn construct_block_header(nonce: u32, merkle_root: String) -> BlockHeader {
nonce: 0,
};

// This is the previous block hash from the mempool
let prev_block_hash = "0000000000000000000205e5b86991b1b0a370fb7e2b7126d32de18e48e556c4";
let decode_prev_block_hash = hex::decode(prev_block_hash).unwrap();
let reversed_prev_block_hash = decode_prev_block_hash.iter().rev().cloned().collect::<Vec<u8>>();
let reversed_hex = hex::encode(reversed_prev_block_hash);
// let decode_prev_block_hash = hex::decode(prev_block_hash).unwrap();
// let reversed_prev_block_hash = decode_prev_block_hash.iter().rev().cloned().collect::<Vec<u8>>();
// let reversed_hex = hex::encode(reversed_prev_block_hash);
let reversed_hex = reverse_bytes(hex::decode(prev_block_hash).unwrap());
block_header.prev_block_hash = reversed_hex.to_string();

let timestamp = SystemTime::now()
Expand Down
Loading

0 comments on commit 7ce1baf

Please sign in to comment.