Skip to content

Commit

Permalink
fix: send syncing start/stop messages to miner
Browse files Browse the repository at this point in the history
The mining loop is no longer async as it is wrapped by
spawn_blocking().

Thus it can no longer await async futures.  So we cannot check
the GlobalState syncing flag, which requires an async lock
acquisition.

Instead, the main task sends StartSyncing and StopSyncing
messages to the mining task which cancels the mining thread
when it receives StartSyncing.
  • Loading branch information
dan-da committed Apr 24, 2024
1 parent b17c8b3 commit b4a939c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ impl MainLoopHandler {
if !stay_in_sync_mode {
info!("Exiting sync mode");
global_state_mut.net.syncing = false;
self.main_to_miner_tx.send(MainToMiner::StopSyncing)?;
}
}

Expand Down Expand Up @@ -459,6 +460,7 @@ impl MainLoopHandler {
socket_addr, claimed_max_height, claimed_max_pow_family
);
global_state_mut.net.syncing = true;
self.main_to_miner_tx.send(MainToMiner::StartSyncing)?;
}
}
PeerThreadToMain::RemovePeerMaxBlockHeight(socket_addr) => {
Expand Down
25 changes: 18 additions & 7 deletions src/mine_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,8 @@ pub async fn mine(
}

let main_message: MainToMiner = from_main.borrow_and_update().clone();
debug!("Miner received message {:?}", main_message);

match main_message {
MainToMiner::Shutdown => {
debug!("Miner shutting down.");
Expand All @@ -399,23 +401,32 @@ pub async fn mine(
info!("Miner thread received {} block height {}", global_state_lock.lock(|s| s.cli().network).await, latest_block.kernel.header.height);
}
MainToMiner::Empty => (),
MainToMiner::ReadyToMineNextBlock => {
debug!("Got {:?} from `main_loop`", MainToMiner::ReadyToMineNextBlock);
}
MainToMiner::ReadyToMineNextBlock => {}
MainToMiner::StopMining => {
debug!("Miner shutting down.");

pause_mine = true;

if let Some(mt) = miner_thread {
mt.abort();
}
}
MainToMiner::StartMining => {
debug!("Starting miner");

pause_mine = false;
}
MainToMiner::StopSyncing => {
// no need to do anything here. Mining will
// resume or not at top of loop depending on
// pause_mine and syncing variables.
}
MainToMiner::StartSyncing => {
// when syncing begins, we must halt the mining
// thread. But we don't change the pause_mine
// variable, because it reflects the logical on/off
// of mining, which syncing can temporarily override
// but not alter the setting.
if let Some(mt) = miner_thread {
mt.abort();
}
}
}
}
new_block_res = worker_thread_rx => {
Expand Down
3 changes: 3 additions & 0 deletions src/models/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ pub enum MainToMiner {

StopMining,
StartMining,

StartSyncing,
StopSyncing,
// SetCoinbasePubkey,
}

Expand Down

0 comments on commit b4a939c

Please sign in to comment.