Rewrite network protocol/service to use channels (#1340)

* rewrite network protocol/service to use channels

* remove use of unwrap

* re-introduce with_spec

* remove unnecessary mut

* remove unused param

* improve with_spec, add with_gossip

* rename job to task

* style: re-add comma

* remove extra string allocs

* rename use of channel

* turn TODO into FIXME

* remove mut in match

* remove Self in new

* pass headers by value to network service

* remove network sender from service

* remove TODO

* better expect

* rationalize use of network sender in ondemand
This commit is contained in:
Gregory Terzian
2019-02-06 19:54:02 +08:00
committed by Bastian Köcher
parent 8aae19e2db
commit a2d2ed69ab
19 changed files with 1314 additions and 903 deletions
+15 -11
View File
@@ -25,7 +25,7 @@ use consensus::import_queue::{ImportQueue, IncomingBlock};
use client::error::Error as ClientError;
use blocks::BlockCollection;
use runtime_primitives::Justification;
use runtime_primitives::traits::{Block as BlockT, Header as HeaderT, As, NumberFor, Zero};
use runtime_primitives::traits::{Block as BlockT, Header as HeaderT, As, NumberFor};
use runtime_primitives::generic::BlockId;
use message::{self, generic::Message as GenericMessage};
use config::Roles;
@@ -211,7 +211,7 @@ impl<B: BlockT> PendingJustifications<B> {
} else {
protocol.report_peer(
who,
Severity::Bad(&format!("Invalid justification provided for #{}", request.0)),
Severity::Bad(format!("Invalid justification provided for #{}", request.0)),
);
}
} else {
@@ -332,13 +332,16 @@ impl<B: BlockT> ChainSync<B> {
match (block_status(&*protocol.client(), &*self.import_queue, info.best_hash), info.best_number) {
(Err(e), _) => {
debug!(target:"sync", "Error reading blockchain: {:?}", e);
protocol.report_peer(who, Severity::Useless(&format!("Error legimimately reading blockchain status: {:?}", e)));
let reason = format!("Error legimimately reading blockchain status: {:?}", e);
protocol.report_peer(who, Severity::Useless(reason));
},
(Ok(BlockStatus::KnownBad), _) => {
protocol.report_peer(who, Severity::Bad(&format!("New peer with known bad best block {} ({}).", info.best_hash, info.best_number)));
let reason = format!("New peer with known bad best block {} ({}).", info.best_hash, info.best_number);
protocol.report_peer(who, Severity::Bad(reason));
},
(Ok(BlockStatus::Unknown), b) if b.is_zero() => {
protocol.report_peer(who, Severity::Bad(&format!("New peer with unknown genesis hash {} ({}).", info.best_hash, info.best_number)));
(Ok(BlockStatus::Unknown), b) if b == As::sa(0) => {
let reason = format!("New peer with unknown genesis hash {} ({}).", info.best_hash, info.best_number);
protocol.report_peer(who, Severity::Bad(reason));
},
(Ok(BlockStatus::Unknown), _) if self.import_queue.status().importing_count > MAJOR_SYNC_BLOCKS => {
// when actively syncing the common point moves too fast.
@@ -457,18 +460,19 @@ impl<B: BlockT> ChainSync<B> {
},
Ok(_) => { // genesis mismatch
trace!(target:"sync", "Ancestry search: genesis mismatch for peer {}", who);
protocol.report_peer(who, Severity::Bad("Ancestry search: genesis mismatch for peer"));
protocol.report_peer(who, Severity::Bad("Ancestry search: genesis mismatch for peer".to_string()));
return None;
},
Err(e) => {
protocol.report_peer(who, Severity::Useless(&format!("Error answering legitimate blockchain query: {:?}", e)));
let reason = format!("Error answering legitimate blockchain query: {:?}", e);
protocol.report_peer(who, Severity::Useless(reason));
return None;
}
}
},
None => {
trace!(target:"sync", "Invalid response when searching for ancestor from {}", who);
protocol.report_peer(who, Severity::Bad("Invalid response when searching for ancestor"));
protocol.report_peer(who, Severity::Bad("Invalid response when searching for ancestor".to_string()));
return None;
}
}
@@ -517,7 +521,7 @@ impl<B: BlockT> ChainSync<B> {
response.hash,
);
protocol.report_peer(who, Severity::Bad(&msg));
protocol.report_peer(who, Severity::Bad(msg));
return;
}
@@ -534,7 +538,7 @@ impl<B: BlockT> ChainSync<B> {
hash,
);
protocol.report_peer(who, Severity::Useless(&msg));
protocol.report_peer(who, Severity::Useless(msg));
return;
},
}