refactor overseer into proc-macro based pattern (#2962)

This commit is contained in:
Bernhard Schuster
2021-07-08 21:09:26 +02:00
committed by GitHub
parent 2510bfc5d7
commit 3c9104daff
119 changed files with 5675 additions and 3864 deletions
@@ -29,11 +29,13 @@ use polkadot_node_primitives::{
approval::{AssignmentCert, BlockApprovalMeta, IndirectSignedApprovalVote, IndirectAssignmentCert},
};
use polkadot_node_subsystem::{
overseer,
messages::{
AllMessages, ApprovalDistributionMessage, ApprovalVotingMessage, NetworkBridgeMessage,
ApprovalDistributionMessage, ApprovalVotingMessage, NetworkBridgeMessage,
AssignmentCheckResult, ApprovalCheckResult, NetworkBridgeEvent,
},
ActiveLeavesUpdate, FromOverseer, OverseerSignal, SpawnedSubsystem, Subsystem, SubsystemContext,
SubsystemError,
ActiveLeavesUpdate, FromOverseer, OverseerSignal, SpawnedSubsystem, SubsystemContext,
};
use polkadot_node_subsystem_util::{
metrics::{self, prometheus},
@@ -187,7 +189,7 @@ enum PendingMessage {
impl State {
async fn handle_network_msg(
&mut self,
ctx: &mut impl SubsystemContext<Message = ApprovalDistributionMessage>,
ctx: &mut (impl SubsystemContext<Message = ApprovalDistributionMessage> + overseer::SubsystemContext<Message = ApprovalDistributionMessage>),
metrics: &Metrics,
event: NetworkBridgeEvent<protocol_v1::ApprovalDistributionMessage>,
) {
@@ -257,8 +259,7 @@ impl State {
async fn handle_new_blocks(
&mut self,
ctx: &mut impl SubsystemContext<Message = ApprovalDistributionMessage>,
metrics: &Metrics,
ctx: &mut (impl SubsystemContext<Message = ApprovalDistributionMessage> + overseer::SubsystemContext<Message = ApprovalDistributionMessage>), metrics: &Metrics,
metas: Vec<BlockApprovalMeta>,
) {
let mut new_hashes = HashSet::new();
@@ -360,8 +361,7 @@ impl State {
async fn process_incoming_peer_message(
&mut self,
ctx: &mut impl SubsystemContext<Message = ApprovalDistributionMessage>,
metrics: &Metrics,
ctx: &mut (impl SubsystemContext<Message = ApprovalDistributionMessage> + overseer::SubsystemContext<Message = ApprovalDistributionMessage>), metrics: &Metrics,
peer_id: PeerId,
msg: protocol_v1::ApprovalDistributionMessage,
) {
@@ -448,8 +448,7 @@ impl State {
async fn handle_peer_view_change(
&mut self,
ctx: &mut impl SubsystemContext<Message = ApprovalDistributionMessage>,
metrics: &Metrics,
ctx: &mut (impl SubsystemContext<Message = ApprovalDistributionMessage> + overseer::SubsystemContext<Message = ApprovalDistributionMessage>), metrics: &Metrics,
peer_id: PeerId,
view: View,
) {
@@ -512,8 +511,7 @@ impl State {
async fn import_and_circulate_assignment(
&mut self,
ctx: &mut impl SubsystemContext<Message = ApprovalDistributionMessage>,
metrics: &Metrics,
ctx: &mut (impl SubsystemContext<Message = ApprovalDistributionMessage> + overseer::SubsystemContext<Message = ApprovalDistributionMessage>), metrics: &Metrics,
source: MessageSource,
assignment: IndirectAssignmentCert,
claimed_candidate_index: CandidateIndex,
@@ -592,11 +590,11 @@ impl State {
let (tx, rx) = oneshot::channel();
ctx.send_message(AllMessages::ApprovalVoting(ApprovalVotingMessage::CheckAndImportAssignment(
ctx.send_message(ApprovalVotingMessage::CheckAndImportAssignment(
assignment.clone(),
claimed_candidate_index,
tx,
))).await;
)).await;
let timer = metrics.time_awaiting_approval_voting();
let result = match rx.await {
@@ -743,14 +741,13 @@ impl State {
protocol_v1::ValidationProtocol::ApprovalDistribution(
protocol_v1::ApprovalDistributionMessage::Assignments(assignments)
),
).into()).await;
)).await;
}
}
async fn import_and_circulate_approval(
&mut self,
ctx: &mut impl SubsystemContext<Message = ApprovalDistributionMessage>,
metrics: &Metrics,
ctx: &mut (impl SubsystemContext<Message = ApprovalDistributionMessage> + overseer::SubsystemContext<Message = ApprovalDistributionMessage>), metrics: &Metrics,
source: MessageSource,
vote: IndirectSignedApprovalVote,
) {
@@ -840,10 +837,10 @@ impl State {
let (tx, rx) = oneshot::channel();
ctx.send_message(AllMessages::ApprovalVoting(ApprovalVotingMessage::CheckAndImportApproval(
ctx.send_message(ApprovalVotingMessage::CheckAndImportApproval(
vote.clone(),
tx,
))).await;
)).await;
let timer = metrics.time_awaiting_approval_voting();
let result = match rx.await {
@@ -989,13 +986,12 @@ impl State {
protocol_v1::ValidationProtocol::ApprovalDistribution(
protocol_v1::ApprovalDistributionMessage::Approvals(approvals)
),
).into()).await;
)).await;
}
}
async fn unify_with_peer(
ctx: &mut impl SubsystemContext<Message = ApprovalDistributionMessage>,
gossip_peers: &HashSet<PeerId>,
ctx: &mut (impl SubsystemContext<Message = ApprovalDistributionMessage> + overseer::SubsystemContext<Message = ApprovalDistributionMessage>), gossip_peers: &HashSet<PeerId>,
metrics: &Metrics,
entries: &mut HashMap<Hash, BlockEntry>,
peer_id: PeerId,
@@ -1060,8 +1056,7 @@ impl State {
async fn send_gossip_messages_to_peer(
entries: &HashMap<Hash, BlockEntry>,
ctx: &mut impl SubsystemContext<Message = ApprovalDistributionMessage>,
peer_id: PeerId,
ctx: &mut (impl SubsystemContext<Message = ApprovalDistributionMessage> + overseer::SubsystemContext<Message = ApprovalDistributionMessage>), peer_id: PeerId,
blocks: Vec<Hash>,
) {
let mut assignments = Vec::new();
@@ -1130,7 +1125,7 @@ impl State {
protocol_v1::ValidationProtocol::ApprovalDistribution(
protocol_v1::ApprovalDistributionMessage::Assignments(assignments)
),
).into()).await;
)).await;
}
if !approvals.is_empty() {
@@ -1147,7 +1142,7 @@ impl State {
protocol_v1::ValidationProtocol::ApprovalDistribution(
protocol_v1::ApprovalDistributionMessage::Approvals(approvals)
),
).into()).await;
)).await;
}
}
}
@@ -1155,7 +1150,7 @@ impl State {
/// Modify the reputation of a peer based on its behavior.
async fn modify_reputation(
ctx: &mut impl SubsystemContext<Message = ApprovalDistributionMessage>,
ctx: &mut (impl SubsystemContext<Message = ApprovalDistributionMessage> + overseer::SubsystemContext<Message = ApprovalDistributionMessage>),
peer_id: PeerId,
rep: Rep,
) {
@@ -1166,9 +1161,9 @@ async fn modify_reputation(
"Reputation change for peer",
);
ctx.send_message(AllMessages::NetworkBridge(
ctx.send_message(
NetworkBridgeMessage::ReportPeer(peer_id, rep),
)).await;
).await;
}
impl ApprovalDistribution {
@@ -1180,6 +1175,7 @@ impl ApprovalDistribution {
async fn run<Context>(self, ctx: Context)
where
Context: SubsystemContext<Message = ApprovalDistributionMessage>,
Context: overseer::SubsystemContext<Message = ApprovalDistributionMessage>,
{
let mut state = State::default();
self.run_inner(ctx, &mut state).await
@@ -1189,6 +1185,7 @@ impl ApprovalDistribution {
async fn run_inner<Context>(self, mut ctx: Context, state: &mut State)
where
Context: SubsystemContext<Message = ApprovalDistributionMessage>,
Context: overseer::SubsystemContext<Message = ApprovalDistributionMessage>,
{
loop {
let message = match ctx.recv().await {
@@ -1261,11 +1258,12 @@ impl ApprovalDistribution {
}
}
impl<C> Subsystem<C> for ApprovalDistribution
impl<Context> overseer::Subsystem<Context, SubsystemError> for ApprovalDistribution
where
C: SubsystemContext<Message = ApprovalDistributionMessage> + Sync + Send,
Context: SubsystemContext<Message = ApprovalDistributionMessage>,
Context: overseer::SubsystemContext<Message = ApprovalDistributionMessage>,
{
fn start(self, ctx: C) -> SpawnedSubsystem {
fn start(self, ctx: Context) -> SpawnedSubsystem {
let future = self.run(ctx)
.map(|_| Ok(()))
.boxed();
@@ -17,7 +17,7 @@
use std::time::Duration;
use futures::{future, Future, executor};
use assert_matches::assert_matches;
use polkadot_node_subsystem::messages::ApprovalCheckError;
use polkadot_node_subsystem::messages::{AllMessages, ApprovalCheckError};
use polkadot_node_subsystem_test_helpers as test_helpers;
use polkadot_node_subsystem_util::TimeoutExt as _;
use polkadot_node_network_protocol::{view, ObservedRole};
@@ -20,7 +20,8 @@ use sp_keystore::SyncCryptoStorePtr;
use polkadot_subsystem::{
messages::AvailabilityDistributionMessage, FromOverseer, OverseerSignal, SpawnedSubsystem,
Subsystem, SubsystemContext, SubsystemError,
SubsystemContext, SubsystemError,
overseer,
};
/// Error and [`Result`] type for this subsystem.
@@ -58,9 +59,10 @@ pub struct AvailabilityDistributionSubsystem {
metrics: Metrics,
}
impl<Context> Subsystem<Context> for AvailabilityDistributionSubsystem
impl<Context> overseer::Subsystem<Context, SubsystemError> for AvailabilityDistributionSubsystem
where
Context: SubsystemContext<Message = AvailabilityDistributionMessage> + Sync + Send,
Context: SubsystemContext<Message = AvailabilityDistributionMessage>,
Context: overseer::SubsystemContext<Message = AvailabilityDistributionMessage>,
{
fn start(self, ctx: Context) -> SpawnedSubsystem {
let future = self
@@ -86,7 +88,8 @@ impl AvailabilityDistributionSubsystem {
/// Start processing work as passed on from the Overseer.
async fn run<Context>(mut self, mut ctx: Context) -> std::result::Result<(), Fatal>
where
Context: SubsystemContext<Message = AvailabilityDistributionMessage> + Sync + Send,
Context: SubsystemContext<Message = AvailabilityDistributionMessage>,
Context: overseer::SubsystemContext<Message = AvailabilityDistributionMessage>,
{
let mut requester = Requester::new(self.metrics.clone()).fuse();
loop {
@@ -29,7 +29,7 @@ use polkadot_primitives::v1::{
use polkadot_node_primitives::PoV;
use polkadot_subsystem::{
SubsystemContext,
messages::{AllMessages, NetworkBridgeMessage, IfDisconnected}
messages::{NetworkBridgeMessage, IfDisconnected}
};
use polkadot_node_subsystem_util::runtime::RuntimeInfo;
@@ -62,7 +62,6 @@ where
let full_req = Requests::PoVFetching(req);
ctx.send_message(
AllMessages::NetworkBridge(
NetworkBridgeMessage::SendRequests(
vec![full_req],
// We are supposed to be connected to validators of our group via `PeerSet`,
@@ -70,7 +69,7 @@ where
// longer to get established, so we try to connect in any case.
IfDisconnected::TryConnect
)
)).await;
).await;
let span = jaeger::Span::new(candidate_hash, "fetch-pov")
.with_validator_index(from_validator)
@@ -130,7 +129,7 @@ mod tests {
use polkadot_primitives::v1::{CandidateHash, Hash, ValidatorIndex};
use polkadot_node_primitives::BlockData;
use polkadot_subsystem_testhelpers as test_helpers;
use polkadot_subsystem::messages::{AvailabilityDistributionMessage, RuntimeApiMessage, RuntimeApiRequest};
use polkadot_subsystem::messages::{AllMessages, AvailabilityDistributionMessage, RuntimeApiMessage, RuntimeApiRequest};
use super::*;
use crate::LOG_TARGET;
@@ -30,7 +30,6 @@ use polkadot_primitives::v1::{CandidateHash, ValidatorIndex};
use polkadot_node_primitives::{BlockData, PoV};
use polkadot_node_network_protocol::request_response::v1;
use polkadot_node_network_protocol::request_response::Recipient;
use polkadot_subsystem::messages::AllMessages;
use crate::metrics::Metrics;
use crate::tests::mock::get_valid_chunk_data;
@@ -300,4 +299,3 @@ fn get_test_running_task() -> (RunningTask, mpsc::Receiver<FromFetchTask>) {
rx
)
}
@@ -33,7 +33,8 @@ use futures::{
use polkadot_node_subsystem_util::runtime::{RuntimeInfo, get_occupied_cores};
use polkadot_primitives::v1::{CandidateHash, Hash, OccupiedCore};
use polkadot_subsystem::{
messages::AllMessages, ActiveLeavesUpdate, SubsystemContext, ActivatedLeaf,
messages::AllMessages,
ActiveLeavesUpdate, SubsystemContext, ActivatedLeaf,
};
use super::{LOG_TARGET, Metrics};
@@ -229,4 +230,3 @@ impl Stream for Requester {
}
}
}
@@ -24,7 +24,7 @@ use polkadot_node_network_protocol::request_response::{request::IncomingRequest,
use polkadot_primitives::v1::{CandidateHash, ValidatorIndex};
use polkadot_node_primitives::{AvailableData, ErasureChunk};
use polkadot_subsystem::{
messages::{AllMessages, AvailabilityStoreMessage},
messages::AvailabilityStoreMessage,
SubsystemContext, jaeger,
};
@@ -158,9 +158,9 @@ where
Context: SubsystemContext,
{
let (tx, rx) = oneshot::channel();
ctx.send_message(AllMessages::AvailabilityStore(
ctx.send_message(
AvailabilityStoreMessage::QueryChunk(candidate_hash, validator_index, tx),
))
)
.await;
let result = rx.await.map_err(|e| {
@@ -185,9 +185,9 @@ where
Context: SubsystemContext,
{
let (tx, rx) = oneshot::channel();
ctx.send_message(AllMessages::AvailabilityStore(
ctx.send_message(
AvailabilityStoreMessage::QueryAvailableData(candidate_hash, tx),
))
)
.await;
let result = rx.await.map_err(|e| NonFatal::QueryAvailableDataResponseChannel(e))?;
@@ -34,12 +34,13 @@ use polkadot_primitives::v1::{
};
use polkadot_node_primitives::{ErasureChunk, AvailableData};
use polkadot_subsystem::{
SubsystemContext, SubsystemResult, SubsystemError, Subsystem, SpawnedSubsystem, FromOverseer,
overseer::{self, Subsystem},
SubsystemContext, SubsystemResult, SubsystemError, SpawnedSubsystem, FromOverseer,
OverseerSignal, ActiveLeavesUpdate, SubsystemSender,
errors::RecoveryError,
jaeger,
messages::{
AvailabilityStoreMessage, AvailabilityRecoveryMessage, AllMessages, NetworkBridgeMessage,
AvailabilityStoreMessage, AvailabilityRecoveryMessage, NetworkBridgeMessage,
},
};
use polkadot_node_network_protocol::{
@@ -573,10 +574,12 @@ impl Default for State {
}
}
impl<C> Subsystem<C> for AvailabilityRecoverySubsystem
where C: SubsystemContext<Message = AvailabilityRecoveryMessage>
impl<Context> Subsystem<Context, SubsystemError> for AvailabilityRecoverySubsystem
where
Context: SubsystemContext<Message = AvailabilityRecoveryMessage>,
Context: overseer::SubsystemContext<Message = AvailabilityRecoveryMessage>,
{
fn start(self, ctx: C) -> SpawnedSubsystem {
fn start(self, ctx: Context) -> SpawnedSubsystem {
let future = self.run(ctx)
.map_err(|e| SubsystemError::with_origin("availability-recovery", e))
.boxed();
@@ -609,14 +612,18 @@ async fn handle_signal(
}
/// Machinery around launching interactions into the background.
async fn launch_interaction(
async fn launch_interaction<Context>(
state: &mut State,
ctx: &mut impl SubsystemContext<Message = AvailabilityRecoveryMessage>,
ctx: &mut Context,
session_info: SessionInfo,
receipt: CandidateReceipt,
backing_group: Option<GroupIndex>,
response_sender: oneshot::Sender<Result<AvailableData, RecoveryError>>,
) -> error::Result<()> {
) -> error::Result<()>
where
Context: SubsystemContext<Message = AvailabilityRecoveryMessage>,
Context: overseer::SubsystemContext<Message = AvailabilityRecoveryMessage>,
{
let candidate_hash = receipt.hash();
let params = InteractionParams {
@@ -662,14 +669,18 @@ async fn launch_interaction(
}
/// Handles an availability recovery request.
async fn handle_recover(
async fn handle_recover<Context>(
state: &mut State,
ctx: &mut impl SubsystemContext<Message = AvailabilityRecoveryMessage>,
ctx: &mut Context,
receipt: CandidateReceipt,
session_index: SessionIndex,
backing_group: Option<GroupIndex>,
response_sender: oneshot::Sender<Result<AvailableData, RecoveryError>>,
) -> error::Result<()> {
) -> error::Result<()>
where
Context: SubsystemContext<Message = AvailabilityRecoveryMessage>,
Context: overseer::SubsystemContext<Message = AvailabilityRecoveryMessage>,
{
let candidate_hash = receipt.hash();
let span = jaeger::Span::new(candidate_hash, "availbility-recovery")
@@ -724,14 +735,18 @@ async fn handle_recover(
}
/// Queries a chunk from av-store.
async fn query_full_data(
ctx: &mut impl SubsystemContext<Message = AvailabilityRecoveryMessage>,
async fn query_full_data<Context>(
ctx: &mut Context,
candidate_hash: CandidateHash,
) -> error::Result<Option<AvailableData>> {
) -> error::Result<Option<AvailableData>>
where
Context: SubsystemContext<Message = AvailabilityRecoveryMessage>,
Context: overseer::SubsystemContext<Message = AvailabilityRecoveryMessage>,
{
let (tx, rx) = oneshot::channel();
ctx.send_message(AllMessages::AvailabilityStore(
ctx.send_message(
AvailabilityStoreMessage::QueryAvailableData(candidate_hash, tx),
)).await;
).await;
Ok(rx.await.map_err(error::Error::CanceledQueryFullData)?)
}
@@ -747,10 +762,14 @@ impl AvailabilityRecoverySubsystem {
Self { fast_path: false }
}
async fn run(
async fn run<Context>(
self,
mut ctx: impl SubsystemContext<Message = AvailabilityRecoveryMessage>,
) -> SubsystemResult<()> {
mut ctx: Context,
) -> SubsystemResult<()>
where
Context: SubsystemContext<Message = AvailabilityRecoveryMessage>,
Context: overseer::SubsystemContext<Message = AvailabilityRecoveryMessage>,
{
let mut state = State::default();
loop {
@@ -34,7 +34,7 @@ use polkadot_erasure_coding::{branches, obtain_chunks_v1 as obtain_chunks};
use polkadot_node_subsystem_util::TimeoutExt;
use polkadot_subsystem_testhelpers as test_helpers;
use polkadot_subsystem::{
messages::{RuntimeApiMessage, RuntimeApiRequest}, jaeger, ActivatedLeaf, LeafStatus,
messages::{AllMessages, RuntimeApiMessage, RuntimeApiRequest}, jaeger, ActivatedLeaf, LeafStatus,
};
type VirtualOverseer = test_helpers::TestSubsystemContextHandle<AvailabilityRecoveryMessage>;
@@ -26,9 +26,10 @@ use futures::{channel::oneshot, FutureExt};
use polkadot_subsystem::messages::*;
use polkadot_subsystem::{
PerLeafSpan, ActiveLeavesUpdate, FromOverseer, OverseerSignal, SpawnedSubsystem, Subsystem,
SubsystemContext, SubsystemResult,
PerLeafSpan, ActiveLeavesUpdate, FromOverseer, OverseerSignal, SpawnedSubsystem,
SubsystemContext, SubsystemResult, SubsystemError,
jaeger,
overseer,
};
use polkadot_node_subsystem_util::{
metrics::{self, prometheus},
@@ -162,6 +163,7 @@ impl BitfieldDistribution {
async fn run<Context>(self, mut ctx: Context)
where
Context: SubsystemContext<Message = BitfieldDistributionMessage>,
Context: overseer::SubsystemContext<Message = BitfieldDistributionMessage>,
{
// work: process incoming messages from the overseer and process accordingly.
let mut state = ProtocolState::default();
@@ -250,9 +252,9 @@ where
{
tracing::trace!(target: LOG_TARGET, ?rep, peer_id = %peer, "reputation change");
ctx.send_message(AllMessages::NetworkBridge(
ctx.send_message(
NetworkBridgeMessage::ReportPeer(peer, rep),
))
)
.await
}
@@ -328,7 +330,7 @@ where
let _span = span.child("provisionable");
// notify the overseer about a new and valid signed bitfield
ctx.send_message(AllMessages::Provisioner(
ctx.send_message(
ProvisionerMessage::ProvisionableData(
message.relay_parent,
ProvisionableData::Bitfield(
@@ -336,7 +338,7 @@ where
message.signed_availability.clone(),
),
),
))
)
.await;
drop(_span);
@@ -383,12 +385,12 @@ where
);
} else {
let _span = span.child("gossip");
ctx.send_message(AllMessages::NetworkBridge(
ctx.send_message(
NetworkBridgeMessage::SendValidationMessage(
interested_peers,
message.into_validation_protocol(),
),
))
)
.await;
}
}
@@ -687,19 +689,20 @@ where
.or_default()
.insert(validator.clone());
ctx.send_message(AllMessages::NetworkBridge(
ctx.send_message(
NetworkBridgeMessage::SendValidationMessage(
vec![dest],
message.into_validation_protocol(),
),
)).await;
).await;
}
impl<C> Subsystem<C> for BitfieldDistribution
impl<Context> overseer::Subsystem<Context, SubsystemError> for BitfieldDistribution
where
C: SubsystemContext<Message = BitfieldDistributionMessage> + Sync + Send,
Context: SubsystemContext<Message = BitfieldDistributionMessage>,
Context: overseer::SubsystemContext<Message = BitfieldDistributionMessage>,
{
fn start(self, ctx: C) -> SpawnedSubsystem {
fn start(self, ctx: Context) -> SpawnedSubsystem {
let future = self.run(ctx)
.map(|_| Ok(()))
.boxed();
@@ -722,18 +725,17 @@ where
let (validators_tx, validators_rx) = oneshot::channel();
let (session_tx, session_rx) = oneshot::channel();
let query_validators = AllMessages::RuntimeApi(RuntimeApiMessage::Request(
// query validators
ctx.send_message(RuntimeApiMessage::Request(
relay_parent.clone(),
RuntimeApiRequest::Validators(validators_tx),
));
)).await;
let query_signing = AllMessages::RuntimeApi(RuntimeApiMessage::Request(
// query signing context
ctx.send_message(RuntimeApiMessage::Request(
relay_parent.clone(),
RuntimeApiRequest::SessionIndexForChild(session_tx),
));
ctx.send_messages(std::iter::once(query_validators).chain(std::iter::once(query_signing)))
.await;
)).await;
match (validators_rx.await?, session_rx.await?) {
(Ok(v), Ok(s)) => Ok(Some((
@@ -837,4 +839,3 @@ impl metrics::Metrics for Metrics {
Ok(Metrics(Some(metrics)))
}
}
+1
View File
@@ -14,6 +14,7 @@ sc-authority-discovery = { git = "https://github.com/paritytech/substrate", bran
sc-network = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-consensus = { git = "https://github.com/paritytech/substrate", branch = "master" }
polkadot-subsystem = { package = "polkadot-node-subsystem", path = "../../subsystem" }
polkadot-overseer = { path = "../../overseer" }
polkadot-node-network-protocol = { path = "../protocol" }
polkadot-node-subsystem-util = { path = "../../subsystem-util"}
strum = "0.20.0"
+33 -16
View File
@@ -27,14 +27,23 @@ use futures::stream::BoxStream;
use sc_network::Event as NetworkEvent;
use sp_consensus::SyncOracle;
use polkadot_subsystem::{
ActivatedLeaf, ActiveLeavesUpdate, FromOverseer, OverseerSignal, SpawnedSubsystem,
Subsystem, SubsystemContext, SubsystemError, SubsystemResult, SubsystemSender,
messages::StatementDistributionMessage
use polkadot_overseer::gen::{
Subsystem,
OverseerError,
};
use polkadot_subsystem::messages::{
NetworkBridgeMessage, AllMessages,
CollatorProtocolMessage, NetworkBridgeEvent,
use polkadot_subsystem::{
overseer,
OverseerSignal,
FromOverseer,
SpawnedSubsystem,
SubsystemContext,
SubsystemSender,
errors::{SubsystemError, SubsystemResult},
ActivatedLeaf, ActiveLeavesUpdate,
messages::{
AllMessages, StatementDistributionMessage,
NetworkBridgeMessage, CollatorProtocolMessage, NetworkBridgeEvent,
},
};
use polkadot_primitives::v1::{Hash, BlockNumber};
use polkadot_node_network_protocol::{
@@ -292,11 +301,11 @@ impl<N, AD> NetworkBridge<N, AD> {
}
}
impl<Net, AD, Context> Subsystem<Context> for NetworkBridge<Net, AD>
impl<Net, AD, Context> Subsystem<Context, SubsystemError> for NetworkBridge<Net, AD>
where
Net: Network + Sync,
AD: validator_discovery::AuthorityDiscovery,
Context: SubsystemContext<Message=NetworkBridgeMessage>,
Context: SubsystemContext<Message = NetworkBridgeMessage> + overseer::SubsystemContext<Message = NetworkBridgeMessage>,
{
fn start(mut self, ctx: Context) -> SpawnedSubsystem {
// The stream of networking events has to be created at initialization, otherwise the
@@ -325,7 +334,7 @@ struct PeerData {
#[derive(Debug)]
enum UnexpectedAbort {
/// Received error from overseer:
SubsystemError(polkadot_subsystem::SubsystemError),
SubsystemError(SubsystemError),
/// The stream of incoming events concluded.
EventStreamConcluded,
/// The stream of incoming requests concluded.
@@ -338,6 +347,12 @@ impl From<SubsystemError> for UnexpectedAbort {
}
}
impl From<OverseerError> for UnexpectedAbort {
fn from(e: OverseerError) -> Self {
UnexpectedAbort::SubsystemError(SubsystemError::from(e))
}
}
#[derive(Default, Clone)]
struct Shared(Arc<Mutex<SharedInner>>);
@@ -363,6 +378,7 @@ async fn handle_subsystem_messages<Context, N, AD>(
) -> Result<(), UnexpectedAbort>
where
Context: SubsystemContext<Message = NetworkBridgeMessage>,
Context: overseer::SubsystemContext<Message = NetworkBridgeMessage>,
N: Network,
AD: validator_discovery::AuthorityDiscovery,
{
@@ -854,14 +870,15 @@ async fn handle_network_messages<AD: validator_discovery::AuthorityDiscovery>(
/// #fn is_send<T: Send>();
/// #is_send::<parking_lot::MutexGuard<'static, ()>();
/// ```
async fn run_network<N, AD>(
async fn run_network<N, AD, Context>(
bridge: NetworkBridge<N, AD>,
mut ctx: impl SubsystemContext<Message=NetworkBridgeMessage>,
mut ctx: Context,
network_stream: BoxStream<'static, NetworkEvent>,
) -> SubsystemResult<()>
where
N: Network,
AD: validator_discovery::AuthorityDiscovery,
Context: SubsystemContext<Message=NetworkBridgeMessage> + overseer::SubsystemContext<Message=NetworkBridgeMessage>,
{
let shared = Shared::default();
@@ -877,7 +894,7 @@ where
.get_statement_fetching()
.expect("Gets initialized, must be `Some` on startup. qed.");
let (remote, network_event_handler) = handle_network_messages(
let (remote, network_event_handler) = handle_network_messages::<>(
ctx.sender().clone(),
network_service.clone(),
network_stream,
@@ -889,9 +906,9 @@ where
ctx.spawn("network-bridge-network-worker", Box::pin(remote))?;
ctx.send_message(AllMessages::StatementDistribution(
ctx.send_message(
StatementDistributionMessage::StatementFetchingReceiver(statement_receiver)
)).await;
).await;
let subsystem_event_handler = handle_subsystem_messages(
ctx,
@@ -952,7 +969,7 @@ fn construct_view(live_heads: impl DoubleEndedIterator<Item = Hash>, finalized_n
fn update_our_view(
net: &mut impl Network,
ctx: &mut impl SubsystemContext<Message = NetworkBridgeMessage>,
ctx: &mut impl SubsystemContext<Message=NetworkBridgeMessage, AllMessages=AllMessages>,
live_heads: &[ActivatedLeaf],
shared: &Shared,
finalized_number: BlockNumber,
@@ -29,7 +29,7 @@ use sc_network::PeerId;
use polkadot_node_network_protocol::request_response::{
request::IncomingRequest, v1, Protocol, RequestResponseConfig,
};
use polkadot_subsystem::messages::AllMessages;
use polkadot_overseer::AllMessages;
/// Multiplex incoming network requests.
///
@@ -151,28 +151,28 @@ fn multiplex_single(
}: network::IncomingRequest,
) -> Result<AllMessages, RequestMultiplexError> {
let r = match p {
Protocol::ChunkFetching => From::from(IncomingRequest::new(
Protocol::ChunkFetching => AllMessages::from(IncomingRequest::new(
peer,
decode_with_peer::<v1::ChunkFetchingRequest>(peer, payload)?,
pending_response,
)),
Protocol::CollationFetching => From::from(IncomingRequest::new(
Protocol::CollationFetching => AllMessages::from(IncomingRequest::new(
peer,
decode_with_peer::<v1::CollationFetchingRequest>(peer, payload)?,
pending_response,
)),
Protocol::PoVFetching => From::from(IncomingRequest::new(
Protocol::PoVFetching => AllMessages::from(IncomingRequest::new(
peer,
decode_with_peer::<v1::PoVFetchingRequest>(peer, payload)?,
pending_response,
)),
Protocol::AvailableDataFetching => From::from(IncomingRequest::new(
Protocol::AvailableDataFetching => AllMessages::from(IncomingRequest::new(
peer,
decode_with_peer::<v1::AvailableDataFetchingRequest>(peer, payload)?,
pending_response,
)),
Protocol::StatementFetching => {
panic!("Statement fetching requests are handled directly. qed.");
unreachable!("Statement fetching requests are handled directly. qed.");
}
};
Ok(r)
@@ -1260,6 +1260,7 @@ fn spread_event_to_subsystems_is_up_to_date() {
let mut cnt = 0_usize;
for msg in AllMessages::dispatch_iter(NetworkBridgeEvent::PeerDisconnected(PeerId::random())) {
match msg {
AllMessages::Empty => unreachable!("Nobody cares about the dummy"),
AllMessages::CandidateValidation(_) => unreachable!("Not interested in network events"),
AllMessages::CandidateBacking(_) => unreachable!("Not interested in network events"),
AllMessages::ChainApi(_) => unreachable!("Not interested in network events"),
@@ -24,9 +24,10 @@ use polkadot_primitives::v1::{
Id as ParaId,
};
use polkadot_subsystem::{
overseer,
FromOverseer, OverseerSignal, PerLeafSpan, SubsystemContext, jaeger,
messages::{
AllMessages, CollatorProtocolMessage, NetworkBridgeEvent, NetworkBridgeMessage,
CollatorProtocolMessage, NetworkBridgeEvent, NetworkBridgeMessage,
},
};
use polkadot_node_network_protocol::{
@@ -301,15 +302,19 @@ impl State {
/// or the relay-parent isn't in the active-leaves set, we ignore the message
/// as it must be invalid in that case - although this indicates a logic error
/// elsewhere in the node.
async fn distribute_collation(
ctx: &mut impl SubsystemContext,
async fn distribute_collation<Context>(
ctx: &mut Context,
runtime: &mut RuntimeInfo,
state: &mut State,
id: ParaId,
receipt: CandidateReceipt,
pov: PoV,
result_sender: Option<oneshot::Sender<SignedFullStatement>>,
) -> Result<()> {
) -> Result<()>
where
Context: SubsystemContext<Message = CollatorProtocolMessage>,
Context: overseer::SubsystemContext<Message = CollatorProtocolMessage>,
{
let relay_parent = receipt.descriptor.relay_parent;
// This collation is not in the active-leaves set.
@@ -400,11 +405,15 @@ async fn distribute_collation(
/// Get the Id of the Core that is assigned to the para being collated on if any
/// and the total number of cores.
async fn determine_core(
ctx: &mut impl SubsystemContext,
async fn determine_core<Context>(
ctx: &mut Context,
para_id: ParaId,
relay_parent: Hash,
) -> Result<Option<(CoreIndex, usize)>> {
) -> Result<Option<(CoreIndex, usize)>>
where
Context: SubsystemContext<Message = CollatorProtocolMessage>,
Context: overseer::SubsystemContext<Message = CollatorProtocolMessage>,
{
let cores = get_availability_cores(ctx, relay_parent).await?;
for (idx, core) in cores.iter().enumerate() {
@@ -430,13 +439,17 @@ struct GroupValidators {
/// Figure out current and next group of validators assigned to the para being collated on.
///
/// Returns [`ValidatorId`]'s of current and next group as determined based on the `relay_parent`.
async fn determine_our_validators(
ctx: &mut impl SubsystemContext,
async fn determine_our_validators<Context>(
ctx: &mut Context,
runtime: &mut RuntimeInfo,
core_index: CoreIndex,
cores: usize,
relay_parent: Hash,
) -> Result<(GroupValidators, GroupValidators)> {
) -> Result<(GroupValidators, GroupValidators)>
where
Context: SubsystemContext<Message = CollatorProtocolMessage>,
Context: overseer::SubsystemContext<Message = CollatorProtocolMessage>,
{
let session_index = runtime.get_session_index(ctx, relay_parent).await?;
let info = &runtime.get_session_info_by_index(ctx, relay_parent, session_index)
.await?
@@ -469,11 +482,15 @@ async fn determine_our_validators(
}
/// Issue a `Declare` collation message to the given `peer`.
async fn declare(
ctx: &mut impl SubsystemContext<Message = CollatorProtocolMessage>,
async fn declare<Context>(
ctx: &mut Context,
state: &mut State,
peer: PeerId,
) {
)
where
Context: SubsystemContext<Message = CollatorProtocolMessage>,
Context: overseer::SubsystemContext<Message = CollatorProtocolMessage>,
{
let declare_signature_payload = protocol_v1::declare_signature_payload(&state.local_peer_id);
if let Some(para_id) = state.collating_on {
@@ -483,39 +500,47 @@ async fn declare(
state.collator_pair.sign(&declare_signature_payload),
);
ctx.send_message(AllMessages::NetworkBridge(
ctx.send_message(
NetworkBridgeMessage::SendCollationMessage(
vec![peer],
protocol_v1::CollationProtocol::CollatorProtocol(wire_message),
)
)).await;
).await;
}
}
/// Issue a connection request to a set of validators and
/// revoke the previous connection request.
async fn connect_to_validators(
ctx: &mut impl SubsystemContext,
async fn connect_to_validators<Context>(
ctx: &mut Context,
validator_ids: Vec<AuthorityDiscoveryId>,
) {
)
where
Context: SubsystemContext<Message = CollatorProtocolMessage>,
Context: overseer::SubsystemContext<Message = CollatorProtocolMessage>,
{
// ignore address resolution failure
// will reissue a new request on new collation
let (failed, _) = oneshot::channel();
ctx.send_message(AllMessages::NetworkBridge(NetworkBridgeMessage::ConnectToValidators {
ctx.send_message(NetworkBridgeMessage::ConnectToValidators {
validator_ids, peer_set: PeerSet::Collation, failed,
})).await;
}).await;
}
/// Advertise collation to the given `peer`.
///
/// This will only advertise a collation if there exists one for the given `relay_parent` and the given `peer` is
/// set as validator for our para at the given `relay_parent`.
async fn advertise_collation(
ctx: &mut impl SubsystemContext,
async fn advertise_collation<Context>(
ctx: &mut Context,
state: &mut State,
relay_parent: Hash,
peer: PeerId,
) {
)
where
Context: SubsystemContext<Message = CollatorProtocolMessage>,
Context: overseer::SubsystemContext<Message = CollatorProtocolMessage>,
{
let should_advertise = state.our_validators_groups
.get(&relay_parent)
.map(|g| g.should_advertise_to(&state.peer_ids, &peer))
@@ -555,12 +580,12 @@ async fn advertise_collation(
relay_parent,
);
ctx.send_message(AllMessages::NetworkBridge(
ctx.send_message(
NetworkBridgeMessage::SendCollationMessage(
vec![peer.clone()],
protocol_v1::CollationProtocol::CollatorProtocol(wire_message),
)
)).await;
).await;
if let Some(validators) = state.our_validators_groups.get_mut(&relay_parent) {
validators.advertised_to_peer(&state.peer_ids, &peer);
@@ -570,12 +595,16 @@ async fn advertise_collation(
}
/// The main incoming message dispatching switch.
async fn process_msg(
ctx: &mut impl SubsystemContext<Message = CollatorProtocolMessage>,
async fn process_msg<Context>(
ctx: &mut Context,
runtime: &mut RuntimeInfo,
state: &mut State,
msg: CollatorProtocolMessage,
) -> Result<()> {
) -> Result<()>
where
Context: SubsystemContext<Message = CollatorProtocolMessage>,
Context: overseer::SubsystemContext<Message = CollatorProtocolMessage>,
{
use CollatorProtocolMessage::*;
let _timer = state.metrics.time_process_msg();
@@ -718,13 +747,17 @@ async fn send_collation(
}
/// A networking messages switch.
async fn handle_incoming_peer_message(
ctx: &mut impl SubsystemContext,
async fn handle_incoming_peer_message<Context>(
ctx: &mut Context,
runtime: &mut RuntimeInfo,
state: &mut State,
origin: PeerId,
msg: protocol_v1::CollatorProtocolMessage,
) -> Result<()> {
) -> Result<()>
where
Context: SubsystemContext<Message = CollatorProtocolMessage>,
Context: overseer::SubsystemContext<Message = CollatorProtocolMessage>,
{
use protocol_v1::CollatorProtocolMessage::*;
match msg {
@@ -737,7 +770,7 @@ async fn handle_incoming_peer_message(
// If we are declared to, this is another collator, and we should disconnect.
ctx.send_message(
NetworkBridgeMessage::DisconnectPeer(origin, PeerSet::Collation).into()
NetworkBridgeMessage::DisconnectPeer(origin, PeerSet::Collation)
).await;
}
AdvertiseCollation(_) => {
@@ -748,12 +781,12 @@ async fn handle_incoming_peer_message(
);
ctx.send_message(
NetworkBridgeMessage::ReportPeer(origin.clone(), COST_UNEXPECTED_MESSAGE).into()
NetworkBridgeMessage::ReportPeer(origin.clone(), COST_UNEXPECTED_MESSAGE)
).await;
// If we are advertised to, this is another collator, and we should disconnect.
ctx.send_message(
NetworkBridgeMessage::DisconnectPeer(origin, PeerSet::Collation).into()
NetworkBridgeMessage::DisconnectPeer(origin, PeerSet::Collation)
).await;
}
CollationSeconded(relay_parent, statement) => {
@@ -789,12 +822,16 @@ async fn handle_incoming_peer_message(
}
/// Our view has changed.
async fn handle_peer_view_change(
ctx: &mut impl SubsystemContext<Message = CollatorProtocolMessage>,
async fn handle_peer_view_change<Context>(
ctx: &mut Context,
state: &mut State,
peer_id: PeerId,
view: View,
) {
)
where
Context: SubsystemContext<Message = CollatorProtocolMessage>,
Context: overseer::SubsystemContext<Message = CollatorProtocolMessage>,
{
let current = state.peer_views.entry(peer_id.clone()).or_default();
let added: Vec<Hash> = view.difference(&*current).cloned().collect();
@@ -807,12 +844,16 @@ async fn handle_peer_view_change(
}
/// Bridge messages switch.
async fn handle_network_msg(
ctx: &mut impl SubsystemContext<Message = CollatorProtocolMessage>,
async fn handle_network_msg<Context>(
ctx: &mut Context,
runtime: &mut RuntimeInfo,
state: &mut State,
bridge_message: NetworkBridgeEvent<protocol_v1::CollatorProtocolMessage>,
) -> Result<()> {
) -> Result<()>
where
Context: SubsystemContext<Message = CollatorProtocolMessage>,
Context: overseer::SubsystemContext<Message = CollatorProtocolMessage>,
{
use NetworkBridgeEvent::*;
match bridge_message {
@@ -917,13 +958,16 @@ async fn handle_our_view_change(
}
/// The collator protocol collator side main loop.
pub(crate) async fn run(
mut ctx: impl SubsystemContext<Message = CollatorProtocolMessage>,
pub(crate) async fn run<Context>(
mut ctx: Context,
local_peer_id: PeerId,
collator_pair: CollatorPair,
metrics: Metrics,
) -> Result<()> {
use FromOverseer::*;
) -> Result<()>
where
Context: SubsystemContext<Message = CollatorProtocolMessage>,
Context: overseer::SubsystemContext<Message = CollatorProtocolMessage>
{
use OverseerSignal::*;
let mut state = State::new(local_peer_id, collator_pair, metrics);
@@ -932,15 +976,15 @@ pub(crate) async fn run(
loop {
select! {
msg = ctx.recv().fuse() => match msg.map_err(Fatal::SubsystemReceive)? {
Communication { msg } => {
FromOverseer::Communication { msg } => {
log_error(
process_msg(&mut ctx, &mut runtime, &mut state, msg).await,
"Failed to process message"
)?;
},
Signal(ActiveLeaves(_update)) => {}
Signal(BlockFinalized(..)) => {}
Signal(Conclude) => return Ok(()),
FromOverseer::Signal(ActiveLeaves(_update)) => {}
FromOverseer::Signal(BlockFinalized(..)) => {}
FromOverseer::Signal(Conclude) => return Ok(()),
},
relay_parent = state.active_collation_fetches.select_next_some() => {
let next = if let Some(waiting) = state.waiting_collation_fetches.get_mut(&relay_parent) {
@@ -39,7 +39,7 @@ use polkadot_primitives::v1::{
use polkadot_node_primitives::BlockData;
use polkadot_subsystem::{
jaeger,
messages::{RuntimeApiMessage, RuntimeApiRequest},
messages::{AllMessages, RuntimeApiMessage, RuntimeApiRequest},
ActiveLeavesUpdate, ActivatedLeaf, LeafStatus,
};
use polkadot_subsystem_testhelpers as test_helpers;
@@ -18,7 +18,7 @@
//! Error handling related code and Error/Result definitions.
use polkadot_node_primitives::UncheckedSignedFullStatement;
use polkadot_subsystem::SubsystemError;
use polkadot_subsystem::errors::SubsystemError;
use thiserror::Error;
use polkadot_node_subsystem_util::{Fault, runtime, unwrap_non_fatal};
@@ -28,9 +28,16 @@ use sp_keystore::SyncCryptoStorePtr;
use polkadot_node_network_protocol::{PeerId, UnifiedReputationChange as Rep};
use polkadot_primitives::v1::CollatorPair;
use polkadot_subsystem::{
messages::{AllMessages, CollatorProtocolMessage, NetworkBridgeMessage},
SpawnedSubsystem, Subsystem, SubsystemContext, SubsystemError,
SpawnedSubsystem,
SubsystemContext,
SubsystemSender,
overseer,
messages::{
CollatorProtocolMessage, NetworkBridgeMessage,
},
errors::SubsystemError,
};
mod error;
@@ -92,7 +99,8 @@ impl CollatorProtocolSubsystem {
async fn run<Context>(self, ctx: Context) -> Result<()>
where
Context: SubsystemContext<Message = CollatorProtocolMessage>,
Context: overseer::SubsystemContext<Message=CollatorProtocolMessage>,
Context: SubsystemContext<Message=CollatorProtocolMessage>,
{
match self.protocol_side {
ProtocolSide::Validator { keystore, eviction_policy, metrics } => validator_side::run(
@@ -111,9 +119,11 @@ impl CollatorProtocolSubsystem {
}
}
impl<Context> Subsystem<Context> for CollatorProtocolSubsystem
impl<Context> overseer::Subsystem<Context, SubsystemError> for CollatorProtocolSubsystem
where
Context: SubsystemContext<Message = CollatorProtocolMessage> + Sync + Send,
Context: SubsystemContext<Message = CollatorProtocolMessage>,
Context: overseer::SubsystemContext<Message = CollatorProtocolMessage>,
<Context as SubsystemContext>::Sender: SubsystemSender,
{
fn start(self, ctx: Context) -> SpawnedSubsystem {
let future = self
@@ -140,7 +150,7 @@ where
"reputation change for peer",
);
ctx.send_message(AllMessages::NetworkBridge(
ctx.send_message(
NetworkBridgeMessage::ReportPeer(peer, rep),
)).await;
).await;
}
@@ -40,9 +40,10 @@ use polkadot_node_primitives::{SignedFullStatement, PoV};
use polkadot_node_subsystem_util::metrics::{self, prometheus};
use polkadot_primitives::v1::{CandidateReceipt, CollatorId, Hash, Id as ParaId};
use polkadot_subsystem::{
overseer,
jaeger,
messages::{
AllMessages, CollatorProtocolMessage, IfDisconnected,
CollatorProtocolMessage, IfDisconnected,
NetworkBridgeEvent, NetworkBridgeMessage, CandidateBackingMessage,
},
FromOverseer, OverseerSignal, PerLeafSpan, SubsystemContext, SubsystemSender,
@@ -583,19 +584,27 @@ fn collator_peer_id(
)
}
async fn disconnect_peer(ctx: &mut impl SubsystemContext, peer_id: PeerId) {
async fn disconnect_peer<Context>(ctx: &mut Context, peer_id: PeerId)
where
Context: overseer::SubsystemContext<Message=CollatorProtocolMessage>,
Context: SubsystemContext<Message=CollatorProtocolMessage>,
{
ctx.send_message(
NetworkBridgeMessage::DisconnectPeer(peer_id, PeerSet::Collation).into()
NetworkBridgeMessage::DisconnectPeer(peer_id, PeerSet::Collation)
).await
}
/// Another subsystem has requested to fetch collations on a particular leaf for some para.
async fn fetch_collation(
ctx: &mut impl SubsystemContext<Message = CollatorProtocolMessage>,
async fn fetch_collation<Context>(
ctx: &mut Context,
state: &mut State,
pc: PendingCollation,
id: CollatorId,
) {
)
where
Context: overseer::SubsystemContext<Message=CollatorProtocolMessage>,
Context: SubsystemContext<Message=CollatorProtocolMessage>,
{
let (tx, rx) = oneshot::channel();
let PendingCollation { relay_parent, para_id, peer_id, .. } = pc;
@@ -627,7 +636,8 @@ async fn note_good_collation<Context>(
id: CollatorId,
)
where
Context: SubsystemContext<Message = CollatorProtocolMessage>
Context: overseer::SubsystemContext<Message=CollatorProtocolMessage>,
Context: SubsystemContext<Message=CollatorProtocolMessage>,
{
if let Some(peer_id) = collator_peer_id(peer_data, &id) {
modify_reputation(ctx, peer_id, BENEFIT_NOTIFY_GOOD).await;
@@ -635,19 +645,23 @@ where
}
/// Notify a collator that its collation got seconded.
async fn notify_collation_seconded(
ctx: &mut impl SubsystemContext<Message = CollatorProtocolMessage>,
async fn notify_collation_seconded<Context>(
ctx: &mut Context,
peer_id: PeerId,
relay_parent: Hash,
statement: SignedFullStatement,
) {
)
where
Context: overseer::SubsystemContext<Message=CollatorProtocolMessage>,
Context: SubsystemContext<Message=CollatorProtocolMessage>,
{
let wire_message = protocol_v1::CollatorProtocolMessage::CollationSeconded(relay_parent, statement.into());
ctx.send_message(AllMessages::NetworkBridge(
ctx.send_message(
NetworkBridgeMessage::SendCollationMessage(
vec![peer_id],
protocol_v1::CollationProtocol::CollatorProtocol(wire_message),
)
)).await;
).await;
modify_reputation(ctx, peer_id, BENEFIT_NOTIFY_GOOD).await;
}
@@ -684,7 +698,8 @@ async fn request_collation<Context>(
result: oneshot::Sender<(CandidateReceipt, PoV)>,
)
where
Context: SubsystemContext<Message = CollatorProtocolMessage>
Context: overseer::SubsystemContext<Message=CollatorProtocolMessage>,
Context: SubsystemContext<Message=CollatorProtocolMessage>,
{
if !state.view.contains(&relay_parent) {
tracing::debug!(
@@ -737,8 +752,8 @@ where
"Requesting collation",
);
ctx.send_message(AllMessages::NetworkBridge(
NetworkBridgeMessage::SendRequests(vec![requests], IfDisconnected::ImmediateError))
ctx.send_message(
NetworkBridgeMessage::SendRequests(vec![requests], IfDisconnected::ImmediateError)
).await;
}
@@ -750,7 +765,8 @@ async fn process_incoming_peer_message<Context>(
msg: protocol_v1::CollatorProtocolMessage,
)
where
Context: SubsystemContext<Message = CollatorProtocolMessage>
Context: overseer::SubsystemContext<Message=CollatorProtocolMessage>,
Context: SubsystemContext<Message=CollatorProtocolMessage>,
{
use protocol_v1::CollatorProtocolMessage::*;
use sp_runtime::traits::AppVerify;
@@ -897,12 +913,16 @@ async fn remove_relay_parent(
}
/// Our view has changed.
async fn handle_our_view_change(
ctx: &mut impl SubsystemContext,
async fn handle_our_view_change<Context>(
ctx: &mut Context,
state: &mut State,
keystore: &SyncCryptoStorePtr,
view: OurView,
) -> Result<()> {
) -> Result<()>
where
Context: overseer::SubsystemContext<Message=CollatorProtocolMessage>,
Context: SubsystemContext<Message=CollatorProtocolMessage>,
{
let old_view = std::mem::replace(&mut state.view, view);
let added: HashMap<Hash, Arc<jaeger::Span>> = state.view
@@ -955,7 +975,8 @@ async fn handle_network_msg<Context>(
bridge_message: NetworkBridgeEvent<protocol_v1::CollatorProtocolMessage>,
) -> Result<()>
where
Context: SubsystemContext<Message = CollatorProtocolMessage>
Context: overseer::SubsystemContext<Message=CollatorProtocolMessage>,
Context: SubsystemContext<Message=CollatorProtocolMessage>,
{
use NetworkBridgeEvent::*;
@@ -993,7 +1014,8 @@ async fn process_msg<Context>(
state: &mut State,
)
where
Context: SubsystemContext<Message = CollatorProtocolMessage>
Context: overseer::SubsystemContext<Message=CollatorProtocolMessage>,
Context: SubsystemContext<Message=CollatorProtocolMessage>,
{
use CollatorProtocolMessage::*;
@@ -1101,9 +1123,10 @@ pub(crate) async fn run<Context>(
eviction_policy: crate::CollatorEvictionPolicy,
metrics: Metrics,
) -> Result<()>
where Context: SubsystemContext<Message = CollatorProtocolMessage>
where
Context: overseer::SubsystemContext<Message=CollatorProtocolMessage>,
Context: SubsystemContext<Message=CollatorProtocolMessage>,
{
use FromOverseer::*;
use OverseerSignal::*;
let mut state = State {
@@ -1122,7 +1145,7 @@ pub(crate) async fn run<Context>(
select! {
res = ctx.recv().fuse() => {
match res {
Ok(Communication { msg }) => {
Ok(FromOverseer::Communication { msg }) => {
tracing::trace!(target: LOG_TARGET, msg = ?msg, "received a message");
process_msg(
&mut ctx,
@@ -1131,7 +1154,7 @@ pub(crate) async fn run<Context>(
&mut state,
).await;
}
Ok(Signal(Conclude)) => break,
Ok(FromOverseer::Signal(Conclude)) => break,
_ => {},
}
}
@@ -1159,11 +1182,15 @@ pub(crate) async fn run<Context>(
}
/// Handle a fetched collation result.
async fn handle_collation_fetched_result(
ctx: &mut impl SubsystemContext<Message = CollatorProtocolMessage>,
async fn handle_collation_fetched_result<Context>(
ctx: &mut Context,
state: &mut State,
(mut collation_event, res): PendingCollationFetch,
) {
)
where
Context: overseer::SubsystemContext<Message=CollatorProtocolMessage>,
Context: SubsystemContext<Message=CollatorProtocolMessage>,
{
// If no prior collation for this relay parent has been seconded, then
// memoize the collation_event for that relay_parent, such that we may
// notify the collator of their successful second backing
@@ -1204,7 +1231,7 @@ async fn handle_collation_fetched_result(
relay_parent.clone(),
candidate_receipt,
pov,
).into()
)
).await;
entry.insert(collation_event);
@@ -1221,11 +1248,15 @@ async fn handle_collation_fetched_result(
// This issues `NetworkBridge` notifications to disconnect from all inactive peers at the
// earliest possible point. This does not yet clean up any metadata, as that will be done upon
// receipt of the `PeerDisconnected` event.
async fn disconnect_inactive_peers(
ctx: &mut impl SubsystemContext,
async fn disconnect_inactive_peers<Context>(
ctx: &mut Context,
eviction_policy: &crate::CollatorEvictionPolicy,
peers: &HashMap<PeerId, PeerData>,
) {
)
where
Context: overseer::SubsystemContext<Message=CollatorProtocolMessage>,
Context: SubsystemContext<Message=CollatorProtocolMessage>,
{
for (peer, peer_data) in peers {
if peer_data.is_inactive(&eviction_policy) {
disconnect_peer(ctx, peer.clone()).await;
@@ -1248,7 +1279,8 @@ async fn poll_collation_response<Context>(
)
-> bool
where
Context: SubsystemContext
Context: overseer::SubsystemContext<Message=CollatorProtocolMessage>,
Context: SubsystemContext,
{
if never!(per_req.from_collator.is_terminated()) {
tracing::error!(
@@ -31,7 +31,7 @@ use polkadot_primitives::v1::{
use polkadot_node_primitives::BlockData;
use polkadot_node_subsystem_util::TimeoutExt;
use polkadot_subsystem_testhelpers as test_helpers;
use polkadot_subsystem::messages::{RuntimeApiMessage, RuntimeApiRequest};
use polkadot_subsystem::messages::{AllMessages, RuntimeApiMessage, RuntimeApiRequest};
use polkadot_node_network_protocol::{
our_view, ObservedRole, request_response::{Requests, ResponseSender},
};
+53 -26
View File
@@ -29,12 +29,16 @@ use futures::{channel::oneshot, FutureExt as _};
use rand::{SeedableRng, seq::SliceRandom as _};
use rand_chacha::ChaCha20Rng;
use polkadot_node_subsystem::{
overseer,
SubsystemError,
FromOverseer, SpawnedSubsystem, SubsystemContext,
messages::{
AllMessages, GossipSupportMessage, NetworkBridgeMessage,
RuntimeApiMessage, RuntimeApiRequest,
GossipSupportMessage,
NetworkBridgeMessage,
RuntimeApiMessage,
RuntimeApiRequest,
},
ActiveLeavesUpdate, FromOverseer, OverseerSignal,
Subsystem, SpawnedSubsystem, SubsystemContext,
ActiveLeavesUpdate, OverseerSignal,
};
use polkadot_node_subsystem_util as util;
use polkadot_primitives::v1::{
@@ -94,6 +98,7 @@ impl GossipSupport {
async fn run<Context>(self, ctx: Context)
where
Context: SubsystemContext<Message = GossipSupportMessage>,
Context: overseer::SubsystemContext<Message = GossipSupportMessage>,
{
let mut state = State::default();
self.run_inner(ctx, &mut state).await;
@@ -102,6 +107,7 @@ impl GossipSupport {
async fn run_inner<Context>(self, mut ctx: Context, state: &mut State)
where
Context: SubsystemContext<Message = GossipSupportMessage>,
Context: overseer::SubsystemContext<Message = GossipSupportMessage>,
{
let Self { keystore } = self;
loop {
@@ -138,10 +144,14 @@ impl GossipSupport {
}
}
async fn determine_relevant_authorities(
ctx: &mut impl SubsystemContext,
async fn determine_relevant_authorities<Context>(
ctx: &mut Context,
relay_parent: Hash,
) -> Result<Vec<AuthorityDiscoveryId>, util::Error> {
) -> Result<Vec<AuthorityDiscoveryId>, util::Error>
where
Context: SubsystemContext<Message = GossipSupportMessage>,
Context: overseer::SubsystemContext<Message = GossipSupportMessage>,
{
let authorities = util::request_authorities(relay_parent, ctx.sender()).await.await??;
tracing::debug!(
target: LOG_TARGET,
@@ -169,19 +179,23 @@ async fn ensure_i_am_an_authority(
}
/// A helper function for making a `ConnectToValidators` request.
async fn connect_to_authorities(
ctx: &mut impl SubsystemContext,
async fn connect_to_authorities<Context>(
ctx: &mut Context,
validator_ids: Vec<AuthorityDiscoveryId>,
peer_set: PeerSet,
) -> oneshot::Receiver<usize> {
) -> oneshot::Receiver<usize>
where
Context: SubsystemContext<Message = GossipSupportMessage>,
Context: overseer::SubsystemContext<Message = GossipSupportMessage>,
{
let (failed, failed_rx) = oneshot::channel();
ctx.send_message(AllMessages::NetworkBridge(
ctx.send_message(
NetworkBridgeMessage::ConnectToValidators {
validator_ids,
peer_set,
failed,
}
)).await;
).await;
failed_rx
}
@@ -193,12 +207,16 @@ async fn connect_to_authorities(
/// This limits the amount of gossip peers to 2 * sqrt(len) and ensures the diameter of 2.
///
/// [web3]: https://research.web3.foundation/en/latest/polkadot/networking/3-avail-valid.html#topology
async fn update_gossip_topology(
ctx: &mut impl SubsystemContext,
async fn update_gossip_topology<Context>(
ctx: &mut Context,
our_index: usize,
authorities: Vec<AuthorityDiscoveryId>,
relay_parent: Hash,
) -> Result<(), util::Error> {
) -> Result<(), util::Error>
where
Context: SubsystemContext<Message = GossipSupportMessage>,
Context: overseer::SubsystemContext<Message = GossipSupportMessage>,
{
// retrieve BABE randomness
let random_seed = {
let (tx, rx) = oneshot::channel();
@@ -206,7 +224,7 @@ async fn update_gossip_topology(
ctx.send_message(RuntimeApiMessage::Request(
relay_parent,
RuntimeApiRequest::CurrentBabeEpoch(tx),
).into()).await;
)).await;
let randomness = rx.await??.randomness;
let mut subject = [0u8; 40];
@@ -227,11 +245,11 @@ async fn update_gossip_topology(
let neighbors = matrix_neighbors(our_shuffled_position, len);
let our_neighbors = neighbors.map(|i| authorities[indices[i]].clone()).collect();
ctx.send_message(AllMessages::NetworkBridge(
ctx.send_message(
NetworkBridgeMessage::NewGossipTopology {
our_neighbors,
}
)).await;
).await;
Ok(())
}
@@ -262,12 +280,16 @@ impl State {
/// 1. Determine if the current session index has changed.
/// 2. If it has, determine relevant validators
/// and issue a connection request.
async fn handle_active_leaves(
async fn handle_active_leaves<Context>(
&mut self,
ctx: &mut impl SubsystemContext,
ctx: &mut Context,
keystore: &SyncCryptoStorePtr,
leaves: impl Iterator<Item = Hash>,
) -> Result<(), util::Error> {
) -> Result<(), util::Error>
where
Context: SubsystemContext<Message = GossipSupportMessage>,
Context: overseer::SubsystemContext<Message = GossipSupportMessage>,
{
for leaf in leaves {
let current_index = util::request_session_index_for_child(leaf, ctx.sender()).await.await??;
let since_failure = self.last_failure.map(|i| i.elapsed()).unwrap_or_default();
@@ -310,11 +332,15 @@ impl State {
Ok(())
}
async fn issue_connection_request(
async fn issue_connection_request<Context>(
&mut self,
ctx: &mut impl SubsystemContext,
ctx: &mut Context,
authorities: Vec<AuthorityDiscoveryId>,
) -> Result<(), util::Error> {
) -> Result<(), util::Error>
where
Context: SubsystemContext<Message = GossipSupportMessage>,
Context: overseer::SubsystemContext<Message = GossipSupportMessage>,
{
let num = authorities.len();
tracing::debug!(target: LOG_TARGET, %num, "Issuing a connection request");
@@ -362,9 +388,10 @@ impl State {
}
}
impl<Context> Subsystem<Context> for GossipSupport
impl<Context> overseer::Subsystem<Context, SubsystemError> for GossipSupport
where
Context: SubsystemContext<Message = GossipSupportMessage> + Sync + Send,
Context: SubsystemContext<Message = GossipSupportMessage>,
Context: overseer::SubsystemContext<Message = GossipSupportMessage>,
{
fn start(self, ctx: Context) -> SpawnedSubsystem {
let future = self.run(ctx)
@@ -19,7 +19,7 @@
use super::*;
use polkadot_node_subsystem::{
jaeger, ActivatedLeaf, LeafStatus,
messages::{RuntimeApiMessage, RuntimeApiRequest},
messages::{AllMessages, RuntimeApiMessage, RuntimeApiRequest},
};
use polkadot_node_subsystem_test_helpers as test_helpers;
use polkadot_node_subsystem_util::TimeoutExt as _;
+3 -1
View File
@@ -87,13 +87,15 @@ impl Into<sc_network::ObservedRole> for ObservedRole {
}
}
/// Implement `TryFrom` for one enum variant into the inner type.
/// `$m_ty::$variant(inner) -> Ok(inner)`
macro_rules! impl_try_from {
($m_ty:ident, $variant:ident, $out:ty) => {
impl TryFrom<$m_ty> for $out {
type Error = crate::WrongVariant;
#[allow(unreachable_patterns)] // when there is only one variant
fn try_from(x: $m_ty) -> Result<$out, Self::Error> {
#[allow(unreachable_patterns)] // when there is only one variant
match x {
$m_ty::$variant(y) => Ok(y),
_ => Err(crate::WrongVariant),
@@ -26,7 +26,8 @@ use error::{FatalResult, NonFatalResult, log_error};
use parity_scale_codec::Encode;
use polkadot_subsystem::{
ActiveLeavesUpdate, FromOverseer, OverseerSignal, PerLeafSpan, SpawnedSubsystem, Subsystem,
overseer,
ActiveLeavesUpdate, FromOverseer, OverseerSignal, PerLeafSpan, SpawnedSubsystem,
SubsystemContext, SubsystemError, jaeger,
messages::{
AllMessages, NetworkBridgeMessage, StatementDistributionMessage,
@@ -107,10 +108,12 @@ pub struct StatementDistribution {
metrics: Metrics,
}
impl<C> Subsystem<C> for StatementDistribution
where C: SubsystemContext<Message=StatementDistributionMessage>
impl<Context> overseer::Subsystem<Context, SubsystemError> for StatementDistribution
where
Context: SubsystemContext<Message=StatementDistributionMessage>,
Context: overseer::SubsystemContext<Message=StatementDistributionMessage>,
{
fn start(self, ctx: C) -> SpawnedSubsystem {
fn start(self, ctx: Context) -> SpawnedSubsystem {
// Swallow error because failure is fatal to the node and we log with more precision
// within `run`.
SpawnedSubsystem {
@@ -588,7 +591,7 @@ enum Message {
impl Message {
async fn receive(
ctx: &mut impl SubsystemContext<Message = StatementDistributionMessage>,
ctx: &mut (impl SubsystemContext<Message = StatementDistributionMessage> + overseer::SubsystemContext<Message = StatementDistributionMessage>),
from_requester: &mut mpsc::Receiver<RequesterMessage>,
from_responder: &mut mpsc::Receiver<ResponderMessage>,
) -> Message {
@@ -846,7 +849,7 @@ async fn circulate_statement_and_dependents(
gossip_peers: &HashSet<PeerId>,
peers: &mut HashMap<PeerId, PeerData>,
active_heads: &mut HashMap<Hash, ActiveHeadData>,
ctx: &mut impl SubsystemContext,
ctx: &mut (impl SubsystemContext + overseer::SubsystemContext),
relay_parent: Hash,
statement: SignedFullStatement,
priority_peers: Vec<PeerId>,
@@ -953,7 +956,7 @@ fn is_statement_large(statement: &SignedFullStatement) -> bool {
async fn circulate_statement<'a>(
gossip_peers: &HashSet<PeerId>,
peers: &mut HashMap<PeerId, PeerData>,
ctx: &mut impl SubsystemContext,
ctx: &mut (impl SubsystemContext + overseer::SubsystemContext),
relay_parent: Hash,
stored: StoredStatement<'a>,
mut priority_peers: Vec<PeerId>,
@@ -1034,7 +1037,7 @@ async fn circulate_statement<'a>(
async fn send_statements_about(
peer: PeerId,
peer_data: &mut PeerData,
ctx: &mut impl SubsystemContext,
ctx: &mut (impl SubsystemContext + overseer::SubsystemContext),
relay_parent: Hash,
candidate_hash: CandidateHash,
active_head: &ActiveHeadData,
@@ -1071,7 +1074,7 @@ async fn send_statements_about(
async fn send_statements(
peer: PeerId,
peer_data: &mut PeerData,
ctx: &mut impl SubsystemContext,
ctx: &mut (impl SubsystemContext + overseer::SubsystemContext),
relay_parent: Hash,
active_head: &ActiveHeadData,
metrics: &Metrics,
@@ -1103,7 +1106,7 @@ async fn send_statements(
}
async fn report_peer(
ctx: &mut impl SubsystemContext,
ctx: &mut (impl SubsystemContext + overseer::SubsystemContext),
peer: PeerId,
rep: Rep,
) {
@@ -1123,7 +1126,7 @@ async fn retrieve_statement_from_message<'a>(
peer: PeerId,
message: protocol_v1::StatementDistributionMessage,
active_head: &'a mut ActiveHeadData,
ctx: &mut impl SubsystemContext,
ctx: &mut (impl SubsystemContext + overseer::SubsystemContext),
req_sender: &mpsc::Sender<RequesterMessage>,
metrics: &Metrics,
) -> Option<UncheckedSignedFullStatement> {
@@ -1225,7 +1228,7 @@ async fn launch_request(
meta: StatementMetadata,
peer: PeerId,
req_sender: mpsc::Sender<RequesterMessage>,
ctx: &mut impl SubsystemContext,
ctx: &mut (impl SubsystemContext + overseer::SubsystemContext),
metrics: &Metrics,
) -> Option<LargeStatementStatus> {
@@ -1263,7 +1266,7 @@ async fn handle_incoming_message_and_circulate<'a>(
gossip_peers: &HashSet<PeerId>,
peers: &mut HashMap<PeerId, PeerData>,
active_heads: &'a mut HashMap<Hash, ActiveHeadData>,
ctx: &mut impl SubsystemContext,
ctx: &mut (impl SubsystemContext + overseer::SubsystemContext),
message: protocol_v1::StatementDistributionMessage,
req_sender: &mpsc::Sender<RequesterMessage>,
metrics: &Metrics,
@@ -1312,7 +1315,7 @@ async fn handle_incoming_message<'a>(
peer: PeerId,
peer_data: &mut PeerData,
active_heads: &'a mut HashMap<Hash, ActiveHeadData>,
ctx: &mut impl SubsystemContext,
ctx: &mut (impl SubsystemContext + overseer::SubsystemContext),
message: protocol_v1::StatementDistributionMessage,
req_sender: &mpsc::Sender<RequesterMessage>,
metrics: &Metrics,
@@ -1447,10 +1450,7 @@ async fn handle_incoming_message<'a>(
// When we receive a new message from a peer, we forward it to the
// candidate backing subsystem.
let message = AllMessages::CandidateBacking(
CandidateBackingMessage::Statement(relay_parent, statement.statement.clone())
);
ctx.send_message(message).await;
ctx.send_message(CandidateBackingMessage::Statement(relay_parent, statement.statement.clone())).await;
Some((relay_parent, statement))
}
@@ -1462,7 +1462,7 @@ async fn update_peer_view_and_maybe_send_unlocked(
peer: PeerId,
gossip_peers: &HashSet<PeerId>,
peer_data: &mut PeerData,
ctx: &mut impl SubsystemContext,
ctx: &mut (impl SubsystemContext + overseer::SubsystemContext),
active_heads: &HashMap<Hash, ActiveHeadData>,
new_view: View,
metrics: &Metrics,
@@ -1506,7 +1506,7 @@ async fn handle_network_update(
gossip_peers: &mut HashSet<PeerId>,
authorities: &mut HashMap<AuthorityDiscoveryId, PeerId>,
active_heads: &mut HashMap<Hash, ActiveHeadData>,
ctx: &mut impl SubsystemContext,
ctx: &mut (impl SubsystemContext + overseer::SubsystemContext),
req_sender: &mpsc::Sender<RequesterMessage>,
update: NetworkBridgeEvent<protocol_v1::StatementDistributionMessage>,
metrics: &Metrics,
@@ -1599,7 +1599,7 @@ async fn handle_network_update(
impl StatementDistribution {
async fn run(
self,
mut ctx: impl SubsystemContext<Message = StatementDistributionMessage>,
mut ctx: (impl SubsystemContext<Message = StatementDistributionMessage> + overseer::SubsystemContext<Message = StatementDistributionMessage>),
) -> std::result::Result<(), Fatal> {
let mut peers: HashMap<PeerId, PeerData> = HashMap::new();
let mut gossip_peers: HashSet<PeerId> = HashSet::new();
@@ -1831,7 +1831,7 @@ impl StatementDistribution {
async fn handle_subsystem_message(
&self,
ctx: &mut impl SubsystemContext,
ctx: &mut (impl SubsystemContext + overseer::SubsystemContext),
runtime: &mut RuntimeInfo,
peers: &mut HashMap<PeerId, PeerData>,
gossip_peers: &mut HashSet<PeerId>,