Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
rename SelectRelayChainWFallback -> SelectRelayChain
Browse files Browse the repository at this point in the history
  • Loading branch information
drahnr committed Sep 20, 2021
1 parent f9c563e commit 291d64b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 33 deletions.
4 changes: 2 additions & 2 deletions node/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ fn jaeger_launch_collector_with_agent(
}

#[cfg(feature = "full-node")]
type FullSelectChain = relay_chain_selection::SelectRelayChainWithFallback<FullBackend>;
type FullSelectChain = relay_chain_selection::SelectRelayChain<FullBackend>;
#[cfg(feature = "full-node")]
type FullGrandpaBlockImport<RuntimeApi, ExecutorDispatch> = grandpa::GrandpaBlockImport<
FullBackend,
Expand Down Expand Up @@ -400,7 +400,7 @@ where

jaeger_launch_collector_with_agent(task_manager.spawn_handle(), &*config, jaeger_agent)?;

let select_chain = relay_chain_selection::SelectRelayChainWithFallback::new(
let select_chain = relay_chain_selection::SelectRelayChain::new(
backend.clone(),
Handle::new_disconnected(),
polkadot_node_subsystem_util::metrics::Metrics::register(config.prometheus_registry())?,
Expand Down
56 changes: 26 additions & 30 deletions node/service/src/relay_chain_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,66 +109,62 @@ 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>> {
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() }
Self { 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,
{
/// Create a new [`SelectRelayChainWithFallback`] wrapping the given chain backend
/// Create a new [`SelectRelayChain`] 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),
SelectRelayChain {
longest_chain: sc_consensus::LongestChain::new(backend.clone()),
selection: SelectRelayChainInner::new(backend, overseer, metrics),
}
}
}

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
/// Given an overseer handle, this connects the [`SelectRelayChain`]'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);
}
}

#[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
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
return self.longest_chain.best_chain().await
}
self.selection.best_chain().await
}
Expand All @@ -179,34 +175,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() {
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> {
Expand Down Expand Up @@ -234,13 +230,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(),
Expand Down Expand Up @@ -273,7 +269,7 @@ impl OverseerHandleT for Handle {
}
}

impl<B, OH> SelectRelayChain<B, OH>
impl<B, OH> SelectRelayChainInner<B, OH>
where
B: HeaderProviderProvider<PolkadotBlock>,
OH: OverseerHandleT,
Expand Down Expand Up @@ -317,7 +313,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>,
Expand Down
2 changes: 1 addition & 1 deletion node/service/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn test_harness<T: Future<Output = VirtualOverseer>>(

let (finality_target_tx, finality_target_rx) = oneshot::channel::<Option<Hash>>();

let select_relay_chain = SelectRelayChain::<TestChainStorage, TestSubsystemSender>::new(
let select_relay_chain = SelectRelayChainInner::<TestChainStorage, TestSubsystemSender>::new(
Arc::new(case_vars.chain.clone()),
context.sender().clone(),
Default::default(),
Expand Down

0 comments on commit 291d64b

Please sign in to comment.