Skip to content

Commit

Permalink
Merge pull request #301 from rustaceanrob/arc-2-18
Browse files Browse the repository at this point in the history
Use `Arc` to clone `Dialog`
  • Loading branch information
rustaceanrob authored Feb 18, 2025
2 parents 12249e0 + 7cc8c7b commit a1a48b1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
11 changes: 8 additions & 3 deletions src/chain/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub(crate) struct Chain<H: HeaderStore> {
heights: Arc<Mutex<HeightMonitor>>,
scripts: HashSet<ScriptBuf>,
block_queue: BlockQueue,
dialog: Dialog,
dialog: Arc<Dialog>,
}

#[allow(dead_code)]
Expand All @@ -70,7 +70,7 @@ impl<H: HeaderStore> Chain<H> {
scripts: HashSet<ScriptBuf>,
anchor: HeaderCheckpoint,
checkpoints: HeaderCheckpoints,
dialog: Dialog,
dialog: Arc<Dialog>,
height_monitor: Arc<Mutex<HeightMonitor>>,
db: H,
quorum_required: usize,
Expand Down Expand Up @@ -926,7 +926,12 @@ mod tests {
HashSet::new(),
anchor,
checkpoints,
Dialog::new(crate::LogLevel::Debug, log_tx, warn_tx, event_tx),
Arc::new(Dialog::new(
crate::LogLevel::Debug,
log_tx,
warn_tx,
event_tx,
)),
height_monitor,
(),
peers,
Expand Down
8 changes: 4 additions & 4 deletions src/core/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub struct Node<H: HeaderStore, P: PeerStore> {
peer_map: Arc<Mutex<PeerMap<P>>>,
tx_broadcaster: Arc<Mutex<Broadcaster>>,
required_peers: PeerRequirement,
dialog: Dialog,
dialog: Arc<Dialog>,
client_recv: Arc<Mutex<Receiver<ClientMessage>>>,
peer_recv: Arc<Mutex<Receiver<PeerThreadMessage>>>,
filter_sync_policy: Arc<RwLock<FilterSyncPolicy>>,
Expand Down Expand Up @@ -105,7 +105,7 @@ impl<H: HeaderStore, P: PeerStore> Node<H, P> {
let (ctx, crx) = mpsc::channel::<ClientMessage>(5);
let client = Client::new(log_rx, warn_rx, event_rx, ctx);
// A structured way to talk to the client
let dialog = Dialog::new(log_level, log_tx, warn_tx, event_tx);
let dialog = Arc::new(Dialog::new(log_level, log_tx, warn_tx, event_tx));
// We always assume we are behind
let state = Arc::new(RwLock::new(NodeState::Behind));
// Configure the peer manager
Expand All @@ -116,7 +116,7 @@ impl<H: HeaderStore, P: PeerStore> Node<H, P> {
network,
peer_store,
white_list,
dialog.clone(),
Arc::clone(&dialog),
connection_type,
target_peer_size,
timeout_config,
Expand All @@ -135,7 +135,7 @@ impl<H: HeaderStore, P: PeerStore> Node<H, P> {
addresses,
checkpoint,
checkpoints,
dialog.clone(),
Arc::clone(&dialog),
height_monitor,
header_store,
required_peers.into(),
Expand Down
6 changes: 3 additions & 3 deletions src/core/peer_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub(crate) struct PeerMap<P: PeerStore> {
db: Arc<Mutex<P>>,
connector: Arc<Mutex<dyn NetworkConnector + Send + Sync>>,
whitelist: Whitelist,
dialog: Dialog,
dialog: Arc<Dialog>,
target_db_size: PeerStoreSizeConfig,
net_groups: HashSet<String>,
timeout_config: PeerTimeoutConfig,
Expand All @@ -83,7 +83,7 @@ impl<P: PeerStore> PeerMap<P> {
network: Network,
db: P,
whitelist: Whitelist,
dialog: Dialog,
dialog: Arc<Dialog>,
connection_type: ConnectionType,
target_db_size: PeerStoreSizeConfig,
timeout_config: PeerTimeoutConfig,
Expand Down Expand Up @@ -177,7 +177,7 @@ impl<P: PeerStore> PeerMap<P> {
self.mtx.clone(),
prx,
loaded_peer.services,
self.dialog.clone(),
Arc::clone(&self.dialog),
self.timeout_config,
);
let mut connector = self.connector.lock().await;
Expand Down
6 changes: 3 additions & 3 deletions src/network/peer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extern crate tokio;
use std::{ops::DerefMut, time::Duration};
use std::{ops::DerefMut, sync::Arc, time::Duration};

use bip324::{AsyncProtocol, PacketReader, PacketWriter, Role};
use bitcoin::{p2p::ServiceFlags, Network};
Expand Down Expand Up @@ -48,7 +48,7 @@ pub(crate) struct Peer {
network: Network,
message_counter: MessageCounter,
services: ServiceFlags,
dialog: Dialog,
dialog: Arc<Dialog>,
timeout_config: PeerTimeoutConfig,
}

Expand All @@ -59,7 +59,7 @@ impl Peer {
main_thread_sender: Sender<PeerThreadMessage>,
main_thread_recv: Receiver<MainThreadMessage>,
services: ServiceFlags,
dialog: Dialog,
dialog: Arc<Dialog>,
timeout_config: PeerTimeoutConfig,
) -> Self {
let message_counter = MessageCounter::new(timeout_config.response_timeout);
Expand Down

0 comments on commit a1a48b1

Please sign in to comment.