Import blocks with bad justification (#1977)

* init version

* chore: improve code

* fix test

* fix: log error resulting of bad justification

* fix: add test to check for disconnected peer
This commit is contained in:
Marcio Diaz
2019-03-14 12:24:29 +01:00
committed by Gav Wood
parent b86c96ea31
commit 534863d6d8
5 changed files with 78 additions and 14 deletions
@@ -40,6 +40,8 @@ pub struct ImportedAux {
pub clear_justification_requests: bool,
/// Request a justification for the given block.
pub needs_justification: bool,
/// Received a bad justification.
pub bad_justification: bool,
}
impl Default for ImportedAux {
@@ -47,6 +49,7 @@ impl Default for ImportedAux {
ImportedAux {
clear_justification_requests: false,
needs_justification: false,
bad_justification: false,
}
}
}
@@ -309,7 +309,7 @@ impl<B: BlockT> BlockImporter<B> {
match result {
Ok(BlockImportResult::ImportedKnown(number)) => link.block_imported(&hash, number),
Ok(BlockImportResult::ImportedUnknown(number, aux)) => {
Ok(BlockImportResult::ImportedUnknown(number, aux, who)) => {
link.block_imported(&hash, number);
if aux.clear_justification_requests {
@@ -321,6 +321,12 @@ impl<B: BlockT> BlockImporter<B> {
trace!(target: "sync", "Block imported but requires justification {}: {:?}", number, hash);
link.request_justification(&hash, number);
}
if aux.bad_justification {
if let Some(peer) = who {
link.useless_peer(peer, "Sent block with bad justification to import");
}
}
},
Err(BlockImportError::IncompleteHeader(who)) => {
if let Some(peer) = who {
@@ -428,12 +434,12 @@ impl<B: BlockT, V: 'static + Verifier<B>> BlockImportWorker<B, V> {
let import_result = if has_error {
Err(BlockImportError::Error)
} else {
import_single_block(
&*self.block_import,
origin.clone(),
block.clone(),
self.verifier.clone(),
)
import_single_block(
&*self.block_import,
origin.clone(),
block.clone(),
self.verifier.clone(),
)
};
let was_ok = import_result.is_ok();
results.push((import_result, block.hash));
@@ -479,7 +485,7 @@ pub enum BlockImportResult<N: ::std::fmt::Debug + PartialEq> {
/// Imported known block.
ImportedKnown(N),
/// Imported unknown block.
ImportedUnknown(N, ImportedAux),
ImportedUnknown(N, ImportedAux, Option<Origin>),
}
/// Block import error.
@@ -528,7 +534,7 @@ pub fn import_single_block<B: BlockT, V: Verifier<B>>(
trace!(target: "sync", "Block already in chain {}: {:?}", number, hash);
Ok(BlockImportResult::ImportedKnown(number))
},
Ok(ImportResult::Imported(aux)) => Ok(BlockImportResult::ImportedUnknown(number, aux)),
Ok(ImportResult::Imported(aux)) => Ok(BlockImportResult::ImportedUnknown(number, aux, peer)),
Ok(ImportResult::UnknownParent) => {
debug!(target: "sync", "Block with unknown parent {}: {:?}, parent: {:?}", number, hash, parent);
Err(BlockImportError::UnknownParent)
@@ -627,10 +633,18 @@ mod tests {
assert_eq!(link_port.recv(), Ok(LinkMsg::BlockImported));
// Send an unknown
let results = vec![(Ok(BlockImportResult::ImportedUnknown(Default::default(), Default::default())), Default::default())];
let results = vec![(Ok(BlockImportResult::ImportedUnknown(Default::default(), Default::default(), None)), Default::default())];
let _ = result_sender.send(BlockImportWorkerMsg::Imported(results)).ok().unwrap();
assert_eq!(link_port.recv(), Ok(LinkMsg::BlockImported));
// Send an unknown with peer and bad justification
let results = vec![(Ok(BlockImportResult::ImportedUnknown(Default::default(),
ImportedAux { needs_justification: true, clear_justification_requests: false, bad_justification: true },
Some(0))), Default::default())];
let _ = result_sender.send(BlockImportWorkerMsg::Imported(results)).ok().unwrap();
assert_eq!(link_port.recv(), Ok(LinkMsg::BlockImported));
assert_eq!(link_port.recv(), Ok(LinkMsg::Disconnected));
// Send an incomplete header
let results = vec![(Err(BlockImportError::IncompleteHeader(Some(Default::default()))), Default::default())];
let _ = result_sender.send(BlockImportWorkerMsg::Imported(results)).ok().unwrap();