Skip to content

Commit

Permalink
Add timestamps to all println and eprintln
Browse files Browse the repository at this point in the history
  • Loading branch information
polespinasa committed May 19, 2024
1 parent b041291 commit 8fb6bcd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 30 deletions.
24 changes: 12 additions & 12 deletions server/grouphug-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub static CONFIG: Lazy<Config> = Lazy::new(|| {
let default_path = "Config.toml";

if args.len() > 2 {
eprintln!("Only 1 argument accepted");
eprintln!("{}: Only 1 argument accepted", Utc::now());
std::process::exit(1);
}

Expand Down Expand Up @@ -82,7 +82,7 @@ fn check_double_spending_other_group(tx_hex: &str) -> (bool, String) {
for group in groups.iter() {
// Checks if a tx input is in the group
if group.contains_txin(&txin) {
eprintln!("Transaction was rejected, Error: transaction input is already in a group\n");
eprintln!("{}: Transaction was rejected, Error: transaction input is already in a group\n", Utc::now());
return (true, String::from("Transaction input is already in a group"));
}
}
Expand Down Expand Up @@ -137,7 +137,7 @@ fn close_group_by_fee() {
}
},
Err(e) => {
eprintln!("There was an error estimating fees for the next {:?} blocks: {:?}", target, e);
eprintln!("{}: There was an error estimating fees for the next {:?} blocks: {:?}",Utc::now(), target, e);
}
}

Expand All @@ -154,12 +154,12 @@ fn handle_addtx(transaction: &str, mut stream: TcpStream) {
// Validate that the tx has the correct format and satisfies all the rules
let (valid, msg, fee_rate) = validate_tx_query_one_to_one_single_anyone_can_pay(transaction);

println!("Client {} sent a new raw transaction: {}",stream.peer_addr().unwrap(), transaction);
println!("{}: Client {} sent a new raw transaction: {}", Utc::now(), stream.peer_addr().unwrap(), transaction);

if !valid {
// should send an error message as the transaction has an invalid format or does not match some rule
let error_msg = format!("Error: {}\n", msg);
eprintln!("Transaction was rejected, {}\n", error_msg);
eprintln!("{}: Transaction was rejected, {}\n", Utc::now(), error_msg);
stream.write(error_msg.as_bytes()).unwrap();
return
}
Expand Down Expand Up @@ -194,7 +194,7 @@ fn handle_addtx(transaction: &str, mut stream: TcpStream) {
None => {
// If none then there is no group for this fee rate so we create one
let mut new_group = Group::new(expected_group_fee);
println!("New group created with fee_rate {}sat/vB", new_group.fee_rate);
println!("{}: New group created with fee_rate {}sat/vB", Utc::now(), new_group.fee_rate);
close_group = new_group.add_tx(transaction);
groups.push(new_group);
}
Expand All @@ -215,7 +215,7 @@ fn handle_addtx(transaction: &str, mut stream: TcpStream) {

fn handle_client(mut stream: TcpStream) {

println!("New user connected: {}\n", stream.peer_addr().unwrap());
println!("{}: New user connected: {}\n", Utc::now(), stream.peer_addr().unwrap());

// send the network configuration
// TODO -> Find a way to ask the electrum server what network is running
Expand All @@ -241,7 +241,7 @@ fn handle_client(mut stream: TcpStream) {
Ok(s) => s,
Err(_e) => {
// If error user has disconnected
println!("Client {} disconnected\n", stream.peer_addr().unwrap());
println!("{}: Client {} disconnected\n", Utc::now(), stream.peer_addr().unwrap());
return;
},
};
Expand All @@ -254,7 +254,7 @@ fn handle_client(mut stream: TcpStream) {
if command_parts.len() > 2 {
// If there's more than two arguments on the call something is worng.
// Expected format: "add_tx raw_tx_data"
eprintln!("Client {} sent a command with wrong number of arguments: {}\n", stream.peer_addr().unwrap(), command_string.trim());
eprintln!("{}: Client {} sent a command with wrong number of arguments: {}\n", Utc::now(), stream.peer_addr().unwrap(), command_string.trim());
stream.write(b"One or two arguments are expected\n").unwrap();
continue;
}
Expand All @@ -272,7 +272,7 @@ fn handle_client(mut stream: TcpStream) {
"add_tx" => handle_addtx(arg, stream.try_clone().unwrap()),
"get_groupsInfo" => handle_get_groups_info(stream.try_clone().unwrap()),
_ => {
eprintln!("Client {} sent an unknown command: {}\n", stream.peer_addr().unwrap(), command);
eprintln!("{}: Client {} sent an unknown command: {}\n", Utc::now(), stream.peer_addr().unwrap(), command);
stream.write(b"Unknown command sent\n").unwrap();
},
}
Expand Down Expand Up @@ -321,7 +321,7 @@ fn main() {
}
});

println!("Server running on {}", endpoint);
println!("{}: Server running on {}", Utc::now(), endpoint);
for stream in listener.incoming(){
match stream {
Ok(stream) => {
Expand All @@ -330,7 +330,7 @@ fn main() {
});
}
Err(e) => {
eprintln!("Unable to connect: {}", e);
eprintln!("{}: Unable to connect: {}", Utc::now(), e);
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions server/grouphug-server/src/server/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Group {
self.transactions.push((tx.input[i].clone(), tx.output[i].clone()));
}

println!("Tx {} added to group with fee_rate {}sat/vB", tx.txid(), self.fee_rate);
println!("{}: Tx {} added to group with fee_rate {}sat/vB", Utc::now(), tx.txid(), self.fee_rate);

// Check if the group should be closed according to the MAX_SIZE limit established in config file
if self.transactions.len() >= crate::CONFIG.group.max_size {
Expand Down Expand Up @@ -112,23 +112,23 @@ impl Group {
i += 1;
}
else {
eprintln!("Double spending detected on a group, deleting that transaction...");
eprintln!("{}: Double spending detected on a group, deleting that transaction...", Utc::now());
self.transactions.remove(i);
return false;
}
},
Err(_e) => {
eprintln!("Error querying for the UTXO");
eprintln!("{}: Error querying for the UTXO", Utc::now());
return false;
}
}
},
Ok(None) => {
eprintln!("Petition succeed but no tx was returned");
eprintln!("{}: Petition succeed but no tx was returned", Utc::now());
return false;
},
Err(_e) => {
println!("Could not retrieve previous transaction");
println!("{}: Could not retrieve previous transaction", Utc::now());
return false;
}
}
Expand All @@ -139,8 +139,8 @@ impl Group {
self.create_group_transaction();

let tx_hex = serialize_hex(&self.transaction_group);
println!("Group transaction: \n");
println!("{:?}", tx_hex);
println!("{}: Group transaction: \n", Utc::now());
println!("{}: {:?}", Utc::now(), tx_hex);

let tx_bytes = hex_decode(tx_hex).unwrap();

Expand All @@ -151,11 +151,11 @@ impl Group {

match txid {
Ok(id) => {
println!("Group {}sat/vb closed! Transaction broadcasted with TXID: {}", self.fee_rate, id);
println!("{}: Group {}sat/vb closed! Transaction broadcasted with TXID: {}", Utc::now(), self.fee_rate, id);
return true;
},
Err(e) => {
eprintln!("There is an error broadcasting the transaction group: {:?}", e);
eprintln!("{}: There is an error broadcasting the transaction group: {:?}", Utc::now(), e);
return false;
}

Expand Down
18 changes: 9 additions & 9 deletions server/grouphug-server/src/utils/transactions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Functions related to the transactions validation and manipulation.
use chrono::Utc;
use bdk::bitcoin::{
OutPoint,
Transaction,
Expand Down Expand Up @@ -28,7 +28,7 @@ pub fn which_network(tx: &Transaction) -> bool {
},
Ok(None) => (),
Err(e) => {
println!("Error: {:?}", e);
println!("{}: Error: {:?}", Utc::now(), e);
},
}
return false;
Expand All @@ -50,12 +50,12 @@ pub fn get_previous_utxo_value(utxo: OutPoint) -> f32 {
return tx.output[utxo.vout as usize].value as f32;
},
Ok(None) => {
eprintln!("Previous transaction query returned NONE");
eprintln!("{}: Previous transaction query returned NONE", Utc::now());
return 0.0;
}
Err(erro) => {
eprintln!("There is an error retrieving previous transaction");
eprintln!("{}", erro);
eprintln!("{}: There is an error retrieving previous transaction", Utc::now());
eprintln!("{}: {}", Utc::now(), erro);
return 0.0;
}

Expand Down Expand Up @@ -85,22 +85,22 @@ pub fn previous_utxo_spent(tx: &Transaction) -> bool {
match utxo_list {
Ok(returned_utxo_list) => {
if returned_utxo_list.len() == 0 {
eprintln!("Transaction already spent");
eprintln!("{}: Transaction already spent", Utc::now());
return false;
}
},
Err(_e) => {
eprintln!("Error querying for the UTXO");
eprintln!("{}: Error querying for the UTXO", Utc::now());
return false;
}
}
},
Ok(None) => {
eprint!("Petition succeed but no tx was returned");
eprint!("{}: Petition succeed but no tx was returned", Utc::now());
return false;
},
Err(_e) => {
eprintln!("Could not retrieve previous transaction");
eprintln!("{}: Could not retrieve previous transaction", Utc::now());
return false;
}
}
Expand Down

0 comments on commit 8fb6bcd

Please sign in to comment.