mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 14:37:57 +00:00
validator-discovery: basic retrying logic (#3059)
* validator_discovery: less flexible, but simpler design * fix test * remove unused struct * smol optimization * validator_discovery: basic retrying logic * add a test * add more tests * update the guide * more test logic * Require at least 2/3 connectivity. * Fix test. * Update node/network/gossip-support/src/lib.rs Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com> * Update node/network/gossip-support/src/lib.rs Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com> Co-authored-by: Robert Klotzner <robert.klotzner@gmx.at> Co-authored-by: Robert Klotzner <eskimor@users.noreply.github.com> Co-authored-by: André Silva <123550+andresilva@users.noreply.github.com>
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 _;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
use futures::{channel::oneshot, FutureExt as _};
|
||||
use polkadot_node_subsystem::{
|
||||
messages::{
|
||||
AllMessages, GossipSupportMessage, NetworkBridgeMessage,
|
||||
@@ -44,6 +47,7 @@ pub struct GossipSupport {
|
||||
#[derive(Default)]
|
||||
struct State {
|
||||
last_session_index: Option<SessionIndex>,
|
||||
force_request: bool,
|
||||
}
|
||||
|
||||
impl GossipSupport {
|
||||
@@ -54,12 +58,18 @@ impl GossipSupport {
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self, ctx), fields(subsystem = LOG_TARGET))]
|
||||
async fn run<Context>(self, mut ctx: Context)
|
||||
async fn run<Context>(self, ctx: Context)
|
||||
where
|
||||
Context: SubsystemContext<Message = GossipSupportMessage>,
|
||||
{
|
||||
let mut state = State::default();
|
||||
self.run_inner(ctx, &mut state).await;
|
||||
}
|
||||
|
||||
async fn run_inner<Context>(self, mut ctx: Context, state: &mut State)
|
||||
where
|
||||
Context: SubsystemContext<Message = GossipSupportMessage>,
|
||||
{
|
||||
let Self { keystore } = self;
|
||||
loop {
|
||||
let message = match ctx.recv().await {
|
||||
@@ -128,13 +138,16 @@ pub async fn connect_to_authorities(
|
||||
ctx: &mut impl SubsystemContext,
|
||||
validator_ids: Vec<AuthorityDiscoveryId>,
|
||||
peer_set: PeerSet,
|
||||
) {
|
||||
) -> oneshot::Receiver<usize> {
|
||||
let (failed, failed_rx) = oneshot::channel();
|
||||
ctx.send_message(AllMessages::NetworkBridge(
|
||||
NetworkBridgeMessage::ConnectToValidators {
|
||||
validator_ids,
|
||||
peer_set,
|
||||
failed,
|
||||
}
|
||||
)).await;
|
||||
failed_rx
|
||||
}
|
||||
|
||||
impl State {
|
||||
@@ -150,7 +163,7 @@ impl State {
|
||||
for leaf in leaves {
|
||||
let current_index = util::request_session_index_for_child(leaf, ctx.sender()).await.await??;
|
||||
let maybe_new_session = match self.last_session_index {
|
||||
Some(i) if i >= current_index => None,
|
||||
Some(i) if current_index <= i && !self.force_request => None,
|
||||
_ => Some((current_index, leaf)),
|
||||
};
|
||||
|
||||
@@ -158,15 +171,22 @@ impl State {
|
||||
tracing::debug!(target: LOG_TARGET, %new_session, "New session detected");
|
||||
let authorities = determine_relevant_authorities(ctx, relay_parent).await?;
|
||||
ensure_i_am_an_authority(keystore, &authorities).await?;
|
||||
tracing::debug!(target: LOG_TARGET, num = ?authorities.len(), "Issuing a connection request");
|
||||
let num = authorities.len();
|
||||
tracing::debug!(target: LOG_TARGET, %num, "Issuing a connection request");
|
||||
|
||||
connect_to_authorities(
|
||||
let failures = connect_to_authorities(
|
||||
ctx,
|
||||
authorities,
|
||||
PeerSet::Validation,
|
||||
).await;
|
||||
|
||||
// we await for the request to be processed
|
||||
// this is fine, it should take much less time than one session
|
||||
let failures = failures.await.unwrap_or(num);
|
||||
|
||||
self.last_session_index = Some(new_session);
|
||||
// issue another request if at least a third of the authorities were not resolved
|
||||
self.force_request = failures >= num / 3;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,319 @@
|
||||
// Copyright 2021 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Polkadot.
|
||||
|
||||
// Polkadot is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Polkadot is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Unit tests for Gossip Support Subsystem.
|
||||
|
||||
use super::*;
|
||||
use polkadot_node_subsystem::{
|
||||
jaeger, ActivatedLeaf,
|
||||
messages::{RuntimeApiMessage, RuntimeApiRequest},
|
||||
};
|
||||
use polkadot_node_subsystem_test_helpers as test_helpers;
|
||||
use polkadot_node_subsystem_util::TimeoutExt as _;
|
||||
use sc_keystore::LocalKeystore;
|
||||
use sp_keyring::Sr25519Keyring;
|
||||
use sp_keystore::SyncCryptoStore;
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use assert_matches::assert_matches;
|
||||
use futures::{Future, executor, future};
|
||||
|
||||
type VirtualOverseer = test_helpers::TestSubsystemContextHandle<GossipSupportMessage>;
|
||||
|
||||
fn test_harness<T: Future<Output = VirtualOverseer>>(
|
||||
mut state: State,
|
||||
test_fn: impl FnOnce(VirtualOverseer) -> T,
|
||||
) -> State {
|
||||
let pool = sp_core::testing::TaskExecutor::new();
|
||||
let (context, virtual_overseer) = test_helpers::make_subsystem_context(pool.clone());
|
||||
|
||||
let keystore = make_ferdie_keystore();
|
||||
let subsystem = GossipSupport::new(keystore);
|
||||
{
|
||||
let subsystem = subsystem.run_inner(context, &mut state);
|
||||
|
||||
let test_fut = test_fn(virtual_overseer);
|
||||
|
||||
futures::pin_mut!(test_fut);
|
||||
futures::pin_mut!(subsystem);
|
||||
|
||||
executor::block_on(future::join(async move {
|
||||
let mut overseer = test_fut.await;
|
||||
overseer
|
||||
.send(FromOverseer::Signal(OverseerSignal::Conclude))
|
||||
.timeout(TIMEOUT)
|
||||
.await
|
||||
.expect("Conclude send timeout");
|
||||
}, subsystem));
|
||||
}
|
||||
|
||||
state
|
||||
}
|
||||
|
||||
const TIMEOUT: Duration = Duration::from_millis(100);
|
||||
|
||||
async fn overseer_signal_active_leaves(
|
||||
overseer: &mut VirtualOverseer,
|
||||
leaf: Hash,
|
||||
) {
|
||||
let leaf = ActivatedLeaf {
|
||||
hash: leaf,
|
||||
number: 0xdeadcafe,
|
||||
span: Arc::new(jaeger::Span::Disabled),
|
||||
};
|
||||
overseer
|
||||
.send(FromOverseer::Signal(OverseerSignal::ActiveLeaves(ActiveLeavesUpdate::start_work(leaf))))
|
||||
.timeout(TIMEOUT)
|
||||
.await
|
||||
.expect("signal send timeout");
|
||||
}
|
||||
|
||||
async fn overseer_recv(
|
||||
overseer: &mut VirtualOverseer,
|
||||
) -> AllMessages {
|
||||
let msg = overseer
|
||||
.recv()
|
||||
.timeout(TIMEOUT)
|
||||
.await
|
||||
.expect("msg recv timeout");
|
||||
|
||||
msg
|
||||
}
|
||||
|
||||
fn make_ferdie_keystore() -> SyncCryptoStorePtr {
|
||||
let keystore: SyncCryptoStorePtr = Arc::new(LocalKeystore::in_memory());
|
||||
SyncCryptoStore::sr25519_generate_new(
|
||||
&*keystore,
|
||||
AuthorityDiscoveryId::ID,
|
||||
Some(&Sr25519Keyring::Ferdie.to_seed()),
|
||||
)
|
||||
.expect("Insert key into keystore");
|
||||
keystore
|
||||
}
|
||||
|
||||
fn authorities() -> Vec<AuthorityDiscoveryId> {
|
||||
vec![
|
||||
Sr25519Keyring::Alice.public().into(),
|
||||
Sr25519Keyring::Bob.public().into(),
|
||||
Sr25519Keyring::Charlie.public().into(),
|
||||
Sr25519Keyring::Ferdie.public().into(),
|
||||
Sr25519Keyring::Eve.public().into(),
|
||||
Sr25519Keyring::One.public().into(),
|
||||
]
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn issues_a_connection_request_on_new_session() {
|
||||
let hash = Hash::repeat_byte(0xAA);
|
||||
let state = test_harness(State::default(), |mut virtual_overseer| async move {
|
||||
let overseer = &mut virtual_overseer;
|
||||
overseer_signal_active_leaves(overseer, hash).await;
|
||||
assert_matches!(
|
||||
overseer_recv(overseer).await,
|
||||
AllMessages::RuntimeApi(RuntimeApiMessage::Request(
|
||||
relay_parent,
|
||||
RuntimeApiRequest::SessionIndexForChild(tx),
|
||||
)) => {
|
||||
assert_eq!(relay_parent, hash);
|
||||
tx.send(Ok(1)).unwrap();
|
||||
}
|
||||
);
|
||||
assert_matches!(
|
||||
overseer_recv(overseer).await,
|
||||
AllMessages::RuntimeApi(RuntimeApiMessage::Request(
|
||||
relay_parent,
|
||||
RuntimeApiRequest::Authorities(tx),
|
||||
)) => {
|
||||
assert_eq!(relay_parent, hash);
|
||||
tx.send(Ok(authorities())).unwrap();
|
||||
}
|
||||
);
|
||||
|
||||
assert_matches!(
|
||||
overseer_recv(overseer).await,
|
||||
AllMessages::NetworkBridge(NetworkBridgeMessage::ConnectToValidators {
|
||||
validator_ids,
|
||||
peer_set,
|
||||
failed,
|
||||
}) => {
|
||||
assert_eq!(validator_ids, authorities());
|
||||
assert_eq!(peer_set, PeerSet::Validation);
|
||||
failed.send(0).unwrap();
|
||||
}
|
||||
);
|
||||
|
||||
virtual_overseer
|
||||
});
|
||||
|
||||
assert_eq!(state.last_session_index, Some(1));
|
||||
assert!(!state.force_request);
|
||||
|
||||
// does not issue on the same session
|
||||
let hash = Hash::repeat_byte(0xBB);
|
||||
let state = test_harness(state, |mut virtual_overseer| async move {
|
||||
let overseer = &mut virtual_overseer;
|
||||
overseer_signal_active_leaves(overseer, hash).await;
|
||||
assert_matches!(
|
||||
overseer_recv(overseer).await,
|
||||
AllMessages::RuntimeApi(RuntimeApiMessage::Request(
|
||||
relay_parent,
|
||||
RuntimeApiRequest::SessionIndexForChild(tx),
|
||||
)) => {
|
||||
assert_eq!(relay_parent, hash);
|
||||
tx.send(Ok(1)).unwrap();
|
||||
}
|
||||
);
|
||||
virtual_overseer
|
||||
});
|
||||
|
||||
assert_eq!(state.last_session_index, Some(1));
|
||||
assert!(!state.force_request);
|
||||
|
||||
// does on the new one
|
||||
let hash = Hash::repeat_byte(0xCC);
|
||||
let state = test_harness(state, |mut virtual_overseer| async move {
|
||||
let overseer = &mut virtual_overseer;
|
||||
overseer_signal_active_leaves(overseer, hash).await;
|
||||
assert_matches!(
|
||||
overseer_recv(overseer).await,
|
||||
AllMessages::RuntimeApi(RuntimeApiMessage::Request(
|
||||
relay_parent,
|
||||
RuntimeApiRequest::SessionIndexForChild(tx),
|
||||
)) => {
|
||||
assert_eq!(relay_parent, hash);
|
||||
tx.send(Ok(2)).unwrap();
|
||||
}
|
||||
);
|
||||
assert_matches!(
|
||||
overseer_recv(overseer).await,
|
||||
AllMessages::RuntimeApi(RuntimeApiMessage::Request(
|
||||
relay_parent,
|
||||
RuntimeApiRequest::Authorities(tx),
|
||||
)) => {
|
||||
assert_eq!(relay_parent, hash);
|
||||
tx.send(Ok(authorities())).unwrap();
|
||||
}
|
||||
);
|
||||
|
||||
assert_matches!(
|
||||
overseer_recv(overseer).await,
|
||||
AllMessages::NetworkBridge(NetworkBridgeMessage::ConnectToValidators {
|
||||
validator_ids,
|
||||
peer_set,
|
||||
failed,
|
||||
}) => {
|
||||
assert_eq!(validator_ids, authorities());
|
||||
assert_eq!(peer_set, PeerSet::Validation);
|
||||
failed.send(0).unwrap();
|
||||
}
|
||||
);
|
||||
|
||||
virtual_overseer
|
||||
});
|
||||
assert_eq!(state.last_session_index, Some(2));
|
||||
assert!(!state.force_request);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn issues_a_connection_request_when_last_request_was_mostly_unresolved() {
|
||||
let hash = Hash::repeat_byte(0xAA);
|
||||
let state = test_harness(State::default(), |mut virtual_overseer| async move {
|
||||
let overseer = &mut virtual_overseer;
|
||||
overseer_signal_active_leaves(overseer, hash).await;
|
||||
assert_matches!(
|
||||
overseer_recv(overseer).await,
|
||||
AllMessages::RuntimeApi(RuntimeApiMessage::Request(
|
||||
relay_parent,
|
||||
RuntimeApiRequest::SessionIndexForChild(tx),
|
||||
)) => {
|
||||
assert_eq!(relay_parent, hash);
|
||||
tx.send(Ok(1)).unwrap();
|
||||
}
|
||||
);
|
||||
assert_matches!(
|
||||
overseer_recv(overseer).await,
|
||||
AllMessages::RuntimeApi(RuntimeApiMessage::Request(
|
||||
relay_parent,
|
||||
RuntimeApiRequest::Authorities(tx),
|
||||
)) => {
|
||||
assert_eq!(relay_parent, hash);
|
||||
tx.send(Ok(authorities())).unwrap();
|
||||
}
|
||||
);
|
||||
|
||||
assert_matches!(
|
||||
overseer_recv(overseer).await,
|
||||
AllMessages::NetworkBridge(NetworkBridgeMessage::ConnectToValidators {
|
||||
validator_ids,
|
||||
peer_set,
|
||||
failed,
|
||||
}) => {
|
||||
assert_eq!(validator_ids, authorities());
|
||||
assert_eq!(peer_set, PeerSet::Validation);
|
||||
failed.send(2).unwrap();
|
||||
}
|
||||
);
|
||||
virtual_overseer
|
||||
});
|
||||
|
||||
assert_eq!(state.last_session_index, Some(1));
|
||||
assert!(state.force_request);
|
||||
|
||||
let hash = Hash::repeat_byte(0xBB);
|
||||
let state = test_harness(state, |mut virtual_overseer| async move {
|
||||
let overseer = &mut virtual_overseer;
|
||||
overseer_signal_active_leaves(overseer, hash).await;
|
||||
assert_matches!(
|
||||
overseer_recv(overseer).await,
|
||||
AllMessages::RuntimeApi(RuntimeApiMessage::Request(
|
||||
relay_parent,
|
||||
RuntimeApiRequest::SessionIndexForChild(tx),
|
||||
)) => {
|
||||
assert_eq!(relay_parent, hash);
|
||||
tx.send(Ok(1)).unwrap();
|
||||
}
|
||||
);
|
||||
assert_matches!(
|
||||
overseer_recv(overseer).await,
|
||||
AllMessages::RuntimeApi(RuntimeApiMessage::Request(
|
||||
relay_parent,
|
||||
RuntimeApiRequest::Authorities(tx),
|
||||
)) => {
|
||||
assert_eq!(relay_parent, hash);
|
||||
tx.send(Ok(authorities())).unwrap();
|
||||
}
|
||||
);
|
||||
|
||||
assert_matches!(
|
||||
overseer_recv(overseer).await,
|
||||
AllMessages::NetworkBridge(NetworkBridgeMessage::ConnectToValidators {
|
||||
validator_ids,
|
||||
peer_set,
|
||||
failed,
|
||||
}) => {
|
||||
assert_eq!(validator_ids, authorities());
|
||||
assert_eq!(peer_set, PeerSet::Validation);
|
||||
failed.send(1).unwrap();
|
||||
}
|
||||
);
|
||||
virtual_overseer
|
||||
});
|
||||
|
||||
assert_eq!(state.last_session_index, Some(1));
|
||||
assert!(!state.force_request);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user