diff --git a/Cargo.toml b/Cargo.toml index 18299e11..4a070873 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/main.rs b/src/main.rs index a5089567..9833056e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,7 +35,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(()); } }; @@ -128,14 +128,14 @@ fn display_wallet_balance(wallet_file_name: &PathBuf, long_form: Option) { 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; } }; @@ -234,7 +234,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; } }; @@ -245,14 +245,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; } }; @@ -272,14 +272,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; } }; @@ -294,14 +294,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; } }; diff --git a/src/maker_protocol.rs b/src/maker_protocol.rs index ba78adc1..f0d26b83 100644 --- a/src/maker_protocol.rs +++ b/src/maker_protocol.rs @@ -66,7 +66,7 @@ struct ConnectionState { async fn run(rpc: Arc, wallet: Arc>, 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::(100); let mut accepting_clients = true; @@ -133,7 +133,7 @@ async fn run(rpc: Arc, wallet: Arc>, 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) => (), @@ -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; @@ -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), @@ -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 ); @@ -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::::new()); for (funding_info, &funding_output_index, &funding_output, &incoming_swapcoin_keys) in izip!( @@ -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::(); - 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) = @@ -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, ); @@ -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 diff --git a/src/offerbook_sync.rs b/src/offerbook_sync.rs index c8422bda..62065e24 100644 --- a/src/offerbook_sync.rs +++ b/src/offerbook_sync.rs @@ -31,7 +31,7 @@ async fn download_maker_offer(host: &str) -> Option { 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; } }; @@ -54,7 +54,7 @@ async fn download_maker_offer(host: &str) -> Option { 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) => (), @@ -62,15 +62,15 @@ async fn download_maker_offer(host: &str) -> Option { 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) => (), @@ -78,7 +78,7 @@ async fn download_maker_offer(host: &str) -> Option { 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; }; diff --git a/src/taker_protocol.rs b/src/taker_protocol.rs index c76254ef..a133888f 100644 --- a/src/taker_protocol.rs +++ b/src/taker_protocol.rs @@ -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(()) @@ -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, @@ -136,7 +136,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; @@ -294,7 +294,7 @@ async fn send_coinswap( wallet.update_swap_coins_list().unwrap(); let mut outgoing_privkeys: Option> = None; - println!( + log::trace!(target: "taker", "handing over hashvalue and privkeys to = {:?}", active_maker_addresses ); @@ -362,12 +362,12 @@ async fn send_coinswap( .await?; } - println!( + log::trace!(target: "taker", "my outgoing txes = {:#?}", my_funding_txes.iter().map(|t| t.txid()).collect::>() ); for (index, watchonly_swapcoin) in watchonly_swapcoins.iter().enumerate() { - println!( + log::trace!(target: "taker", "maker[{}] funding txes = {:#?}", index, watchonly_swapcoin @@ -376,7 +376,7 @@ async fn send_coinswap( .collect::>() ); } - println!( + log::trace!(target: "taker", "my incoming txes = {:#?}", incoming_swapcoins .iter() @@ -417,14 +417,13 @@ async fn read_message(reader: &mut BufReader>) -> Result r, Err(_e) => return Err(Error::Protocol("json parsing error")), }; - println!("<= {:?}", message); + log::trace!(target: "taker", "<= {:?}", message); Ok(message) } @@ -451,7 +450,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 ); @@ -643,7 +642,7 @@ async fn wait_for_funding_tx_confirmation( if gettx.info.confirmations >= 1 { txid_tx_map.insert(*txid, deserialize::(&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() { @@ -653,7 +652,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() diff --git a/src/wallet_sync.rs b/src/wallet_sync.rs index 12fbbc79..99318fb7 100644 --- a/src/wallet_sync.rs +++ b/src/wallet_sync.rs @@ -499,7 +499,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, @@ -533,7 +533,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"); } } @@ -667,7 +667,7 @@ impl Wallet { } let addr_type = path_chunks[1].parse::(); 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::(); @@ -874,7 +874,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::::new(); outputs.insert(address.to_string(), Amount::from_sat(output_value)); @@ -937,7 +937,7 @@ impl Wallet { }; self.sign_transaction(&mut spending_tx, &decoded_psbt); - println!( + log::trace!(target: "wallet", "txhex = {}", bitcoin::consensus::encode::serialize_hex(&spending_tx) ); @@ -1061,7 +1061,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)?;