Skip to content

Commit

Permalink
Before I was pushing the valid txs vin.txid instead of double hashing…
Browse files Browse the repository at this point in the history
… the serialized tx so i changed that
  • Loading branch information
slanesuke committed Apr 12, 2024
1 parent 275a653 commit 542c231
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions mine-your-first-block/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,11 +652,14 @@ fn check_double_spending(transaction: &Transaction, mempool: &Vec<Transaction>)
}

/// This function will validate a P2PKH transaction
fn p2pkh_script_validation(transaction: &mut Transaction) -> Result<bool, Box<dyn Error>> {
fn p2pkh_script_validation(transaction: &mut Transaction) -> Result<(bool, String), Box<dyn Error>> {

// Create a stack to hold the data
let mut stack: Vec<Vec<u8>> = Vec::new();

// Initalize the serialized tx
let mut serialized_tx_for_message = String::new();

for (i,vin) in transaction.vin.iter().enumerate() {

// Clearing the stack
Expand All @@ -674,7 +677,9 @@ fn p2pkh_script_validation(transaction: &mut Transaction) -> Result<bool, Box<dy
let mut tx_for_signing = transaction.clone();
tx_for_signing.vin = vec![vin.clone()];
let message_tx = get_tx_readyfor_signing_legacy(&mut tx_for_signing);
let serialized_tx_for_message = serialize_tx(&message_tx);

// Update the serialized tx
serialized_tx_for_message = serialize_tx(&message_tx);

// Convert the serialized tx into bytes for the message
let message_in_bytes = hex::decode(serialized_tx_for_message)
Expand Down Expand Up @@ -772,7 +777,11 @@ fn p2pkh_script_validation(transaction: &mut Transaction) -> Result<bool, Box<dy
}
}

Ok(true)
//////
let serialized_validtx = serialized_tx_for_message.as_bytes();
let txid: [u8; 32] = double_sha256(serialized_validtx.to_vec());

Ok((true, hex::encode(txid)))
}

/// This function will remove dust transactions from a transaction
Expand Down Expand Up @@ -848,24 +857,19 @@ fn process_mempool(mempool_path: &str) -> io::Result<Vec<(String, u64)>> {
if path.is_file() {
if let Some(path_str) = path.to_str() {
let mut transaction = deserialize_tx(path_str);
// I can add this once i put all the valid tx's in a vec
// if !check_double_spending(&transaction, Vec<>) {
// continue;
// }

match p2pkh_script_validation(&mut transaction) {
Ok(is_valid) => {
if !is_valid {
//eprintln!("Transaction is not valid: {:?}", path);
continue;
}
},

let (is_valid, txid) = match p2pkh_script_validation(&mut transaction) {
Ok(result) => result,
Err(e) => {
//eprintln!("An error occured, failed to validate transaction: {:?}", e);
continue;
}
};
if !is_valid {
//eprintln!("Transaction is not valid: {:?}", path);
}


// Get the fee if valid so i can add it to my vec
let fee = verify_tx_fee(&transaction);
if fee < 0 {
Expand All @@ -881,8 +885,10 @@ fn process_mempool(mempool_path: &str) -> io::Result<Vec<(String, u64)>> {
remove_dust_transactions(&mut transaction, min_relay_fee_per_byte);


println!("Transaction is valid for txid: {}", transaction.vin[0].txid);
valid_txs.push((transaction.vin[0].txid.clone(), fee));
println!("Transaction is valid for txid: {}", txid);

// Push the txid and fee to the valid_txs vec
valid_txs.push((txid, fee));
}else {
//eprintln!("Failed to convert path to string: {:?}", path);
}
Expand Down Expand Up @@ -959,9 +965,6 @@ fn main() {
// Initialize nonce value;
let mut nonce = 0u32;

// Get fees
let mut fees = 0u64;

// Get the valid txs from the mempool
let valid_tx = process_mempool(mempool_path).unwrap();

Expand All @@ -981,6 +984,7 @@ fn main() {




// Start Mining!
loop {
// Get the block header and serialize it
Expand Down Expand Up @@ -1020,11 +1024,7 @@ fn main() {
}
}

fn main2() {
let coinbase_tx = create_coinbase_tx(0);
let serialized_coinbase_tx = serialize_tx(&coinbase_tx);
println!("{}", serialized_coinbase_tx);
}




0 comments on commit 542c231

Please sign in to comment.