Skip to content

Commit fdc78fe

Browse files
committed
Refactor: Move selectors & routing logic to mining-proxy, remove selectors dependency
- Moved `selectors` and `routing_logic` modules to `mining-proxy`, isolating proxy-specific logic. - Removed `selectors` dependency from `common_properties.rs`, `handlers/mining.rs`, `routing_logic.rs`, and related modules. - Updated `IsUpstream` and `IsMiningUpstream` to remove `DownstreamSelector` and simplify their implementation. - Removed unused imports related to `selectors` and `routing_logic` across multiple modules. - Updated various `impl` blocks to match the new trait definitions without selectors. - Fixed type inconsistencies in `on_open_standard_channel_success` and `on_open_standard_channel`. - Ensured `UpstreamMiningNode` implements `HasDownstreamSelector` for proper selector access. - Fixed `DownstreamMiningNode` compatibility by enforcing `Down = DownstreamMiningNode` where needed. - Adjusted `config-examples/proxy-config-example.toml` to update upstream port. This refactor improves modularity, makes mining-proxy self-contained, and eliminates unnecessary dependencies.
1 parent 7db0936 commit fdc78fe

File tree

14 files changed

+145
-188
lines changed

14 files changed

+145
-188
lines changed

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

+4-16
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
//! and channel management. These definitions form the foundation for consistent communication and
66
//! behavior across Sv2 roles/applications.
77
8-
use crate::selectors::{
9-
DownstreamMiningSelector, DownstreamSelector, NullDownstreamMiningSelector,
10-
};
118
use common_messages_sv2::{has_requires_std_job, Protocol, SetupConnection};
129
use mining_sv2::{Extranonce, Target};
1310
use nohash_hasher::BuildNoHashHasher;
@@ -72,7 +69,7 @@ pub struct PairSettings {
7269
}
7370

7471
/// Properties defining behaviors common to all Sv2 upstream nodes.
75-
pub trait IsUpstream<Down: IsDownstream, Sel: DownstreamSelector<Down> + ?Sized> {
72+
pub trait IsUpstream<Down: IsDownstream> {
7673
/// Returns the protocol version used by the upstream node.
7774
fn get_version(&self) -> u16;
7875

@@ -101,9 +98,6 @@ pub trait IsUpstream<Down: IsDownstream, Sel: DownstreamSelector<Down> + ?Sized>
10198

10299
/// Provides a request ID mapper for viewing and managing upstream-downstream communication.
103100
fn get_mapper(&mut self) -> Option<&mut RequestIdMapper>;
104-
105-
/// Returns the selector ([`crate::selectors`] for managing downstream nodes.
106-
fn get_remote_selector(&mut self) -> &mut Sel;
107101
}
108102

109103
/// The types of channels that can be opened with upstream nodes.
@@ -167,9 +161,7 @@ pub struct StandardChannel {
167161
///
168162
/// This trait extends [`IsUpstream`] with additional functionality specific to mining, such as
169163
/// hashrate management and channel updates.
170-
pub trait IsMiningUpstream<Down: IsMiningDownstream, Sel: DownstreamMiningSelector<Down> + ?Sized>:
171-
IsUpstream<Down, Sel>
172-
{
164+
pub trait IsMiningUpstream<Down: IsMiningDownstream>: IsUpstream<Down> {
173165
/// Returns the total hashrate managed by the upstream node.
174166
fn total_hash_rate(&self) -> u64;
175167

@@ -207,7 +199,7 @@ pub trait IsMiningDownstream: IsDownstream {
207199
}
208200

209201
// Implemented for the `NullDownstreamMiningSelector`.
210-
impl<Down: IsDownstream + D> IsUpstream<Down, NullDownstreamMiningSelector> for () {
202+
impl<Down: IsDownstream + D> IsUpstream<Down> for () {
211203
fn get_version(&self) -> u16 {
212204
unreachable!("Null upstream do not have a version");
213205
}
@@ -226,10 +218,6 @@ impl<Down: IsDownstream + D> IsUpstream<Down, NullDownstreamMiningSelector> for
226218
fn get_mapper(&mut self) -> Option<&mut RequestIdMapper> {
227219
unreachable!("Null upstream do not have a mapper")
228220
}
229-
230-
fn get_remote_selector(&mut self) -> &mut NullDownstreamMiningSelector {
231-
unreachable!("Null upstream do not have a selector")
232-
}
233221
}
234222

235223
// Implemented for the `NullDownstreamMiningSelector`.
@@ -239,7 +227,7 @@ impl IsDownstream for () {
239227
}
240228
}
241229

242-
impl<Down: IsMiningDownstream + D> IsMiningUpstream<Down, NullDownstreamMiningSelector> for () {
230+
impl<Down: IsMiningDownstream + D> IsMiningUpstream<Down> for () {
243231
fn total_hash_rate(&self) -> u64 {
244232
unreachable!("Null selector do not have hash rate");
245233
}

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

+6-16
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,7 @@ use mining_sv2::{
3838
UpdateChannel, UpdateChannelError,
3939
};
4040

41-
use crate::{
42-
common_properties::{IsMiningDownstream, IsMiningUpstream},
43-
routing_logic::MiningRouter,
44-
selectors::DownstreamMiningSelector,
45-
};
41+
use crate::common_properties::{IsMiningDownstream, IsMiningUpstream};
4642

4743
use super::{SendTo_, SupportedChannelTypes};
4844

@@ -58,11 +54,8 @@ pub type SendTo<Remote> = SendTo_<Mining<'static>, Remote>;
5854
///
5955
/// This trait defines methods for parsing and routing downstream messages
6056
/// related to mining operations.
61-
pub trait ParseMiningMessagesFromDownstream<
62-
Up: IsMiningUpstream<Self, Selector> + D,
63-
Selector: DownstreamMiningSelector<Self> + D,
64-
Router: MiningRouter<Self, Up, Selector>,
65-
> where
57+
pub trait ParseMiningMessagesFromDownstream<Up: IsMiningUpstream<Self> + D>
58+
where
6659
Self: IsMiningDownstream + Sized + D,
6760
{
6861
/// Returns the type of channel supported by the downstream connection.
@@ -314,12 +307,9 @@ pub trait ParseMiningMessagesFromDownstream<
314307
///
315308
/// This trait provides the functionality to handle and route various types of mining messages
316309
/// from the upstream based on the message type and payload.
317-
pub trait ParseMiningMessagesFromUpstream<
318-
Down: IsMiningDownstream + D,
319-
Selector: DownstreamMiningSelector<Down> + D,
320-
Router: MiningRouter<Down, Self, Selector>,
321-
> where
322-
Self: IsMiningUpstream<Down, Selector> + Sized + D,
310+
pub trait ParseMiningMessagesFromUpstream<Down: IsMiningDownstream + D>
311+
where
312+
Self: IsMiningUpstream<Down> + Sized + D,
323313
{
324314
/// Retrieves the type of the channel supported by this upstream parser.
325315
fn get_channel_type(&self) -> SupportedChannelTypes;

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

-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ pub mod handlers;
2424
pub mod job_creator;
2525
pub mod job_dispatcher;
2626
pub mod parsers;
27-
pub mod routing_logic;
28-
pub mod selectors;
2927
pub mod utils;
3028
pub use common_messages_sv2;
3129
pub use errors::Error;

roles/jd-client/src/lib/downstream.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -433,16 +433,8 @@ impl DownstreamMiningNode {
433433
}
434434
}
435435

436-
use roles_logic_sv2::selectors::NullDownstreamMiningSelector;
437-
438436
/// It impl UpstreamMining cause the proxy act as an upstream node for the DownstreamMiningNode
439-
impl
440-
ParseMiningMessagesFromDownstream<
441-
UpstreamMiningNode,
442-
NullDownstreamMiningSelector,
443-
roles_logic_sv2::routing_logic::NoRouting,
444-
> for DownstreamMiningNode
445-
{
437+
impl ParseMiningMessagesFromDownstream<UpstreamMiningNode> for DownstreamMiningNode {
446438
fn get_channel_type(&self) -> SupportedChannelTypes {
447439
SupportedChannelTypes::Extended
448440
}

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

+13-19
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,29 @@ use key_utils::Secp256k1PublicKey;
1717
use network_helpers_sv2::noise_connection::Connection;
1818
use roles_logic_sv2::{
1919
channel_logic::channel_factory::PoolChannelFactory,
20-
common_messages_sv2::{Protocol, SetupConnection},
20+
common_messages_sv2::{Protocol, Reconnect, SetupConnection},
2121
common_properties::{IsMiningUpstream, IsUpstream},
2222
handlers::{
2323
common::{ParseCommonMessagesFromUpstream, SendTo as SendToCommon},
2424
mining::{ParseMiningMessagesFromUpstream, SendTo},
25+
SupportedChannelTypes,
2526
},
2627
job_declaration_sv2::DeclareMiningJob,
27-
mining_sv2::{ExtendedExtranonce, Extranonce, SetCustomMiningJob},
28+
mining_sv2::{ExtendedExtranonce, Extranonce, SetCustomMiningJob, SetGroupChannel},
2829
parsers::{AnyMessage, Mining, MiningDeviceMessages},
29-
routing_logic::NoRouting,
30-
selectors::NullDownstreamMiningSelector,
3130
utils::{Id, Mutex},
3231
Error as RolesLogicError,
3332
};
34-
use std::{collections::HashMap, net::SocketAddr, sync::Arc, thread::sleep, time::Duration};
33+
use std::{
34+
collections::{HashMap, VecDeque},
35+
net::SocketAddr,
36+
sync::Arc,
37+
thread::sleep,
38+
time::Duration,
39+
};
3540
use tokio::{net::TcpStream, task, task::AbortHandle};
3641
use tracing::{error, info, warn};
3742

38-
use roles_logic_sv2::{
39-
common_messages_sv2::Reconnect, handlers::SupportedChannelTypes, mining_sv2::SetGroupChannel,
40-
};
41-
use std::collections::VecDeque;
42-
4343
#[derive(Debug)]
4444
struct CircularBuffer {
4545
buffer: VecDeque<(u64, u32)>,
@@ -450,7 +450,7 @@ impl Upstream {
450450
}
451451
}
452452

453-
impl IsUpstream<Downstream, NullDownstreamMiningSelector> for Upstream {
453+
impl IsUpstream<Downstream> for Upstream {
454454
fn get_version(&self) -> u16 {
455455
todo!()
456456
}
@@ -470,13 +470,9 @@ impl IsUpstream<Downstream, NullDownstreamMiningSelector> for Upstream {
470470
fn get_mapper(&mut self) -> Option<&mut roles_logic_sv2::common_properties::RequestIdMapper> {
471471
todo!()
472472
}
473-
474-
fn get_remote_selector(&mut self) -> &mut NullDownstreamMiningSelector {
475-
todo!()
476-
}
477473
}
478474

479-
impl IsMiningUpstream<Downstream, NullDownstreamMiningSelector> for Upstream {
475+
impl IsMiningUpstream<Downstream> for Upstream {
480476
fn total_hash_rate(&self) -> u64 {
481477
todo!()
482478
}
@@ -525,9 +521,7 @@ impl ParseCommonMessagesFromUpstream for Upstream {
525521

526522
/// Connection-wide SV2 Upstream role messages parser implemented by a downstream ("downstream"
527523
/// here is relative to the SV2 Upstream role and is represented by this `Upstream` struct).
528-
impl ParseMiningMessagesFromUpstream<Downstream, NullDownstreamMiningSelector, NoRouting>
529-
for Upstream
530-
{
524+
impl ParseMiningMessagesFromUpstream<Downstream> for Upstream {
531525
/// Returns the channel type between the SV2 Upstream role and the `Upstream`, which will
532526
/// always be `Extended` for a SV1/SV2 Translator Proxy.
533527
fn get_channel_type(&self) -> SupportedChannelTypes {

roles/mining-proxy/config-examples/proxy-config-example.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
upstreams = [
2-
{ channel_kind = "Extended", address = "0.0.0.0", port = 34265, pub_key = "9auqWEzQDVyd2oe1JVGFLMLHZtCo2FFqZwtKA5gd9xbuEu7PH72"}
2+
{ channel_kind = "Extended", address = "0.0.0.0", port = 34254, pub_key = "9auqWEzQDVyd2oe1JVGFLMLHZtCo2FFqZwtKA5gd9xbuEu7PH72"}
33
]
44
listen_address = "127.0.0.1"
55
listen_mining_port = 34255

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

+8-18
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ use async_channel::{Receiver, SendError, Sender};
44
use tokio::{net::TcpListener, sync::oneshot::Receiver as TokioReceiver};
55
use tracing::{info, trace, warn};
66

7-
use super::upstream_mining::{ProxyRemoteSelector, StdFrame as UpstreamFrame, UpstreamMiningNode};
7+
use super::{
8+
routing_logic::{CommonRouter, CommonRoutingLogic, MiningRouter, MiningRoutingLogic},
9+
upstream_mining::{StdFrame as UpstreamFrame, UpstreamMiningNode},
10+
};
811
use codec_sv2::{StandardEitherFrame, StandardSv2Frame};
912
use network_helpers_sv2::plain_connection::PlainConnection;
1013
use roles_logic_sv2::{
@@ -18,9 +21,6 @@ use roles_logic_sv2::{
1821
},
1922
mining_sv2::*,
2023
parsers::{AnyMessage, Mining, MiningDeviceMessages},
21-
routing_logic::{
22-
CommonRouter, CommonRoutingLogic, MiningProxyRoutingLogic, MiningRouter, MiningRoutingLogic,
23-
},
2424
utils::Mutex,
2525
};
2626

@@ -288,13 +288,7 @@ impl DownstreamMiningNode {
288288
}
289289

290290
/// It impl UpstreamMining cause the proxy act as an upstream node for the DownstreamMiningNode
291-
impl
292-
ParseMiningMessagesFromDownstream<
293-
UpstreamMiningNode,
294-
ProxyRemoteSelector,
295-
MiningProxyRoutingLogic<Self, UpstreamMiningNode, ProxyRemoteSelector>,
296-
> for DownstreamMiningNode
297-
{
291+
impl ParseMiningMessagesFromDownstream<UpstreamMiningNode> for DownstreamMiningNode {
298292
fn get_channel_type(&self) -> SupportedChannelTypes {
299293
SupportedChannelTypes::Group
300294
}
@@ -445,13 +439,9 @@ impl ParseCommonMessagesFromDownstream for DownstreamMiningNode {
445439
.map_err(|e| Error::PoisonLock(e.to_string()))?;
446440
let (data, message) = result?;
447441
let upstream = match super::get_routing_logic() {
448-
roles_logic_sv2::routing_logic::MiningRoutingLogic::Proxy(proxy_routing) => {
449-
proxy_routing
450-
.safe_lock(|r| {
451-
r.downstream_to_upstream_map.get(&data).unwrap()[0].clone()
452-
})
453-
.unwrap()
454-
}
442+
MiningRoutingLogic::Proxy(proxy_routing) => proxy_routing
443+
.safe_lock(|r| r.downstream_to_upstream_map.get(&data).unwrap()[0].clone())
444+
.unwrap(),
455445
_ => unreachable!(),
456446
};
457447
self.upstream = Some(upstream);

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
pub mod downstream_mining;
22
pub mod error;
3+
pub mod routing_logic;
4+
pub mod selectors;
35
pub mod upstream_mining;
46

57
use once_cell::sync::OnceCell;
6-
use roles_logic_sv2::{
7-
routing_logic::{CommonRoutingLogic, MiningProxyRoutingLogic, MiningRoutingLogic},
8-
selectors::GeneralMiningSelector,
9-
utils::{GroupId, Id, Mutex},
10-
};
8+
use roles_logic_sv2::utils::{GroupId, Id, Mutex};
9+
use routing_logic::{CommonRoutingLogic, MiningProxyRoutingLogic, MiningRoutingLogic};
10+
use selectors::GeneralMiningSelector;
1111
use serde::Deserialize;
1212
use std::{net::SocketAddr, sync::Arc};
1313
use tokio::{net::TcpListener, sync::oneshot};

0 commit comments

Comments
 (0)