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

Commit

Permalink
Fix build.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvc94ch committed Sep 17, 2020
1 parent 6dc3fa1 commit 4396ee2
Show file tree
Hide file tree
Showing 16 changed files with 368 additions and 89 deletions.
339 changes: 283 additions & 56 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions bin/node-template/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ name = "node-template"

[dependencies]
structopt = "0.3.8"
tiny-multihash = "0.4.5"

sc-cli = { version = "0.8.0-rc6", path = "../../../client/cli", features = ["wasmtime"] }
sp-core = { version = "2.0.0-rc6", path = "../../../primitives/core" }
Expand Down
5 changes: 3 additions & 2 deletions bin/node-template/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use sc_executor::native_executor_instance;
pub use sc_executor::NativeExecutor;
use sp_consensus_aura::sr25519::{AuthorityPair as AuraPair};
use sc_finality_grandpa::{FinalityProofProvider as GrandpaFinalityProofProvider, SharedVoterState};
use tiny_multihash::Multihash;

// Our native executor instance.
native_executor_instance!(
Expand Down Expand Up @@ -91,7 +92,7 @@ pub fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
GrandpaFinalityProofProvider::new_for_service(backend.clone(), client.clone());

let (network, network_status_sinks, system_rpc_tx, network_starter) =
sc_service::build_network(sc_service::BuildNetworkParams {
sc_service::build_network::<_, _, _, _, Multihash>(sc_service::BuildNetworkParams {
config: &config,
client: client.clone(),
transaction_pool: transaction_pool.clone(),
Expand Down Expand Up @@ -263,7 +264,7 @@ pub fn new_light(config: Configuration) -> Result<TaskManager, ServiceError> {
GrandpaFinalityProofProvider::new_for_service(backend.clone(), client.clone());

let (network, network_status_sinks, system_rpc_tx, network_starter) =
sc_service::build_network(sc_service::BuildNetworkParams {
sc_service::build_network::<_, _, _, _, Multihash>(sc_service::BuildNetworkParams {
config: &config,
client: client.clone(),
transaction_pool: transaction_pool.clone(),
Expand Down
1 change: 1 addition & 0 deletions bin/node/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ rand = "0.7.2"
structopt = { version = "0.3.8", optional = true }
tracing = "0.1.18"
parking_lot = "0.10.0"
tiny-multihash = "0.4.5"

# primitives
sp-authority-discovery = { version = "2.0.0-rc6", path = "../../../primitives/authority-discovery" }
Expand Down
5 changes: 3 additions & 2 deletions bin/node/cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use futures::prelude::*;
use sc_client_api::{ExecutorProvider, RemoteBackend};
use sp_core::traits::BareCryptoStorePtr;
use node_executor::Executor;
use tiny_multihash::Multihash;

type FullClient = sc_service::TFullClient<Block, RuntimeApi, Executor>;
type FullBackend = sc_service::TFullBackend<Block>;
Expand Down Expand Up @@ -155,7 +156,7 @@ pub struct NewFullBase {
pub task_manager: TaskManager,
pub inherent_data_providers: InherentDataProviders,
pub client: Arc<FullClient>,
pub network: Arc<NetworkService<Block, <Block as BlockT>::Hash>>,
pub network: Arc<NetworkService<Block, <Block as BlockT>::Hash, Multihash>>,
pub network_status_sinks: sc_service::NetworkStatusSinks<Block>,
pub transaction_pool: Arc<sc_transaction_pool::FullPool<Block, FullClient>>,
}
Expand Down Expand Up @@ -351,7 +352,7 @@ pub fn new_full(config: Configuration)

pub fn new_light_base(config: Configuration) -> Result<(
TaskManager, RpcHandlers, Arc<LightClient>,
Arc<NetworkService<Block, <Block as BlockT>::Hash>>,
Arc<NetworkService<Block, <Block as BlockT>::Hash, Multihash>>,
Arc<sc_transaction_pool::LightPool<Block, LightClient, sc_network::config::OnDemand<Block>>>
), ServiceError> {
let (client, backend, keystore, mut task_manager, on_demand) =
Expand Down
5 changes: 4 additions & 1 deletion client/authority-discovery/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use sc_network::{
DhtEvent,
ExHashT,
Multiaddr,
MultihashDigest,
NetworkStateInfo,
PeerId,
};
Expand Down Expand Up @@ -455,6 +456,7 @@ where
"Failed to put hash '{:?}' on Dht.", hash
)
},
Some(_) => {}
None => {
debug!(target: LOG_TARGET, "Dht event stream terminated.");
return Poll::Ready(());
Expand Down Expand Up @@ -707,10 +709,11 @@ pub trait NetworkProvider: NetworkStateInfo {
fn get_value(&self, key: &libp2p::kad::record::Key);
}

impl<B, H> NetworkProvider for sc_network::NetworkService<B, H>
impl<B, H, M> NetworkProvider for sc_network::NetworkService<B, H, M>
where
B: BlockT + 'static,
H: ExHashT,
M: MultihashDigest,
{
fn set_priority_group(
&self,
Expand Down
5 changes: 3 additions & 2 deletions client/finality-grandpa/src/communication/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use std::{pin::Pin, sync::Arc, task::{Context, Poll}};
use sp_core::traits::BareCryptoStorePtr;
use finality_grandpa::Message::{Prevote, Precommit, PrimaryPropose};
use finality_grandpa::{voter, voter_set::VoterSet};
use sc_network::{NetworkService, ReputationChange};
use sc_network::{MultihashDigest, NetworkService, ReputationChange};
use sc_network_gossip::{GossipEngine, Network as GossipNetwork};
use parity_scale_codec::{Encode, Decode};
use sp_runtime::traits::{Block as BlockT, Hash as HashT, Header as HeaderT, NumberFor};
Expand Down Expand Up @@ -152,9 +152,10 @@ pub trait Network<Block: BlockT>: GossipNetwork<Block> + Clone + Send + 'static
fn set_sync_fork_request(&self, peers: Vec<sc_network::PeerId>, hash: Block::Hash, number: NumberFor<Block>);
}

impl<B, H> Network<B> for Arc<NetworkService<B, H>> where
impl<B, H, M> Network<B> for Arc<NetworkService<B, H, M>> where
B: BlockT,
H: sc_network::ExHashT,
M: MultihashDigest,
{
fn set_sync_fork_request(&self, peers: Vec<sc_network::PeerId>, hash: B::Hash, number: NumberFor<B>) {
NetworkService::set_sync_fork_request(self, peers, hash, number)
Expand Down
3 changes: 2 additions & 1 deletion client/network-gossip/src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ impl<B: BlockT> Future for GossipEngine<B> {

this.forwarding_state = ForwardingState::Busy(to_forward.into());
},
Event::Dht(_) => {}
Event::Dht(_) => {},
Event::Bitswap(_) => {},
}
// The network event stream closed. Do the same for [`GossipValidator`].
Poll::Ready(None) => return Poll::Ready(()),
Expand Down
4 changes: 2 additions & 2 deletions client/network-gossip/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub use self::state_machine::TopicNotification;
pub use self::validator::{DiscardAll, MessageIntent, Validator, ValidatorContext, ValidationResult};

use futures::prelude::*;
use sc_network::{Event, ExHashT, NetworkService, PeerId, ReputationChange};
use sc_network::{Event, ExHashT, MultihashDigest, NetworkService, PeerId, ReputationChange};
use sp_runtime::{traits::Block as BlockT, ConsensusEngineId};
use std::{borrow::Cow, pin::Pin, sync::Arc};

Expand Down Expand Up @@ -97,7 +97,7 @@ pub trait Network<B: BlockT> {
fn announce(&self, block: B::Hash, associated_data: Vec<u8>);
}

impl<B: BlockT, H: ExHashT> Network<B> for Arc<NetworkService<B, H>> {
impl<B: BlockT, H: ExHashT, M: MultihashDigest> Network<B> for Arc<NetworkService<B, H, M>> {
fn event_stream(&self) -> Pin<Box<dyn Stream<Item = Event> + Send>> {
Box::pin(NetworkService::event_stream(self, "network-gossip"))
}
Expand Down
3 changes: 3 additions & 0 deletions client/network/test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ substrate-test-runtime = { version = "2.0.0-rc6", path = "../../../test-utils/ru
tempfile = "3.1.0"
sp-tracing = { version = "2.0.0-rc6", path = "../../../primitives/tracing" }
sc-service = { version = "0.8.0-rc6", default-features = false, features = ["test-helpers"], path = "../../service" }
tiny-cid = "0.2.5"
multihash = "0.11.2"
tiny-multihash = "0.4.5"
33 changes: 32 additions & 1 deletion client/network/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@
mod block_import;
#[cfg(test)]
mod sync;
#[cfg(test)]
mod bitswap;

use std::{
borrow::Cow, collections::HashMap, pin::Pin, sync::Arc, marker::PhantomData,
task::{Poll, Context as FutureContext}
};

use libp2p::build_multiaddr;
use log::trace;
use log::{trace, warn};
use sc_network::config::FinalityProofProvider;
use sp_blockchain::{
HeaderBackend, Result as ClientResult,
Expand Down Expand Up @@ -227,6 +229,7 @@ pub struct Peer<D> {
network: NetworkWorker<Block, <Block as BlockT>::Hash>,
imported_blocks_stream: Pin<Box<dyn Stream<Item = BlockImportNotification<Block>> + Send>>,
finality_notification_stream: Pin<Box<dyn Stream<Item = FinalityNotification<Block>> + Send>>,
bitswap_stream: Pin<Box<dyn Stream<Item = sc_network::BitswapEvent> + Send>>,
}

impl<D> Peer<D> {
Expand Down Expand Up @@ -696,6 +699,14 @@ pub trait TestNetFactory: Sized {
let imported_blocks_stream = Box::pin(client.import_notification_stream().fuse());
let finality_notification_stream = Box::pin(client.finality_notification_stream().fuse());

let bitswap_stream = network.service().event_stream("bitswap")
.filter_map(|event| futures::future::ready(if let sc_network::Event::Bitswap(event) = event {
Some(event)
} else {
None
}))
.boxed();

peers.push(Peer {
data,
client: PeersClient::Full(client, backend.clone()),
Expand All @@ -706,6 +717,7 @@ pub trait TestNetFactory: Sized {
block_import,
verifier,
network,
bitswap_stream,
});
});
}
Expand Down Expand Up @@ -775,6 +787,14 @@ pub trait TestNetFactory: Sized {
let imported_blocks_stream = Box::pin(client.import_notification_stream().fuse());
let finality_notification_stream = Box::pin(client.finality_notification_stream().fuse());

let bitswap_stream = network.service().event_stream("bitswap")
.filter_map(|event| futures::future::ready(if let sc_network::Event::Bitswap(event) = event {
Some(event)
} else {
None
}))
.boxed();

peers.push(Peer {
data,
verifier,
Expand All @@ -785,6 +805,7 @@ pub trait TestNetFactory: Sized {
imported_blocks_stream,
finality_notification_stream,
network,
bitswap_stream,
});
});
}
Expand Down Expand Up @@ -890,6 +911,16 @@ pub trait TestNetFactory: Sized {
if let Some(notification) = last {
peer.network.on_block_finalized(notification.hash, notification.header);
}

while let Poll::Ready(Some(event)) = peer.bitswap_stream.as_mut().poll_next(cx) {
if let Some(offchain) = peer.client.offchain_storage() {
if let Err(err) =
sc_service::handle_bitswap_event(peer.network.service(), event, offchain)
{
warn!("{}", err);
}
}
}
}
});
}
Expand Down
7 changes: 4 additions & 3 deletions client/offchain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use threadpool::ThreadPool;
use sp_api::{ApiExt, ProvideRuntimeApi};
use futures::future::Future;
use log::{debug, warn};
use sc_network::{ExHashT, NetworkService, NetworkStateInfo, PeerId};
use sc_network::{ExHashT, MultihashDigest, NetworkService, NetworkStateInfo, PeerId};
use sp_core::{offchain::{self, OffchainStorage}, ExecutionContext, traits::SpawnNamed};
use sp_runtime::{generic::BlockId, traits::{self, Header}};
use futures::{prelude::*, future::ready};
Expand All @@ -58,15 +58,16 @@ pub use sp_offchain::{OffchainWorkerApi, STORAGE_PREFIX};
pub trait NetworkProvider: NetworkStateInfo {
/// Set the authorized peers.
fn set_authorized_peers(&self, peers: HashSet<PeerId>);

/// Set the authorized only flag.
fn set_authorized_only(&self, reserved_only: bool);
}

impl<B, H> NetworkProvider for NetworkService<B, H>
impl<B, H, M> NetworkProvider for NetworkService<B, H, M>
where
B: traits::Block + 'static,
H: ExHashT,
M: MultihashDigest,
{
fn set_authorized_peers(&self, peers: HashSet<PeerId>) {
self.set_authorized_peers(peers)
Expand Down
32 changes: 18 additions & 14 deletions client/service/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use jsonrpc_pubsub::manager::SubscriptionManager;
use sc_keystore::Store as Keystore;
use log::{info, warn};
use sc_network::config::{Role, FinalityProofProvider, OnDemand, BoxFinalityProofRequestBuilder};
use sc_network::NetworkService;
use sc_network::{MultihashDigest, NetworkService};
use parking_lot::RwLock;
use sp_runtime::generic::BlockId;
use sp_runtime::traits::{
Expand Down Expand Up @@ -380,7 +380,7 @@ pub fn new_client<E, Block, RA>(
}

/// Parameters to pass into `build`.
pub struct SpawnTasksParams<'a, TBl: BlockT, TCl, TExPool, TRpc, Backend> {
pub struct SpawnTasksParams<'a, TBl: BlockT, TCl, TExPool, TRpc, Backend, M: MultihashDigest> {
/// The service configuration.
pub config: Configuration,
/// A shared client returned by `new_full_parts`/`new_light_parts`.
Expand All @@ -401,7 +401,7 @@ pub struct SpawnTasksParams<'a, TBl: BlockT, TCl, TExPool, TRpc, Backend> {
/// An optional, shared remote blockchain instance. Used for light clients.
pub remote_blockchain: Option<Arc<dyn RemoteBlockchain<TBl>>>,
/// A shared network instance.
pub network: Arc<NetworkService<TBl, <TBl as BlockT>::Hash>>,
pub network: Arc<NetworkService<TBl, <TBl as BlockT>::Hash, M>>,
/// Sinks to propagate network status updates.
pub network_status_sinks: NetworkStatusSinks<TBl>,
/// A Sender for RPC requests.
Expand All @@ -411,18 +411,19 @@ pub struct SpawnTasksParams<'a, TBl: BlockT, TCl, TExPool, TRpc, Backend> {
}

/// Build a shared offchain workers instance.
pub fn build_offchain_workers<TBl, TBackend, TCl>(
pub fn build_offchain_workers<TBl, TBackend, TCl, M>(
config: &Configuration,
backend: Arc<TBackend>,
spawn_handle: SpawnTaskHandle,
client: Arc<TCl>,
network: Arc<NetworkService<TBl, <TBl as BlockT>::Hash>>,
network: Arc<NetworkService<TBl, <TBl as BlockT>::Hash, M>>,
) -> Option<Arc<sc_offchain::OffchainWorkers<TCl, TBackend::OffchainStorage, TBl>>>
where
TBl: BlockT, TBackend: sc_client_api::Backend<TBl>,
<TBackend as sc_client_api::Backend<TBl>>::OffchainStorage: 'static,
TCl: Send + Sync + ProvideRuntimeApi<TBl> + BlockchainEvents<TBl> + 'static,
<TCl as ProvideRuntimeApi<TBl>>::Api: sc_offchain::OffchainWorkerApi<TBl>,
M: MultihashDigest,
{
let offchain_workers = match backend.offchain_storage() {
Some(db) => {
Expand Down Expand Up @@ -452,8 +453,8 @@ pub fn build_offchain_workers<TBl, TBackend, TCl>(
}

/// Spawn the tasks that are required to run a node.
pub fn spawn_tasks<TBl, TBackend, TExPool, TRpc, TCl>(
params: SpawnTasksParams<TBl, TCl, TExPool, TRpc, TBackend>,
pub fn spawn_tasks<TBl, TBackend, TExPool, TRpc, TCl, M>(
params: SpawnTasksParams<TBl, TCl, TExPool, TRpc, TBackend, M>,
) -> Result<RpcHandlers, Error>
where
TCl: ProvideRuntimeApi<TBl> + HeaderMetadata<TBl, Error=sp_blockchain::Error> + Chain<TBl> +
Expand All @@ -472,7 +473,8 @@ pub fn spawn_tasks<TBl, TBackend, TExPool, TRpc, TCl>(
TBackend: 'static + sc_client_api::backend::Backend<TBl> + Send,
TExPool: MaintainedTransactionPool<Block=TBl, Hash = <TBl as BlockT>::Hash> +
MallocSizeOfWasm + 'static,
TRpc: sc_rpc::RpcExtension<sc_rpc::Metadata>
TRpc: sc_rpc::RpcExtension<sc_rpc::Metadata>,
M: MultihashDigest,
{
let SpawnTasksParams {
mut config,
Expand Down Expand Up @@ -585,13 +587,14 @@ pub fn spawn_tasks<TBl, TBackend, TExPool, TRpc, TCl>(
Ok(rpc_handlers)
}

async fn transaction_notifications<TBl, TExPool>(
async fn transaction_notifications<TBl, TExPool, M>(
transaction_pool: Arc<TExPool>,
network: Arc<NetworkService<TBl, <TBl as BlockT>::Hash>>
network: Arc<NetworkService<TBl, <TBl as BlockT>::Hash, M>>
)
where
TBl: BlockT,
TExPool: MaintainedTransactionPool<Block=TBl, Hash = <TBl as BlockT>::Hash>,
M: MultihashDigest,
{
// transaction notifications
transaction_pool.import_notification_stream()
Expand All @@ -607,11 +610,11 @@ async fn transaction_notifications<TBl, TExPool>(
.await;
}

fn build_telemetry<TBl: BlockT>(
fn build_telemetry<TBl: BlockT, M: MultihashDigest>(
config: &mut Configuration,
endpoints: sc_telemetry::TelemetryEndpoints,
telemetry_connection_sinks: TelemetryConnectionSinks,
network: Arc<NetworkService<TBl, <TBl as BlockT>::Hash>>,
network: Arc<NetworkService<TBl, <TBl as BlockT>::Hash, M>>,
spawn_handle: SpawnTaskHandle,
genesis_hash: <TBl as BlockT>::Hash,
) -> sc_telemetry::Telemetry {
Expand Down Expand Up @@ -771,11 +774,11 @@ pub struct BuildNetworkParams<'a, TBl: BlockT, TExPool, TImpQu, TCl> {
}

/// Build the network service, the network status sinks and an RPC sender.
pub fn build_network<TBl, TExPool, TImpQu, TCl>(
pub fn build_network<TBl, TExPool, TImpQu, TCl, M>(
params: BuildNetworkParams<TBl, TExPool, TImpQu, TCl>
) -> Result<
(
Arc<NetworkService<TBl, <TBl as BlockT>::Hash>>,
Arc<NetworkService<TBl, <TBl as BlockT>::Hash, M>>,
NetworkStatusSinks<TBl>,
TracingUnboundedSender<sc_rpc::system::Request<TBl>>,
NetworkStarter,
Expand All @@ -789,6 +792,7 @@ pub fn build_network<TBl, TExPool, TImpQu, TCl>(
HeaderBackend<TBl> + BlockchainEvents<TBl> + 'static,
TExPool: MaintainedTransactionPool<Block=TBl, Hash = <TBl as BlockT>::Hash> + 'static,
TImpQu: ImportQueue<TBl> + 'static,
M: MultihashDigest,
{
let BuildNetworkParams {
config, client, transaction_pool, spawn_handle, import_queue, on_demand,
Expand Down
Loading

0 comments on commit 4396ee2

Please sign in to comment.