This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Register listeners in statement distribution #1759
Merged
2 commits merged into
paritytech:master
from
montekki:fs-statement-distribution-listener
Sep 28, 2020
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 |
|---|---|---|
|
|
@@ -36,7 +36,7 @@ use polkadot_node_network_protocol::{ | |
| }; | ||
|
|
||
| use futures::prelude::*; | ||
| use futures::channel::oneshot; | ||
| use futures::channel::{mpsc, oneshot}; | ||
| use indexmap::IndexSet; | ||
|
|
||
| use std::collections::{HashMap, HashSet}; | ||
|
|
@@ -476,6 +476,21 @@ fn check_statement_signature( | |
| .and_then(|v| statement.check_signature(&signing_context, v)) | ||
| } | ||
|
|
||
| /// Informs all registered listeners about a newly received statement. | ||
| /// | ||
| /// Removes all closed listeners. | ||
| async fn inform_statement_listeners( | ||
| statement: SignedFullStatement, | ||
| listeners: &mut Vec<mpsc::Sender<SignedFullStatement>>, | ||
| ) { | ||
| // Ignore the errors since these will be removed later. | ||
| for listener in listeners.iter_mut() { | ||
| let _ = listener.send(statement.clone()).await; | ||
| } | ||
| // Remove any closed listeners. | ||
| listeners.retain(|tx| !tx.is_closed()); | ||
| } | ||
|
|
||
| /// Places the statement in storage if it is new, and then | ||
| /// circulates the statement to all peers who have not seen it yet, and | ||
| /// sends all statements dependent on that statement to peers who could previously not receive | ||
|
|
@@ -821,6 +836,7 @@ async fn run( | |
| let mut peers: HashMap<PeerId, PeerData> = HashMap::new(); | ||
| let mut our_view = View::default(); | ||
| let mut active_heads: HashMap<Hash, ActiveHeadData> = HashMap::new(); | ||
| let mut statement_listeners: Vec<mpsc::Sender<SignedFullStatement>> = Vec::new(); | ||
|
|
||
| loop { | ||
| let message = ctx.recv().await?; | ||
|
|
@@ -874,14 +890,19 @@ async fn run( | |
| } | ||
| FromOverseer::Signal(OverseerSignal::Conclude) => break, | ||
| FromOverseer::Communication { msg } => match msg { | ||
| StatementDistributionMessage::Share(relay_parent, statement) => | ||
| StatementDistributionMessage::Share(relay_parent, statement) => { | ||
| inform_statement_listeners( | ||
| statement.clone(), | ||
|
||
| &mut statement_listeners, | ||
| ).await; | ||
| circulate_statement_and_dependents( | ||
| &mut peers, | ||
| &mut active_heads, | ||
| &mut ctx, | ||
| relay_parent, | ||
| statement, | ||
| ).await?, | ||
| ).await?; | ||
| } | ||
| StatementDistributionMessage::NetworkBridgeUpdateV1(event) => | ||
| handle_network_update( | ||
| &mut peers, | ||
|
|
@@ -890,6 +911,9 @@ async fn run( | |
| &mut our_view, | ||
| event, | ||
| ).await?, | ||
| StatementDistributionMessage::RegisterStatementListener(tx) => { | ||
| statement_listeners.push(tx); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or 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 hidden or 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.
a DQ: shouldn't we fan out sending and await the joined future?
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.
Something like
futures::stream::iter(listeners.iter_mut()).for_each_concurrent(None, |listener| listener.send(statement.clone())is probably a good idea. Then again, I have the impression that we're rarely going to have more than a few listeners at a time, so it may not make any practical difference.