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 34 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
10 changes: 6 additions & 4 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 @@ -41,9 +41,8 @@ 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 sc_network_common::{header_backend::NetworkHeaderBackend, protocol::event::DhtEvent};
use sp_authority_discovery::AuthorityId;
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> + NetworkHeaderBackend<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::{
header_backend::NetworkHeaderBackend,
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_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> + NetworkHeaderBackend<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"
65 changes: 65 additions & 0 deletions client/network/common/src/header_backend.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// This file is part of Substrate.

// Copyright (C) 2022 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use sp_blockchain::{HeaderBackend, Info, Result};
use sp_runtime::{
generic::BlockId,
traits::{Block as BlockT, Header as HeaderT, NumberFor},
};

/// Cut down wrapper trait for [`HeaderBackend`](sp_blockchain::HeaderBackend) with narrower scope.
/// Takes hash instead of [`BlockId`](sp_runtime::generic::BlockId) as parameter.
pub trait NetworkHeaderBackend<Block: BlockT>: Send + Sync {
/// Get block header. Returns `None` if block is not found.
fn header(&self, hash: <Block as BlockT>::Hash) -> Result<Option<<Block as BlockT>::Header>>;

/// Get blockchain info.
fn info(&self) -> Info<Block>;

/// Get block number by hash. Returns `None` if the header is not in the chain.
fn number(
&self,
hash: <Block as BlockT>::Hash,
) -> Result<Option<<<Block as BlockT>::Header as HeaderT>::Number>>;

/// Get block hash by number. Returns `None` if the header is not in the chain.
fn hash(&self, number: NumberFor<Block>) -> Result<Option<<Block as BlockT>::Hash>>;
}

impl<B: BlockT, T> NetworkHeaderBackend<B> for T
where
T: HeaderBackend<B>,
{
fn header(&self, hash: <B as BlockT>::Hash) -> Result<Option<<B as BlockT>::Header>> {
self.header(BlockId::Hash(hash))
}

fn info(&self) -> Info<B> {
self.info()
}

fn number(
&self,
hash: <B as BlockT>::Hash,
) -> Result<Option<<<B as BlockT>::Header as HeaderT>::Number>> {
self.number(hash)
}

fn hash(&self, number: NumberFor<B>) -> Result<Option<<B as BlockT>::Hash>> {
self.hash(number)
}
}
1 change: 1 addition & 0 deletions client/network/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
//! Common data structures of the networking layer.

pub mod config;
pub mod header_backend;
pub mod message;
pub mod protocol;
pub mod request_responses;
Expand Down
Loading