From b4571d7f9e8e0eba70522c7561390097ea5fa596 Mon Sep 17 00:00:00 2001 From: mojoX911 Date: Mon, 4 Nov 2024 23:33:13 +0530 Subject: [PATCH 1/5] use optional wallet name --- src/bin/makerd.rs | 8 ++++---- src/bin/taker.rs | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/bin/makerd.rs b/src/bin/makerd.rs index cae9fe3dd..17eae662c 100644 --- a/src/bin/makerd.rs +++ b/src/bin/makerd.rs @@ -46,8 +46,8 @@ struct Cli { )] pub rpc_network: String, /// Sets the maker wallet's name. If the wallet file already exists at data-directory, it will load that wallet. - #[clap(name = "WALLET", long, short = 'w', default_value = "maker")] - pub wallet_name: String, + #[clap(name = "WALLET", long, short = 'w')] + pub wallet_name: Option, } fn main() -> Result<(), MakerError> { @@ -63,12 +63,12 @@ fn main() -> Result<(), MakerError> { url: args.rpc, auth: Auth::UserPass(args.auth.0, args.auth.1), network: rpc_network, - wallet_name: args.wallet_name.clone(), + wallet_name: "random".to_string(), // we can put anything here as it will get updated in the init. }; let maker = Arc::new(Maker::init( args.data_directory, - Some(args.wallet_name), + args.wallet_name, Some(rpc_config), None, None, diff --git a/src/bin/taker.rs b/src/bin/taker.rs index 79c1dcee7..9c341cbe7 100644 --- a/src/bin/taker.rs +++ b/src/bin/taker.rs @@ -39,8 +39,8 @@ struct Cli { )] pub bitcoin_network: String, /// Sets the taker wallet's name. If the wallet file already exists at data-directory, it will load that wallet. - #[clap(name = "WALLET", long, short = 'w', default_value = "taker")] - pub wallet_name: String, + #[clap(name = "WALLET", long, short = 'w')] + pub wallet_name: Option, /// Sets the verbosity level of logs. /// Default: Determined by the command passed. #[clap(long, short = 'v', possible_values = &["off", "error", "warn", "info", "debug", "trace"])] @@ -109,7 +109,7 @@ fn main() -> Result<(), TakerError> { url: args.rpc, auth: Auth::UserPass(args.auth.0, args.auth.1), network: rpc_network, - wallet_name: args.wallet_name.clone(), + wallet_name: "random".to_string(), // we can put anything here as it will get updated in the init. }; let swap_params = SwapParams { @@ -122,7 +122,7 @@ fn main() -> Result<(), TakerError> { let mut taker = Taker::init( args.data_directory.clone(), - Some(args.wallet_name.clone()), + args.wallet_name.clone(), Some(rpc_config.clone()), TakerBehavior::Normal, Some(connection_type), From bf9f0c0b23b5815a4349a29629afee96a4cb8c5e Mon Sep 17 00:00:00 2001 From: mojoX911 Date: Mon, 4 Nov 2024 23:34:06 +0530 Subject: [PATCH 2/5] add dynamic delays for fidelity sync --- src/maker/server.rs | 24 +++++++++++---------- src/wallet/fidelity.rs | 47 ++++++++++++++++++++---------------------- 2 files changed, 35 insertions(+), 36 deletions(-) diff --git a/src/maker/server.rs b/src/maker/server.rs index 3677a545b..8e51e1512 100644 --- a/src/maker/server.rs +++ b/src/maker/server.rs @@ -211,7 +211,7 @@ fn setup_fidelity_bond(maker: &Arc, maker_address: &str) -> Result<(), Ma .get_block_count() .map_err(WalletError::Rpc)? as u32; - // Set 100 blocks locktime for test + // Set 300 blocks locktime for test let locktime = if cfg!(feature = "integration-test") { LockTime::from_height(current_height + 100).map_err(WalletError::Locktime)? } else { @@ -219,16 +219,14 @@ fn setup_fidelity_bond(maker: &Arc, maker_address: &str) -> Result<(), Ma .map_err(WalletError::Locktime)? }; - let sleep_delay = if cfg!(feature = "integration-test") { - 3 // Wait for 3 sec in tests - } else { - 300 // Wait for 5 mins in production - }; + let sleep_increment = 10; + let mut sleep_multiplier = 0; log::info!("Fidelity value chosen = {:?} BTC", amount.to_btc()); log::info!("Fidelity Tx fee = 1000 sats"); while !maker.shutdown.load(Relaxed) { + sleep_multiplier += 1; // sync the wallet maker.get_wallet().write()?.sync()?; @@ -250,10 +248,11 @@ fn setup_fidelity_bond(maker: &Arc, maker_address: &str) -> Result<(), Ma let amount = required - available; let addr = maker.get_wallet().write()?.get_next_external_address()?; - log::info!("Send > {:?} BTC to {:?}. Extra will be used for swaps after fidelity creation.", amount, addr); - log::info!("Next sync in {:?} secs", sleep_delay); + log::info!("Send at least {:?} BTC to {:?} | If you send extra, that will be added to your swap balance", amount, addr); - thread::sleep(Duration::from_secs(sleep_delay)); + let total_sleep = sleep_increment * sleep_multiplier.min(10 * 60); + log::info!("Next sync in {:?} secs", total_sleep); + thread::sleep(Duration::from_secs(total_sleep)); } else { log::error!( "[{}] Fidelity Bond Creation failed: {:?}. Shutting Down Maker server", @@ -406,6 +405,8 @@ pub fn start_maker_server(maker: Arc) -> Result<(), MakerError> { maker_address ); + let heart_beat_interval = maker.config.heart_beat_interval_secs; // All maker internal threads loops at this frequency. + // Setup the wallet with fidelity bond. let network = maker.get_wallet().read()?.store.network; let balance = maker.get_wallet().read()?.balance()?; @@ -475,6 +476,8 @@ pub fn start_maker_server(maker: Arc) -> Result<(), MakerError> { })?; thread_pool.push(rpc_thread); + + sleep(Duration::from_secs(heart_beat_interval)); // wait for 1 beat, to complete spawns of all the threads. maker.setup_complete()?; log::info!("[{}] Maker setup is ready", maker.config.port); } @@ -484,12 +487,11 @@ pub fn start_maker_server(maker: Arc) -> Result<(), MakerError> { // This loop beats at `maker.config.heart_beat_interval_secs` while !maker.shutdown.load(Relaxed) { let maker = maker.clone(); // This clone is needed to avoid moving the Arc in each iterations. - let heart_beat_interval = maker.config.heart_beat_interval_secs; // Block client connections if accepting_client=false if !*accepting_clients.lock()? { log::warn!( - "[{}] Bitcoin Core RPC Connection broken. Not accepting clients temporarily", + "[{}] Temporary failure in backend node. Not accepting swap request. Check your node if this error persists", maker.config.port ); sleep(Duration::from_secs(heart_beat_interval)); diff --git a/src/wallet/fidelity.rs b/src/wallet/fidelity.rs index 87baf065e..f140aa4e4 100644 --- a/src/wallet/fidelity.rs +++ b/src/wallet/fidelity.rs @@ -371,29 +371,25 @@ impl Wallet { let txid = self.rpc.send_raw_transaction(&tx)?; - let sleep_delay = if cfg!(feature = "integration-test") { - 1 // wait for 1 sec in tests - } else { - 60 // wait for 1 min in production - }; + let sleep_increment = 10; + let mut sleep_multiplier = 0; let conf_height = loop { - if let Ok(get_tx_result) = self.rpc.get_transaction(&txid, None) { - if let Some(ht) = get_tx_result.info.blockheight { - log::info!("Fidelity Bond confirmed at blockheight: {}", ht); - break ht; - } else { - log::info!( - "Fildelity Transaction {} seen in mempool, waiting for confirmation.", - txid - ); - - log::info!("Next sync in {:?} secs", sleep_delay); + sleep_multiplier += 1; - thread::sleep(Duration::from_secs(sleep_delay)); - } + let get_tx_result = self.rpc.get_transaction(&txid, None)?; + if let Some(ht) = get_tx_result.info.blockheight { + log::info!("Fidelity Bond confirmed at blockheight: {}", ht); + break ht; } else { - log::info!("Waiting for {} in mempool", txid); + log::info!( + "Fildelity Transaction {} seen in mempool, waiting for confirmation.", + txid + ); + + let total_sleep = sleep_increment * sleep_multiplier.min(10 * 60); // Caps at 1 Block interval i.e 10 mins + log::info!("Next sync in {:?} secs", total_sleep); + thread::sleep(Duration::from_secs(total_sleep)); } }; @@ -467,7 +463,11 @@ impl Wallet { let txid = self.rpc.send_raw_transaction(&tx)?; + let sleep_increment = 10; + let mut sleep_multiplier = 0; + let conf_height = loop { + sleep_multiplier += 1; if let Ok(get_tx_result) = self.rpc.get_transaction(&txid, None) { if let Some(ht) = get_tx_result.info.blockheight { log::info!("Fidelity Bond confirmed at blockheight: {}", ht); @@ -478,12 +478,9 @@ impl Wallet { txid ); - if cfg!(feature = "integration-test") { - thread::sleep(Duration::from_secs(1)); // wait for 1 sec in tests - } else { - thread::sleep(Duration::from_secs(60)); // wait for 1 min in prod - } - + let total_sleep = sleep_increment * sleep_multiplier.min(10 * 60); + log::info!("Next sync in {:?} secs", total_sleep); + thread::sleep(Duration::from_secs(total_sleep)); continue; } } else { From 546fb89072140cf8511bb72426cea23e99a46ffc Mon Sep 17 00:00:00 2001 From: mojoX911 Date: Mon, 4 Nov 2024 23:34:28 +0530 Subject: [PATCH 3/5] make rpc logs debug --- src/wallet/rpc.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/wallet/rpc.rs b/src/wallet/rpc.rs index 8f62badbd..d766aea5f 100644 --- a/src/wallet/rpc.rs +++ b/src/wallet/rpc.rs @@ -79,10 +79,10 @@ impl Wallet { // Create or load the watch-only bitcoin core wallet let wallet_name = &self.store.file_name; if self.rpc.list_wallets()?.contains(wallet_name) { - log::info!("wallet already loaded: {}", wallet_name); + log::debug!("wallet already loaded: {}", wallet_name); } else if list_wallet_dir(&self.rpc)?.contains(wallet_name) { self.rpc.load_wallet(wallet_name)?; - log::info!("wallet loaded: {}", wallet_name); + log::debug!("wallet loaded: {}", wallet_name); } else { // pre-0.21 use legacy wallets if self.rpc.version()? < 210_000 { @@ -101,7 +101,7 @@ impl Wallet { let _: Value = self.rpc.call("createwallet", &args)?; } - log::info!("wallet created: {}", wallet_name); + log::debug!("wallet created: {}", wallet_name); } let descriptors_to_import = self.descriptors_to_import()?; @@ -126,7 +126,7 @@ impl Wallet { .unwrap_or(0) .max(self.store.wallet_birthday.unwrap_or(0)); let node_synced = self.rpc.get_block_count()?; - log::info!( + log::debug!( "rescan_blockchain from:{} to:{}", last_synced_height, node_synced From 1579552b207d39bfb268e37a4f8ef4350896b024 Mon Sep 17 00:00:00 2001 From: mojoX911 Date: Wed, 6 Nov 2024 20:13:16 +0530 Subject: [PATCH 4/5] fix fidelity test --- src/maker/server.rs | 6 +++--- src/wallet/fidelity.rs | 10 +++++++++- tests/fidelity.rs | 9 +++++---- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/maker/server.rs b/src/maker/server.rs index 8e51e1512..8fa6088b4 100644 --- a/src/maker/server.rs +++ b/src/maker/server.rs @@ -213,7 +213,7 @@ fn setup_fidelity_bond(maker: &Arc, maker_address: &str) -> Result<(), Ma // Set 300 blocks locktime for test let locktime = if cfg!(feature = "integration-test") { - LockTime::from_height(current_height + 100).map_err(WalletError::Locktime)? + LockTime::from_height(current_height + 300).map_err(WalletError::Locktime)? } else { LockTime::from_height(maker.config.fidelity_timelock + current_height) .map_err(WalletError::Locktime)? @@ -248,7 +248,7 @@ fn setup_fidelity_bond(maker: &Arc, maker_address: &str) -> Result<(), Ma let amount = required - available; let addr = maker.get_wallet().write()?.get_next_external_address()?; - log::info!("Send at least {:?} BTC to {:?} | If you send extra, that will be added to your swap balance", amount, addr); + log::info!("Send at least {:.8} BTC to {:?} | If you send extra, that will be added to your swap balance", amount, addr); let total_sleep = sleep_increment * sleep_multiplier.min(10 * 60); log::info!("Next sync in {:?} secs", total_sleep); @@ -490,7 +490,7 @@ pub fn start_maker_server(maker: Arc) -> Result<(), MakerError> { // Block client connections if accepting_client=false if !*accepting_clients.lock()? { - log::warn!( + log::debug!( "[{}] Temporary failure in backend node. Not accepting swap request. Check your node if this error persists", maker.config.port ); diff --git a/src/wallet/fidelity.rs b/src/wallet/fidelity.rs index f140aa4e4..45a6e3297 100644 --- a/src/wallet/fidelity.rs +++ b/src/wallet/fidelity.rs @@ -47,6 +47,7 @@ pub enum FidelityError { WrongScriptType, BondDoesNotExist, BondAlreadySpent, + BondLocktimeExpired, CertExpired, General(String), } @@ -263,7 +264,14 @@ impl Wallet { (info.height, info.time as u64) }; // Estimated locktime from block height = [current-time + (maturity-height - block-count) * 10 * 60] sec - tip_time + ((blocks.to_consensus_u32() as u64 - tip_height as u64) * 10 * 60) + let height_diff = + if let Some(x) = blocks.to_consensus_u32().checked_sub(tip_height as u32) { + x as u64 + } else { + return Err(FidelityError::BondLocktimeExpired.into()); + }; + + tip_time + (height_diff * 10 * 60) } LockTime::Seconds(sec) => sec.to_consensus_u32() as u64, }; diff --git a/tests/fidelity.rs b/tests/fidelity.rs index 950e2760e..61151ee0b 100644 --- a/tests/fidelity.rs +++ b/tests/fidelity.rs @@ -52,7 +52,7 @@ fn test_fidelity() { let maker_thread = thread::spawn(move || start_maker_server(maker_clone)); - thread::sleep(Duration::from_secs(12)); + thread::sleep(Duration::from_secs(6)); // TODO: Assert that fund request for fidelity is printed in the log. @@ -78,7 +78,7 @@ fn test_fidelity() { let bond_value = wallet_read .calculate_bond_value(highest_bond_index) .unwrap(); - assert_eq!(bond_value, Amount::from_sat(185)); + assert_eq!(bond_value, Amount::from_sat(1960)); let (bond, _, is_spent) = wallet_read .get_fidelity_bonds() @@ -93,13 +93,14 @@ fn test_fidelity() { // Create another fidelity bond of 0.08 BTC and validate it. let second_maturity_height = { + log::info!("Creating another fidelity bond using the `create_fidelity` API"); let index = maker .get_wallet() .write() .unwrap() .create_fidelity( Amount::from_sat(8000000), - LockTime::from_height((test_framework.get_block_count() as u32) + 150).unwrap(), + LockTime::from_height((test_framework.get_block_count() as u32) + 300).unwrap(), ) .unwrap(); @@ -110,7 +111,7 @@ fn test_fidelity() { assert_eq!(highest_bond_index, index); let bond_value = wallet_read.calculate_bond_value(index).unwrap(); - assert_eq!(bond_value, Amount::from_sat(1801)); + assert_eq!(bond_value, Amount::from_sat(4231)); let (bond, _, is_spent) = wallet_read.get_fidelity_bonds().get(&index).unwrap(); assert_eq!(bond.amount, Amount::from_sat(8000000)); From 33d48f3979dd68b75ea4858d6afde9987aa39315 Mon Sep 17 00:00:00 2001 From: KnowWhoami Date: Fri, 8 Nov 2024 15:38:08 +0530 Subject: [PATCH 5/5] fixes --- src/maker/server.rs | 4 ++-- src/wallet/fidelity.rs | 30 +++++++++++++----------------- tests/fidelity.rs | 6 +++--- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src/maker/server.rs b/src/maker/server.rs index 8fa6088b4..8c5f92235 100644 --- a/src/maker/server.rs +++ b/src/maker/server.rs @@ -211,9 +211,9 @@ fn setup_fidelity_bond(maker: &Arc, maker_address: &str) -> Result<(), Ma .get_block_count() .map_err(WalletError::Rpc)? as u32; - // Set 300 blocks locktime for test + // Set 150 blocks locktime for test let locktime = if cfg!(feature = "integration-test") { - LockTime::from_height(current_height + 300).map_err(WalletError::Locktime)? + LockTime::from_height(current_height + 150).map_err(WalletError::Locktime)? } else { LockTime::from_height(maker.config.fidelity_timelock + current_height) .map_err(WalletError::Locktime)? diff --git a/src/wallet/fidelity.rs b/src/wallet/fidelity.rs index 45a6e3297..f29d8b18b 100644 --- a/src/wallet/fidelity.rs +++ b/src/wallet/fidelity.rs @@ -476,24 +476,20 @@ impl Wallet { let conf_height = loop { sleep_multiplier += 1; - if let Ok(get_tx_result) = self.rpc.get_transaction(&txid, None) { - if let Some(ht) = get_tx_result.info.blockheight { - log::info!("Fidelity Bond confirmed at blockheight: {}", ht); - break ht; - } else { - log::info!( - "Fildelity Transaction {} seen in mempool, waiting for confirmation.", - txid - ); - - let total_sleep = sleep_increment * sleep_multiplier.min(10 * 60); - log::info!("Next sync in {:?} secs", total_sleep); - thread::sleep(Duration::from_secs(total_sleep)); - continue; - } + + let get_tx_result = self.rpc.get_transaction(&txid, None)?; + if let Some(ht) = get_tx_result.info.blockheight { + log::info!("Fidelity Bond confirmed at blockheight: {}", ht); + break ht; } else { - log::info!("Waiting for {} in mempool", txid); - continue; + log::info!( + "Redeem Fildelity Transaction {} seen in mempool, waiting for confirmation.", + txid + ); + + let total_sleep = sleep_increment * sleep_multiplier.min(10 * 60); // Caps at 1 Block interval i.e 10 mins + log::info!("Next sync in {:?} secs", total_sleep); + thread::sleep(Duration::from_secs(total_sleep)); } }; diff --git a/tests/fidelity.rs b/tests/fidelity.rs index 61151ee0b..42c3e9b98 100644 --- a/tests/fidelity.rs +++ b/tests/fidelity.rs @@ -78,7 +78,7 @@ fn test_fidelity() { let bond_value = wallet_read .calculate_bond_value(highest_bond_index) .unwrap(); - assert_eq!(bond_value, Amount::from_sat(1960)); + assert_eq!(bond_value, Amount::from_sat(542)); let (bond, _, is_spent) = wallet_read .get_fidelity_bonds() @@ -100,7 +100,7 @@ fn test_fidelity() { .unwrap() .create_fidelity( Amount::from_sat(8000000), - LockTime::from_height((test_framework.get_block_count() as u32) + 300).unwrap(), + LockTime::from_height((test_framework.get_block_count() as u32) + 150).unwrap(), ) .unwrap(); @@ -111,7 +111,7 @@ fn test_fidelity() { assert_eq!(highest_bond_index, index); let bond_value = wallet_read.calculate_bond_value(index).unwrap(); - assert_eq!(bond_value, Amount::from_sat(4231)); + assert_eq!(bond_value, Amount::from_sat(1317)); let (bond, _, is_spent) = wallet_read.get_fidelity_bonds().get(&index).unwrap(); assert_eq!(bond.amount, Amount::from_sat(8000000));