From 169a0e0fdb94385330c7c78e4cec17aeba8e7801 Mon Sep 17 00:00:00 2001 From: Ian Slane Date: Fri, 12 Apr 2024 13:50:40 -0600 Subject: [PATCH] Block header was too short so pushed the serialized header rather than hash to see if that makes a difference --- mine-your-first-block/src/main.rs | 62 +++---------------------------- 1 file changed, 6 insertions(+), 56 deletions(-) diff --git a/mine-your-first-block/src/main.rs b/mine-your-first-block/src/main.rs index 1697046..5f98d9d 100644 --- a/mine-your-first-block/src/main.rs +++ b/mine-your-first-block/src/main.rs @@ -1,7 +1,3 @@ -/// TODO -/// FIGURE OUT HOW TO VERIFY THE SIGNATURE OF A TRANSACTION UGHH - - use std::fmt::{Debug}; use serde::Deserialize; use serde_json; @@ -9,21 +5,15 @@ use sha2::{Digest as ShaDigest, Sha256}; use std::fs::File; use std::io::{self, Read, Write, BufReader}; use ripemd::Ripemd160; -// use ripemd::{Digest as RipemdDigest, Ripemd160}; use std::fs::OpenOptions; use std::time::{SystemTime, UNIX_EPOCH}; use itertools::{Itertools, sorted}; - - -// Unsure if i need to use the extern crate for secp256k1 extern crate secp256k1; use secp256k1::{PublicKey, Secp256k1, Message}; use std::error::Error; use std::fs; use secp256k1::ecdsa::Signature; - - // Transaction struct that may be overcomplicated right now. We will see #[derive(Debug, Deserialize, Clone)] struct Transaction { @@ -80,15 +70,6 @@ struct BlockHeader { nonce: u32 } -// TODO Before I turn it in -// Implement the CoinbaseTx function! Need to add the serialized coinbase tx to output.txt -// If the coinbase tx has a segwit tx according to BIP 141: all coinbase transactions since the segwit -// upgrade need to include a witness reserved value in -// the witness field for the input, and then use that along with a witness root hash to put a wTXID -// commitment in the ScriptPubKey of one of the outputs in the transaction. - -// This function will create a coinbase transaction - // TODO need to return a Transaction struct becasue this is bad practice and inefficient fn create_coinbase_tx(total_tx_fee: u64) -> Transaction { let mut coinbase_tx = Transaction { @@ -174,16 +155,6 @@ fn construct_block_header(valid_tx_vec: Vec, nonce: u32) -> BlockHeader let merkle_root = get_merkle_root(txids); block_header.merkle_root = merkle_root; - // The time the block was constructed in unix time - // 4 bytes little endian - // let timestamp = SystemTime::now() - // .duration_since(UNIX_EPOCH) - // .unwrap() - // .as_secs(); - // let timestamp_bytes = timestamp.to_le_bytes(); - // let timestamp_hex = hex::encode(timestamp_bytes); - // block_header.timestamp = timestamp_hex.parse().unwrap(); - let timestamp = SystemTime::now() .duration_since(UNIX_EPOCH) .unwrap() @@ -279,21 +250,6 @@ fn get_merkle_root(txids: Vec) -> String { } } -// TODO -// Need to get my head together and figure out how to simplify and verify the .json transactions. After -// that i need to write a mining algo to efficiently fit the transactions with the highest fees into a block -// and pass the block fees to coinbase tx so I can add it to the block reward. -// Need to then just get there txid's and put them in a vec to add to output.txt - -// TODO -// Implement the BlockHeader function! Need to add the serialized block header to output.txt - - - - - - -// First just working with one tx so I can learn fn deserialize_tx(filename: &str) -> Transaction { // Open the file of a tx let mut file = File::open(filename).unwrap(); @@ -507,8 +463,6 @@ fn serialized_segwit_tx(transaction: &Transaction) -> String { // serialized_tx.push_str(& as Clone>::clone(&transaction.sighash).unwrap()); // } - - serialized_tx } @@ -881,8 +835,8 @@ fn process_mempool(mempool_path: &str) -> io::Result