This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
client/finality-grandpa: Reintegrate gossip validator report stream #4661
Merged
gavofyork
merged 2 commits into
paritytech:master
from
mxinden:reintegrate-report-stream
Jan 17, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,11 +27,12 @@ | |
//! In the future, there will be a fallback for allowing sending the same message | ||
//! under certain conditions that are used to un-stick the protocol. | ||
|
||
use futures::{prelude::*, future::Executor as _, sync::mpsc}; | ||
use futures::{prelude::*, sync::mpsc}; | ||
use futures03::{ | ||
channel::mpsc as mpsc03, | ||
compat::Compat, | ||
future::{Future as Future03}, | ||
stream::StreamExt, | ||
future::{Future as Future03, FutureExt as _, TryFutureExt as _}, | ||
}; | ||
use log::{debug, trace}; | ||
use parking_lot::Mutex; | ||
|
@@ -52,7 +53,12 @@ use crate::{ | |
}; | ||
use crate::environment::HasVoted; | ||
use gossip::{ | ||
GossipMessage, FullCatchUpMessage, FullCommitMessage, VoteMessage, GossipValidator | ||
FullCatchUpMessage, | ||
FullCommitMessage, | ||
GossipMessage, | ||
GossipValidator, | ||
PeerReport, | ||
VoteMessage, | ||
}; | ||
use sp_finality_grandpa::{ | ||
AuthorityPair, AuthorityId, AuthoritySignature, SetId as SetIdNumber, RoundNumber, | ||
|
@@ -148,9 +154,18 @@ pub(crate) struct NetworkBridge<B: BlockT, N: Network<B>> { | |
|
||
/// `NeighborPacketWorker` processing packets sent through the `NeighborPacketSender`. | ||
// | ||
// NetworkBridge is required to be clonable, thus one needs to be able to clone its children, | ||
// thus one has to wrap neighor_packet_worker with an Arc Mutex. | ||
// `NetworkBridge` is required to be clonable, thus one needs to be able to clone its children, | ||
// thus one has to wrap neighor_packet_worker with an `Arc` `Mutex`. | ||
neighbor_packet_worker: Arc<Mutex<periodic::NeighborPacketWorker<B>>>, | ||
|
||
/// Receiver side of the peer report stream populated by the gossip validator, forwarded to the | ||
/// gossip engine. | ||
// | ||
// `NetworkBridge` is required to be clonable, thus one needs to be able to clone its children, | ||
// thus one has to wrap gossip_validator_report_stream with an `Arc` `Mutex`. Given that it is | ||
// just an `UnboundedReceiver`, one could also switch to a multi-producer-*multi*-consumer | ||
// channel implementation. | ||
gossip_validator_report_stream: Arc<Mutex<mpsc03::UnboundedReceiver<PeerReport>>>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. out of curiosity, what does the plan to phase out the unbounded channel look like after this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #4661 (comment). |
||
} | ||
|
||
impl<B: BlockT, N: Network<B>> Unpin for NetworkBridge<B, N> {} | ||
|
@@ -165,7 +180,6 @@ impl<B: BlockT, N: Network<B>> NetworkBridge<B, N> { | |
config: crate::Config, | ||
set_state: crate::environment::SharedVoterSetState<B>, | ||
executor: &impl futures03::task::Spawn, | ||
on_exit: impl futures03::Future<Output = ()> + Clone + Send + Unpin + 'static, | ||
) -> Self { | ||
let (validator, report_stream) = GossipValidator::new( | ||
config, | ||
|
@@ -214,20 +228,16 @@ impl<B: BlockT, N: Network<B>> NetworkBridge<B, N> { | |
} | ||
|
||
let (neighbor_packet_worker, neighbor_packet_sender) = periodic::NeighborPacketWorker::new(); | ||
let reporting_job = report_stream.consume(gossip_engine.clone()); | ||
|
||
let bridge = NetworkBridge { | ||
service, | ||
gossip_engine, | ||
validator, | ||
neighbor_sender: neighbor_packet_sender, | ||
neighbor_packet_worker: Arc::new(Mutex::new(neighbor_packet_worker)), | ||
gossip_validator_report_stream: Arc::new(Mutex::new(report_stream)), | ||
}; | ||
|
||
let executor = Compat::new(executor); | ||
executor.execute(Box::new(reporting_job.select(on_exit.clone().map(Ok).compat()).then(|_| Ok(())))) | ||
.expect("failed to spawn grandpa reporting job task"); | ||
|
||
bridge | ||
} | ||
|
||
|
@@ -418,13 +428,30 @@ impl<B: BlockT, N: Network<B>> Future03 for NetworkBridge<B, N> { | |
|
||
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll03<Self::Output> { | ||
loop { | ||
match futures03::ready!((self.neighbor_packet_worker.lock()).poll_next_unpin(cx)) { | ||
None => return Poll03::Ready( | ||
Err(Error::Network("NeighborPacketWorker stream closed.".into())) | ||
match self.neighbor_packet_worker.lock().poll_next_unpin(cx) { | ||
Poll03::Ready(Some((to, packet))) => { | ||
self.gossip_engine.send_message(to, packet.encode()); | ||
}, | ||
Poll03::Ready(None) => return Poll03::Ready( | ||
Err(Error::Network("Neighbor packet worker stream closed.".into())) | ||
), | ||
Some((to, packet)) => self.gossip_engine.send_message(to, packet.encode()), | ||
Poll03::Pending => break, | ||
} | ||
} | ||
|
||
loop { | ||
match self.gossip_validator_report_stream.lock().poll_next_unpin(cx) { | ||
Poll03::Ready(Some(PeerReport { who, cost_benefit })) => { | ||
self.gossip_engine.report(who, cost_benefit); | ||
}, | ||
Poll03::Ready(None) => return Poll03::Ready( | ||
Err(Error::Network("Gossip validator report stream closed.".into())) | ||
), | ||
Poll03::Pending => break, | ||
} | ||
} | ||
|
||
Poll03::Pending | ||
} | ||
} | ||
|
||
|
@@ -568,6 +595,7 @@ impl<B: BlockT, N: Network<B>> Clone for NetworkBridge<B, N> { | |
validator: Arc::clone(&self.validator), | ||
neighbor_sender: self.neighbor_sender.clone(), | ||
neighbor_packet_worker: self.neighbor_packet_worker.clone(), | ||
gossip_validator_report_stream: self.gossip_validator_report_stream.clone(), | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
docs on this struct & members?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #4684