Skip to content

Commit

Permalink
fix rejection of old sv1 shares
Browse files Browse the repository at this point in the history
  • Loading branch information
plebhash committed Jun 2, 2024
1 parent 843eb38 commit 6a238a2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ pub enum OnNewShare {
/// Indicate that the share meet downstream target, in the case we could send a success
/// response dowmstream.
ShareMeetDownstreamTarget,
/// Indicate that an invalid share was received
InvalidShare(SubmitSharesError<'static>),
}

impl OnNewShare {
Expand Down Expand Up @@ -114,6 +116,7 @@ impl OnNewShare {
}
},
OnNewShare::ShareMeetDownstreamTarget => todo!(),
OnNewShare::InvalidShare(_) => (),
}
}
}
Expand Down Expand Up @@ -1506,7 +1509,7 @@ impl ProxyExtendedChannelFactory {
.try_into()
.unwrap(),
};
return Ok(OnNewShare::SendErrorDownstream(error));
return Ok(OnNewShare::InvalidShare(error));
}

if let Some(job_creator) = self.job_creator.as_mut() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ pool_signature = "Stratum v2 SRI Pool"

# Template Provider config
# Local TP (this is pointing to localhost so you must run a TP locally for this configuration to work)
tp_address = "127.0.0.1:8442"
tp_address = "100.126.210.102:8442"
21 changes: 15 additions & 6 deletions roles/translator/src/lib/downstream_sv1/downstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub struct Downstream {
extranonce2_len: usize,
pub(super) difficulty_mgmt: DownstreamDifficultyConfig,
pub(super) upstream_difficulty_config: Arc<Mutex<UpstreamDifficultyConfig>>,
last_job_id: String, // we usually receive a String on SV1 messages, no need to cast to u32
}

impl Downstream {
Expand Down Expand Up @@ -131,6 +132,7 @@ impl Downstream {
extranonce2_len,
difficulty_mgmt: difficulty_config,
upstream_difficulty_config,
last_job_id: "".to_string(),
}));
let self_ = downstream.clone();

Expand Down Expand Up @@ -290,9 +292,11 @@ impl Downstream {
// if hashrate has changed, update difficulty management, and send new mining.set_difficulty
handle_result!(tx_status_notify, Self::try_update_difficulty_settings(downstream.clone()).await);


let sv1_mining_notify_msg = handle_result!(tx_status_notify, res);
let message: json_rpc::Message = sv1_mining_notify_msg.into();
let message: json_rpc::Message = sv1_mining_notify_msg.clone().into();

self_.safe_lock(|s| s.last_job_id = sv1_mining_notify_msg.job_id).unwrap();

handle_result!(tx_status_notify, Downstream::send_message_downstream(downstream.clone(), message).await);
},
_ = rx_shutdown.recv().fuse() => {
Expand Down Expand Up @@ -491,12 +495,12 @@ impl IsServer<'static> for Downstream {
/// When miner find the job which meets requested difficulty, it can submit share to the server.
/// Only [Submit](client_to_server::Submit) requests for authorized user names can be submitted.
fn handle_submit(&self, request: &client_to_server::Submit<'static>) -> bool {
info!("Down: Submitting Share");
info!("Down: Submitting Share {:?}", request);
debug!("Down: Handling mining.submit: {:?}", &request);

// TODO: Check if receiving valid shares by adding diff field to Downstream

if self.first_job_received {
// if self.first_job_received {
let to_send = SubmitShareWithChannelId {
channel_id: self.connection_id,
share: request.clone(),
Expand All @@ -507,8 +511,13 @@ impl IsServer<'static> for Downstream {
self.tx_sv1_bridge
.try_send(DownstreamMessages::SubmitShares(to_send))
.unwrap();
};
true

// };
if request.job_id != self.last_job_id {
false
} else {
true
}
}

/// Indicates to the server that the client supports the mining.set_extranonce method.
Expand Down
7 changes: 4 additions & 3 deletions roles/translator/src/lib/proxy/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use super::super::{
};
use error_handling::handle_result;
use roles_logic_sv2::{channel_logic::channel_factory::OnNewShare, Error as RolesLogicError};
use tracing::{debug, error, info};
use tracing::{debug, error, info, warn};

/// Bridge between the SV2 `Upstream` and SV1 `Downstream` responsible for the following messaging
/// translation:
Expand Down Expand Up @@ -234,8 +234,9 @@ impl Bridge {
.map_err(|_| PoisonLock);

match res {
Ok(Ok(OnNewShare::SendErrorDownstream(e))) => {
error!(
Ok(Ok(OnNewShare::SendErrorDownstream(_))) => unreachable!(),
Ok(Ok(OnNewShare::InvalidShare(e))) => {
warn!(
"Submit share error {:?}",
std::str::from_utf8(&e.error_code.to_vec()[..])
);
Expand Down

0 comments on commit 6a238a2

Please sign in to comment.