mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 14:37:57 +00:00
gossip: do not try to connect if we are not validators (#2786)
* gossip: do not issue a connection request if we are not a validator * guide updates * use all relevant authorities when issuing a request * use AuthorityDiscoveryApi instead * update comments to the status quo
This commit is contained in:
@@ -18,7 +18,10 @@
|
||||
//! and issuing a connection request to the validators relevant to
|
||||
//! the gossiping subsystems on every new session.
|
||||
|
||||
use futures::FutureExt as _;
|
||||
use futures::{channel::mpsc, FutureExt as _};
|
||||
use std::sync::Arc;
|
||||
use sp_api::ProvideRuntimeApi;
|
||||
use sp_authority_discovery::AuthorityDiscoveryApi;
|
||||
use polkadot_node_subsystem::{
|
||||
messages::{
|
||||
GossipSupportMessage,
|
||||
@@ -27,30 +30,42 @@ use polkadot_node_subsystem::{
|
||||
Subsystem, SpawnedSubsystem, SubsystemContext,
|
||||
};
|
||||
use polkadot_node_subsystem_util::{
|
||||
validator_discovery::{ConnectionRequest, self},
|
||||
validator_discovery,
|
||||
self as util,
|
||||
};
|
||||
use polkadot_primitives::v1::{
|
||||
Hash, ValidatorId, SessionIndex,
|
||||
Hash, SessionIndex, AuthorityDiscoveryId, Block, BlockId,
|
||||
};
|
||||
use polkadot_node_network_protocol::peer_set::PeerSet;
|
||||
use polkadot_node_network_protocol::{peer_set::PeerSet, PeerId};
|
||||
use sp_keystore::{CryptoStore, SyncCryptoStorePtr};
|
||||
use sp_application_crypto::{Public, AppKey};
|
||||
|
||||
const LOG_TARGET: &str = "parachain::gossip-support";
|
||||
|
||||
/// The Gossip Support subsystem.
|
||||
pub struct GossipSupport {}
|
||||
pub struct GossipSupport<Client> {
|
||||
client: Arc<Client>,
|
||||
keystore: SyncCryptoStorePtr,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct State {
|
||||
last_session_index: Option<SessionIndex>,
|
||||
/// when we overwrite this, it automatically drops the previous request
|
||||
_last_connection_request: Option<ConnectionRequest>,
|
||||
_last_connection_request: Option<mpsc::Receiver<(AuthorityDiscoveryId, PeerId)>>,
|
||||
}
|
||||
|
||||
impl GossipSupport {
|
||||
impl<Client> GossipSupport<Client>
|
||||
where
|
||||
Client: ProvideRuntimeApi<Block>,
|
||||
Client::Api: AuthorityDiscoveryApi<Block>,
|
||||
{
|
||||
/// Create a new instance of the [`GossipSupport`] subsystem.
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
pub fn new(keystore: SyncCryptoStorePtr, client: Arc<Client>) -> Self {
|
||||
Self {
|
||||
client,
|
||||
keystore,
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, ctx), fields(subsystem = LOG_TARGET))]
|
||||
@@ -59,6 +74,7 @@ impl GossipSupport {
|
||||
Context: SubsystemContext<Message = GossipSupportMessage>,
|
||||
{
|
||||
let mut state = State::default();
|
||||
let Self { client, keystore } = self;
|
||||
loop {
|
||||
let message = match ctx.recv().await {
|
||||
Ok(message) => message,
|
||||
@@ -80,7 +96,7 @@ impl GossipSupport {
|
||||
tracing::trace!(target: LOG_TARGET, "active leaves signal");
|
||||
|
||||
let leaves = activated.into_iter().map(|a| a.hash);
|
||||
if let Err(e) = state.handle_active_leaves(&mut ctx, leaves).await {
|
||||
if let Err(e) = state.handle_active_leaves(&mut ctx, client.clone(), &keystore, leaves).await {
|
||||
tracing::debug!(target: LOG_TARGET, error = ?e);
|
||||
}
|
||||
}
|
||||
@@ -93,24 +109,51 @@ impl GossipSupport {
|
||||
}
|
||||
}
|
||||
|
||||
async fn determine_relevant_validators(
|
||||
ctx: &mut impl SubsystemContext,
|
||||
async fn determine_relevant_authorities<Client>(
|
||||
client: Arc<Client>,
|
||||
relay_parent: Hash,
|
||||
_session: SessionIndex,
|
||||
) -> Result<Vec<ValidatorId>, util::Error> {
|
||||
let validators = util::request_validators_ctx(relay_parent, ctx).await?.await??;
|
||||
Ok(validators)
|
||||
) -> Result<Vec<AuthorityDiscoveryId>, util::Error>
|
||||
where
|
||||
Client: ProvideRuntimeApi<Block>,
|
||||
Client::Api: AuthorityDiscoveryApi<Block>,
|
||||
{
|
||||
let api = client.runtime_api();
|
||||
let result = api.authorities(&BlockId::Hash(relay_parent))
|
||||
.map_err(|e| util::Error::RuntimeApi(format!("{:?}", e).into()));
|
||||
result
|
||||
}
|
||||
|
||||
/// Return an error if we're not a validator in the given set (do not have keys).
|
||||
async fn ensure_i_am_an_authority(
|
||||
keystore: &SyncCryptoStorePtr,
|
||||
authorities: &[AuthorityDiscoveryId],
|
||||
) -> Result<(), util::Error> {
|
||||
for v in authorities {
|
||||
if CryptoStore::has_keys(&**keystore, &[(v.to_raw_vec(), AuthorityDiscoveryId::ID)])
|
||||
.await
|
||||
{
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
Err(util::Error::NotAValidator)
|
||||
}
|
||||
|
||||
|
||||
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<Client>(
|
||||
&mut self,
|
||||
ctx: &mut impl SubsystemContext,
|
||||
client: Arc<Client>,
|
||||
keystore: &SyncCryptoStorePtr,
|
||||
leaves: impl Iterator<Item = Hash>,
|
||||
) -> Result<(), util::Error> {
|
||||
) -> Result<(), util::Error>
|
||||
where
|
||||
Client: ProvideRuntimeApi<Block>,
|
||||
Client::Api: AuthorityDiscoveryApi<Block>,
|
||||
{
|
||||
for leaf in leaves {
|
||||
let current_index = util::request_session_index_for_child_ctx(leaf, ctx).await?.await??;
|
||||
let maybe_new_session = match self.last_session_index {
|
||||
@@ -120,16 +163,15 @@ impl State {
|
||||
|
||||
if let Some((new_session, relay_parent)) = maybe_new_session {
|
||||
tracing::debug!(target: LOG_TARGET, %new_session, "New session detected");
|
||||
let validators = determine_relevant_validators(ctx, relay_parent, new_session).await?;
|
||||
tracing::debug!(target: LOG_TARGET, num = ?validators.len(), "Issuing a connection request");
|
||||
let authorities = determine_relevant_authorities(client.clone(), relay_parent).await?;
|
||||
ensure_i_am_an_authority(keystore, &authorities).await?;
|
||||
tracing::debug!(target: LOG_TARGET, num = ?authorities.len(), "Issuing a connection request");
|
||||
|
||||
let request = validator_discovery::connect_to_validators_in_session(
|
||||
let request = validator_discovery::connect_to_authorities(
|
||||
ctx,
|
||||
relay_parent,
|
||||
validators,
|
||||
authorities,
|
||||
PeerSet::Validation,
|
||||
new_session,
|
||||
).await?;
|
||||
).await;
|
||||
|
||||
self.last_session_index = Some(new_session);
|
||||
self._last_connection_request = Some(request);
|
||||
@@ -140,11 +182,13 @@ impl State {
|
||||
}
|
||||
}
|
||||
|
||||
impl<C> Subsystem<C> for GossipSupport
|
||||
impl<Client, Context> Subsystem<Context> for GossipSupport<Client>
|
||||
where
|
||||
C: SubsystemContext<Message = GossipSupportMessage> + Sync + Send,
|
||||
Context: SubsystemContext<Message = GossipSupportMessage> + Sync + Send,
|
||||
Client: ProvideRuntimeApi<Block> + Send + 'static + Sync,
|
||||
Client::Api: AuthorityDiscoveryApi<Block>,
|
||||
{
|
||||
fn start(self, ctx: C) -> SpawnedSubsystem {
|
||||
fn start(self, ctx: Context) -> SpawnedSubsystem {
|
||||
let future = self.run(ctx)
|
||||
.map(|_| Ok(()))
|
||||
.boxed();
|
||||
|
||||
Reference in New Issue
Block a user