Skip to content

Commit

Permalink
feat: allow splitting by algo (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
stringhandler authored Nov 6, 2024
1 parent 59f10d5 commit 6d29904
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/cli/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ pub struct StartArgs {

#[arg(long)]
pub max_connections: Option<u32>,

#[arg(long, default_value_t = false)]
pub randomx_disabled: bool,
#[arg(long, default_value_t = false)]
pub sha3x_disabled: bool,
}

#[derive(Clone, Parser, Debug)]
Expand Down
3 changes: 2 additions & 1 deletion src/cli/commands/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use std::{collections::HashMap, env, fs, sync::Arc};

use hickory_resolver::config;
use libp2p::identity::Keypair;
use log::info;
use tari_common::{configuration::Network, initialize_logging};
Expand Down Expand Up @@ -72,6 +71,8 @@ pub async fn server(
if let Some(max_connections) = args.max_connections {
config_builder.with_max_connections(max_connections);
}
config_builder.with_randomx_enabled(!args.randomx_disabled);
config_builder.with_sha3x_enabled(!args.sha3x_disabled);

// try to extract env var based private key
if let Ok(identity_cbor) = env::var("SHA_P2POOL_IDENTITY") {
Expand Down
10 changes: 10 additions & 0 deletions src/server/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ impl ConfigBuilder {
self
}

pub fn with_sha3x_enabled(&mut self, config: bool) -> &mut Self {
self.config.p2p_service.sha3x_enabled = config;
self
}

pub fn with_randomx_enabled(&mut self, config: bool) -> &mut Self {
self.config.p2p_service.randomx_enabled = config;
self
}

pub fn with_private_key(&mut self, config: Option<Keypair>) -> &mut Self {
self.config.p2p_service.private_key = config;
self
Expand Down
21 changes: 20 additions & 1 deletion src/server/p2p/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ pub struct Config {
pub max_blocks_to_request: usize,
pub sync_job_enabled: bool,
pub peer_list_folder: PathBuf,
pub sha3x_enabled: bool,
pub randomx_enabled: bool,
}

impl Default for Config {
Expand All @@ -199,6 +201,8 @@ impl Default for Config {
num_peers_to_sync: 2,
max_blocks_to_request: 20,
sync_job_enabled: true,
sha3x_enabled: true,
randomx_enabled: true,
}
}
}
Expand Down Expand Up @@ -703,6 +707,14 @@ where S: ShareChain
debug!(target: LOG_TARGET, squad = &self.config.squad; "Peer {} sent a notify message that is too old, skipping", source_peer);
return Ok(MessageAcceptance::Ignore);
}
if payload.algo() == PowAlgorithm::RandomX && !self.config.randomx_enabled {
debug!(target: LOG_TARGET, squad = &self.config.squad; "Peer {} sent a RandomX block but RandomX is disabled, skipping", source_peer);
return Ok(MessageAcceptance::Ignore);
}
if payload.algo() == PowAlgorithm::Sha3x && !self.config.sha3x_enabled {
debug!(target: LOG_TARGET, squad = &self.config.squad; "Peer {} sent a Sha3x block but Sha3x is disabled, skipping", source_peer);
return Ok(MessageAcceptance::Ignore);
}
let payload = Arc::new(payload);
let message_peer = payload.peer_id();
if message_peer.to_string() != source_peer.to_string() {
Expand Down Expand Up @@ -1777,7 +1789,14 @@ where S: ShareChain
}

async fn try_sync_from_best_peer(&mut self) {
for algo in &[PowAlgorithm::RandomX, PowAlgorithm::Sha3x] {
let mut algos = vec![];
if self.config.sha3x_enabled {
algos.push(PowAlgorithm::Sha3x);
}
if self.config.randomx_enabled {
algos.push(PowAlgorithm::RandomX);
}
for algo in &algos {
// Find any blocks we are missing.
let _chain = match algo {
PowAlgorithm::RandomX => self.share_chain_random_x.clone(),
Expand Down

0 comments on commit 6d29904

Please sign in to comment.