From 238006c108bd551aed1fadbcf7ec9653b33f54de Mon Sep 17 00:00:00 2001 From: claddy <0xcladdy@gmail.com> Date: Sat, 30 Nov 2024 18:25:06 +0530 Subject: [PATCH] Add threadpool to join the recovery threads --- Cargo.toml | 1 + src/maker/api.rs | 66 ++++++++++++++++++++++++------------------------ 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9f020574..2df2f13e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ libtor = { version = "47.13.0", optional = true, features = ["vendored-openssl"] mitosis = { version = "0.1.1", optional = true } log4rs = "1.3.0" openssl-sys = { version = "0.9.68", optional = true } +threadpool = "1.8.1" #Empty default feature set, (helpful to generalise in github actions) [features] diff --git a/src/maker/api.rs b/src/maker/api.rs index 6a3235f0..e7e86d5b 100644 --- a/src/maker/api.rs +++ b/src/maker/api.rs @@ -6,17 +6,24 @@ //! contract broadcasts and handle idle Taker connections. Additionally, it handles recovery by broadcasting //! contract transactions and claiming funds after an unsuccessful swap event. -use std::{ - collections::HashMap, - net::IpAddr, - path::PathBuf, - sync::{ - atomic::{AtomicBool, Ordering::Relaxed}, - Arc, Mutex, RwLock, +use super::{config::MakerConfig, error::MakerError}; +use crate::{ + protocol::{ + contract::{ + check_hashlock_has_pubkey, check_hashvalues_are_equal, check_multisig_has_pubkey, + check_reedemscript_is_multisig, find_funding_output_index, read_contract_locktime, + }, + messages::{FidelityProof, ProofOfFunding, ReqContractSigsForSender}, + Hash160, + }, + utill::{ + get_maker_dir, redeemscript_to_scriptpubkey, seed_phrase_to_unique_id, ConnectionType, + }, + wallet::{ + IncomingSwapCoin, OutgoingSwapCoin, RPCConfig, SwapCoin, Wallet, WalletError, + WalletSwapCoin, }, - time::{Duration, Instant}, }; - use bip39::Mnemonic; use bitcoin::{ ecdsa::Signature, @@ -24,35 +31,21 @@ use bitcoin::{ OutPoint, PublicKey, ScriptBuf, Transaction, }; use bitcoind::bitcoincore_rpc::RpcApi; - -use crate::{ - protocol::{ - contract::check_hashvalues_are_equal, - messages::{FidelityProof, ReqContractSigsForSender}, - Hash160, - }, - utill::{ - get_maker_dir, redeemscript_to_scriptpubkey, seed_phrase_to_unique_id, ConnectionType, - }, - wallet::{RPCConfig, SwapCoin, WalletSwapCoin}, -}; - -use crate::{ - protocol::{ - contract::{ - check_hashlock_has_pubkey, check_multisig_has_pubkey, check_reedemscript_is_multisig, - find_funding_output_index, read_contract_locktime, - }, - messages::ProofOfFunding, +use std::{ + collections::HashMap, + net::IpAddr, + path::PathBuf, + sync::{ + atomic::{AtomicBool, Ordering::Relaxed}, }, - wallet::{IncomingSwapCoin, OutgoingSwapCoin, Wallet, WalletError}, + thread, + time::{Duration, Instant}, }; -use super::{config::MakerConfig, error::MakerError}; - use crate::maker::server::{ HEART_BEAT_INTERVAL_SECS, MIN_CONTRACT_REACTION_TIME, REQUIRED_CONFIRMS, }; +use threadpool::ThreadPool; /// Used to configure the maker for testing purposes. #[derive(Debug, Clone, Copy)] @@ -108,6 +101,8 @@ pub struct Maker { pub highest_fidelity_proof: RwLock>, /// Is setup complete pub is_setup_complete: AtomicBool, + /// ThreadPool for managing recovery tasks + pub threadpool: Arc, } #[allow(clippy::too_many_arguments)] @@ -140,6 +135,8 @@ impl Maker { MakerBehavior::Normal }; + let pool = ThreadPool::new(4); + // Get provided data directory or the default data directory. let data_dir = if cfg!(feature = "integration-test") { // We only append port number in data-dir for integration test @@ -222,6 +219,7 @@ impl Maker { connection_state: Mutex::new(HashMap::new()), highest_fidelity_proof: RwLock::new(None), is_setup_complete: AtomicBool::new(false), + threadpool: Arc::new(pool), }) } @@ -484,7 +482,9 @@ pub fn check_for_broadcasted_contracts(maker: Arc) -> Result<(), MakerErr std::thread::sleep(Duration::from_secs(HEART_BEAT_INTERVAL_SECS)); } - + if maker.shutdown.load(Relaxed) { + maker.threadpool.join(); + } Ok(()) }