Skip to content

Commit c13c721

Browse files
committed
routing_logic removal from ParseMiningMessagesFromUpstream and adaptation on roles
1 parent 413ddaf commit c13c721

File tree

6 files changed

+58
-106
lines changed

6 files changed

+58
-106
lines changed

protocols/v2/roles-logic-sv2/src/handlers/mining.rs

+15-33
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use mining_sv2::{
4040

4141
use crate::{
4242
common_properties::{IsMiningDownstream, IsMiningUpstream},
43-
routing_logic::{MiningRouter, MiningRoutingLogic},
43+
routing_logic::MiningRouter,
4444
selectors::DownstreamMiningSelector,
4545
};
4646

@@ -332,12 +332,10 @@ pub trait ParseMiningMessagesFromUpstream<
332332
self_mutex: Arc<Mutex<Self>>,
333333
message_type: u8,
334334
payload: &mut [u8],
335-
routing_logic: MiningRoutingLogic<Down, Self, Selector, Router>,
336335
) -> Result<SendTo<Down>, Error> {
337336
match Self::handle_message_mining_deserialized(
338337
self_mutex,
339338
(message_type, payload).try_into(),
340-
routing_logic,
341339
) {
342340
Err(Error::UnexpectedMessage(0)) => Err(Error::UnexpectedMessage(message_type)),
343341
result => result,
@@ -349,41 +347,26 @@ pub trait ParseMiningMessagesFromUpstream<
349347
fn handle_message_mining_deserialized(
350348
self_mutex: Arc<Mutex<Self>>,
351349
message: Result<Mining, Error>,
352-
routing_logic: MiningRoutingLogic<Down, Self, Selector, Router>,
353350
) -> Result<SendTo<Down>, Error> {
354351
let (channel_type, is_work_selection_enabled) = self_mutex
355352
.safe_lock(|s| (s.get_channel_type(), s.is_work_selection_enabled()))
356353
.map_err(|e| crate::Error::PoisonLock(e.to_string()))?;
357354

358355
match message {
359-
Ok(Mining::OpenStandardMiningChannelSuccess(mut m)) => {
360-
let remote = match routing_logic {
361-
MiningRoutingLogic::None => None,
362-
MiningRoutingLogic::Proxy(r_logic) => {
363-
let up = r_logic
364-
.safe_lock(|r_logic| {
365-
r_logic.on_open_standard_channel_success(self_mutex.clone(), &mut m)
366-
})
367-
.map_err(|e| crate::Error::PoisonLock(e.to_string()))?;
368-
Some(up?)
369-
}
370-
MiningRoutingLogic::_P(_) => panic!("Must use either MiningRoutingLogic::None or MiningRoutingLogic::Proxy for `routing_logic` param"),
371-
};
372-
match channel_type {
373-
SupportedChannelTypes::Standard => self_mutex
374-
.safe_lock(|s| s.handle_open_standard_mining_channel_success(m, remote))
375-
.map_err(|e| crate::Error::PoisonLock(e.to_string()))?,
376-
SupportedChannelTypes::Extended => Err(Error::UnexpectedMessage(
377-
MESSAGE_TYPE_OPEN_STANDARD_MINING_CHANNEL_SUCCESS,
378-
)),
379-
SupportedChannelTypes::Group => self_mutex
380-
.safe_lock(|s| s.handle_open_standard_mining_channel_success(m, remote))
381-
.map_err(|e| crate::Error::PoisonLock(e.to_string()))?,
382-
SupportedChannelTypes::GroupAndExtended => self_mutex
383-
.safe_lock(|s| s.handle_open_standard_mining_channel_success(m, remote))
384-
.map_err(|e| crate::Error::PoisonLock(e.to_string()))?,
385-
}
386-
}
356+
Ok(Mining::OpenStandardMiningChannelSuccess(m)) => match channel_type {
357+
SupportedChannelTypes::Standard => self_mutex
358+
.safe_lock(|s| s.handle_open_standard_mining_channel_success(m))
359+
.map_err(|e| crate::Error::PoisonLock(e.to_string()))?,
360+
SupportedChannelTypes::Extended => Err(Error::UnexpectedMessage(
361+
MESSAGE_TYPE_OPEN_STANDARD_MINING_CHANNEL_SUCCESS,
362+
)),
363+
SupportedChannelTypes::Group => self_mutex
364+
.safe_lock(|s| s.handle_open_standard_mining_channel_success(m))
365+
.map_err(|e| crate::Error::PoisonLock(e.to_string()))?,
366+
SupportedChannelTypes::GroupAndExtended => self_mutex
367+
.safe_lock(|s| s.handle_open_standard_mining_channel_success(m))
368+
.map_err(|e| crate::Error::PoisonLock(e.to_string()))?,
369+
},
387370
Ok(Mining::OpenExtendedMiningChannelSuccess(m)) => {
388371
info!("Received OpenExtendedMiningChannelSuccess with request id: {} and channel id: {}", m.request_id, m.channel_id);
389372
debug!("OpenStandardMiningChannelSuccess: {:?}", m);
@@ -673,7 +656,6 @@ pub trait ParseMiningMessagesFromUpstream<
673656
fn handle_open_standard_mining_channel_success(
674657
&mut self,
675658
m: OpenStandardMiningChannelSuccess,
676-
remote: Option<Arc<Mutex<Down>>>,
677659
) -> Result<SendTo<Down>, Error>;
678660

679661
/// Handles a successful response for opening an extended mining channel.

protocols/v2/roles-logic-sv2/src/routing_logic.rs

+14-21
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub trait MiningRouter<
7373
/// Handles an `OpenStandardMiningChannelSuccess` message from an upstream.
7474
fn on_open_standard_channel_success(
7575
&mut self,
76-
upstream: Arc<Mutex<Up>>,
76+
upstream: &mut Up,
7777
request: &mut OpenStandardMiningChannelSuccess,
7878
) -> Result<Arc<Mutex<Down>>, Error>;
7979
}
@@ -109,7 +109,7 @@ impl<
109109

110110
fn on_open_standard_channel_success(
111111
&mut self,
112-
_upstream: Arc<Mutex<Up>>,
112+
_upstream: &mut Up,
113113
_request: &mut OpenStandardMiningChannelSuccess,
114114
) -> Result<Arc<Mutex<Down>>, Error> {
115115
unreachable!()
@@ -257,30 +257,23 @@ impl<
257257
// updates the associated group and channel IDs in the upstream.
258258
fn on_open_standard_channel_success(
259259
&mut self,
260-
upstream: Arc<Mutex<Up>>,
260+
upstream: &mut Up,
261261
request: &mut OpenStandardMiningChannelSuccess,
262262
) -> Result<Arc<Mutex<Down>>, Error> {
263263
let upstream_request_id = request.get_request_id_as_u32();
264264
let original_request_id = upstream
265-
.safe_lock(|u| {
266-
u.get_mapper()
267-
.ok_or(crate::Error::RequestIdNotMapped(upstream_request_id))?
268-
.remove(upstream_request_id)
269-
.ok_or(Error::RequestIdNotMapped(upstream_request_id))
270-
})
271-
.map_err(|e| Error::PoisonLock(e.to_string()))??;
265+
.get_mapper()
266+
.ok_or(crate::Error::RequestIdNotMapped(upstream_request_id))?
267+
.remove(upstream_request_id)
268+
.ok_or(Error::RequestIdNotMapped(upstream_request_id));
272269

273-
request.update_id(original_request_id);
274-
upstream
275-
.safe_lock(|u| {
276-
let selector = u.get_remote_selector();
277-
selector.on_open_standard_channel_success(
278-
upstream_request_id,
279-
request.group_channel_id,
280-
request.channel_id,
281-
)
282-
})
283-
.map_err(|e| Error::PoisonLock(e.to_string()))?
270+
request.update_id(original_request_id?);
271+
let selector = upstream.get_remote_selector();
272+
selector.on_open_standard_channel_success(
273+
upstream_request_id,
274+
request.group_channel_id,
275+
request.channel_id,
276+
)
284277
}
285278
}
286279

roles/jd-client/src/lib/upstream_sv2/upstream.rs

+3-14
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use roles_logic_sv2::{
2626
job_declaration_sv2::DeclareMiningJob,
2727
mining_sv2::{ExtendedExtranonce, Extranonce, SetCustomMiningJob},
2828
parsers::{AnyMessage, Mining, MiningDeviceMessages},
29-
routing_logic::{MiningRoutingLogic, NoRouting},
29+
routing_logic::NoRouting,
3030
selectors::NullDownstreamMiningSelector,
3131
utils::{Id, Mutex},
3232
Error as RolesLogicError,
@@ -340,21 +340,11 @@ impl Upstream {
340340

341341
let payload = incoming.payload();
342342

343-
// Since this is not communicating with an SV2 proxy, but instead a custom SV1
344-
// proxy where the routing logic is handled via the `Upstream`'s communication
345-
// channels, we do not use the mining routing logic in the SV2 library and
346-
// specify no mining routing logic here
347-
let routing_logic = MiningRoutingLogic::None;
348-
349343
// Gets the response message for the received SV2 Upstream role message
350344
// `handle_message_mining` takes care of the SetupConnection +
351345
// SetupConnection.Success
352-
let next_message_to_send = Upstream::handle_message_mining(
353-
self_.clone(),
354-
message_type,
355-
payload,
356-
routing_logic,
357-
);
346+
let next_message_to_send =
347+
Upstream::handle_message_mining(self_.clone(), message_type, payload);
358348

359349
// Routes the incoming messages accordingly
360350
match next_message_to_send {
@@ -560,7 +550,6 @@ impl ParseMiningMessagesFromUpstream<Downstream, NullDownstreamMiningSelector, N
560550
fn handle_open_standard_mining_channel_success(
561551
&mut self,
562552
_m: roles_logic_sv2::mining_sv2::OpenStandardMiningChannelSuccess,
563-
_remote: Option<Arc<Mutex<Downstream>>>,
564553
) -> Result<roles_logic_sv2::handlers::mining::SendTo<Downstream>, RolesLogicError> {
565554
panic!("Standard Mining Channels are not used in Translator Proxy")
566555
}

roles/mining-proxy/src/lib/upstream_mining.rs

+20-15
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ use nohash_hasher::BuildNoHashHasher;
99
use tokio::{net::TcpStream, task};
1010
use tracing::{debug, error, info};
1111

12+
use super::{
13+
downstream_mining::{Channel, DownstreamMiningNode, StdFrame as DownstreamFrame},
14+
EXTRANONCE_RANGE_1_LENGTH,
15+
};
1216
use codec_sv2::{HandshakeRole, Initiator, StandardEitherFrame, StandardSv2Frame};
1317
use network_helpers_sv2::noise_connection::Connection;
1418
use roles_logic_sv2::{
@@ -28,18 +32,13 @@ use roles_logic_sv2::{
2832
job_dispatcher::GroupChannelJobDispatcher,
2933
mining_sv2::*,
3034
parsers::{AnyMessage, CommonMessages, Mining, MiningDeviceMessages},
31-
routing_logic::MiningProxyRoutingLogic,
35+
routing_logic::{MiningProxyRoutingLogic, MiningRouter, MiningRoutingLogic},
3236
selectors::{DownstreamMiningSelector, ProxyDownstreamMiningSelector as Prs},
3337
template_distribution_sv2::SubmitSolution,
3438
utils::{GroupId, Id, Mutex},
3539
};
3640
use stratum_common::bitcoin::TxOut;
3741

38-
use super::{
39-
downstream_mining::{Channel, DownstreamMiningNode, StdFrame as DownstreamFrame},
40-
EXTRANONCE_RANGE_1_LENGTH,
41-
};
42-
4342
pub type Message = AnyMessage<'static>;
4443
pub type StdFrame = StandardSv2Frame<Message>;
4544
pub type EitherFrame = StandardEitherFrame<Message>;
@@ -581,14 +580,8 @@ impl UpstreamMiningNode {
581580
let message_type = incoming.get_header().unwrap().msg_type();
582581
let payload = incoming.payload();
583582

584-
let routing_logic = super::get_routing_logic();
585-
586-
let next_message_to_send = UpstreamMiningNode::handle_message_mining(
587-
self_mutex.clone(),
588-
message_type,
589-
payload,
590-
routing_logic,
591-
);
583+
let next_message_to_send =
584+
UpstreamMiningNode::handle_message_mining(self_mutex.clone(), message_type, payload);
592585
Self::match_next_message(self_mutex, next_message_to_send, incoming).await;
593586
}
594587

@@ -898,8 +891,20 @@ impl
898891
fn handle_open_standard_mining_channel_success(
899892
&mut self,
900893
m: OpenStandardMiningChannelSuccess,
901-
remote: Option<Arc<Mutex<DownstreamMiningNode>>>,
902894
) -> Result<SendTo<DownstreamMiningNode>, Error> {
895+
let routing_logic = super::get_routing_logic();
896+
let remote = match routing_logic {
897+
MiningRoutingLogic::None => None,
898+
MiningRoutingLogic::Proxy(r_logic) => {
899+
let up = r_logic
900+
.safe_lock(|r_logic| {
901+
r_logic.on_open_standard_channel_success(self, &mut m.clone())
902+
})
903+
.map_err(|e| Error::PoisonLock(e.to_string()))?;
904+
Some(up?)
905+
}
906+
MiningRoutingLogic::_P(_) => panic!("Must use either MiningRoutingLogic::None or MiningRoutingLogic::Proxy for `routing_logic` param"),
907+
};
903908
match &mut self.channel_kind {
904909
ChannelKind::Group(group) => {
905910
let down_is_header_only = remote

roles/test-utils/mining-device/src/lib/mod.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use roles_logic_sv2::{
1717
},
1818
mining_sv2::*,
1919
parsers::{Mining, MiningDeviceMessages},
20-
routing_logic::{MiningRoutingLogic, NoRouting},
20+
routing_logic::NoRouting,
2121
selectors::NullDownstreamMiningSelector,
2222
utils::{Id, Mutex},
2323
};
@@ -294,13 +294,8 @@ impl Device {
294294
let mut incoming: StdFrame = receiver.recv().await.unwrap().try_into().unwrap();
295295
let message_type = incoming.get_header().unwrap().msg_type();
296296
let payload = incoming.payload();
297-
let next = Device::handle_message_mining(
298-
self_mutex.clone(),
299-
message_type,
300-
payload,
301-
MiningRoutingLogic::None,
302-
)
303-
.unwrap();
297+
let next =
298+
Device::handle_message_mining(self_mutex.clone(), message_type, payload).unwrap();
304299
let mut notify_changes_to_mining_thread = self_mutex
305300
.safe_lock(|s| s.notify_changes_to_mining_thread.clone())
306301
.unwrap();
@@ -407,7 +402,6 @@ impl ParseMiningMessagesFromUpstream<(), NullDownstreamMiningSelector, NoRouting
407402
fn handle_open_standard_mining_channel_success(
408403
&mut self,
409404
m: OpenStandardMiningChannelSuccess,
410-
_: Option<std::sync::Arc<Mutex<()>>>,
411405
) -> Result<SendTo<()>, Error> {
412406
self.channel_opened = true;
413407
self.channel_id = Some(m.channel_id);

roles/translator/src/lib/upstream_sv2/upstream.rs

+3-14
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use roles_logic_sv2::{
2626
SetNewPrevHash, SubmitSharesExtended,
2727
},
2828
parsers::Mining,
29-
routing_logic::{MiningRoutingLogic, NoRouting},
29+
routing_logic::NoRouting,
3030
selectors::NullDownstreamMiningSelector,
3131
utils::Mutex,
3232
Error as RolesLogicError,
@@ -327,21 +327,11 @@ impl Upstream {
327327

328328
let payload = incoming.payload();
329329

330-
// Since this is not communicating with an SV2 proxy, but instead a custom SV1
331-
// proxy where the routing logic is handled via the `Upstream`'s communication
332-
// channels, we do not use the mining routing logic in the SV2 library and specify
333-
// no mining routing logic here
334-
let routing_logic = MiningRoutingLogic::None;
335-
336330
// Gets the response message for the received SV2 Upstream role message
337331
// `handle_message_mining` takes care of the SetupConnection +
338332
// SetupConnection.Success
339-
let next_message_to_send = Upstream::handle_message_mining(
340-
self_.clone(),
341-
message_type,
342-
payload,
343-
routing_logic,
344-
);
333+
let next_message_to_send =
334+
Upstream::handle_message_mining(self_.clone(), message_type, payload);
345335

346336
// Routes the incoming messages accordingly
347337
match next_message_to_send {
@@ -671,7 +661,6 @@ impl ParseMiningMessagesFromUpstream<Downstream, NullDownstreamMiningSelector, N
671661
fn handle_open_standard_mining_channel_success(
672662
&mut self,
673663
_m: roles_logic_sv2::mining_sv2::OpenStandardMiningChannelSuccess,
674-
_remote: Option<Arc<Mutex<Downstream>>>,
675664
) -> Result<roles_logic_sv2::handlers::mining::SendTo<Downstream>, RolesLogicError> {
676665
panic!("Standard Mining Channels are not used in Translator Proxy")
677666
}

0 commit comments

Comments
 (0)