Преглед на файлове

consensus/consensus_sync: fixed stupid check

aggstam преди 3 години
родител
ревизия
1af30e6975
променени са 2 файла, в които са добавени 14 реда и са изтрити 3 реда
  1. 9 0
      src/blockchain/mod.rs
  2. 5 3
      src/consensus/task/consensus_sync.rs

+ 9 - 0
src/blockchain/mod.rs

@@ -252,4 +252,13 @@ impl Blockchain {
     pub fn has_slot_checkpoint(&self, slot_checkpoint: &SlotCheckpoint) -> Result<bool> {
         Ok(self.slot_checkpoints.get(&[slot_checkpoint.slot], true).is_ok())
     }
+
+    /// Check if block order for the given slot is in the database.
+    pub fn has_slot(&self, slot: u64) -> Result<bool> {
+        let vec = match self.order.get(&[slot], true) {
+            Ok(v) => v,
+            Err(_) => return Ok(false),
+        };
+        Ok(!vec.is_empty())
+    }
 }

+ 5 - 3
src/consensus/task/consensus_sync.rs

@@ -27,6 +27,7 @@ use crate::{
         ValidatorStatePtr,
     },
     net::P2pPtr,
+    util::async_util::sleep,
     Result,
 };
 
@@ -114,6 +115,7 @@ pub async fn consensus_sync_task(p2p: P2pPtr, state: ValidatorStatePtr) -> Resul
     loop {
         if response.forks.len() != 1 || response.forks[0].sequence.len() != 1 {
             warn!("Peer has not finished finalization, retrying...");
+            sleep(1).await;
             peer.send(ConsensusRequest {}).await?;
             response = response_sub.receive().await?;
             continue
@@ -122,11 +124,11 @@ pub async fn consensus_sync_task(p2p: P2pPtr, state: ValidatorStatePtr) -> Resul
     }
 
     // Verify that the node has received all finalized blocks
+    let last_finalized_slot = response.forks[0].sequence[0].proposal.block.header.slot - 1;
     loop {
-        let lock = state.read().await;
-        let last_finalized = lock.consensus.current_slot() - 1;
-        if lock.blockchain.last().unwrap().0 != last_finalized {
+        if !state.read().await.blockchain.has_slot(last_finalized_slot)? {
             warn!("Node has not finished finalization, retrying...");
+            sleep(1).await;
             continue
         }
         break