From 54fb3e82a6ee1593bde4b4ff2364685bb0a3f77c Mon Sep 17 00:00:00 2001 From: lorban Date: Fri, 12 Jul 2024 11:28:29 +0200 Subject: [PATCH] jdc fallback to solo-mining in this PR is fixed the fallback to solo-mining in the case that the upstream sends a `SubmitShareError` on a valid share AND there are no other available upstreams in the JDC config. To check this PR you should follow the same procedure as described in the commit messages of the branch of this PR (which deals with a similar problem). https://github.com/stratum-mining/stratum/pull/1001 The only exception is that the JDC should use this config test/config/change-upstream/jdc-config-local-example-change-upstream-solo-fallback.toml panics: 2024-07-11T15:57:25.769622Z INFO roles_logic_sv2::handlers::template_distribution: Received NewTemplate with id: 694, is future: true thread 'tokio-runtime-worker' panicked at jd-client/src/lib/downstream.rs:379:74: called `Option::unwrap()` on a `None` value note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace 2024-07-11T15:57:35.319832Z INFO roles_logic_sv2::handlers::mining: Received UpdateChannel->Extended message thread 'tokio-runtime-worker' panicked at jd-client/src/lib/downstream.rs:525:13: not yet implemented 2024-07-11T15:57:41.633138Z ERROR network_helpers_sv2::noise_connection_tokio: Disconnected from client while reading : early eof - 127.0.0.1:56278 ^C2024-07-11T15:57:57.023091Z INFO jd_client: Interrupt received --- benches/Cargo.lock | 6 +- roles/jd-client/src/lib/downstream.rs | 27 ++++--- ...example-change-upstream-solo-fallback.toml | 70 +++++++++++++++++++ 3 files changed, 91 insertions(+), 12 deletions(-) create mode 100644 test/config/change-upstream/jdc-config-local-example-change-upstream-solo-fallback.toml diff --git a/benches/Cargo.lock b/benches/Cargo.lock index 946b7c02b8..3964748381 100644 --- a/benches/Cargo.lock +++ b/benches/Cargo.lock @@ -373,7 +373,7 @@ dependencies = [ [[package]] name = "buffer_sv2" -version = "1.0.0" +version = "1.1.0" dependencies = [ "aes-gcm", ] @@ -497,7 +497,7 @@ checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" [[package]] name = "codec_sv2" -version = "1.1.0" +version = "1.2.0" dependencies = [ "binary_sv2", "buffer_sv2", @@ -1564,7 +1564,7 @@ checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" [[package]] name = "sv1_api" -version = "1.0.0" +version = "1.0.1" dependencies = [ "binary_sv2", "bitcoin_hashes 0.3.2", diff --git a/roles/jd-client/src/lib/downstream.rs b/roles/jd-client/src/lib/downstream.rs index 5b26cef2f7..fab6e96b31 100644 --- a/roles/jd-client/src/lib/downstream.rs +++ b/roles/jd-client/src/lib/downstream.rs @@ -47,7 +47,7 @@ pub struct DownstreamMiningNode { miner_coinbase_output: Vec, // used to retreive the job id of the share that we send upstream last_template_id: u64, - jd: Option>>, + pub jd: Option>>, } #[allow(clippy::large_enum_variant)] @@ -376,12 +376,12 @@ impl DownstreamMiningNode { let to_send = to_send.into_values(); for message in to_send { let message = if let Mining::NewExtendedMiningJob(job) = message { - let jd = self_mutex.safe_lock(|s| s.jd.clone()).unwrap().unwrap(); - jd.safe_lock(|jd| jd.coinbase_tx_prefix = job.coinbase_tx_prefix.clone()) - .unwrap(); - jd.safe_lock(|jd| jd.coinbase_tx_suffix = job.coinbase_tx_suffix.clone()) - .unwrap(); - + if let Some(jd) = self_mutex.safe_lock(|s| s.jd.clone()).unwrap() { + jd.safe_lock(|jd| jd.coinbase_tx_prefix = job.coinbase_tx_prefix.clone()) + .unwrap(); + jd.safe_lock(|jd| jd.coinbase_tx_suffix = job.coinbase_tx_suffix.clone()) + .unwrap(); + } Mining::NewExtendedMiningJob(job) } else { message @@ -514,7 +514,7 @@ impl fn handle_update_channel( &mut self, - _: UpdateChannel, + m: UpdateChannel, ) -> Result, Error> { if !self.status.is_solo_miner() { // Safe unwrap alreay checked if it cointains upstream with is_solo_miner @@ -522,7 +522,16 @@ impl self.status.get_upstream().unwrap(), )) } else { - todo!() + let maximum_target = + roles_logic_sv2::utils::hash_rate_to_target(m.nominal_hash_rate.into(), 10.0)?; + self.status + .get_channel() + .update_target_for_channel(m.channel_id, maximum_target.clone().into()); + let set_target = SetTarget { + channel_id: m.channel_id, + maximum_target, + }; + Ok(SendTo::Respond(Mining::SetTarget(set_target))) } } diff --git a/test/config/change-upstream/jdc-config-local-example-change-upstream-solo-fallback.toml b/test/config/change-upstream/jdc-config-local-example-change-upstream-solo-fallback.toml new file mode 100644 index 0000000000..23b7e15df9 --- /dev/null +++ b/test/config/change-upstream/jdc-config-local-example-change-upstream-solo-fallback.toml @@ -0,0 +1,70 @@ +# SRI JDC config +downstream_address = "127.0.0.1" +downstream_port = 34265 + +# Version support +max_supported_version = 2 +min_supported_version = 2 + +# Minimum extranonce2 size for downstream +# Max value: 16 (leaves 0 bytes for search space splitting of downstreams) +# Max value for CGminer: 8 +# Min value: 2 +min_extranonce2_size = 8 + +# Withhold +withhold = false + +# Auth keys for open encrypted connection downstream +authority_public_key = "9auqWEzQDVyd2oe1JVGFLMLHZtCo2FFqZwtKA5gd9xbuEu7PH72" +authority_secret_key = "mkDLTBBRxdBv998612qipDYoTK3YUrqLe8uWw7gu3iXbSrn2n" +cert_validity_sec = 3600 + +# How many time the JDC try to reinitialize itself after a failure +retry = 10 + +# 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" +# Hosted testnet TP +# tp_address = "75.119.150.111:8442" + +# Solo Mining config +# List of coinbase outputs used to build the coinbase tx in case of Solo Mining (as last-resort solution of the pools fallback system) +# ! Put your Extended Public Key or Script as output_script_value ! +# ! Right now only one output is supported, so comment all the ones you don't need ! +# For P2PK, P2PKH, P2WPKH, P2TR a public key is needed. For P2SH and P2WSH, a redeem script is needed. +coinbase_outputs = [ + #{ output_script_type = "P2PK", output_script_value = "0372c47307e5b75ce365daf835f226d246c5a7a92fe24395018d5552123354f086" }, + #{ output_script_type = "P2PKH", output_script_value = "0372c47307e5b75ce365daf835f226d246c5a7a92fe24395018d5552123354f086" }, + #{ output_script_type = "P2SH", output_script_value = "00142ef89234bc95136eb9e6fee9d32722ebd8c1f0ab" }, + #{ output_script_type = "P2WSH", output_script_value = "00142ef89234bc95136eb9e6fee9d32722ebd8c1f0ab" }, + { output_script_type = "P2WPKH", output_script_value = "036adc3bdf21e6f9a0f0fb0066bf517e5b7909ed1563d6958a10993849a7554075" }, + #{ output_script_type = "P2TR", output_script_value = "036adc3bdf21e6f9a0f0fb0066bf517e5b7909ed1563d6958a10993849a7554075" }, +] + +[timeout] +unit = "secs" +value = 1 + +# List of upstreams (JDS) used as backup endpoints +# In case of shares refused by the JDS, the fallback system will propose the same job to the next upstream in this list +[[upstreams]] +authority_pubkey = "9auqWEzQDVyd2oe1JVGFLMLHZtCo2FFqZwtKA5gd9xbuEu7PH72" +pool_address = "127.0.0.1:44254" +jd_address = "127.0.0.1:34264" +# Pool signature (string to be included in coinbase tx) +pool_signature = "Stratum v2 SRI Pool" +#[[upstreams]] +#authority_pubkey = "9auqWEzQDVyd2oe1JVGFLMLHZtCo2FFqZwtKA5gd9xbuEu7PH72" +#pool_address = "127.0.0.1:34254" +#jd_address = "127.0.0.1:34264" +# Pool signature (string to be included in coinbase tx) +#pool_signature = "Stratum v2 SRI Pool" + +# [[upstreams]] +# authority_pubkey = "2di19GHYQnAZJmEpoUeP7C3Eg9TCcksHr23rZCC83dvUiZgiDL" +# pool_address = "127.0.0.1:34254" +# jd_address = "127.0.0.1:34264" +# Pool signature (string to be included in coinbase tx) +# pool_signature = "Stratum v2 SRI Pool"