Skip to content

Commit

Permalink
feat: add config for block time and share window (#198)
Browse files Browse the repository at this point in the history
Description
---
adds config option to set share window and block time
  • Loading branch information
SWvheerden authored Dec 4, 2024
1 parent c12ace2 commit 5974235
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 53 deletions.
6 changes: 6 additions & 0 deletions src/cli/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ pub(crate) struct StartArgs {
pub randomx_disabled: bool,
#[arg(long, default_value_t = false)]
pub sha3x_disabled: bool,

#[arg(long, value_name = "bt")]
pub block_time: Option<u64>,

#[arg(long, value_name = "sw")]
pub share_window: Option<u64>,
}

#[derive(Clone, Parser, Debug)]
Expand Down
10 changes: 10 additions & 0 deletions src/cli/commands/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ pub async fn server(
config_builder.with_p2p_port(p2p_port);
}

if let Some(block_time) = args.block_time {
config_builder.with_block_time(block_time);
}

if let Some(share_window) = args.share_window {
config_builder.with_share_window(share_window);
}

config_builder.with_squad(Squad::from(args.squad.clone()));

// set default tari network specific seed peer address
Expand Down Expand Up @@ -129,6 +137,7 @@ genesis_block_hash.to_hex());
let stats_collector = StatsCollector::new(shutdown_signal.clone(), stats_rx);

let share_chain_sha3x = InMemoryShareChain::new(
config.clone(),
PowAlgorithm::Sha3x,
None,
consensus_manager.clone(),
Expand All @@ -137,6 +146,7 @@ genesis_block_hash.to_hex());
)?;
let coinbase_extras_random_x = Arc::new(RwLock::new(HashMap::<String, Vec<u8>>::new()));
let share_chain_random_x = InMemoryShareChain::new(
config.clone(),
PowAlgorithm::RandomX,
Some(block_validation_params.clone()),
consensus_manager,
Expand Down
14 changes: 14 additions & 0 deletions src/server/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub struct Config {
pub network_silence_delay: u64,
pub max_relay_circuits: Option<usize>,
pub max_relay_circuits_per_peer: Option<usize>,
pub block_time: u64,
pub share_window: u64,
}

impl Default for Config {
Expand All @@ -37,6 +39,8 @@ impl Default for Config {
network_silence_delay: 300,
max_relay_circuits: None,
max_relay_circuits_per_peer: None,
block_time: 10,
share_window: 2160,
}
}
}
Expand Down Expand Up @@ -182,6 +186,16 @@ impl ConfigBuilder {
self
}

pub fn with_block_time(&mut self, config: u64) -> &mut Self {
self.config.block_time = config;
self
}

pub fn with_share_window(&mut self, config: u64) -> &mut Self {
self.config.share_window = config;
self
}

pub fn build(&self) -> Config {
self.config.clone()
}
Expand Down
29 changes: 17 additions & 12 deletions src/sharechain/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,9 @@ use tari_core::{
use tari_utilities::{epoch_time::EpochTime, hex::Hex};
use tokio::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};

use super::{
MAIN_REWARD_SHARE,
MAX_BLOCKS_COUNT,
MIN_RANDOMX_DIFFICULTY,
MIN_SHA3X_DIFFICULTY,
SHARE_WINDOW,
UNCLE_REWARD_SHARE,
};
use super::{MAIN_REWARD_SHARE, MIN_RANDOMX_DIFFICULTY, MIN_SHA3X_DIFFICULTY, UNCLE_REWARD_SHARE};
use crate::{
server::{http::stats_collector::StatsBroadcastClient, PROTOCOL_VERSION},
server::{http::stats_collector::StatsBroadcastClient, Config, PROTOCOL_VERSION},
sharechain::{
error::{ShareChainError, ValidationError},
p2block::{P2Block, P2BlockBuilder},
Expand Down Expand Up @@ -58,11 +51,13 @@ pub(crate) struct InMemoryShareChain {
consensus_manager: ConsensusManager,
coinbase_extras: Arc<RwLock<HashMap<String, Vec<u8>>>>,
stat_client: StatsBroadcastClient,
config: Config,
}

#[allow(dead_code)]
impl InMemoryShareChain {
pub fn new(
config: Config,
pow_algo: PowAlgorithm,
block_validation_params: Option<Arc<BlockValidationParams>>,
consensus_manager: ConsensusManager,
Expand All @@ -74,12 +69,17 @@ impl InMemoryShareChain {
}

Ok(Self {
p2_chain: Arc::new(RwLock::new(P2Chain::new_empty(MAX_BLOCKS_COUNT, SHARE_WINDOW))),
p2_chain: Arc::new(RwLock::new(P2Chain::new_empty(
config.share_window * 2,
config.share_window,
config.block_time,
))),
pow_algo,
block_validation_params,
consensus_manager,
coinbase_extras,
stat_client,
config,
})
}

Expand Down Expand Up @@ -198,7 +198,7 @@ impl InMemoryShareChain {
let tip_height = p2_chain.get_tip().unwrap().height;
// We keep more blocks than the share window, but its only to validate the share window. If a block comes in
// older than the share window is way too old for us to care about.
if block.height < tip_height.saturating_sub(SHARE_WINDOW as u64) && !syncing {
if block.height < tip_height.saturating_sub(self.config.share_window) && !syncing {
return Err(ShareChainError::BlockValidation(
"Block is older than share window".to_string(),
));
Expand Down Expand Up @@ -257,7 +257,7 @@ impl InMemoryShareChain {
};

// we want to count 1 short,as the final share will be for this node
let stop_height = tip_level.height.saturating_sub(SHARE_WINDOW as u64 - 1);
let stop_height = tip_level.height.saturating_sub(self.config.share_window - 1);
let mut cur_block = tip_level
.blocks
.get(&tip_level.chain_block)
Expand Down Expand Up @@ -800,6 +800,7 @@ pub mod test {
let (stats_tx, _) = tokio::sync::broadcast::channel(1000);
let stats_broadcast_client = StatsBroadcastClient::new(stats_tx);
InMemoryShareChain::new(
Config::default(),
PowAlgorithm::Sha3x,
None,
consensus_manager,
Expand All @@ -823,6 +824,7 @@ pub mod test {
let (stats_tx, _) = tokio::sync::broadcast::channel(1000);
let stats_broadcast_client = StatsBroadcastClient::new(stats_tx);
let share_chain = InMemoryShareChain::new(
Config::default(),
PowAlgorithm::Sha3x,
None,
consensus_manager,
Expand Down Expand Up @@ -872,6 +874,7 @@ pub mod test {
let static_coinbase_extra = Vec::new();
let stats_broadcast_client = StatsBroadcastClient::new(stats_tx);
let share_chain = InMemoryShareChain::new(
Config::default(),
PowAlgorithm::Sha3x,
None,
consensus_manager,
Expand Down Expand Up @@ -925,6 +928,7 @@ pub mod test {
let stats_broadcast_client = StatsBroadcastClient::new(stats_tx);
let static_coinbase_extra = Vec::new();
let share_chain = InMemoryShareChain::new(
Config::default(),
PowAlgorithm::Sha3x,
None,
consensus_manager,
Expand Down Expand Up @@ -1083,6 +1087,7 @@ pub mod test {
let (stats_tx, _) = tokio::sync::broadcast::channel(1000);
let stats_broadcast_client = StatsBroadcastClient::new(stats_tx);
let share_chain = InMemoryShareChain::new(
Config::default(),
PowAlgorithm::Sha3x,
None,
consensus_manager,
Expand Down
8 changes: 0 additions & 8 deletions src/sharechain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,12 @@ use crate::sharechain::{error::ShareChainError, p2block::P2Block, p2chain::Chain
/// Note: This must be updated when new logic applied to blocks handling.
pub const CHAIN_ID: usize = 2;

/// How many blocks to keep overall.
pub const MAX_BLOCKS_COUNT: usize = 4320;

/// How many blocks are used to calculate current shares to be paid out.
pub const SHARE_WINDOW: usize = 2160;

/// Using 5 and 4 m,eans uncles get 80% of the reward
pub const MAIN_REWARD_SHARE: u64 = 5;
pub const UNCLE_REWARD_SHARE: u64 = 4;

pub const DIFFICULTY_ADJUSTMENT_WINDOW: usize = 90;

pub const BLOCK_TARGET_TIME: u64 = 10;

pub const MIN_RANDOMX_DIFFICULTY: u64 = 1_000; // 1 Khs every ten seconds
pub const MIN_SHA3X_DIFFICULTY: u64 = 100_000_000; // 1 Mhs every ten seconds

Expand Down
Loading

0 comments on commit 5974235

Please sign in to comment.