Skip to content

Commit

Permalink
Use of eprint for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
polespinasa committed Apr 21, 2024
1 parent 4a0c7da commit 7fbae41
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
13 changes: 4 additions & 9 deletions server/grouphug-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use crate::utils::transactions::validate_tx_query_one_to_one_single_anyone_can_p
use crate::config::Config;
use crate::server::group::Group;



// External libraries
use std::{
thread,
Expand Down Expand Up @@ -51,13 +49,10 @@ pub static CONFIG: Lazy<Config> = Lazy::new(|| {
});



// Array with Group list
type GroupHug = Group;
static GLOBAL_GROUPS: Lazy<Arc<Mutex<Vec<GroupHug>>>> = Lazy::new(|| Arc::new(Mutex::new(Vec::new())));



fn check_double_spending_other_group(tx_hex: &str) -> (bool, String) {
// Check if an input from a transaction is already duplicated on another group
// Return true if cheating is detected
Expand All @@ -84,7 +79,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) {
println!("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");
return (true, String::from("Transaction input is already in a group"));
}
}
Expand All @@ -103,7 +98,7 @@ fn handle_addtx(transaction: &str, mut stream: TcpStream) {
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);
println!("Transaction was rejected, {}\n", error_msg);
eprintln!("Transaction was rejected, {}\n", error_msg);
stream.write(error_msg.as_bytes()).unwrap();
return
}
Expand Down Expand Up @@ -191,7 +186,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"
println!("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", stream.peer_addr().unwrap(), command_string.trim());
stream.write(b"Two arguments are expected\n").unwrap();
continue;
}
Expand All @@ -201,7 +196,7 @@ fn handle_client(mut stream: TcpStream) {
// This allows to add more commands in the future
"add_tx" => handle_addtx(arg, stream.try_clone().unwrap()),
_ => {
println!("Client {} sent an unknown command: {}\n", stream.peer_addr().unwrap(), command);
eprintln!("Client {} sent an unknown command: {}\n", stream.peer_addr().unwrap(), command);
stream.write(b"Unknown command sent\n").unwrap();
},
}
Expand Down
8 changes: 4 additions & 4 deletions server/grouphug-server/src/server/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,19 @@ impl Group {
i += 1;
}
else {
println!("Double spending detected on a group, deleting that transaction...");
eprintln!("Double spending detected on a group, deleting that transaction...");
self.transactions.remove(i);
return false;
}
},
Err(_e) => {
println!("Error querying for the UTXO");
eprintln!("Error querying for the UTXO");
return false;
}
}
},
Ok(None) => {
println!("Petition succeed but no tx was returned");
eprintln!("Petition succeed but no tx was returned");
return false;
},
Err(_e) => {
Expand Down Expand Up @@ -147,7 +147,7 @@ impl Group {
return true;
},
Err(e) => {
println!("There is an error broadcasting the transaction group: {:?}", e);
eprintln!("There is an error broadcasting the transaction group: {:?}", e);
return false;
}

Expand Down
14 changes: 7 additions & 7 deletions server/grouphug-server/src/utils/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ pub fn get_previous_utxo_value(utxo: OutPoint) -> f32 {
return tx.output[utxo.vout as usize].value as f32;
},
Ok(None) => {
println!("Previous transaction query returned NONE");
eprintln!("Previous transaction query returned NONE");
return 0.0;
}
Err(erro) => {
println!("{}", erro);
println!("There is an error retrieving previous transaction");
eprintln!("There is an error retrieving previous transaction");
eprintln!("{}", erro);
return 0.0;
}

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

0 comments on commit 7fbae41

Please sign in to comment.