Skip to content

Commit fdae9c3

Browse files
authored
Merge pull request dmnd-pool#135 from Fi3/AddPoolSignature
Add pool signature
2 parents b8c25dd + 8e648ba commit fdae9c3

6 files changed

Lines changed: 76 additions & 20 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "demand-cli"
3-
version = "0.2.1"
3+
version = "0.2.2"
44
edition = "2021"
55

66
[dependencies]

src/config.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{
77
path::PathBuf,
88
time::Duration,
99
};
10-
use tracing::{debug, error, info, warn};
10+
use tracing::{debug, error, info};
1111

1212
use crate::{
1313
shared::error::Error, HashUnit, DEFAULT_SV1_HASHPOWER, PRODUCTION_URL, STAGING_URL,
@@ -50,6 +50,8 @@ struct Args {
5050
monitor: bool,
5151
#[clap(long, short = 'u')]
5252
auto_update: bool,
53+
#[clap(long)]
54+
signature: Option<String>,
5355
}
5456

5557
#[derive(Serialize, Deserialize)]
@@ -109,6 +111,7 @@ pub struct Configuration {
109111
api_server_port: String,
110112
monitor: bool,
111113
auto_update: bool,
114+
signature: String,
112115
}
113116
impl Configuration {
114117
pub fn token() -> Option<String> {
@@ -213,6 +216,10 @@ impl Configuration {
213216
CONFIG.auto_update
214217
}
215218

219+
pub fn signature() -> String {
220+
CONFIG.signature.clone()
221+
}
222+
216223
// Loads config from CLI, file, or env vars with precedence: CLI > file > env.
217224
fn load_config() -> Self {
218225
let args = Args::parse();
@@ -226,7 +233,23 @@ impl Configuration {
226233
.token
227234
.or(config.token)
228235
.or_else(|| std::env::var("TOKEN").ok());
229-
debug!("User Token: {:?}", token);
236+
println!("User Token: {:?}", token);
237+
238+
let signature = match args.signature {
239+
Some(s) => {
240+
if s.len() == 2 {
241+
println!("Signature provided: DDx{}", s);
242+
format!("DDx{}", s)
243+
} else {
244+
println!("Invalid signature provided, using DDxDD");
245+
"DDxDD".to_string()
246+
}
247+
}
248+
None => {
249+
println!("Signature not provided, using DDxDD");
250+
"DDxDD".to_string()
251+
}
252+
};
230253

231254
let tp_address = args
232255
.tp_address
@@ -261,13 +284,13 @@ impl Configuration {
261284
let downstream_hashrate;
262285
if let Some(hashpower) = expected_hashrate {
263286
downstream_hashrate = hashpower;
264-
info!(
287+
println!(
265288
"Using downstream hashrate: {}h/s",
266289
HashUnit::format_value(hashpower)
267290
);
268291
} else {
269292
downstream_hashrate = DEFAULT_SV1_HASHPOWER;
270-
warn!(
293+
println!(
271294
"No downstream hashrate provided, using default value: {}h/s",
272295
HashUnit::format_value(DEFAULT_SV1_HASHPOWER)
273296
);
@@ -332,6 +355,7 @@ impl Configuration {
332355
api_server_port,
333356
monitor,
334357
auto_update,
358+
signature,
335359
}
336360
}
337361
}

src/main.rs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ mod translator;
3131
const TRANSLATOR_BUFFER_SIZE: usize = 32;
3232
const MIN_EXTRANONCE_SIZE: u16 = 6;
3333
const MIN_EXTRANONCE2_SIZE: u16 = 5;
34-
const UPSTREAM_EXTRANONCE1_SIZE: usize = 15;
34+
const UPSTREAM_EXTRANONCE1_SIZE: usize = 20;
3535
const DEFAULT_SV1_HASHPOWER: f32 = 100_000_000_000_000.0;
3636
const SHARE_PER_MIN: f32 = 10.0;
3737
const CHANNEL_DIFF_UPDTATE_INTERVAL: u32 = 10;
@@ -124,7 +124,13 @@ async fn main() {
124124
let mut router = router::Router::new(pool_addresses, auth_pub_k, None, None);
125125
let epsilon = Duration::from_millis(30_000);
126126
let best_upstream = router.select_pool_connect().await;
127-
initialize_proxy(&mut router, best_upstream, epsilon).await;
127+
initialize_proxy(
128+
&mut router,
129+
best_upstream,
130+
epsilon,
131+
Configuration::signature(),
132+
)
133+
.await;
128134
info!("exiting");
129135
tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
130136
}
@@ -133,6 +139,7 @@ async fn initialize_proxy(
133139
router: &mut Router,
134140
mut pool_addr: Option<std::net::SocketAddr>,
135141
epsilon: Duration,
142+
signature: String,
136143
) {
137144
loop {
138145
let stats_sender = api::stats::StatsSender::new();
@@ -155,17 +162,23 @@ async fn initialize_proxy(
155162
let sv1_ingress_abortable = ingress::sv1_ingress::start_listen_for_downstream(downs_sv1_tx);
156163

157164
let (translator_up_tx, mut translator_up_rx) = channel(10);
158-
let translator_abortable =
159-
match translator::start(downs_sv1_rx, translator_up_tx, stats_sender.clone()).await {
160-
Ok(abortable) => abortable,
161-
Err(e) => {
162-
error!("Impossible to initialize translator: {e}");
163-
// Impossible to start the proxy so we restart proxy
164-
ProxyState::update_translator_state(TranslatorState::Down);
165-
ProxyState::update_tp_state(TpState::Down);
166-
return;
167-
}
168-
};
165+
let translator_abortable = match translator::start(
166+
downs_sv1_rx,
167+
translator_up_tx,
168+
stats_sender.clone(),
169+
signature.clone(),
170+
)
171+
.await
172+
{
173+
Ok(abortable) => abortable,
174+
Err(e) => {
175+
error!("Impossible to initialize translator: {e}");
176+
// Impossible to start the proxy so we restart proxy
177+
ProxyState::update_translator_state(TranslatorState::Down);
178+
ProxyState::update_tp_state(TpState::Down);
179+
return;
180+
}
181+
};
169182

170183
let (from_jdc_to_share_accounter_send, from_jdc_to_share_accounter_recv) = channel(10);
171184
let (from_share_accounter_to_jdc_send, from_share_accounter_to_jdc_recv) = channel(10);

src/translator/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub async fn start(
3535
Option<Address>,
3636
)>,
3737
stats_sender: crate::api::stats::StatsSender,
38+
signature: String,
3839
) -> Result<AbortOnDrop, Error<'static>> {
3940
let task_manager = TaskManager::initialize(pool_connection.clone());
4041
let abortable = task_manager
@@ -102,6 +103,7 @@ pub async fn start(
102103
target.clone(),
103104
diff_config.clone(),
104105
send_to_up,
106+
signature,
105107
)
106108
.await?;
107109

src/translator/upstream/upstream.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ pub struct Upstream {
8686
// than the configured percentage
8787
pub(super) difficulty_config: Arc<Mutex<UpstreamDifficultyConfig>>,
8888
pub sender: TSender<Mining<'static>>,
89+
signature: String,
8990
}
9091

9192
impl PartialEq for Upstream {
@@ -109,6 +110,7 @@ impl Upstream {
109110
target: Arc<Mutex<Vec<u8>>>,
110111
difficulty_config: Arc<Mutex<UpstreamDifficultyConfig>>,
111112
sender: TSender<Mining<'static>>,
113+
signature: String,
112114
) -> ProxyResult<'static, Arc<Mutex<Self>>> {
113115
Ok(Arc::new(Mutex::new(Self {
114116
extranonce_prefix: None,
@@ -123,6 +125,7 @@ impl Upstream {
123125
target,
124126
difficulty_config,
125127
sender,
128+
signature,
126129
})))
127130
}
128131

@@ -383,8 +386,18 @@ impl Upstream {
383386
return;
384387
}
385388
};
389+
let signature = match self_.safe_lock(|s| s.signature.clone()) {
390+
Ok(s) => s,
391+
Err(e) => {
392+
error!("Translator upstream mutex corrupted: {e}");
393+
return;
394+
}
395+
};
386396

387397
sv2_submit.channel_id = channel_id;
398+
let mut extranonce = signature.as_bytes().to_vec();
399+
extranonce.extend_from_slice(&sv2_submit.extranonce.to_vec());
400+
sv2_submit.extranonce = extranonce.try_into().unwrap();
388401

389402
let message =
390403
roles_logic_sv2::parsers::Mining::SubmitSharesExtended(sv2_submit);
@@ -506,12 +519,16 @@ impl ParseUpstreamMiningMessages<Downstream, NullDownstreamMiningSelector, NoRou
506519
/// role in a SV1 `mining.subscribe` message response.
507520
fn handle_open_extended_mining_channel_success(
508521
&mut self,
509-
m: roles_logic_sv2::mining_sv2::OpenExtendedMiningChannelSuccess,
522+
mut m: roles_logic_sv2::mining_sv2::OpenExtendedMiningChannelSuccess,
510523
) -> Result<SendTo<Downstream>, RolesLogicError> {
511524
info!(
512525
"Handling OpenExtendedMiningChannelSuccess message from Pool for Channel Id: {}",
513526
m.channel_id
514527
);
528+
let mut prefix = m.extranonce_prefix.to_vec();
529+
prefix.extend_from_slice(self.signature.as_bytes());
530+
m.extranonce_prefix = prefix.try_into().unwrap();
531+
m.extranonce_size -= self.signature.len() as u16;
515532
let tproxy_e1_len =
516533
proxy_extranonce1_len(m.extranonce_size as usize, self.min_extranonce_size.into())
517534
as u16;

0 commit comments

Comments
 (0)