Skip to content

Commit

Permalink
fix bug for re execute dag blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
jackzhhuang committed Jul 4, 2024
1 parent 883ddf4 commit 0e96775
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 44 deletions.
5 changes: 0 additions & 5 deletions flexidag/src/blockdag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,6 @@ impl BlockDAG {
header.id(),
reachability::ReachabilityError::DataInconsistency
);
self.set_reindex_root(origin)?;
bail!(
"failed to add a block: {:?} when committing for data inconsistency.",
header.id()
);
}
Err(reachability::ReachabilityError::StoreError(StoreError::KeyNotFound(msg))) => {
if msg == *REINDEX_ROOT_KEY.to_string() {
Expand Down
40 changes: 11 additions & 29 deletions sync/src/tasks/block_sync_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use starcoin_crypto::HashValue;
use starcoin_logger::prelude::*;
use starcoin_network_rpc_api::MAX_BLOCK_REQUEST_SIZE;
use starcoin_storage::db_storage::SchemaIterator;
use starcoin_storage::{Store, BARNARD_HARD_FORK_HASH};
use starcoin_storage::Store;
use starcoin_sync_api::SyncTarget;
use starcoin_types::block::{Block, BlockHeader, BlockIdAndNumber, BlockInfo, BlockNumber};
use std::collections::HashMap;
Expand Down Expand Up @@ -330,32 +330,6 @@ where
}

fn apply_block(&mut self, block: Block, peer_id: Option<PeerId>) -> Result<()> {
if let Some((_failed_block, pre_peer_id, err, version)) = self
.chain
.get_storage()
.get_failed_block_by_id(block.id())?
{
if version == *G_CRATE_VERSION {
warn!(
"[sync] apply a previous failed block: {}, previous_peer_id:{:?}, err: {}",
block.id(),
pre_peer_id,
err
);
if let Some(peer) = peer_id {
self.peer_provider
.report_peer(peer, ConnectBlockError::REP_VERIFY_BLOCK_FAILED);
}
return Err(format_err!("collect previous failed block:{}", block.id()));
}
}
if block.id() == *BARNARD_HARD_FORK_HASH {
if let Some(peer) = peer_id {
warn!("[barnard hard fork] ban peer {}", peer);
self.peer_provider.ban_peer(peer, true);
}
return Err(format_err!("reject barnard hard fork block:{}", block.id()));
}
let apply_result = if self.skip_pow_verify {
self.chain
.apply_with_verifier::<BasicVerifier>(block.clone())
Expand Down Expand Up @@ -521,8 +495,16 @@ where
match self.local_store.get_dag_sync_block(*id) {
Ok(op_dag_sync_block) => {
if let Some(dag_sync_block) = op_dag_sync_block {
result.push(dag_sync_block.block.header().clone());
false // read from local store, remove from p2p request
match self.sync_dag_store.save_block(dag_sync_block.block.clone()) {
Ok(_) => {
result.push(dag_sync_block.block.header().clone());
false // read from local store, remove from p2p request
}
Err(e) => {
debug!("failed to save block for: {:?}", e);
true // need retaining
}
}
} else {
true // need retaining
}
Expand Down
20 changes: 10 additions & 10 deletions sync/src/tasks/continue_execute_absent_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ impl<'a> ContinueExecuteAbsentBlock<'a> {
}
}
self.local_store.delete_dag_sync_block(parent_block_id)?;
self.sync_dag_store
.delete_dag_sync_block(parent_block.block.header().number(), parent_block_id)?;
}

parent_block_ids = next_parent_blocks;
Expand All @@ -116,8 +114,10 @@ impl<'a> ContinueExecuteAbsentBlock<'a> {
block_header.number()
)
})?;
parent_block.children.push(block_header.id());
self.local_store.save_dag_sync_block(parent_block)?;
if !parent_block.children.contains(&block_header.id()) {
parent_block.children.push(block_header.id());
self.local_store.save_dag_sync_block(parent_block)?;
}
result = Ok(false);
}
}
Expand Down Expand Up @@ -146,7 +146,9 @@ impl<'a> ContinueExecuteAbsentBlock<'a> {
});

let result: anyhow::Result<()> = absent_ancestor.iter().try_for_each(|block| {
if self.check_parents_exist(block.header())? {
if self.check_parents_exist(block.header())?
&& !self.operator.has_dag_block(block.id())?
{
info!(
"now apply for sync after fetching a dag block: {:?}, number: {:?}",
block.id(),
Expand All @@ -164,13 +166,11 @@ impl<'a> ContinueExecuteAbsentBlock<'a> {
self.local_store
.delete_dag_sync_block(executed_block.block.id())?;

self.sync_dag_store.delete_dag_sync_block(
executed_block.block.header().number(),
executed_block.block.id(),
)?;

self.operator.notify(executed_block)?;
}
// delete the block anyway
self.sync_dag_store
.delete_dag_sync_block(block.header().number(), block.id())?;
anyhow::Result::Ok(())
});
result
Expand Down

0 comments on commit 0e96775

Please sign in to comment.