Skip to content

Commit

Permalink
Merge 3184d82 into 0e8b424
Browse files Browse the repository at this point in the history
  • Loading branch information
jolestar authored Aug 16, 2021
2 parents 0e8b424 + 3184d82 commit 0ce3711
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
23 changes: 12 additions & 11 deletions chain/api/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,20 @@ pub enum ConnectBlockError {
VerifyBlockFailed(VerifyBlockField, Error),
}

#[allow(clippy::from_over_into)]
impl Into<ReputationChange> for &ConnectBlockError {
fn into(self) -> ReputationChange {
impl ConnectBlockError {
// future block do not change the reputation
pub const REP_FUTURE_BLOCK: ReputationChange = ReputationChange::new(0, "FutureBlock");
pub const REP_PARENT_NOT_EXIST: ReputationChange =
ReputationChange::new_fatal("ParentNotExist");
pub const REP_VERIFY_BLOCK_FAILED: ReputationChange =
ReputationChange::new_fatal("VerifyBlockFailed");

pub fn reputation(&self) -> ReputationChange {
match self {
ConnectBlockError::FutureBlock(_) => {
// future block do not change the reputation
ReputationChange::new(0, "FutureBlock")
}
ConnectBlockError::ParentNotExist(_) => {
ReputationChange::new(i32::min_value() / 2, "ParentNotExist")
}
ConnectBlockError::FutureBlock(_) => ConnectBlockError::REP_FUTURE_BLOCK,
ConnectBlockError::ParentNotExist(_) => ConnectBlockError::REP_PARENT_NOT_EXIST,
ConnectBlockError::VerifyBlockFailed(_, _) => {
ReputationChange::new(i32::min_value() / 2, "VerifyBlockFailed")
ConnectBlockError::REP_VERIFY_BLOCK_FAILED
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion sync/src/block_connector/block_connector_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl EventHandler<Self, PeerNewBlock> for BlockConnectorService {

if let Err(e1) = ctx
.get_shared::<NetworkServiceRef>()
.map(|network| network.report_peer(peer_id, (&e).into()))
.map(|network| network.report_peer(peer_id, e.reputation()))
{
warn!("Get NetworkServiceRef err: {:?}.", e1);
}
Expand Down
29 changes: 24 additions & 5 deletions sync/src/tasks/block_sync_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,15 +222,34 @@ where
.sync_apply_block_time
.with_label_values(&["time"])
.start_timer();
if let Err(err) = if self.skip_pow_verify {
if let Some((_failed_block, pre_peer_id, err)) = self
.chain
.get_storage()
.get_failed_block_by_id(block.id())?
{
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()));
}
let apply_result = if self.skip_pow_verify {
self.chain
.apply_with_verifier::<BasicVerifier>(block.clone())
} else {
self.chain.apply(block.clone())
} {
};
if let Err(err) = apply_result {
let error_msg = err.to_string();
error!(
"[sync] collect block error: {:?}, peer_id:{:?} ",
err, peer_id
error_msg, peer_id
);
match err.downcast::<ConnectBlockError>() {
Ok(connect_error) => match connect_error {
Expand All @@ -242,10 +261,10 @@ where
block.id(),
block,
peer_id.clone(),
format!("{:?}", e),
error_msg,
)?;
if let Some(peer) = peer_id {
self.peer_provider.report_peer(peer, (&e).into());
self.peer_provider.report_peer(peer, e.reputation());
}

Err(e.into())
Expand Down

0 comments on commit 0ce3711

Please sign in to comment.