Skip to content

Commit

Permalink
Simplify trait bounds in network to prepare for collator-rpc (parityt…
Browse files Browse the repository at this point in the history
…ech#12082)

* Hack towards PoC

* Abstract away runtime requirement

* blockchainevents

* Remove bitswap

* Remove unused sync more

* Remove unused features in network

* Re-enable bitswap change

* Remove `Chain` trait bound

* Reimplement blockchain-rpc-events

* Move network to cumulus

* Make AuthorityDiscovery async

* Remove `ProofProvider` requirement from network behaviour

* Extract bitswap

* Adjustments after merge

* Remove HeaderMetadata trait from network

* Introduce NetworkHeaderBackend

* Add comments

* Improve comments

* Move NetworkHeaderBackend to new module

* Improve naming, remove redundand send + sync

* Clean up generics

* Fix CI

* Improve comment and readability

* Remove NetworkHeaderBackend

* Fix Cargo.lock

Co-authored-by: Sebastian Kunert <[email protected]>
  • Loading branch information
2 people authored and ark0f committed Feb 27, 2023
1 parent c3c2cde commit 72a026b
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 155 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions client/authority-discovery/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ sp-blockchain = { version = "4.0.0-dev", path = "../../primitives/blockchain" }
sp-core = { version = "6.0.0", path = "../../primitives/core" }
sp-keystore = { version = "0.12.0", path = "../../primitives/keystore" }
sp-runtime = { version = "6.0.0", path = "../../primitives/runtime" }
async-trait = "0.1.56"

[dev-dependencies]
quickcheck = { version = "1.0.3", default-features = false }
Expand Down
13 changes: 5 additions & 8 deletions client/authority-discovery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
pub use crate::{
service::Service,
worker::{NetworkProvider, Role, Worker},
worker::{AuthorityDiscovery, NetworkProvider, Role, Worker},
};

use std::{collections::HashSet, sync::Arc, time::Duration};
Expand All @@ -40,10 +40,9 @@ use futures::{
};

use libp2p::{Multiaddr, PeerId};
use sc_client_api::blockchain::HeaderBackend;
use sc_network_common::protocol::event::DhtEvent;
use sp_api::ProvideRuntimeApi;
use sp_authority_discovery::{AuthorityDiscoveryApi, AuthorityId};
use sp_authority_discovery::AuthorityId;
use sp_blockchain::HeaderBackend;
use sp_runtime::traits::Block as BlockT;

mod error;
Expand Down Expand Up @@ -122,8 +121,7 @@ pub fn new_worker_and_service<Client, Network, Block, DhtEventStream>(
where
Block: BlockT + Unpin + 'static,
Network: NetworkProvider,
Client: ProvideRuntimeApi<Block> + Send + Sync + 'static + HeaderBackend<Block>,
<Client as ProvideRuntimeApi<Block>>::Api: AuthorityDiscoveryApi<Block>,
Client: AuthorityDiscovery<Block> + Send + Sync + 'static + HeaderBackend<Block>,
DhtEventStream: Stream<Item = DhtEvent> + Unpin,
{
new_worker_and_service_with_config(
Expand All @@ -150,8 +148,7 @@ pub fn new_worker_and_service_with_config<Client, Network, Block, DhtEventStream
where
Block: BlockT + Unpin + 'static,
Network: NetworkProvider,
Client: ProvideRuntimeApi<Block> + Send + Sync + 'static + HeaderBackend<Block>,
<Client as ProvideRuntimeApi<Block>>::Api: AuthorityDiscoveryApi<Block>,
Client: AuthorityDiscovery<Block> + HeaderBackend<Block> + 'static,
DhtEventStream: Stream<Item = DhtEvent> + Unpin,
{
let (to_worker, from_service) = mpsc::channel(0);
Expand Down
44 changes: 34 additions & 10 deletions client/authority-discovery/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,16 @@ use log::{debug, error, log_enabled};
use prometheus_endpoint::{register, Counter, CounterVec, Gauge, Opts, U64};
use prost::Message;
use rand::{seq::SliceRandom, thread_rng};
use sc_client_api::blockchain::HeaderBackend;
use sc_network_common::{
protocol::event::DhtEvent,
service::{KademliaKey, NetworkDHTProvider, NetworkSigner, NetworkStateInfo, Signature},
};
use sp_api::ProvideRuntimeApi;
use sp_api::{ApiError, ProvideRuntimeApi};
use sp_authority_discovery::{
AuthorityDiscoveryApi, AuthorityId, AuthorityPair, AuthoritySignature,
};
use sp_blockchain::HeaderBackend;

use sp_core::crypto::{key_types, CryptoTypePublicPair, Pair};
use sp_keystore::CryptoStore;
use sp_runtime::{generic::BlockId, traits::Block as BlockT};
Expand Down Expand Up @@ -150,12 +151,35 @@ pub struct Worker<Client, Network, Block, DhtEventStream> {
phantom: PhantomData<Block>,
}

/// Wrapper for [`AuthorityDiscoveryApi`](sp_authority_discovery::AuthorityDiscoveryApi). Can be
/// be implemented by any struct without dependency on the runtime.
#[async_trait::async_trait]
pub trait AuthorityDiscovery<Block: BlockT> {
/// Retrieve authority identifiers of the current and next authority set.
async fn authorities(&self, at: Block::Hash)
-> std::result::Result<Vec<AuthorityId>, ApiError>;
}

#[async_trait::async_trait]
impl<Block, T> AuthorityDiscovery<Block> for T
where
T: ProvideRuntimeApi<Block> + Send + Sync,
T::Api: AuthorityDiscoveryApi<Block>,
Block: BlockT,
{
async fn authorities(
&self,
at: Block::Hash,
) -> std::result::Result<Vec<AuthorityId>, ApiError> {
self.runtime_api().authorities(&BlockId::Hash(at))
}
}

impl<Client, Network, Block, DhtEventStream> Worker<Client, Network, Block, DhtEventStream>
where
Block: BlockT + Unpin + 'static,
Network: NetworkProvider,
Client: ProvideRuntimeApi<Block> + Send + Sync + 'static + HeaderBackend<Block>,
<Client as ProvideRuntimeApi<Block>>::Api: AuthorityDiscoveryApi<Block>,
Client: AuthorityDiscovery<Block> + HeaderBackend<Block> + 'static,
DhtEventStream: Stream<Item = DhtEvent> + Unpin,
{
/// Construct a [`Worker`].
Expand Down Expand Up @@ -354,7 +378,7 @@ where
}

async fn refill_pending_lookups_queue(&mut self) -> Result<()> {
let id = BlockId::hash(self.client.info().best_hash);
let best_hash = self.client.info().best_hash;

let local_keys = match &self.role {
Role::PublishAndDiscover(key_store) => key_store
Expand All @@ -367,8 +391,8 @@ where

let mut authorities = self
.client
.runtime_api()
.authorities(&id)
.authorities(best_hash)
.await
.map_err(|e| Error::CallingRuntime(e.into()))?
.into_iter()
.filter(|id| !local_keys.contains(id.as_ref()))
Expand Down Expand Up @@ -574,10 +598,10 @@ where
.into_iter()
.collect::<HashSet<_>>();

let id = BlockId::hash(client.info().best_hash);
let best_hash = client.info().best_hash;
let authorities = client
.runtime_api()
.authorities(&id)
.authorities(best_hash)
.await
.map_err(|e| Error::CallingRuntime(e.into()))?
.into_iter()
.map(Into::into)
Expand Down
1 change: 1 addition & 0 deletions client/authority-discovery/src/worker/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use futures::{
use libp2p::{core::multiaddr, identity::Keypair, PeerId};
use prometheus_endpoint::prometheus::default_registry;

use sc_client_api::HeaderBackend;
use sc_network_common::service::{KademliaKey, Signature, SigningError};
use sp_api::{ApiRef, ProvideRuntimeApi};
use sp_keystore::{testing::KeyStore, CryptoStore};
Expand Down
1 change: 1 addition & 0 deletions client/network/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ serde = { version = "1.0.136", features = ["derive"] }
sp-consensus = { version = "0.10.0-dev", path = "../../../primitives/consensus/common" }
sp-finality-grandpa = { version = "4.0.0-dev", path = "../../../primitives/finality-grandpa" }
sp-runtime = { version = "6.0.0", path = "../../../primitives/runtime" }
sp-blockchain = { version = "4.0.0-dev", path = "../../../primitives/blockchain" }
thiserror = "1.0"
71 changes: 11 additions & 60 deletions client/network/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@ use libp2p::{
};
use log::debug;

use sc_client_api::{BlockBackend, ProofProvider};
use sc_consensus::import_queue::{IncomingBlock, Origin};
use sc_network_common::{
config::ProtocolId,
protocol::event::{DhtEvent, ObservedRole},
request_responses::{IfDisconnected, ProtocolConfig, RequestFailure},
};
use sc_peerset::PeersetHandle;
use sp_blockchain::{HeaderBackend, HeaderMetadata};
use sp_blockchain::HeaderBackend;
use sp_consensus::BlockOrigin;
use sp_runtime::{
traits::{Block as BlockT, NumberFor},
Expand All @@ -69,13 +68,7 @@ pub use crate::request_responses::{InboundFailure, OutboundFailure, RequestId, R
pub struct Behaviour<B, Client>
where
B: BlockT,
Client: HeaderBackend<B>
+ BlockBackend<B>
+ HeaderMetadata<B, Error = sp_blockchain::Error>
+ ProofProvider<B>
+ Send
+ Sync
+ 'static,
Client: HeaderBackend<B> + 'static,
{
/// All the substrate-specific protocols.
substrate: Protocol<B, Client>,
Expand All @@ -85,7 +78,7 @@ where
/// Discovers nodes of the network.
discovery: DiscoveryBehaviour,
/// Bitswap server for blockchain data.
bitswap: Toggle<Bitswap<B, Client>>,
bitswap: Toggle<Bitswap<B>>,
/// Generic request-response protocols.
request_responses: request_responses::RequestResponsesBehaviour,

Expand Down Expand Up @@ -208,13 +201,7 @@ pub enum BehaviourOut<B: BlockT> {
impl<B, Client> Behaviour<B, Client>
where
B: BlockT,
Client: HeaderBackend<B>
+ BlockBackend<B>
+ HeaderMetadata<B, Error = sp_blockchain::Error>
+ ProofProvider<B>
+ Send
+ Sync
+ 'static,
Client: HeaderBackend<B> + 'static,
{
/// Builds a new `Behaviour`.
pub fn new(
Expand All @@ -225,7 +212,7 @@ where
block_request_protocol_config: ProtocolConfig,
state_request_protocol_config: ProtocolConfig,
warp_sync_protocol_config: Option<ProtocolConfig>,
bitswap: Option<Bitswap<B, Client>>,
bitswap: Option<Bitswap<B>>,
light_client_request_protocol_config: ProtocolConfig,
// All remaining request protocol configs.
mut request_response_protocols: Vec<ProtocolConfig>,
Expand Down Expand Up @@ -352,13 +339,7 @@ fn reported_roles_to_observed_role(roles: Roles) -> ObservedRole {
impl<B, Client> NetworkBehaviourEventProcess<void::Void> for Behaviour<B, Client>
where
B: BlockT,
Client: HeaderBackend<B>
+ BlockBackend<B>
+ HeaderMetadata<B, Error = sp_blockchain::Error>
+ ProofProvider<B>
+ Send
+ Sync
+ 'static,
Client: HeaderBackend<B> + 'static,
{
fn inject_event(&mut self, event: void::Void) {
void::unreachable(event)
Expand All @@ -368,13 +349,7 @@ where
impl<B, Client> NetworkBehaviourEventProcess<CustomMessageOutcome<B>> for Behaviour<B, Client>
where
B: BlockT,
Client: HeaderBackend<B>
+ BlockBackend<B>
+ HeaderMetadata<B, Error = sp_blockchain::Error>
+ ProofProvider<B>
+ Send
+ Sync
+ 'static,
Client: HeaderBackend<B> + 'static,
{
fn inject_event(&mut self, event: CustomMessageOutcome<B>) {
match event {
Expand Down Expand Up @@ -483,13 +458,7 @@ where
impl<B, Client> NetworkBehaviourEventProcess<request_responses::Event> for Behaviour<B, Client>
where
B: BlockT,
Client: HeaderBackend<B>
+ BlockBackend<B>
+ HeaderMetadata<B, Error = sp_blockchain::Error>
+ ProofProvider<B>
+ Send
+ Sync
+ 'static,
Client: HeaderBackend<B> + 'static,
{
fn inject_event(&mut self, event: request_responses::Event) {
match event {
Expand All @@ -515,13 +484,7 @@ where
impl<B, Client> NetworkBehaviourEventProcess<peer_info::PeerInfoEvent> for Behaviour<B, Client>
where
B: BlockT,
Client: HeaderBackend<B>
+ BlockBackend<B>
+ HeaderMetadata<B, Error = sp_blockchain::Error>
+ ProofProvider<B>
+ Send
+ Sync
+ 'static,
Client: HeaderBackend<B> + 'static,
{
fn inject_event(&mut self, event: peer_info::PeerInfoEvent) {
let peer_info::PeerInfoEvent::Identified {
Expand All @@ -548,13 +511,7 @@ where
impl<B, Client> NetworkBehaviourEventProcess<DiscoveryOut> for Behaviour<B, Client>
where
B: BlockT,
Client: HeaderBackend<B>
+ BlockBackend<B>
+ HeaderMetadata<B, Error = sp_blockchain::Error>
+ ProofProvider<B>
+ Send
+ Sync
+ 'static,
Client: HeaderBackend<B> + 'static,
{
fn inject_event(&mut self, out: DiscoveryOut) {
match out {
Expand Down Expand Up @@ -592,13 +549,7 @@ where
impl<B, Client> Behaviour<B, Client>
where
B: BlockT,
Client: HeaderBackend<B>
+ BlockBackend<B>
+ HeaderMetadata<B, Error = sp_blockchain::Error>
+ ProofProvider<B>
+ Send
+ Sync
+ 'static,
Client: HeaderBackend<B> + 'static,
{
fn poll(
&mut self,
Expand Down
Loading

0 comments on commit 72a026b

Please sign in to comment.