-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding some validations for tx, implementation of block.push_transact…
…ion() and modularization of lib.rs
- Loading branch information
1 parent
f2a7a6b
commit f7eedf6
Showing
7 changed files
with
200 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,68 @@ | ||
// use std::env; | ||
#![allow(dead_code, unused)] | ||
// // use std::env; | ||
// #![allow(dead_code, unused)] | ||
|
||
mod transactions; | ||
mod mempool; | ||
// use core::panicking::panic; | ||
use std::io::{self, Read}; | ||
use std::fs::{self, File}; | ||
use std::path::Path; | ||
use serde_json; | ||
use transactions::tx::{Tx, TxInput, TxOutput}; | ||
use mempool::mempool::Mempool; | ||
// mod transactions; | ||
// mod mempool; | ||
// // use core::panicking::panic; | ||
// use std::io::{self, Read}; | ||
// use std::fs::{self, File}; | ||
// use std::path::Path; | ||
// use serde_json; | ||
// use transactions::tx::{Tx, TxInput, TxOutput}; | ||
// use mempool::mempool::Mempool; | ||
|
||
pub fn read_mempool(path: &str){ | ||
// pub fn read_mempool(path: &str){ | ||
|
||
// let files = get_files_in_directory(path) | ||
// .expect("Error while reading Dir"); | ||
// // let files = get_files_in_directory(path) | ||
// // .expect("Error while reading Dir"); | ||
|
||
// let tx = read_tx_from_file("/home/gabriel/projects/bitcoin-mining-challenge/mempool/0.json"); | ||
// // let tx = read_tx_from_file("/home/gabriel/projects/bitcoin-mining-challenge/mempool/0.json"); | ||
|
||
let mut mempool: Mempool = Mempool::get_mempool_from_dir(path); | ||
// let mut mempool: Mempool = Mempool::get_mempool_from_dir(path); | ||
|
||
println!("{}", mempool.txs.len()); | ||
// println!("{}", mempool.txs.len()); | ||
|
||
println!("==================== Before sorting ===================="); | ||
for i in 0..100{ | ||
println!("{}", mempool.txs[i]); | ||
} | ||
// println!("==================== Before sorting ===================="); | ||
// for i in 0..100{ | ||
// println!("{}", mempool.txs[i]); | ||
// } | ||
|
||
println!("==================== After sorting ===================="); | ||
mempool.sort_mempool_by_tx(); | ||
for i in 0..100{ | ||
println!("{}", mempool.txs[i]); | ||
} | ||
} | ||
// println!("==================== After sorting ===================="); | ||
// mempool.sort_mempool_by_tx(); | ||
// for i in 0..100{ | ||
// println!("{}", mempool.txs[i]); | ||
// } | ||
// } | ||
|
||
pub fn read_tx_from_file(file_path: &str) -> Tx { | ||
let mut file_content: String = String::new(); | ||
let path = Path::new(&file_path); | ||
// pub fn read_tx_from_file(file_path: &str) -> Tx { | ||
// let mut file_content: String = String::new(); | ||
// let path = Path::new(&file_path); | ||
|
||
let mut file = File::open(path).expect("Error while loading file"); | ||
file.read_to_string(&mut file_content).expect("File can not be read"); | ||
// let mut file = File::open(path).expect("Error while loading file"); | ||
// file.read_to_string(&mut file_content).expect("File can not be read"); | ||
|
||
let error_in_file_message:String = String::from("Error while parsing json to Tx in file ") + file_path; | ||
// let error_in_file_message:String = String::from("Error while parsing json to Tx in file ") + file_path; | ||
|
||
let tx_in_json: Tx = serde_json::from_str(&file_content).expect(&error_in_file_message); | ||
// let tx_in_json: Tx = serde_json::from_str(&file_content).expect(&error_in_file_message); | ||
|
||
return tx_in_json; | ||
} | ||
// return tx_in_json; | ||
// } | ||
|
||
fn get_files_in_directory(path: &str) -> io::Result<Vec<String>> { | ||
// Get a list of all entries in the folder | ||
let entries = fs::read_dir(path)?; | ||
// fn get_files_in_directory(path: &str) -> io::Result<Vec<String>> { | ||
// // Get a list of all entries in the folder | ||
// let entries = fs::read_dir(path)?; | ||
|
||
// Extract the filenames from the directory entries and store them in a vector | ||
let file_names: Vec<String> = entries | ||
.filter_map(|entry| { | ||
let path = entry.ok()?.path(); | ||
if path.is_file() { | ||
path.file_name()?.to_str().map(|s| s.to_owned()) | ||
} else { | ||
None | ||
} | ||
}) | ||
.collect(); | ||
// // Extract the filenames from the directory entries and store them in a vector | ||
// let file_names: Vec<String> = entries | ||
// .filter_map(|entry| { | ||
// let path = entry.ok()?.path(); | ||
// if path.is_file() { | ||
// path.file_name()?.to_str().map(|s| s.to_owned()) | ||
// } else { | ||
// None | ||
// } | ||
// }) | ||
// .collect(); | ||
|
||
Ok(file_names) | ||
} | ||
// Ok(file_names) | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// use std::env; | ||
#![allow(dead_code, unused)] | ||
|
||
// use core::panicking::panic; | ||
|
||
use crate::transactions::tx::{Tx, TxInput, TxOutput}; | ||
use crate::mempool::mempool::Mempool; | ||
use serde_json; | ||
use std::path::Path; | ||
use std::io::{self, Read}; | ||
use std::fs::{self, File}; | ||
|
||
pub fn read_mempool(path: &str) -> Mempool { | ||
|
||
// let files = get_files_in_directory(path) | ||
// .expect("Error while reading Dir"); | ||
|
||
// let tx = read_tx_from_file("/home/gabriel/projects/bitcoin-mining-challenge/mempool/0.json"); | ||
|
||
let mut mempool: Mempool = Mempool::get_mempool_from_dir(path); | ||
|
||
// println!("{:?}", mempool); | ||
|
||
// println!("==================== Before sorting ===================="); | ||
// for i in 0..100{ | ||
// println!("{}", mempool.txs[i]); | ||
// } | ||
|
||
// println!("==================== After sorting ===================="); | ||
// mempool.sort_mempool_by_tx(); | ||
// for i in 0..100{ | ||
// println!("{}", mempool.txs[i]); | ||
// } | ||
mempool | ||
} | ||
|
||
pub fn get_files_in_directory(path: &str) -> io::Result<Vec<String>> { | ||
// Get a list of all entries in the folder | ||
let entries = fs::read_dir(path)?; | ||
|
||
// Extract the filenames from the directory entries and store them in a vector | ||
let file_names: Vec<String> = entries | ||
.filter_map(|entry| { | ||
let path = entry.ok()?.path(); | ||
if path.is_file() { | ||
path.file_name()?.to_str().map(|s| s.to_owned()) | ||
} else { | ||
None | ||
} | ||
}) | ||
.collect(); | ||
|
||
Ok(file_names) | ||
} | ||
|
||
pub fn read_tx_from_file(file_path: &str) -> Tx { | ||
let mut file_content: String = String::new(); | ||
let path = Path::new(&file_path); | ||
|
||
let mut file = File::open(path).expect("Error while loading file"); | ||
file.read_to_string(&mut file_content).expect("File can not be read"); | ||
|
||
let error_message_in_file:String = String::from("Error while parsing json to Tx in file ") + file_path; | ||
|
||
let tx_in_json: Tx = serde_json::from_str(&file_content).expect(&error_message_in_file); | ||
|
||
return tx_in_json; | ||
} |