Skip to content

Commit

Permalink
Testint the tx structure
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielgusn committed Apr 20, 2024
1 parent f9310f5 commit 5f20a7d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 26 deletions.
15 changes: 9 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// use std::env;
use std::{fs, io};
use std::{fs, io, fs::File};
use serde_json;

pub fn read_mempool(path: &str){
Expand All @@ -8,12 +8,15 @@ pub fn read_mempool(path: &str){
.expect("Error while reading Dir");

for file in files{
let path = path.to_string() + &file;
// println!("{path}");
let file_path = path.to_string() + &file;
let file: File = File::create(&file_path).unwrap();
let file_size = fs::metadata(&file_path).expect("Falha ao ler o arquivo");
// file.sync_all();
println!("Size: {} ", file_size.len());
// println!("Size: {} File: {}", file);
// let transaction: serde_json::Value= read_tx_from_file(&file_path);

let transaction: serde_json::Value= read_tx_from_file(&path);

is_coinbase(transaction);
// is_coinbase(transaction);

}
// is_coinbase(tx_in_json);
Expand Down
49 changes: 29 additions & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,48 +1,57 @@
#![allow(dead_code)]
mod block;
use block::block_header::BlockHeader;
use chrono::Utc;
use mining_challenge::read_mempool;
use ring::digest::{Context, Digest, SHA256};
use std::fmt::Write;
// use hex_literal::hex;
use std::fs;

fn main() {
read_mempool("/home/gabriel/projects/bitcoin-mining-challenge/mempool/");
let file_size = fs::metadata("/home/gabriel/projects/bitcoin-mining-challenge/mempool/ff73248e38bfcdac87261f5a51f3143478937fda6b0d1def46e794b8f250e6dd.json").expect("Falha ao ler o arquivo");
println!("Size: {} ", file_size.len());
// read_mempool("/home/gabriel/projects/bitcoin-mining-challenge/mempool/");
// for

// let mut hasher = Sha256::new();
let first_block_header: BlockHeader = BlockHeader::new(String::from("00000000000000000000000000000000"), String::from("00000000000000000000000000000000"), Utc::now(), 128);

println!("{}", first_block_header);

println!("Size of Block Header: {}", std::mem::size_of::<BlockHeader>());

let data: &str = "texto 1234";

let mut nonce: u64 = 0_u64;

loop{

let mut context: Context = Context::new(&SHA256);

// loop{

// let mut context: Context = Context::new(&SHA256);

// let target_hash: String = String::from("0000ffff00000000000000000000000000000000000000000000000000000000");
let target_hash: String = String::from("00000cff00000000000000000000000000000000000000000000000000000000");
// // let target_hash: String = String::from("0000ffff00000000000000000000000000000000000000000000000000000000");
// let target_hash: String = String::from("00000cff00000000000000000000000000000000000000000000000000000000");

context.update(data.as_bytes());
context.update(&nonce.to_be_bytes());
// context.update(data.as_bytes());
// context.update(&nonce.to_be_bytes());

let digest: Digest = context.finish();
let mut actual_hex: String = String::new();
// let digest: Digest = context.finish();
// let mut actual_hex: String = String::new();

for &byte in digest.as_ref() {
write!(&mut actual_hex, "{:02x}", byte).expect("Failed to write hex");
}
// for &byte in digest.as_ref() {
// write!(&mut actual_hex, "{:02x}", byte).expect("Failed to write hex");
// }

println!("Nonce: {}, Hash: {}", nonce, actual_hex);

if actual_hex <= target_hash {
println!("Found the nonce for the target Hash! It is: {} and you can attach this block to the blockchain", nonce);
break
}
nonce += 1;
}
// println!("Nonce: {}, Hash: {}", nonce, actual_hex);

// if actual_hex <= target_hash {
// println!("Found the nonce for the target Hash! It is: {} and you can attach this block to the blockchain", nonce);
// break
// }
// nonce += 1;
// }
}


9 changes: 9 additions & 0 deletions src/transactions/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,13 @@ pub struct TxPrevOut {
pub struct Tx{
vin: TxInput,
tx_size: u64,
}


impl Tx{
fn Tx(tx_input: TxInput, tx_output: T) -> Self {
Self {
vin: tx_input
}
}
}

0 comments on commit 5f20a7d

Please sign in to comment.