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

Simplify trait bounds in network to prepare for collator-rpc #12082

Merged
merged 37 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
ceb706e
Hack towards PoC
skunert Mar 15, 2022
b2e5b5d
Abstract away runtime requirement
skunert Mar 15, 2022
97264ff
blockchainevents
skunert Mar 24, 2022
21ead6f
Remove bitswap
skunert Apr 5, 2022
bacd2b4
Merge branch 'master' into collator-rpc-poc
Apr 8, 2022
e74cc52
Remove unused sync more
skunert Apr 25, 2022
22ec4b9
Merge branch 'cumulus-2022-04-25' into collator-rpc-poc
skunert Apr 25, 2022
5ef24b8
Merge branch 'cumulus-2022-05-11' into collator-rpc-poc
skunert May 11, 2022
b0a149e
Remove unused features in network
skunert May 27, 2022
a1b1244
Re-enable bitswap change
skunert May 27, 2022
ebd73bd
Merge branch 'cumulus-2022-05-30' into collator-rpc-poc
skunert May 30, 2022
a2f2c60
Remove `Chain` trait bound
skunert Jun 1, 2022
44da032
Reimplement blockchain-rpc-events
skunert Jun 20, 2022
acf4446
Merge branch 'cumulus-2022-06-20' into collator-rpc-poc
skunert Jun 20, 2022
63581d0
Move network to cumulus
skunert Jun 22, 2022
ab192df
Make AuthorityDiscovery async
skunert Jun 23, 2022
9aae7e0
Merge branch 'cumulus-2022-07-11' into collator-rpc-poc
skunert Jul 11, 2022
f52fa93
Merge branch 'cumulus-2022-07-26' into collator-rpc-poc
skunert Jul 26, 2022
8b7820a
Merge branch 'cumulus-2022-08-03' into collator-rpc-poc
skunert Aug 3, 2022
9782ad4
Remove `ProofProvider` requirement from network behaviour
skunert Aug 5, 2022
f02bd87
Extract bitswap
skunert Aug 8, 2022
1ee5965
Merge branch 'cumulus-2022-08-11' into collator-rpc-poc
skunert Aug 11, 2022
6e0645c
Adjustments after merge
skunert Aug 11, 2022
ddc8ef9
Remove HeaderMetadata trait from network
skunert Aug 17, 2022
054c327
Introduce NetworkHeaderBackend
skunert Aug 18, 2022
a7213c2
Add comments
skunert Aug 19, 2022
e040f2f
Merge commit 'b7d2cb5c2dbfbc49cb3c06f6198daa60b04f16c0' into collator…
skunert Aug 22, 2022
7165e1a
Improve comments
skunert Aug 22, 2022
25067f6
Move NetworkHeaderBackend to new module
skunert Aug 22, 2022
073e40f
Improve naming, remove redundand send + sync
skunert Aug 22, 2022
a240068
Clean up generics
skunert Aug 22, 2022
25578ec
Fix CI
skunert Aug 23, 2022
f3663f3
Merge branch 'master' into collator-rpc-poc
skunert Aug 24, 2022
0459dd1
Improve comment and readability
skunert Aug 25, 2022
f0f56ec
Remove NetworkHeaderBackend
skunert Aug 26, 2022
0659230
Merge branch 'master' into collator-rpc-poc
skunert Aug 29, 2022
b4b49a6
Fix Cargo.lock
skunert Aug 29, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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