Skip to content

Commit 0483895

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 d8bbd79 commit 0483895

File tree

14 files changed

+145
-190
lines changed

14 files changed

+145
-190
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_;
4844

@@ -68,11 +64,8 @@ pub enum SupportedChannelTypes {
6864
///
6965
/// This trait defines methods for parsing and routing downstream messages
7066
/// related to mining operations.
71-
pub trait ParseMiningMessagesFromDownstream<
72-
Up: IsMiningUpstream<Self, Selector> + D,
73-
Selector: DownstreamMiningSelector<Self> + D,
74-
Router: MiningRouter<Self, Up, Selector>,
75-
> where
67+
pub trait ParseMiningMessagesFromDownstream<Up: IsMiningUpstream<Self> + D>
68+
where
7669
Self: IsMiningDownstream + Sized + D,
7770
{
7871
/// Returns the type of channel supported by the downstream connection.
@@ -304,12 +297,9 @@ pub trait ParseMiningMessagesFromDownstream<
304297
///
305298
/// This trait provides the functionality to handle and route various types of mining messages
306299
/// from the upstream based on the message type and payload.
307-
pub trait ParseMiningMessagesFromUpstream<
308-
Down: IsMiningDownstream + D,
309-
Selector: DownstreamMiningSelector<Down> + D,
310-
Router: MiningRouter<Down, Self, Selector>,
311-
> where
312-
Self: IsMiningUpstream<Down, Selector> + Sized + D,
300+
pub trait ParseMiningMessagesFromUpstream<Down: IsMiningDownstream + D>
301+
where
302+
Self: IsMiningUpstream<Down> + Sized + D,
313303
{
314304
/// Retrieves the type of the channel supported by this upstream parser.
315305
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
@@ -432,16 +432,8 @@ impl DownstreamMiningNode {
432432
}
433433
}
434434

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

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

+13-21
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,28 @@ 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},
24-
mining::{ParseMiningMessagesFromUpstream, SendTo},
24+
mining::{ParseMiningMessagesFromUpstream, SendTo, SupportedChannelTypes},
2525
},
2626
job_declaration_sv2::DeclareMiningJob,
27-
mining_sv2::{ExtendedExtranonce, Extranonce, SetCustomMiningJob},
27+
mining_sv2::{ExtendedExtranonce, Extranonce, SetCustomMiningJob, SetGroupChannel},
2828
parsers::{AnyMessage, Mining, MiningDeviceMessages},
29-
routing_logic::NoRouting,
30-
selectors::NullDownstreamMiningSelector,
3129
utils::{Id, Mutex},
3230
Error as RolesLogicError,
3331
};
34-
use std::{collections::HashMap, net::SocketAddr, sync::Arc, thread::sleep, time::Duration};
32+
use std::{
33+
collections::{HashMap, VecDeque},
34+
net::SocketAddr,
35+
sync::Arc,
36+
thread::sleep,
37+
time::Duration,
38+
};
3539
use tokio::{net::TcpStream, task, task::AbortHandle};
3640
use tracing::{error, info, warn};
3741

38-
use roles_logic_sv2::{
39-
common_messages_sv2::Reconnect, handlers::mining::SupportedChannelTypes,
40-
mining_sv2::SetGroupChannel,
41-
};
42-
use std::collections::VecDeque;
43-
4442
#[derive(Debug)]
4543
struct CircularBuffer {
4644
buffer: VecDeque<(u64, u32)>,
@@ -451,7 +449,7 @@ impl Upstream {
451449
}
452450
}
453451

454-
impl IsUpstream<Downstream, NullDownstreamMiningSelector> for Upstream {
452+
impl IsUpstream<Downstream> for Upstream {
455453
fn get_version(&self) -> u16 {
456454
todo!()
457455
}
@@ -471,13 +469,9 @@ impl IsUpstream<Downstream, NullDownstreamMiningSelector> for Upstream {
471469
fn get_mapper(&mut self) -> Option<&mut roles_logic_sv2::common_properties::RequestIdMapper> {
472470
todo!()
473471
}
474-
475-
fn get_remote_selector(&mut self) -> &mut NullDownstreamMiningSelector {
476-
todo!()
477-
}
478472
}
479473

480-
impl IsMiningUpstream<Downstream, NullDownstreamMiningSelector> for Upstream {
474+
impl IsMiningUpstream<Downstream> for Upstream {
481475
fn total_hash_rate(&self) -> u64 {
482476
todo!()
483477
}
@@ -526,9 +520,7 @@ impl ParseCommonMessagesFromUpstream for Upstream {
526520

527521
/// Connection-wide SV2 Upstream role messages parser implemented by a downstream ("downstream"
528522
/// here is relative to the SV2 Upstream role and is represented by this `Upstream` struct).
529-
impl ParseMiningMessagesFromUpstream<Downstream, NullDownstreamMiningSelector, NoRouting>
530-
for Upstream
531-
{
523+
impl ParseMiningMessagesFromUpstream<Downstream> for Upstream {
532524
/// Returns the channel type between the SV2 Upstream role and the `Upstream`, which will
533525
/// always be `Extended` for a SV1/SV2 Translator Proxy.
534526
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::{
@@ -17,9 +20,6 @@ use roles_logic_sv2::{
1720
},
1821
mining_sv2::*,
1922
parsers::{AnyMessage, Mining, MiningDeviceMessages},
20-
routing_logic::{
21-
CommonRouter, CommonRoutingLogic, MiningProxyRoutingLogic, MiningRouter, MiningRoutingLogic,
22-
},
2323
utils::Mutex,
2424
};
2525

@@ -287,13 +287,7 @@ impl DownstreamMiningNode {
287287
}
288288

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