remove connected disconnected state only (#3868)

* remove connected disconnected state from overseer

* foo

* split new partial

* fix

* refactor init code to not require a `OverseerHandle` when we don't have an overseer

* intermediate

* fixins

* X

* fixup

* foo

* fixup

* docs

* conditional

* Update node/service/src/lib.rs

* review by ladi
This commit is contained in:
Bernhard Schuster
2021-09-17 21:39:33 +02:00
committed by GitHub
parent 63a520b056
commit 5f637c510e
17 changed files with 552 additions and 307 deletions
@@ -39,7 +39,7 @@ use super::{HeaderProvider, HeaderProviderProvider};
use consensus_common::{Error as ConsensusError, SelectChain};
use futures::channel::oneshot;
use polkadot_node_subsystem_util::metrics::{self, prometheus};
use polkadot_overseer::{AllMessages, Handle, OverseerHandle};
use polkadot_overseer::{AllMessages, Handle};
use polkadot_primitives::v1::{
Block as PolkadotBlock, BlockNumber, Hash, Header as PolkadotHeader,
};
@@ -109,66 +109,57 @@ impl Metrics {
}
/// A chain-selection implementation which provides safety for relay chains.
pub struct SelectRelayChainWithFallback<B: sc_client_api::Backend<PolkadotBlock>> {
// A fallback to use in case the overseer is disconnected.
//
// This is used on relay chains which have not yet enabled
// parachains as well as situations where the node is offline.
fallback: sc_consensus::LongestChain<B, PolkadotBlock>,
selection: SelectRelayChain<B, Handle>,
pub struct SelectRelayChain<B: sc_client_api::Backend<PolkadotBlock>> {
is_relay_chain: bool,
longest_chain: sc_consensus::LongestChain<B, PolkadotBlock>,
selection: SelectRelayChainInner<B, Handle>,
}
impl<B> Clone for SelectRelayChainWithFallback<B>
impl<B> Clone for SelectRelayChain<B>
where
B: sc_client_api::Backend<PolkadotBlock>,
SelectRelayChain<B, Handle>: Clone,
SelectRelayChainInner<B, Handle>: Clone,
{
fn clone(&self) -> Self {
Self { fallback: self.fallback.clone(), selection: self.selection.clone() }
}
}
impl<B> SelectRelayChainWithFallback<B>
where
B: sc_client_api::Backend<PolkadotBlock> + 'static,
{
/// Create a new [`SelectRelayChainWithFallback`] wrapping the given chain backend
/// and a handle to the overseer.
pub fn new(backend: Arc<B>, overseer: Handle, metrics: Metrics) -> Self {
SelectRelayChainWithFallback {
fallback: sc_consensus::LongestChain::new(backend.clone()),
selection: SelectRelayChain::new(backend, overseer, metrics),
Self {
is_relay_chain: self.is_relay_chain,
longest_chain: self.longest_chain.clone(),
selection: self.selection.clone(),
}
}
}
impl<B> SelectRelayChainWithFallback<B>
impl<B> SelectRelayChain<B>
where
B: sc_client_api::Backend<PolkadotBlock> + 'static,
{
/// Given an overseer handle, this connects the [`SelectRelayChainWithFallback`]'s
/// internal handle and its clones to the same overseer.
pub fn connect_to_overseer(&mut self, handle: OverseerHandle) {
self.selection.overseer.connect_to_overseer(handle);
/// Create a new [`SelectRelayChain`] wrapping the given chain backend
/// and a handle to the overseer.
pub fn new(backend: Arc<B>, is_relay_chain: bool, overseer: Handle, metrics: Metrics) -> Self {
SelectRelayChain {
is_relay_chain,
longest_chain: sc_consensus::LongestChain::new(backend.clone()),
selection: SelectRelayChainInner::new(backend, overseer, metrics),
}
}
}
#[async_trait::async_trait]
impl<B> SelectChain<PolkadotBlock> for SelectRelayChainWithFallback<B>
impl<B> SelectChain<PolkadotBlock> for SelectRelayChain<B>
where
B: sc_client_api::Backend<PolkadotBlock> + 'static,
{
async fn leaves(&self) -> Result<Vec<Hash>, ConsensusError> {
if self.selection.overseer.is_disconnected() {
return self.fallback.leaves().await
if !self.is_relay_chain {
return self.longest_chain.leaves().await
}
self.selection.leaves().await
}
async fn best_chain(&self) -> Result<PolkadotHeader, ConsensusError> {
if self.selection.overseer.is_disconnected() {
return self.fallback.best_chain().await
if !self.is_relay_chain {
return self.longest_chain.best_chain().await
}
self.selection.best_chain().await
}
@@ -179,34 +170,34 @@ where
maybe_max_number: Option<BlockNumber>,
) -> Result<Option<Hash>, ConsensusError> {
let longest_chain_best =
self.fallback.finality_target(target_hash, maybe_max_number).await?;
self.longest_chain.finality_target(target_hash, maybe_max_number).await?;
if self.selection.overseer.is_disconnected() {
if !self.is_relay_chain {
return Ok(longest_chain_best)
}
self.selection
.finality_target_with_fallback(target_hash, longest_chain_best, maybe_max_number)
.finality_target_with_longest_chain(target_hash, longest_chain_best, maybe_max_number)
.await
}
}
/// A chain-selection implementation which provides safety for relay chains
/// but does not handle situations where the overseer is not yet connected.
pub struct SelectRelayChain<B, OH> {
pub struct SelectRelayChainInner<B, OH> {
backend: Arc<B>,
overseer: OH,
metrics: Metrics,
}
impl<B, OH> SelectRelayChain<B, OH>
impl<B, OH> SelectRelayChainInner<B, OH>
where
B: HeaderProviderProvider<PolkadotBlock>,
OH: OverseerHandleT,
{
/// Create a new [`SelectRelayChain`] wrapping the given chain backend
/// Create a new [`SelectRelayChainInner`] wrapping the given chain backend
/// and a handle to the overseer.
pub fn new(backend: Arc<B>, overseer: OH, metrics: Metrics) -> Self {
SelectRelayChain { backend, overseer, metrics }
SelectRelayChainInner { backend, overseer, metrics }
}
fn block_header(&self, hash: Hash) -> Result<PolkadotHeader, ConsensusError> {
@@ -234,13 +225,13 @@ where
}
}
impl<B, OH> Clone for SelectRelayChain<B, OH>
impl<B, OH> Clone for SelectRelayChainInner<B, OH>
where
B: HeaderProviderProvider<PolkadotBlock> + Send + Sync,
OH: OverseerHandleT,
{
fn clone(&self) -> Self {
SelectRelayChain {
SelectRelayChainInner {
backend: self.backend.clone(),
overseer: self.overseer.clone(),
metrics: self.metrics.clone(),
@@ -273,7 +264,7 @@ impl OverseerHandleT for Handle {
}
}
impl<B, OH> SelectRelayChain<B, OH>
impl<B, OH> SelectRelayChainInner<B, OH>
where
B: HeaderProviderProvider<PolkadotBlock>,
OH: OverseerHandleT,
@@ -317,7 +308,7 @@ where
///
/// It will also constrain the chain to only chains which are fully
/// approved, and chains which contain no disputes.
pub(crate) async fn finality_target_with_fallback(
pub(crate) async fn finality_target_with_longest_chain(
&self,
target_hash: Hash,
best_leaf: Option<Hash>,