Skip to content

Commit ae73953

Browse files
committed
move channel_id_factory to Downstream
move extended channel from the downstream_data to downstream move standard channel from the downstream_data to downstream move group_channel from the downstream_data to downstream move negotiated_extensions from the downstream_data to downstream remove Downstream_data from the downstream_data to downstream move downstream_id_factory to channel manager move downstream to channel manager make vardiffKey clonable move vardiff to channelManager struct move coinbase_output to channelManager struct move last_future_template to ChannelManager move last_new_prev_hash to ChannelManager move extranonce_prefix_factory_extended to ChannelManager move extranonce_prefix_factory_standard to ChannelManager remove channel manager data from Channel Manager remove possibility of sub shared deadlock on dashmap buckets make the lock ordering in vardiff very similar to pattern used throughout the app add wrapper around tricky lock constructs
1 parent d685c94 commit ae73953

13 files changed

Lines changed: 1466 additions & 1228 deletions

File tree

integration-tests/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pool-apps/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pool-apps/pool/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ bitcoin_core_sv2 = { version = "0.1.0", path = "../../bitcoin-core-sv2" }
2828
jd_server_sv2 = { version = "0.1.0", path = "../jd-server" }
2929
hex = "0.4.3"
3030
hotpath = "0.9"
31+
dashmap = "6.1.0"
3132

3233
[features]
3334
default = ["monitoring"]

pool-apps/pool/src/lib/channel_manager/mining_message_handler.rs

Lines changed: 913 additions & 833 deletions
Large diffs are not rendered by default.

pool-apps/pool/src/lib/channel_manager/mod.rs

Lines changed: 118 additions & 122 deletions
Large diffs are not rendered by default.

pool-apps/pool/src/lib/channel_manager/template_distribution_message_handler.rs

Lines changed: 200 additions & 146 deletions
Large diffs are not rendered by default.

pool-apps/pool/src/lib/downstream/common_message_handler.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ impl HandleCommonMessagesFromClientAsync for Downstream {
2424
&self,
2525
_client_id: Option<usize>,
2626
) -> Result<Vec<u16>, Self::Error> {
27-
Ok(self
28-
.downstream_data
29-
.super_safe_lock(|data| data.negotiated_extensions.clone()))
27+
Ok(self.negotiated_extensions.get())
3028
}
3129

3230
async fn handle_setup_connection(

pool-apps/pool/src/lib/downstream/extensions_message_handler.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ impl HandleExtensionsFromClientAsync for Downstream {
2222
&self,
2323
_client_id: Option<usize>,
2424
) -> Result<Vec<u16>, Self::Error> {
25-
Ok(self
26-
.downstream_data
27-
.super_safe_lock(|data| data.negotiated_extensions.clone()))
25+
Ok(self.negotiated_extensions.get())
2826
}
2927

3028
async fn handle_request_extensions(
@@ -114,9 +112,7 @@ impl HandleExtensionsFromClientAsync for Downstream {
114112
);
115113

116114
// Store the negotiated extensions in the shared downstream data
117-
self.downstream_data.super_safe_lock(|data| {
118-
data.negotiated_extensions = supported.clone();
119-
});
115+
self.negotiated_extensions.set(supported.clone());
120116

121117
let success = RequestExtensionsSuccess {
122118
request_id: msg.request_id,

pool-apps/pool/src/lib/downstream/mod.rs

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
use std::{
2-
collections::HashMap,
3-
sync::{
4-
atomic::{AtomicBool, AtomicU32},
5-
Arc,
6-
},
1+
use std::sync::{
2+
atomic::{AtomicBool, AtomicU32},
3+
Arc,
74
};
85

96
use async_channel::{unbounded, Receiver, Sender};
107
use bitcoin_core_sv2::template_distribution_protocol::CancellationToken;
118
use stratum_apps::{
12-
custom_mutex::Mutex,
139
network_helpers::noise_stream::NoiseTcpStream,
1410
stratum_core::{
1511
channels_sv2::server::{
@@ -35,34 +31,14 @@ use tracing::{debug, error, warn};
3531
use crate::{
3632
error::{self, PoolError, PoolErrorKind, PoolResult},
3733
io_task::spawn_io_tasks,
34+
shared::{Shared, SharedMap},
3835
status::{handle_error, Status, StatusSender},
3936
utils::PayoutMode,
4037
};
4138

4239
mod common_message_handler;
4340
mod extensions_message_handler;
4441

45-
/// Holds state related to a downstream connection's mining channels.
46-
///
47-
/// This includes:
48-
/// - Whether the downstream requires a standard job (`require_std_job`).
49-
/// - A [`GroupChannel`].
50-
/// - Active [`ExtendedChannel`]s keyed by channel ID.
51-
/// - Active [`StandardChannel`]s keyed by channel ID.
52-
/// - Extensions that have been successfully negotiated with this client
53-
pub struct DownstreamData {
54-
pub group_channel: GroupChannel<'static, DefaultJobStore<ExtendedJob<'static>>>,
55-
pub extended_channels:
56-
HashMap<ChannelId, ExtendedChannel<'static, DefaultJobStore<ExtendedJob<'static>>>>,
57-
pub standard_channels:
58-
HashMap<ChannelId, StandardChannel<'static, DefaultJobStore<StandardJob<'static>>>>,
59-
pub channel_id_factory: AtomicU32,
60-
/// Extensions that have been successfully negotiated with this client
61-
pub negotiated_extensions: Vec<u16>,
62-
/// Payout mode derived from user_identity (None until channel is opened)
63-
pub payout_mode: Option<PayoutMode>,
64-
}
65-
6642
/// Communication layer for a downstream connection.
6743
///
6844
/// Provides the messaging primitives for interacting with the
@@ -86,7 +62,6 @@ pub struct DownstreamChannel {
8662
/// Represents a downstream client connected to this node.
8763
#[derive(Clone)]
8864
pub struct Downstream {
89-
pub downstream_data: Arc<Mutex<DownstreamData>>,
9065
downstream_channel: DownstreamChannel,
9166
pub downstream_id: usize,
9267
pub requires_standard_jobs: Arc<AtomicBool>,
@@ -95,6 +70,19 @@ pub struct Downstream {
9570
pub supported_extensions: Vec<u16>,
9671
/// Extensions that the pool requires
9772
pub required_extensions: Vec<u16>,
73+
/// Channel id factory
74+
pub channel_id_factory: Arc<AtomicU32>,
75+
/// Extended Channels
76+
pub extended_channels:
77+
SharedMap<ChannelId, ExtendedChannel<'static, DefaultJobStore<ExtendedJob<'static>>>>,
78+
/// Standard Channels
79+
pub standard_channels:
80+
SharedMap<ChannelId, StandardChannel<'static, DefaultJobStore<StandardJob<'static>>>>,
81+
/// Group Channel
82+
pub group_channel: Shared<GroupChannel<'static, DefaultJobStore<ExtendedJob<'static>>>>,
83+
/// Extensions that have been successfully negotiated with this client
84+
pub negotiated_extensions: Shared<Vec<u16>>,
85+
pub payout_mode: Shared<Option<PayoutMode>>,
9886
}
9987

10088
#[cfg_attr(not(test), hotpath::measure_all)]
@@ -141,23 +129,19 @@ impl Downstream {
141129
connection_token,
142130
};
143131

144-
let downstream_data = Arc::new(Mutex::new(DownstreamData {
145-
extended_channels: HashMap::new(),
146-
standard_channels: HashMap::new(),
147-
group_channel,
148-
channel_id_factory,
149-
negotiated_extensions: vec![],
150-
payout_mode: None,
151-
}));
152-
153132
Downstream {
154133
downstream_channel,
155-
downstream_data,
156134
downstream_id,
157135
requires_standard_jobs: Arc::new(AtomicBool::new(false)),
158136
requires_custom_work: Arc::new(AtomicBool::new(false)),
137+
channel_id_factory: Arc::new(channel_id_factory),
138+
extended_channels: SharedMap::new(),
139+
standard_channels: SharedMap::new(),
140+
group_channel: Shared::new(group_channel),
141+
negotiated_extensions: Shared::new(vec![]),
159142
supported_extensions,
160143
required_extensions,
144+
payout_mode: Shared::new(None),
161145
}
162146
}
163147

@@ -310,9 +294,7 @@ impl Downstream {
310294
match protocol_message_type(header.ext_type(), header.msg_type()) {
311295
MessageType::Mining => {
312296
debug!("Received mining SV2 frame from downstream.");
313-
let negotiated_extensions = self
314-
.downstream_data
315-
.super_safe_lock(|data| data.negotiated_extensions.clone());
297+
let negotiated_extensions = self.negotiated_extensions.get();
316298
let (any_message, tlv_fields) = parse_message_frame_with_tlvs(
317299
header,
318300
sv2_frame.payload(),

pool-apps/pool/src/lib/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub mod error;
3939
mod io_task;
4040
#[cfg(feature = "monitoring")]
4141
mod monitoring;
42+
pub(crate) mod shared;
4243
pub mod status;
4344
pub mod template_receiver;
4445
pub mod utils;

0 commit comments

Comments
 (0)