Skip to content

Commit 4b3cbd7

Browse files
authored
feat: add delay to start of mining (#131)
Description --- adds a 10 min delay to pool mining where it will solo mine till
1 parent c50e8cc commit 4b3cbd7

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/server/grpc/p2pool.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
use std::{
55
collections::{HashMap, VecDeque},
66
str::FromStr,
7-
sync::Arc,
7+
sync::{
8+
atomic::{AtomicBool, Ordering},
9+
Arc,
10+
},
811
time::Instant,
912
};
1013

@@ -76,6 +79,7 @@ where S: ShareChain
7679
list_of_templates_sha3x: RwLock<VecDeque<FixedHash>>,
7780
template_store_rx: RwLock<HashMap<FixedHash, P2Block>>,
7881
list_of_templates_rx: RwLock<VecDeque<FixedHash>>,
82+
are_we_synced_with_p2pool: Arc<AtomicBool>,
7983
}
8084

8185
impl<S> ShaP2PoolGrpc<S>
@@ -93,6 +97,7 @@ where S: ShareChain
9397
genesis_block_hash: FixedHash,
9498
stats_broadcast: StatsBroadcastClient,
9599
squad: Squad,
100+
are_we_synced_with_p2pool: Arc<AtomicBool>,
96101
) -> Result<Self, Error> {
97102
Ok(Self {
98103
local_peer_id,
@@ -116,11 +121,16 @@ where S: ShareChain
116121
list_of_templates_sha3x: RwLock::new(VecDeque::with_capacity(MAX_STORED_TEMPLATES_SHA3X + 1)),
117122
template_store_rx: RwLock::new(HashMap::new()),
118123
list_of_templates_rx: RwLock::new(VecDeque::with_capacity(MAX_STORED_TEMPLATES_RX + 1)),
124+
are_we_synced_with_p2pool,
119125
})
120126
}
121127

122128
/// Submits a new block to share chain and broadcasts to the p2p network.
123129
pub async fn submit_share_chain_block(&self, block: Arc<P2Block>) -> Result<(), Status> {
130+
if !self.are_we_synced_with_p2pool.load(Ordering::Relaxed) {
131+
info!(target: LOG_TARGET, "We are not synced yet, not submitting blok atm");
132+
return Ok(());
133+
}
124134
let pow_algo = block.original_block.header.pow.pow_algo;
125135
let share_chain = match pow_algo {
126136
PowAlgorithm::RandomX => self.share_chain_random_x.clone(),
@@ -274,7 +284,9 @@ where S: ShareChain
274284
}
275285

276286
// what happens p2pool difficulty > base chain diff
277-
if target_difficulty.as_u64() < miner_data.target_difficulty {
287+
if target_difficulty.as_u64() < miner_data.target_difficulty &&
288+
self.are_we_synced_with_p2pool.load(Ordering::Relaxed)
289+
{
278290
miner_data.target_difficulty = target_difficulty.as_u64();
279291
}
280292
}

src/server/server.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use std::{
55
net::{AddrParseError, SocketAddr},
66
str::FromStr,
7-
sync::Arc,
7+
sync::{atomic::AtomicBool, Arc},
88
};
99

1010
use log::{error, info};
@@ -53,6 +53,7 @@ where S: ShareChain
5353
stats_collector: Option<StatsCollector>,
5454
shutdown_signal: ShutdownSignal,
5555
stats_broadcast_client: StatsBroadcastClient,
56+
are_we_synced_with_p2pool: Arc<AtomicBool>,
5657
}
5758

5859
impl<S> Server<S>
@@ -69,7 +70,7 @@ where S: ShareChain
6970
let share_chain_sha3x = Arc::new(share_chain_sha3x);
7071
let share_chain_random_x = Arc::new(share_chain_random_x);
7172
let network_peer_store = PeerStore::new(&config.peer_store, stats_broadcast_client.clone());
72-
73+
let are_we_synced_with_p2pool = Arc::new(AtomicBool::new(false));
7374
let stats_client = stats_collector.create_client();
7475

7576
let mut p2p_service: p2p::Service<S> = p2p::Service::new(
@@ -107,6 +108,7 @@ where S: ShareChain
107108
genesis_block_hash,
108109
stats_broadcast_client.clone(),
109110
config.p2p_service.squad.clone(),
111+
are_we_synced_with_p2pool.clone(),
110112
)
111113
.await
112114
.map_err(Error::Grpc)?;
@@ -135,6 +137,7 @@ where S: ShareChain
135137
stats_collector: Some(stats_collector),
136138
shutdown_signal,
137139
stats_broadcast_client,
140+
are_we_synced_with_p2pool,
138141
})
139142
}
140143

@@ -167,6 +170,15 @@ where S: ShareChain
167170
pub async fn start(&mut self) -> Result<(), Error> {
168171
info!(target: LOG_TARGET, "⛏ Starting Tari SHA-3 mining P2Pool...");
169172

173+
// this is stupid way of checking sync, but should atleast give as something to mine solo for a while and giving
174+
// p2pool 4 mins to sync
175+
let sync_start = self.are_we_synced_with_p2pool.clone();
176+
tokio::spawn(async move {
177+
tokio::time::sleep(tokio::time::Duration::from_secs(240)).await;
178+
info!(target: LOG_TARGET, "Setting as synced");
179+
sync_start.store(true, std::sync::atomic::Ordering::Relaxed);
180+
});
181+
170182
if self.config.mining_enabled {
171183
// local base node and p2pool node grpc services
172184
let base_node_grpc_service = self.base_node_grpc_service.clone().unwrap();

0 commit comments

Comments
 (0)