consensus/block_sync: ignore already existing blocks

This commit is contained in:
aggstam
2022-08-03 18:19:19 +03:00
parent a6b487c617
commit f3d1107d2c
2 changed files with 32 additions and 1 deletions

View File

@@ -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(

View File

@@ -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);