From 6956639f32ad84c6f0e348457e00f6da058a0c03 Mon Sep 17 00:00:00 2001 From: fbock Date: Fri, 23 Aug 2024 11:07:16 +0200 Subject: [PATCH] add atomic bool to disable logging output for escrow cli input --- taptrade-cli-demo/coordinator/Cargo.lock | 62 +++++++++++++++++++ taptrade-cli-demo/coordinator/Cargo.toml | 1 + .../coordinator/src/communication/mod.rs | 2 +- .../coordinator/src/database/mod.rs | 29 ++------- taptrade-cli-demo/coordinator/src/main.rs | 53 +++++++++++++--- .../coordinator/src/wallet/mod.rs | 2 +- 6 files changed, 112 insertions(+), 37 deletions(-) diff --git a/taptrade-cli-demo/coordinator/Cargo.lock b/taptrade-cli-demo/coordinator/Cargo.lock index a624712..278df88 100644 --- a/taptrade-cli-demo/coordinator/Cargo.lock +++ b/taptrade-cli-demo/coordinator/Cargo.lock @@ -44,6 +44,21 @@ version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f" +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "anstream" version = "0.6.15" @@ -463,6 +478,20 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401" +dependencies = [ + "android-tzdata", + "iana-time-zone", + "js-sys", + "num-traits", + "wasm-bindgen", + "windows-targets 0.52.6", +] + [[package]] name = "colorchoice" version = "1.0.2" @@ -493,6 +522,7 @@ dependencies = [ "bdk", "bincode", "bitcoin 0.32.2", + "chrono", "dotenvy", "env_logger", "futures-util", @@ -1206,6 +1236,29 @@ dependencies = [ "tracing", ] +[[package]] +name = "iana-time-zone" +version = "0.1.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7ffbb5a1b541ea2561f8c41c087286cc091e21e556a4f09a8f6cbf17b69b141" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + [[package]] name = "ident_case" version = "1.0.1" @@ -3039,6 +3092,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets 0.52.6", +] + [[package]] name = "windows-sys" version = "0.48.0" diff --git a/taptrade-cli-demo/coordinator/Cargo.toml b/taptrade-cli-demo/coordinator/Cargo.toml index de876aa..30f30b2 100644 --- a/taptrade-cli-demo/coordinator/Cargo.toml +++ b/taptrade-cli-demo/coordinator/Cargo.toml @@ -28,6 +28,7 @@ sha2 = "0.10" validator = { version = "0.18", features = ["derive"] } musig2 = "0.0.11" bincode = "1.3.3" +chrono = "0.4.38" [profile.release] lto = true diff --git a/taptrade-cli-demo/coordinator/src/communication/mod.rs b/taptrade-cli-demo/coordinator/src/communication/mod.rs index deae6c6..0c74dc3 100755 --- a/taptrade-cli-demo/coordinator/src/communication/mod.rs +++ b/taptrade-cli-demo/coordinator/src/communication/mod.rs @@ -191,7 +191,7 @@ async fn submit_obligation_confirmation( // or // gets called if one of the traders wants to initiate escrow (e.g. claiming they didn't receive the fiat) -// before timeout ends +// before timeout ends, just sets the maker unhappy and escrow onging flag in the db async fn request_escrow( Extension(coordinator): Extension>, Json(payload): Json, diff --git a/taptrade-cli-demo/coordinator/src/database/mod.rs b/taptrade-cli-demo/coordinator/src/database/mod.rs index 790e5b8..6b71844 100644 --- a/taptrade-cli-demo/coordinator/src/database/mod.rs +++ b/taptrade-cli-demo/coordinator/src/database/mod.rs @@ -150,7 +150,7 @@ impl CoordinatorDB { ) .execute(&db_pool) .await?; - dbg!("Database initialized"); + debug!("Database initialized"); let shared_db_pool = Arc::new(db_pool); Ok(Self { db_pool: shared_db_pool, @@ -745,6 +745,8 @@ impl CoordinatorDB { Ok(()) } + // checked by the payout handler on request to determine if the trade is ready for payout and + // if escrow is required pub async fn fetch_trader_happiness(&self, offer_id: &str) -> Result { let row = sqlx::query( "SELECT maker_happy, taker_happy, escrow_ongoing FROM taken_offers WHERE offer_id = ?", @@ -764,6 +766,7 @@ impl CoordinatorDB { }) } + // this will be checked by the payout handler on request, the escrow winner will be set trough CLI input pub async fn fetch_escrow_result(&self, offer_id: &str) -> Result> { let row = sqlx::query("SELECT escrow_winner_robohash FROM taken_offers WHERE offer_id = ?") .bind(offer_id) @@ -776,30 +779,6 @@ impl CoordinatorDB { Ok(winner_robohash) } - // pub async fn fetch_escrow_tx_payout_data( - // &self, - // offer_id: &str, - // ) -> Result { - // let row = sqlx::query("SELECT taproot_xonly_pubkey_hex_maker, taproot_xonly_pubkey_hex_taker, musig_pubkey_compressed_hex_maker, musig_pubkey_compressed_hex_taker FROM taken_offers WHERE offer_id = ?") - // .bind(offer_id) - // .fetch_one(&*self.db_pool) - // .await?; - - // let taproot_xonly_pubkey_hex_maker: String = row.get("taproot_xonly_pubkey_hex_maker"); - // let taproot_xonly_pubkey_hex_taker: String = row.get("taproot_xonly_pubkey_hex_taker"); - // let musig_pubkey_compressed_hex_maker: String = - // row.get("musig_pubkey_compressed_hex_maker"); - // let musig_pubkey_compressed_hex_taker: String = - // row.get("musig_pubkey_compressed_hex_taker"); - - // Ok(EscrowPsbtConstructionData { - // taproot_xonly_pubkey_hex_maker, - // taproot_xonly_pubkey_hex_taker, - // musig_pubkey_compressed_hex_maker, - // musig_pubkey_compressed_hex_taker, - // }) - // } - pub async fn get_escrow_tx_amounts( &self, trade_id: &str, diff --git a/taptrade-cli-demo/coordinator/src/main.rs b/taptrade-cli-demo/coordinator/src/main.rs index 0ac6091..507da4c 100755 --- a/taptrade-cli-demo/coordinator/src/main.rs +++ b/taptrade-cli-demo/coordinator/src/main.rs @@ -3,16 +3,6 @@ mod coordinator; mod database; mod wallet; -use std::{ - collections::{HashMap, HashSet}, - env, fmt, - net::SocketAddr, - ops::Deref, - str::FromStr, - sync::{Arc, RwLock}, - time::{SystemTime, UNIX_EPOCH}, -}; - use anyhow::{anyhow, Context, Result}; use axum::{ http::StatusCode, @@ -42,6 +32,7 @@ use bdk::{ wallet::verify::*, KeychainKind, SignOptions, SyncOptions, Wallet, }; +use chrono::Local; use communication::{api::*, api_server, communication_utils::*, handler_errors::*}; use coordinator::{ bond_monitoring::*, coordinator_utils::*, mempool_monitoring::MempoolHandler, @@ -58,6 +49,19 @@ use musig2::{ use rand::Rng; use serde::{Deserialize, Serialize}; use sqlx::{sqlite::SqlitePoolOptions, Pool, Row, Sqlite}; +use std::{ + collections::{HashMap, HashSet}, + env, fmt, + io::Write, + net::SocketAddr, + ops::Deref, + str::FromStr, + sync::{ + atomic::{AtomicBool, Ordering}, + Arc, RwLock, + }, + time::{SystemTime, UNIX_EPOCH}, +}; use tokio::{ net::TcpListener, sync::{oneshot, Mutex}, @@ -70,12 +74,41 @@ pub struct Coordinator { pub coordinator_wallet: Arc>, } +static LOGGING_ENABLED: AtomicBool = AtomicBool::new(true); + +fn get_logging_color_code(level: log::Level) -> &'static str { + match level { + log::Level::Error => "\x1B[31m", // Red + log::Level::Warn => "\x1B[33m", // Yellow + log::Level::Info => "\x1B[32m", // Green + log::Level::Debug => "\x1B[34m", // Blue + log::Level::Trace => "\x1B[36m", // Cyan + } +} + // populate .env with values before starting #[tokio::main] async fn main() -> Result<()> { env_logger::builder() .filter_module("coordinator", log::LevelFilter::Trace) .filter_level(log::LevelFilter::Info) + .format(|buf, record| { + if LOGGING_ENABLED.load(Ordering::Relaxed) { + let level = record.level(); + let color_code = get_logging_color_code(level); + writeln!( + buf, + "{} [{}{}{}] - {}", + Local::now().format("%Y-%m-%d %H:%M:%S"), + color_code, + level, + "\x1B[0m", // Reset color + record.args() + ) + } else { + Ok(()) + } + }) .init(); dotenv().ok(); debug!("Starting coordinator"); diff --git a/taptrade-cli-demo/coordinator/src/wallet/mod.rs b/taptrade-cli-demo/coordinator/src/wallet/mod.rs index b780993..17a8048 100644 --- a/taptrade-cli-demo/coordinator/src/wallet/mod.rs +++ b/taptrade-cli-demo/coordinator/src/wallet/mod.rs @@ -76,7 +76,7 @@ pub async fn init_coordinator_wallet() -> Result