Skip to content

Commit

Permalink
Add logging level
Browse files Browse the repository at this point in the history
This changes adds log crate to dependencies and use it to have
logging in different levels.
  • Loading branch information
mojoX911 committed Jul 2, 2021
1 parent 9ddd6db commit a5aea8c
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 46 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ bitcoin = "0.25"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tokio = { version = "0.3", features = ["full"] }
log = "^0.4"
futures = "0.3"
rand = "0.7.3"
itertools = "0.9.0"
Expand Down
20 changes: 10 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn generate_wallet(wallet_file_name: &PathBuf) -> std::io::Result<()> {
let rpc = match get_bitcoin_rpc() {
Ok(rpc) => rpc,
Err(error) => {
println!("error connecting to bitcoin node: {:?}", error);
log::trace!(target: "main", "error connecting to bitcoin node: {:?}", error);
return Ok(());
}
};
Expand Down Expand Up @@ -121,14 +121,14 @@ fn display_wallet_balance(wallet_file_name: &PathBuf, long_form: Option<bool>) {
let mut wallet = match Wallet::load_wallet_from_file(wallet_file_name) {
Ok(w) => w,
Err(error) => {
println!("error loading wallet file: {:?}", error);
log::trace!(target: "main", "error loading wallet file: {:?}", error);
return;
}
};
let rpc = match get_bitcoin_rpc() {
Ok(rpc) => rpc,
Err(error) => {
println!("error connecting to bitcoin node: {:?}", error);
log::trace!(target: "main", "error connecting to bitcoin node: {:?}", error);
return;
}
};
Expand Down Expand Up @@ -170,7 +170,7 @@ fn display_wallet_keys(wallet_file_name: &PathBuf) {
let wallet = match Wallet::load_wallet_from_file(wallet_file_name) {
Ok(w) => w,
Err(error) => {
println!("error loading wallet file: {:?}", error);
log::trace!(target: "main", "error loading wallet file: {:?}", error);
return;
}
};
Expand All @@ -181,14 +181,14 @@ fn print_receive_invoice(wallet_file_name: &PathBuf) {
let mut wallet = match Wallet::load_wallet_from_file(wallet_file_name) {
Ok(w) => w,
Err(error) => {
println!("error loading wallet file: {:?}", error);
log::trace!(target: "main", "error loading wallet file: {:?}", error);
return;
}
};
let rpc = match get_bitcoin_rpc() {
Ok(rpc) => rpc,
Err(error) => {
println!("error connecting to bitcoin node: {:?}", error);
log::trace!(target: "main", "error connecting to bitcoin node: {:?}", error);
return;
}
};
Expand All @@ -208,14 +208,14 @@ fn run_maker(wallet_file_name: &PathBuf, port: u16) {
let rpc = match get_bitcoin_rpc() {
Ok(rpc) => rpc,
Err(error) => {
println!("error connecting to bitcoin node: {:?}", error);
log::trace!(target: "main", "error connecting to bitcoin node: {:?}", error);
return;
}
};
let mut wallet = match Wallet::load_wallet_from_file(wallet_file_name) {
Ok(w) => w,
Err(error) => {
println!("error loading wallet file: {:?}", error);
log::trace!(target: "main", "error loading wallet file: {:?}", error);
return;
}
};
Expand All @@ -230,14 +230,14 @@ fn run_taker(wallet_file_name: &PathBuf) {
let rpc = match get_bitcoin_rpc() {
Ok(rpc) => rpc,
Err(error) => {
println!("error connecting to bitcoin node: {:?}", error);
log::trace!(target: "main", "error connecting to bitcoin node: {:?}", error);
return;
}
};
let mut wallet = match Wallet::load_wallet_from_file(wallet_file_name) {
Ok(w) => w,
Err(error) => {
println!("error loading wallet file: {:?}", error);
log::trace!(target: "main", "error loading wallet file: {:?}", error);
return;
}
};
Expand Down
22 changes: 11 additions & 11 deletions src/maker_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ struct ConnectionState {
async fn run(rpc: Arc<Client>, wallet: Arc<RwLock<Wallet>>, port: u16) -> Result<(), Error> {
//TODO port number in config file
let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, port)).await?;
println!("listening on port {}", port);
log::trace!(target: "maker", "listening on port {}", port);

let (server_loop_comms_tx, mut server_loop_comms_rx) = mpsc::channel::<Error>(100);
let mut accepting_clients = true;
Expand Down Expand Up @@ -133,7 +133,7 @@ async fn run(rpc: Arc<Client>, wallet: Arc<RwLock<Wallet>>, port: u16) -> Result
let mut line = String::new();
match reader.read_line(&mut line).await {
Ok(n) if n == 0 => {
println!("reached EOF");
log::trace!(target: "maker", "reached EOF");
break;
}
Ok(_n) => (),
Expand Down Expand Up @@ -229,7 +229,7 @@ fn handle_message(
Ok(r) => r,
Err(_e) => return Err(Error::Protocol("message parsing error")),
};
println!("<== {:?}", request);
log::trace!(target: "maker", "<== {:?}", request);
let outgoing_message = match request {
TakerToMakerMessage::TakerHello(_message) => {
connection_state.allowed_method = None;
Expand Down Expand Up @@ -271,7 +271,7 @@ fn handle_message(
handle_private_key_handover(wallet, message)?
}
};
println!("==> {:?}", outgoing_message);
log::trace!(target: "maker", "==> {:?}", outgoing_message);
match outgoing_message {
Some(result) => Ok(Some(result)),
None => Ok(None),
Expand Down Expand Up @@ -321,7 +321,7 @@ fn handle_proof_of_funding(
for funding_info in &proof.confirmed_funding_txes {
//check that the claimed multisig redeemscript is in the transaction

println!(
log::trace!(target: "maker",
"tx = {:?}\nms_rs = {:x}",
funding_info.funding_tx, funding_info.multisig_redeemscript
);
Expand Down Expand Up @@ -364,7 +364,7 @@ fn handle_proof_of_funding(
}
}

println!("proof of funding valid, creating own funding txes");
log::trace!(target: "maker", "proof of funding valid, creating own funding txes");

connection_state.incoming_swapcoins = Some(Vec::<WalletSwapCoin>::new());
for (funding_info, &funding_output_index, &funding_output, &incoming_swapcoin_keys) in izip!(
Expand Down Expand Up @@ -414,7 +414,7 @@ fn handle_proof_of_funding(
//set up the next coinswap address in the route
let coinswap_fees = 10000; //TODO calculate them properly
let incoming_amount = funding_outputs.iter().map(|o| o.value).sum::<u64>();
println!("incoming amount = {}", incoming_amount);
log::trace!(target: "maker", "incoming amount = {}", incoming_amount);
let amount = incoming_amount - coinswap_fees;

let (my_funding_txes, outgoing_swapcoins, timelock_pubkeys) =
Expand All @@ -435,11 +435,11 @@ fn handle_proof_of_funding(
proof.next_locktime,
)?;

println!("my_funding_txes = {:?}", my_funding_txes);
log::trace!(target: "maker", "my_funding_txes = {:?}", my_funding_txes);

connection_state.pending_funding_txes = Some(my_funding_txes);
connection_state.outgoing_swapcoins = Some(outgoing_swapcoins);
println!(
log::trace!(target: "maker",
"incoming_swapcoins = {:?}\noutgoing_swapcoins = {:?}",
connection_state.incoming_swapcoins, connection_state.outgoing_swapcoins,
);
Expand Down Expand Up @@ -529,13 +529,13 @@ fn handle_senders_and_receivers_contract_sigs(
//TODO add coin to watchtowers

for my_funding_tx in connection_state.pending_funding_txes.as_ref().unwrap() {
println!(
log::trace!(target: "maker",
"broadcasting tx = {}",
bitcoin::consensus::encode::serialize_hex(my_funding_tx)
);
let txid = rpc.send_raw_transaction(my_funding_tx)?;
assert_eq!(txid, my_funding_tx.txid());
println!("broadcasted my funding tx, txid={}", txid);
log::trace!(target: "maker", "broadcasted my funding tx, txid={}", txid);
}

//set these to None which might be helpful in picking up logic errors later
Expand Down
12 changes: 6 additions & 6 deletions src/offerbook_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async fn download_maker_offer(host: &str) -> Option<OfferAddress> {
let mut socket = match TcpStream::connect(host).await {
Ok(s) => s,
Err(_e) => {
println!("failed to connect to: {}", host);
log::trace!(target: "offer_book", "failed to connect to: {}", host);
return None;
}
};
Expand All @@ -54,31 +54,31 @@ async fn download_maker_offer(host: &str) -> Option<OfferAddress> {
let mut line1 = String::new();
match socket_reader.read_line(&mut line1).await {
Ok(0) | Err(_) => {
println!("failed to read line");
log::trace!(target: "offer_book", "failed to read line");
return None;
}
Ok(_n) => (),
};
let _makerhello = if let MakerToTakerMessage::MakerHello(m) = parse_message(&line1)? {
m
} else {
println!("wrong protocol message");
log::trace!(target: "offer_book", "wrong protocol message");
return None;
};
println!("maker hello = {:?}", _makerhello);
log::trace!(target: "offer_book", "maker hello = {:?}", _makerhello);

let mut line2 = String::new();
match socket_reader.read_line(&mut line2).await {
Ok(0) | Err(_) => {
println!("failed to read line2");
log::trace!(target: "oofer_book", "failed to read line2");
return None;
}
Ok(_n) => (),
};
let offer = if let MakerToTakerMessage::Offer(o) = parse_message(&line2)? {
o
} else {
println!("wrong protocol message2");
log::trace!(target: "offer_book", "wrong protocol message2");
return None;
};

Expand Down
25 changes: 12 additions & 13 deletions src/taker_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ use crate::wallet_sync::{generate_keypair, CoreAddressLabelType, Wallet, WalletS
pub async fn start_taker(rpc: &Client, wallet: &mut Wallet) {
match run(rpc, wallet).await {
Ok(_o) => (),
Err(e) => println!("err {:?}", e),
Err(e) => log::trace!(target: "taker", "err {:?}", e),
};
}

async fn run(rpc: &Client, wallet: &mut Wallet) -> Result<(), Error> {
let mut offers_addresses = sync_offerbook().await;
println!("offers_addresses = {:?}", offers_addresses);
log::trace!(target: "taker", "offers_addresses = {:?}", offers_addresses);

send_coinswap(rpc, wallet, &mut offers_addresses).await?;
Ok(())
Expand All @@ -76,7 +76,7 @@ async fn send_coinswap(

let first_maker = maker_offers_addresses.last().unwrap();
let last_maker = maker_offers_addresses.first().unwrap().clone();
println!("coinswapping to first maker1 = {}", first_maker.address);
log::trace!(target: "taker", "coinswapping to first maker1 = {}", first_maker.address);

let (
first_maker_multisig_pubkeys,
Expand Down Expand Up @@ -131,7 +131,7 @@ async fn send_coinswap(

for maker_index in 0..maker_count {
let maker = maker_offers_addresses.pop().unwrap();
println!("coinswapping to maker = {:?}", maker.address);
log::trace!(target: "taker", "coinswapping to maker = {:?}", maker.address);
let maker_refund_locktime =
REFUND_LOCKTIME + REFUND_LOCKTIME_STEP * (maker_count - maker_index - 1);
let is_taker_next_peer = maker_index == maker_count - 1;
Expand Down Expand Up @@ -285,7 +285,7 @@ async fn send_coinswap(
}

let mut outgoing_privkeys: Option<Vec<SwapCoinPrivateKey>> = None;
println!(
log::trace!(target: "taker",
"handing over hashvalue and privkeys to = {:?}",
active_maker_addresses
);
Expand Down Expand Up @@ -353,12 +353,12 @@ async fn send_coinswap(
.await?;
}

println!(
log::trace!(target: "taker",
"my outgoing txes = {:#?}",
my_funding_txes.iter().map(|t| t.txid()).collect::<Vec<_>>()
);
for (index, watchonly_swapcoin) in watchonly_swapcoins.iter().enumerate() {
println!(
log::trace!(target: "taker",
"maker[{}] funding txes = {:#?}",
index,
watchonly_swapcoin
Expand All @@ -367,7 +367,7 @@ async fn send_coinswap(
.collect::<Vec<_>>()
);
}
println!(
log::trace!(target: "taker",
"my incoming txes = {:#?}",
incoming_swapcoins
.iter()
Expand Down Expand Up @@ -403,14 +403,13 @@ async fn read_message(reader: &mut BufReader<ReadHalf<'_>>) -> Result<MakerToTak
"EOF",
))));
}
//println!("<== {}", line.trim_end().to_string());

let message: MakerToTakerMessage = match serde_json::from_str(&line) {
Ok(r) => r,
Err(_e) => return Err(Error::Protocol("json parsing error")),
};

println!("<= {:?}", message);
log::trace!(target: "taker", "<= {:?}", message);
Ok(message)
}

Expand All @@ -437,7 +436,7 @@ async fn handshake_maker(
} else {
return Err(Error::Protocol("expected method makerhello"));
};
println!(
log::trace!(target: "taker",
"protocol version min/max = {}/{}",
makerhello.protocol_version_min, makerhello.protocol_version_max
);
Expand Down Expand Up @@ -629,7 +628,7 @@ async fn wait_for_funding_tx_confirmation(
if gettx.info.confirmations >= 1 {
txid_tx_map.insert(*txid, deserialize::<Transaction>(&gettx.hex).unwrap());
txid_blockhash_map.insert(*txid, gettx.info.blockhash.unwrap());
println!("funding tx {} reached 1 confirmation(s)", txid);
log::trace!(target: "taker", "funding tx {} reached 1 confirmation(s)", txid);
}
}
if txid_tx_map.len() == funding_txids.len() {
Expand All @@ -639,7 +638,7 @@ async fn wait_for_funding_tx_confirmation(
#[cfg(test)]
crate::test::generate_1_block(&get_bitcoin_rpc().unwrap());
}
println!("funding transactions confirmed");
log::trace!(target: "taker", "funding transactions confirmed");

let txes = funding_txids
.iter()
Expand Down
12 changes: 6 additions & 6 deletions src/wallet_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ impl Wallet {
return Ok(());
}

println!("new wallet detected, synchronizing balance...");
log::trace!(target: "wallet", "new wallet detected, synchronizing balance...");
self.import_initial_addresses(
rpc,
&hd_descriptors_to_import,
Expand Down Expand Up @@ -529,7 +529,7 @@ impl Wallet {
&[Value::String(rawtx_hex), Value::String(merkleproof)],
)?;
} else {
println!("block pruned, TODO add UTXO to wallet file");
log::error!(target: "wallet", "block pruned, TODO add UTXO to wallet file");
panic!("teleport doesnt work with pruning yet, try rescanning");
}
}
Expand Down Expand Up @@ -616,7 +616,7 @@ impl Wallet {
}
let addr_type = path_chunks[1].parse::<u32>();
if addr_type.is_err() {
println!("unexpected address_type = {}", path);
log::trace!(target: "wallet", "unexpected address_type = {}", path);
return None;
}
let index = path_chunks[2].parse::<i32>();
Expand Down Expand Up @@ -823,7 +823,7 @@ impl Wallet {
output_values.iter(),
change_addresses.iter()
) {
println!("output_value = {} to addr={}", output_value, address);
log::trace!(target: "wallet", "output_value = {} to addr={}", output_value, address);

let mut outputs = HashMap::<String, Amount>::new();
outputs.insert(address.to_string(), Amount::from_sat(output_value));
Expand Down Expand Up @@ -886,7 +886,7 @@ impl Wallet {
};
self.sign_transaction(&mut spending_tx, &decoded_psbt);

println!(
log::trace!(target: "wallet",
"txhex = {}",
bitcoin::consensus::encode::serialize_hex(&spending_tx)
);
Expand Down Expand Up @@ -1010,7 +1010,7 @@ impl Wallet {
.iter()
.map(|other_key| self.create_and_import_coinswap_address(rpc, other_key))
.unzip();
println!("coinswap_addresses = {:#?}", coinswap_addresses);
log::trace!(target: "wallet", "coinswap_addresses = {:#?}", coinswap_addresses);

let (my_funding_txes, utxo_indexes, funding_amounts) =
self.create_spending_txes(rpc, total_coinswap_amount, &coinswap_addresses)?;
Expand Down

0 comments on commit a5aea8c

Please sign in to comment.