Move ProtocolMsg to service.rs (#2560)

* Move ProtocolMsg to service.rs

* Fix line width

* Fix line width again

* Correct whitespace

* Another whitespace correction
This commit is contained in:
Pierre Krieger
2019-05-14 15:26:52 +02:00
committed by André Silva
parent a29fd10859
commit 18f7acce98
3 changed files with 388 additions and 188 deletions
+65 -8
View File
@@ -44,13 +44,11 @@ use crate::message::Message;
use network_libp2p::PeerId;
use parking_lot::{Mutex, RwLock};
use primitives::{H256, sr25519::Public as AuthorityId, Blake2Hasher};
use crate::protocol::{
ConnectedPeer, Context, Protocol, ProtocolStatus, ProtocolMsg, CustomMessageOutcome,
};
use crate::protocol::{ConnectedPeer, Context, Protocol, ProtocolStatus, CustomMessageOutcome};
use runtime_primitives::generic::BlockId;
use runtime_primitives::traits::{AuthorityIdFor, Block as BlockT, Digest, DigestItem, Header, NumberFor};
use runtime_primitives::{Justification, ConsensusEngineId};
use crate::service::{network_channel, NetworkChan, NetworkLink, NetworkMsg, NetworkPort, TransactionPool};
use crate::service::{network_channel, NetworkChan, NetworkLink, NetworkMsg, NetworkPort, ProtocolMsg, TransactionPool};
use crate::specialization::NetworkSpecialization;
use test_client::{self, AccountKeyring};
@@ -520,7 +518,9 @@ impl<D, S: NetworkSpecialization<Block>> Peer<D, S> {
}
let header = self.client.header(&BlockId::Hash(info.chain.finalized_hash)).unwrap().unwrap();
self.net_proto_channel.send_from_client(ProtocolMsg::BlockFinalized(info.chain.finalized_hash, header.clone()));
self.net_proto_channel.send_from_client(
ProtocolMsg::BlockFinalized(info.chain.finalized_hash, header.clone())
);
*finalized_hash = Some(info.chain.finalized_hash);
}
@@ -748,6 +748,7 @@ pub trait TestNetFactory: Sized {
import_queue: Box<BasicQueue<Block>>,
mut protocol: Protocol<Block, Self::Specialization, Hash>,
mut network_to_protocol_rx: mpsc::UnboundedReceiver<FromNetworkMsg<Block>>,
mut protocol_rx: mpsc::UnboundedReceiver<ProtocolMsg<Block, Self::Specialization>>,
peer: Arc<Peer<Self::PeerData, Self::Specialization>>,
) {
std::thread::spawn(move || {
@@ -782,12 +783,64 @@ pub trait TestNetFactory: Sized {
}
}
loop {
let msg = match protocol_rx.poll() {
Ok(Async::Ready(Some(msg))) => msg,
Ok(Async::Ready(None)) | Err(_) => return Ok(Async::Ready(())),
Ok(Async::NotReady) => break,
};
match msg {
ProtocolMsg::BlockImported(hash, header) =>
protocol.on_block_imported(hash, &header),
ProtocolMsg::BlockFinalized(hash, header) =>
protocol.on_block_finalized(hash, &header),
ProtocolMsg::ExecuteWithSpec(task) => {
let (mut context, spec) = protocol.specialization_lock();
task.call_box(spec, &mut context);
},
ProtocolMsg::ExecuteWithGossip(task) => {
let (mut context, gossip) = protocol.consensus_gossip_lock();
task.call_box(gossip, &mut context);
}
ProtocolMsg::GossipConsensusMessage(topic, engine_id, message, recipient) =>
protocol.gossip_consensus_message(topic, engine_id, message, recipient),
ProtocolMsg::BlocksProcessed(hashes, has_error) =>
protocol.blocks_processed(hashes, has_error),
ProtocolMsg::RestartSync =>
protocol.restart(),
ProtocolMsg::AnnounceBlock(hash) =>
protocol.announce_block(hash),
ProtocolMsg::BlockImportedSync(hash, number) =>
protocol.block_imported(&hash, number),
ProtocolMsg::ClearJustificationRequests =>
protocol.clear_justification_requests(),
ProtocolMsg::RequestJustification(hash, number) =>
protocol.request_justification(&hash, number),
ProtocolMsg::JustificationImportResult(hash, number, success) =>
protocol.justification_import_result(hash, number, success),
ProtocolMsg::SetFinalityProofRequestBuilder(builder) =>
protocol.set_finality_proof_request_builder(builder),
ProtocolMsg::RequestFinalityProof(hash, number) =>
protocol.request_finality_proof(&hash, number),
ProtocolMsg::FinalityProofImportResult(requested_block, finalziation_result) =>
protocol.finality_proof_import_result(requested_block, finalziation_result),
ProtocolMsg::PropagateExtrinsics => protocol.propagate_extrinsics(),
#[cfg(any(test, feature = "test-helpers"))]
ProtocolMsg::Tick => protocol.tick(),
#[cfg(any(test, feature = "test-helpers"))]
ProtocolMsg::Synchronize => {
trace!(target: "sync", "handle_client_msg: received Synchronize msg");
protocol.synchronize();
}
}
}
if let Async::Ready(_) = protocol.poll().unwrap() {
return Ok(Async::Ready(()))
}
*protocol_status.write() = protocol.status();
Ok(Async::NotReady)
}));
});
@@ -825,8 +878,9 @@ pub trait TestNetFactory: Sized {
let peers: Arc<RwLock<HashMap<PeerId, ConnectedPeer<Block>>>> = Arc::new(Default::default());
let (network_to_protocol_sender, network_to_protocol_rx) = mpsc::unbounded();
let (protocol_sender, protocol_rx) = mpsc::unbounded();
let (protocol, protocol_sender) = Protocol::new(
let protocol = Protocol::new(
peers.clone(),
network_sender.clone(),
config.clone(),
@@ -843,6 +897,7 @@ pub trait TestNetFactory: Sized {
import_queue.clone(),
protocol,
network_to_protocol_rx,
protocol_rx,
Arc::new(Peer::new(
protocol_status,
peers,
@@ -880,8 +935,9 @@ pub trait TestNetFactory: Sized {
let peers: Arc<RwLock<HashMap<PeerId, ConnectedPeer<Block>>>> = Arc::new(Default::default());
let (network_to_protocol_sender, network_to_protocol_rx) = mpsc::unbounded();
let (protocol_sender, protocol_rx) = mpsc::unbounded();
let (protocol, protocol_sender) = Protocol::new(
let protocol = Protocol::new(
peers.clone(),
network_sender.clone(),
config,
@@ -898,6 +954,7 @@ pub trait TestNetFactory: Sized {
import_queue.clone(),
protocol,
network_to_protocol_rx,
protocol_rx,
Arc::new(Peer::new(
protocol_status,
peers,