Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

routing_logic and selectors refactoring #1573

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 4 additions & 16 deletions protocols/v2/roles-logic-sv2/src/common_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
//! and channel management. These definitions form the foundation for consistent communication and
//! behavior across Sv2 roles/applications.

use crate::selectors::{
DownstreamMiningSelector, DownstreamSelector, NullDownstreamMiningSelector,
};
use common_messages_sv2::{has_requires_std_job, Protocol, SetupConnection};
use mining_sv2::{Extranonce, Target};
use nohash_hasher::BuildNoHashHasher;
Expand Down Expand Up @@ -72,7 +69,7 @@ pub struct PairSettings {
}

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

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

/// Provides a request ID mapper for viewing and managing upstream-downstream communication.
fn get_mapper(&mut self) -> Option<&mut RequestIdMapper>;

/// Returns the selector ([`crate::selectors`] for managing downstream nodes.
fn get_remote_selector(&mut self) -> &mut Sel;
}

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

Expand Down Expand Up @@ -207,7 +199,7 @@ pub trait IsMiningDownstream: IsDownstream {
}

// Implemented for the `NullDownstreamMiningSelector`.
impl<Down: IsDownstream + D> IsUpstream<Down, NullDownstreamMiningSelector> for () {
impl<Down: IsDownstream + D> IsUpstream<Down> for () {
fn get_version(&self) -> u16 {
unreachable!("Null upstream do not have a version");
}
Expand All @@ -226,10 +218,6 @@ impl<Down: IsDownstream + D> IsUpstream<Down, NullDownstreamMiningSelector> for
fn get_mapper(&mut self) -> Option<&mut RequestIdMapper> {
unreachable!("Null upstream do not have a mapper")
}

fn get_remote_selector(&mut self) -> &mut NullDownstreamMiningSelector {
unreachable!("Null upstream do not have a selector")
}
}

// Implemented for the `NullDownstreamMiningSelector`.
Expand All @@ -239,7 +227,7 @@ impl IsDownstream for () {
}
}

impl<Down: IsMiningDownstream + D> IsMiningUpstream<Down, NullDownstreamMiningSelector> for () {
impl<Down: IsMiningDownstream + D> IsMiningUpstream<Down> for () {
fn total_hash_rate(&self) -> u64 {
unreachable!("Null selector do not have hash rate");
}
Expand Down
22 changes: 6 additions & 16 deletions protocols/v2/roles-logic-sv2/src/handlers/mining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@ use mining_sv2::{
UpdateChannel, UpdateChannelError,
};

use crate::{
common_properties::{IsMiningDownstream, IsMiningUpstream},
routing_logic::MiningRouter,
selectors::DownstreamMiningSelector,
};
use crate::common_properties::{IsMiningDownstream, IsMiningUpstream};

use super::SendTo_;

Expand All @@ -67,11 +63,8 @@ pub enum SupportedChannelTypes {
///
/// This trait defines methods for parsing and routing downstream messages
/// related to mining operations.
pub trait ParseMiningMessagesFromDownstream<
Up: IsMiningUpstream<Self, Selector> + D,
Selector: DownstreamMiningSelector<Self> + D,
Router: MiningRouter<Self, Up, Selector>,
> where
pub trait ParseMiningMessagesFromDownstream<Up: IsMiningUpstream<Self> + D>
where
Self: IsMiningDownstream + Sized + D,
{
/// Returns the type of channel supported by the downstream connection.
Expand Down Expand Up @@ -243,12 +236,9 @@ pub trait ParseMiningMessagesFromDownstream<
///
/// This trait provides the functionality to handle and route various types of mining messages
/// from the upstream based on the message type and payload.
pub trait ParseMiningMessagesFromUpstream<
Down: IsMiningDownstream + D,
Selector: DownstreamMiningSelector<Down> + D,
Router: MiningRouter<Down, Self, Selector>,
> where
Self: IsMiningUpstream<Down, Selector> + Sized + D,
pub trait ParseMiningMessagesFromUpstream<Down: IsMiningDownstream + D>
where
Self: IsMiningUpstream<Down> + Sized + D,
{
/// Retrieves the type of the channel supported by this upstream parser.
fn get_channel_type(&self) -> SupportedChannelTypes;
Expand Down
2 changes: 0 additions & 2 deletions protocols/v2/roles-logic-sv2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ pub mod handlers;
pub mod job_creator;
pub mod job_dispatcher;
pub mod parsers;
pub mod routing_logic;
pub mod selectors;
pub mod utils;
pub use common_messages_sv2;
pub use errors::Error;
Expand Down
10 changes: 1 addition & 9 deletions roles/jd-client/src/lib/downstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,16 +432,8 @@ impl DownstreamMiningNode {
}
}

use roles_logic_sv2::selectors::NullDownstreamMiningSelector;

/// It impl UpstreamMining cause the proxy act as an upstream node for the DownstreamMiningNode
impl
ParseMiningMessagesFromDownstream<
UpstreamMiningNode,
NullDownstreamMiningSelector,
roles_logic_sv2::routing_logic::NoRouting,
> for DownstreamMiningNode
{
impl ParseMiningMessagesFromDownstream<UpstreamMiningNode> for DownstreamMiningNode {
fn get_channel_type(&self) -> SupportedChannelTypes {
SupportedChannelTypes::Extended
}
Expand Down
34 changes: 13 additions & 21 deletions roles/jd-client/src/lib/upstream_sv2/upstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,28 @@ use key_utils::Secp256k1PublicKey;
use network_helpers_sv2::noise_connection::Connection;
use roles_logic_sv2::{
channel_logic::channel_factory::PoolChannelFactory,
common_messages_sv2::{Protocol, SetupConnection},
common_messages_sv2::{Protocol, Reconnect, SetupConnection},
common_properties::{IsMiningUpstream, IsUpstream},
handlers::{
common::{ParseCommonMessagesFromUpstream, SendTo as SendToCommon},
mining::{ParseMiningMessagesFromUpstream, SendTo},
mining::{ParseMiningMessagesFromUpstream, SendTo, SupportedChannelTypes},
},
job_declaration_sv2::DeclareMiningJob,
mining_sv2::{ExtendedExtranonce, Extranonce, SetCustomMiningJob},
mining_sv2::{ExtendedExtranonce, Extranonce, SetCustomMiningJob, SetGroupChannel},
parsers::{AnyMessage, Mining, MiningDeviceMessages},
routing_logic::NoRouting,
selectors::NullDownstreamMiningSelector,
utils::{Id, Mutex},
Error as RolesLogicError,
};
use std::{collections::HashMap, net::SocketAddr, sync::Arc, thread::sleep, time::Duration};
use std::{
collections::{HashMap, VecDeque},
net::SocketAddr,
sync::Arc,
thread::sleep,
time::Duration,
};
use tokio::{net::TcpStream, task, task::AbortHandle};
use tracing::{debug, error, info, warn};

use roles_logic_sv2::{
common_messages_sv2::Reconnect, handlers::mining::SupportedChannelTypes,
mining_sv2::SetGroupChannel,
};
use std::collections::VecDeque;

#[derive(Debug)]
struct CircularBuffer {
buffer: VecDeque<(u64, u32)>,
Expand Down Expand Up @@ -451,7 +449,7 @@ impl Upstream {
}
}

impl IsUpstream<Downstream, NullDownstreamMiningSelector> for Upstream {
impl IsUpstream<Downstream> for Upstream {
fn get_version(&self) -> u16 {
todo!()
}
Expand All @@ -471,13 +469,9 @@ impl IsUpstream<Downstream, NullDownstreamMiningSelector> for Upstream {
fn get_mapper(&mut self) -> Option<&mut roles_logic_sv2::common_properties::RequestIdMapper> {
todo!()
}

fn get_remote_selector(&mut self) -> &mut NullDownstreamMiningSelector {
todo!()
}
}

impl IsMiningUpstream<Downstream, NullDownstreamMiningSelector> for Upstream {
impl IsMiningUpstream<Downstream> for Upstream {
fn total_hash_rate(&self) -> u64 {
todo!()
}
Expand Down Expand Up @@ -530,9 +524,7 @@ impl ParseCommonMessagesFromUpstream for Upstream {

/// Connection-wide SV2 Upstream role messages parser implemented by a downstream ("downstream"
/// here is relative to the SV2 Upstream role and is represented by this `Upstream` struct).
impl ParseMiningMessagesFromUpstream<Downstream, NullDownstreamMiningSelector, NoRouting>
for Upstream
{
impl ParseMiningMessagesFromUpstream<Downstream> for Upstream {
/// Returns the channel type between the SV2 Upstream role and the `Upstream`, which will
/// always be `Extended` for a SV1/SV2 Translator Proxy.
fn get_channel_type(&self) -> SupportedChannelTypes {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
upstreams = [
{ channel_kind = "Extended", address = "0.0.0.0", port = 34265, pub_key = "9auqWEzQDVyd2oe1JVGFLMLHZtCo2FFqZwtKA5gd9xbuEu7PH72"}
{ channel_kind = "Extended", address = "0.0.0.0", port = 34254, pub_key = "9auqWEzQDVyd2oe1JVGFLMLHZtCo2FFqZwtKA5gd9xbuEu7PH72"}
]
listen_address = "127.0.0.1"
listen_mining_port = 34255
Expand Down
26 changes: 8 additions & 18 deletions roles/mining-proxy/src/lib/downstream_mining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use async_channel::{Receiver, SendError, Sender};
use tokio::{net::TcpListener, sync::oneshot::Receiver as TokioReceiver};
use tracing::{debug, info, trace, warn};

use super::upstream_mining::{ProxyRemoteSelector, StdFrame as UpstreamFrame, UpstreamMiningNode};
use super::{
routing_logic::{CommonRouter, CommonRoutingLogic, MiningRouter, MiningRoutingLogic},
upstream_mining::{StdFrame as UpstreamFrame, UpstreamMiningNode},
};
use codec_sv2::{StandardEitherFrame, StandardSv2Frame};
use network_helpers_sv2::plain_connection::PlainConnection;
use roles_logic_sv2::{
Expand All @@ -17,9 +20,6 @@ use roles_logic_sv2::{
},
mining_sv2::*,
parsers::{AnyMessage, Mining, MiningDeviceMessages},
routing_logic::{
CommonRouter, CommonRoutingLogic, MiningProxyRoutingLogic, MiningRouter, MiningRoutingLogic,
},
utils::Mutex,
};

Expand Down Expand Up @@ -287,13 +287,7 @@ impl DownstreamMiningNode {
}

/// It impl UpstreamMining cause the proxy act as an upstream node for the DownstreamMiningNode
impl
ParseMiningMessagesFromDownstream<
UpstreamMiningNode,
ProxyRemoteSelector,
MiningProxyRoutingLogic<Self, UpstreamMiningNode, ProxyRemoteSelector>,
> for DownstreamMiningNode
{
impl ParseMiningMessagesFromDownstream<UpstreamMiningNode> for DownstreamMiningNode {
fn get_channel_type(&self) -> SupportedChannelTypes {
SupportedChannelTypes::Group
}
Expand Down Expand Up @@ -452,13 +446,9 @@ impl ParseCommonMessagesFromDownstream for DownstreamMiningNode {
let result = r_logic.safe_lock(|r_logic| r_logic.on_setup_connection(&m))?;
let (data, message) = result?;
let upstream = match super::get_routing_logic() {
roles_logic_sv2::routing_logic::MiningRoutingLogic::Proxy(proxy_routing) => {
proxy_routing
.safe_lock(|r| {
r.downstream_to_upstream_map.get(&data).unwrap()[0].clone()
})
.unwrap()
}
MiningRoutingLogic::Proxy(proxy_routing) => proxy_routing
.safe_lock(|r| r.downstream_to_upstream_map.get(&data).unwrap()[0].clone())
.unwrap(),
_ => unreachable!(),
};
self.upstream = Some(upstream);
Expand Down
10 changes: 5 additions & 5 deletions roles/mining-proxy/src/lib/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
pub mod downstream_mining;
pub mod error;
pub mod routing_logic;
pub mod selectors;
pub mod upstream_mining;

use once_cell::sync::OnceCell;
use roles_logic_sv2::{
routing_logic::{CommonRoutingLogic, MiningProxyRoutingLogic, MiningRoutingLogic},
selectors::GeneralMiningSelector,
utils::{GroupId, Id, Mutex},
};
use roles_logic_sv2::utils::{GroupId, Id, Mutex};
use routing_logic::{CommonRoutingLogic, MiningProxyRoutingLogic, MiningRoutingLogic};
use selectors::GeneralMiningSelector;
use serde::Deserialize;
use std::{net::SocketAddr, sync::Arc};
use tokio::{net::TcpListener, sync::oneshot};
Expand Down
Loading
Loading