Skip to content

Commit

Permalink
Add threadpool to join the recovery threads
Browse files Browse the repository at this point in the history
  • Loading branch information
claddyy committed Nov 30, 2024
1 parent 0835a5a commit 238006c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 33 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
66 changes: 33 additions & 33 deletions src/maker/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,46 @@
//! 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,
secp256k1::{self, Secp256k1},
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)]
Expand Down Expand Up @@ -108,6 +101,8 @@ pub struct Maker {
pub highest_fidelity_proof: RwLock<Option<FidelityProof>>,
/// Is setup complete
pub is_setup_complete: AtomicBool,
/// ThreadPool for managing recovery tasks
pub threadpool: Arc<ThreadPool>,
}

#[allow(clippy::too_many_arguments)]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
})
}

Expand Down Expand Up @@ -484,7 +482,9 @@ pub fn check_for_broadcasted_contracts(maker: Arc<Maker>) -> Result<(), MakerErr

std::thread::sleep(Duration::from_secs(HEART_BEAT_INTERVAL_SECS));
}

if maker.shutdown.load(Relaxed) {
maker.threadpool.join();
}
Ok(())
}

Expand Down

0 comments on commit 238006c

Please sign in to comment.