Переглянути джерело

consensus/block_sync: ignore already existing blocks

aggstam 4 роки тому
батько
коміт
f3d1107d2c
2 змінених файлів з 32 додано та 1 видалено
  1. 31 0
      src/consensus/state.rs
  2. 1 1
      src/consensus/task/block_sync.rs

+ 31 - 0
src/consensus/state.rs

@@ -970,6 +970,37 @@ impl ValidatorState {
         Ok(true)
     }
 
+    /// Validate and append to canonical state received finalized blocks from block sync task.
+    /// Already existing blocks are ignored.
+    pub async fn receive_sync_blocks(&mut self, blocks: &[BlockInfo]) -> Result<()> {
+        let mut new_blocks = vec![];
+        for block in blocks {
+            match self.blockchain.has_block(&block) {
+                Ok(v) => {
+                    if v {
+                        debug!("receive_sync_blocks(): Existing block received");
+                        continue
+                    }
+                    new_blocks.push(block.clone());
+                }
+                Err(e) => {
+                    error!("receive_sync_blocks(): failed checking for has_block(): {}", e);
+                    continue
+                }
+            };
+        }
+
+        if new_blocks.is_empty() {
+            debug!("receive_sync_blocks(): no new blocks to append");
+            return Ok(())
+        }
+
+        debug!("receive_sync_blocks(): Executing state transitions");
+        self.receive_blocks(&new_blocks[..]).await?;
+
+        Ok(())
+    }
+
     /// Validate state transitions for given transactions and state and
     /// return a vector of [`StateUpdate`]
     pub fn validate_state_transitions(

+ 1 - 1
src/consensus/task/block_sync.rs

@@ -40,7 +40,7 @@ pub async fn block_sync_task(p2p: net::P2pPtr, state: ValidatorStatePtr) -> Resu
 
             // Verify and store retrieved blocks
             debug!("block_sync_task(): Processing received blocks");
-            state.write().await.receive_blocks(&resp.blocks).await?;
+            state.write().await.receive_sync_blocks(&resp.blocks).await?;
 
             let last_received = state.read().await.blockchain.last()?;
             info!("Last received block: {:?} - {:?}", last_received.0, last_received.1);