/// function, this function gets called when a new block event is triggered. It will ensure that the miner
/// restarts/starts to mine.
function, this function gets called when a new block event is triggered. It will ensure that the miner
restarts/starts to mine.
function this function gets called when a new block event is triggered . It will ensure that the miner restarts / starts to mine .
pub async fn mine(mut self) {
        // This flag is used to stop the mining;
        let stop_mining_flag = self.stop_mining_flag.clone();
        let mining_enabled_by_user = self.mining_enabled_by_user.clone();
        let mempool_state_event = self
            .mempool_state_event_rx
            .take()
            .expect("Miner does not have access to the mempool state event stream")
            .fuse();
        let blockchain_event = self
            .node_state_event_rx
            .take()
            .expect("Miner does not have access to state event stream")
            .fuse();
        let mining_instruction_event = self.miner_instruction_events.subscribe().fuse();
        let mut kill_signal = self.kill_signal.clone();
        pin_mut!(mempool_state_event);
        pin_mut!(blockchain_event);
        pin_mut!(mining_instruction_event);
        // Start mining immediately in case we're the only node on the network and we never receive a BlockSync event
        let mut spawn_mining_task = true;
        trace!("starting mining thread");
        'main: loop {
            stop_mining_flag.store(false, Ordering::Relaxed); // ensure we can mine if we need to
            if !mining_enabled_by_user.load(Ordering::Relaxed) {
                spawn_mining_task = false;
            }
            #[allow(clippy::match_bool)]
            let mining_future = match spawn_mining_task {
                true => task::spawn(self.mining()),
                false => task::spawn(self.not_mining()),
            };
            // This flag will let the future select loop again if the miner has not been issued a shutdown command.
            let mut wait_for_mining_event = true;
            while wait_for_mining_event {
                futures::select! {
                    mempool_event = mempool_state_event.select_next_some() => {
                        if let Ok(mempool_event) = mempool_event {
                            match mempool_event {
                                MempoolStateEvent::Updated => {
                                    stop_mining_flag.store(true, Ordering::Relaxed);
                                    spawn_mining_task = true;
                                    wait_for_mining_event = false;
                                },
                                _ => (),
                            }
                        }
                    },
                    blockchain_event = blockchain_event.select_next_some() => {
                        if let Ok(blockchain_event) = blockchain_event {
                            use StateEvent::*;
                            match &*blockchain_event {
                                BlocksSynchronized | NetworkSilence => {
                                    info!(target: LOG_TARGET,
                                    "Our chain has synchronised with the network, or is a seed node. Starting miner");
                                    stop_mining_flag.store(true, Ordering::Relaxed);
                                    spawn_mining_task = true;
                                    wait_for_mining_event = false;
                                },
                                FallenBehind(SyncStatus::Lagging(_, _)) => {
                                    info!(target: LOG_TARGET, "Our chain has fallen behind the network. Pausing miner");
                                    stop_mining_flag.store(true, Ordering::Relaxed);
                                    spawn_mining_task = false;
                                    wait_for_mining_event = false;
                                },
                                _ => {
                                    stop_mining_flag.store(true, Ordering::Relaxed);
                                    wait_for_mining_event = false;
                                },
                            }
                        }
                    },
                    instruction = mining_instruction_event.select_next_some() => {
                        use MinerInstruction::*;
                        match instruction.unwrap_or_else(|_| (IgnoreInstruction)) {
                            PauseMining => {
                                info!(target: LOG_TARGET, "Mining pause event received from the CLI");
                                stop_mining_flag.store(true, Ordering::Relaxed);
                                mining_enabled_by_user.store(false, Ordering::Relaxed);
                                spawn_mining_task = false;
                                wait_for_mining_event = false;
                            },
                            StartMining => {
                                info!(target: LOG_TARGET, "Mining start event received from the CLI");
                                mining_enabled_by_user.store(true, Ordering::Relaxed);
                                spawn_mining_task = true;
                                wait_for_mining_event = false;
                            },
                            _ => (),
                        }
                    },
                    _ = kill_signal => {
                        info!(target: LOG_TARGET, "Mining kill signal received! Miner is shutting down");
                        stop_mining_flag.store(true, Ordering::Relaxed);
                        break 'main;
                    }
                };
            }
            self = mining_future.await.expect("Miner crashed").expect("Miner crashed");
        }
        debug!(target: LOG_TARGET, "Mining thread stopped.");
    }
pub async fn mine ( mut self ) { // This flag is used to stop the mining; let stop_mining_flag = self . stop_mining_flag . clone ( ) ; let mining_enabled_by_user = self . mining_enabled_by_user . clone ( ) ; let mempool_state_event = self . mempool_state_event_rx . take ( ) . expect ( " " ) . fuse ( ) ; let blockchain_event = self . node_state_event_rx . take ( ) . expect ( " " ) . fuse ( ) ; let mining_instruction_event = self . miner_instruction_events . subscribe ( ) . fuse ( ) ; let mut kill_signal = self . kill_signal . clone ( ) ; pin_mut ! ( mempool_state_event ) ; pin_mut ! ( blockchain_event ) ; pin_mut ! ( mining_instruction_event ) ; // Start mining immediately in case we're the only node on the network and we never receive a BlockSync event let mut spawn_mining_task = true ; trace ! ( " " ) ; ' main : loop { stop_mining_flag . store ( false , Ordering :: Relaxed ) ; // ensure we can mine if we need to if ! mining_enabled_by_user . load ( Ordering :: Relaxed ) { spawn_mining_task = false ; } # [ allow ( clippy match_bool ) ] let mining_future = match spawn_mining_task { true => task :: spawn ( self . mining ( ) ) , false => task :: spawn ( self . not_mining ( ) ) , } ; // This flag will let the future select loop again if the miner has not been issued a shutdown command. let mut wait_for_mining_event = true ; while wait_for_mining_event { futures :: select ! { mempool_event mempool_state_event select_next_some ( ) { if let Ok ( mempool_event ) mempool_event { match mempool_event { MempoolStateEvent Updated { stop_mining_flag store ( true Ordering Relaxed ) spawn_mining_task true wait_for_mining_event false } ( ) } } } blockchain_event blockchain_event select_next_some ( ) { if let Ok ( blockchain_event ) blockchain_event { use StateEvent match blockchain_event { BlocksSynchronized NetworkSilence { info ( target LOG_TARGET " " ) stop_mining_flag store ( true Ordering Relaxed ) spawn_mining_task true wait_for_mining_event false } FallenBehind ( SyncStatus Lagging ( ) ) { info ( target LOG_TARGET " " ) stop_mining_flag store ( true Ordering Relaxed ) spawn_mining_task false wait_for_mining_event false } { stop_mining_flag store ( true Ordering Relaxed ) wait_for_mining_event false } } } } instruction mining_instruction_event select_next_some ( ) { use MinerInstruction match instruction unwrap_or_else ( ( IgnoreInstruction ) ) { PauseMining { info ( target LOG_TARGET " " ) stop_mining_flag store ( true Ordering Relaxed ) mining_enabled_by_user store ( false Ordering Relaxed ) spawn_mining_task false wait_for_mining_event false } StartMining { info ( target LOG_TARGET " " ) mining_enabled_by_user store ( true Ordering Relaxed ) spawn_mining_task true wait_for_mining_event false } ( ) } } kill_signal { info ( target LOG_TARGET " " ) stop_mining_flag store ( true Ordering Relaxed ) break ' main } } ; } self = mining_future . await . expect ( " " ) . expect ( " " ) ; } debug ! ( target LOG_TARGET " " ) ; }
function, this function gets called when a new block event is triggered.
function this function gets called when a new block event is triggered .
mut self