From b4a939c8cae55be88a17ad34003f506872398551 Mon Sep 17 00:00:00 2001 From: danda Date: Sat, 30 Mar 2024 10:51:56 -0700 Subject: [PATCH] fix: send syncing start/stop messages to miner 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. --- src/main_loop.rs | 2 ++ src/mine_loop.rs | 25 ++++++++++++++++++------- src/models/channel.rs | 3 +++ 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/main_loop.rs b/src/main_loop.rs index 3eb08ab6..2a48211c 100644 --- a/src/main_loop.rs +++ b/src/main_loop.rs @@ -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)?; } } @@ -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) => { diff --git a/src/mine_loop.rs b/src/mine_loop.rs index a16c8e6a..e207f246 100644 --- a/src/mine_loop.rs +++ b/src/mine_loop.rs @@ -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."); @@ -399,12 +401,8 @@ 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 { @@ -412,10 +410,23 @@ pub async fn mine( } } 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 => { diff --git a/src/models/channel.rs b/src/models/channel.rs index 0d5112d1..a5392553 100644 --- a/src/models/channel.rs +++ b/src/models/channel.rs @@ -24,6 +24,9 @@ pub enum MainToMiner { StopMining, StartMining, + + StartSyncing, + StopSyncing, // SetCoinbasePubkey, }