Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 0 additions & 2 deletions client/network/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ use std::{

pub use behaviour::{InboundFailure, OutboundFailure, ResponseFailure};

#[cfg(test)]
mod chainsync_tests;
mod metrics;
mod out_events;
#[cfg(test)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,178 +16,26 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::{config, ChainSyncInterface, NetworkWorker};
use crate::service::tests::TestNetworkBuilder;

use futures::prelude::*;
use libp2p::PeerId;
use sc_block_builder::BlockBuilderProvider;
use sc_client_api::{BlockBackend, HeaderBackend};
use sc_client_api::HeaderBackend;
use sc_consensus::JustificationSyncLink;
use sc_network_common::{
config::{
NonDefaultSetConfig, NonReservedPeerMode, NotificationHandshake, ProtocolId, SetConfig,
TransportConfig,
},
protocol::role::Roles,
service::NetworkSyncForkRequest,
sync::{message::BlockAnnouncesHandshake, ChainSync as ChainSyncT, SyncState, SyncStatus},
};
use sc_network_light::light_client_requests::handler::LightClientRequestHandler;
use sc_network_sync::{
block_request_handler::BlockRequestHandler, mock::MockChainSync,
service::mock::MockChainSyncInterface, state_request_handler::StateRequestHandler,
sync::{SyncState, SyncStatus},
};
use sc_network_sync::{mock::MockChainSync, service::mock::MockChainSyncInterface};
use sp_core::H256;
use sp_runtime::{
generic::BlockId,
traits::{Block as BlockT, Header as _, Zero},
traits::{Block as BlockT, Header as _},
};
use std::{iter, sync::Arc, task::Poll};
use substrate_test_runtime_client::{TestClientBuilder, TestClientBuilderExt as _};

type TestNetworkWorker = NetworkWorker<
substrate_test_runtime_client::runtime::Block,
substrate_test_runtime_client::runtime::Hash,
substrate_test_runtime_client::TestClient,
>;

const BLOCK_ANNOUNCE_PROTO_NAME: &str = "/block-announces";
const PROTOCOL_NAME: &str = "/foo";

fn make_network(
chain_sync: Box<dyn ChainSyncT<substrate_test_runtime_client::runtime::Block>>,
chain_sync_service: Box<dyn ChainSyncInterface<substrate_test_runtime_client::runtime::Block>>,
client: Arc<substrate_test_runtime_client::TestClient>,
) -> (TestNetworkWorker, Arc<substrate_test_runtime_client::TestClient>) {
let network_config = config::NetworkConfiguration {
extra_sets: vec![NonDefaultSetConfig {
notifications_protocol: PROTOCOL_NAME.into(),
fallback_names: Vec::new(),
max_notification_size: 1024 * 1024,
handshake: None,
set_config: Default::default(),
}],
listen_addresses: vec![config::build_multiaddr![Memory(rand::random::<u64>())]],
transport: TransportConfig::MemoryOnly,
..config::NetworkConfiguration::new_local()
};

#[derive(Clone)]
struct PassThroughVerifier(bool);

#[async_trait::async_trait]
impl<B: BlockT> sc_consensus::Verifier<B> for PassThroughVerifier {
async fn verify(
&mut self,
mut block: sc_consensus::BlockImportParams<B, ()>,
) -> Result<
(
sc_consensus::BlockImportParams<B, ()>,
Option<Vec<(sp_blockchain::well_known_cache_keys::Id, Vec<u8>)>>,
),
String,
> {
let maybe_keys = block
.header
.digest()
.log(|l| {
l.try_as_raw(sp_runtime::generic::OpaqueDigestItemId::Consensus(b"aura"))
.or_else(|| {
l.try_as_raw(sp_runtime::generic::OpaqueDigestItemId::Consensus(
b"babe",
))
})
})
.map(|blob| {
vec![(sp_blockchain::well_known_cache_keys::AUTHORITIES, blob.to_vec())]
});

block.finalized = self.0;
block.fork_choice = Some(sc_consensus::ForkChoiceStrategy::LongestChain);
Ok((block, maybe_keys))
}
}

let import_queue = Box::new(sc_consensus::BasicQueue::new(
PassThroughVerifier(false),
Box::new(client.clone()),
None,
&sp_core::testing::TaskExecutor::new(),
None,
));

let protocol_id = ProtocolId::from("/test-protocol-name");

let fork_id = Some(String::from("test-fork-id"));

let block_request_protocol_config = {
let (handler, protocol_config) =
BlockRequestHandler::new(&protocol_id, None, client.clone(), 50);
async_std::task::spawn(handler.run().boxed());
protocol_config
};

let state_request_protocol_config = {
let (handler, protocol_config) =
StateRequestHandler::new(&protocol_id, None, client.clone(), 50);
async_std::task::spawn(handler.run().boxed());
protocol_config
};

let light_client_request_protocol_config = {
let (handler, protocol_config) =
LightClientRequestHandler::new(&protocol_id, None, client.clone());
async_std::task::spawn(handler.run().boxed());
protocol_config
};

let block_announce_config = NonDefaultSetConfig {
notifications_protocol: BLOCK_ANNOUNCE_PROTO_NAME.into(),
fallback_names: vec![],
max_notification_size: 1024 * 1024,
handshake: Some(NotificationHandshake::new(BlockAnnouncesHandshake::<
substrate_test_runtime_client::runtime::Block,
>::build(
Roles::from(&config::Role::Full),
client.info().best_number,
client.info().best_hash,
client
.block_hash(Zero::zero())
.ok()
.flatten()
.expect("Genesis block exists; qed"),
))),
set_config: SetConfig {
in_peers: 0,
out_peers: 0,
reserved_nodes: Vec::new(),
non_reserved_mode: NonReservedPeerMode::Deny,
},
};

let worker = NetworkWorker::new(config::Params {
block_announce_config,
role: config::Role::Full,
executor: None,
network_config,
chain: client.clone(),
protocol_id,
fork_id,
import_queue,
chain_sync,
chain_sync_service,
metrics_registry: None,
block_request_protocol_config,
state_request_protocol_config,
light_client_request_protocol_config,
warp_sync_protocol_config: None,
request_response_protocol_configs: Vec::new(),
})
.unwrap();

(worker, client)
}

fn set_default_expecations_no_peers(
chain_sync: &mut MockChainSync<substrate_test_runtime_client::runtime::Block>,
) {
Expand All @@ -208,8 +56,6 @@ fn set_default_expecations_no_peers(

#[async_std::test]
async fn normal_network_poll_no_peers() {
let client = Arc::new(TestClientBuilder::with_default_backend().build_with_longest_chain().0);

// build `ChainSync` and set default expectations for it
let mut chain_sync =
Box::new(MockChainSync::<substrate_test_runtime_client::runtime::Block>::new());
Expand All @@ -220,20 +66,20 @@ async fn normal_network_poll_no_peers() {
let chain_sync_service =
Box::new(MockChainSyncInterface::<substrate_test_runtime_client::runtime::Block>::new());

let (mut network, _) = make_network(chain_sync, chain_sync_service, client);
let mut network = TestNetworkBuilder::new()
.with_chain_sync((chain_sync, chain_sync_service))
.build();

// poll the network once
futures::future::poll_fn(|cx| {
let _ = network.poll_unpin(cx);
let _ = network.network().poll_unpin(cx);
Poll::Ready(())
})
.await;
}

#[async_std::test]
async fn request_justification() {
let client = Arc::new(TestClientBuilder::with_default_backend().build_with_longest_chain().0);

// build `ChainSyncInterface` provider and set no expecations for it (i.e., it cannot be
// called)
let chain_sync_service =
Expand All @@ -253,22 +99,22 @@ async fn request_justification() {
.returning(|_, _| ());

set_default_expecations_no_peers(&mut chain_sync);
let (mut network, _) = make_network(chain_sync, chain_sync_service, client);
let mut network = TestNetworkBuilder::new()
.with_chain_sync((chain_sync, chain_sync_service))
.build();

// send "request justifiction" message and poll the network
network.service().request_justification(&hash, number);

futures::future::poll_fn(|cx| {
let _ = network.poll_unpin(cx);
let _ = network.network().poll_unpin(cx);
Poll::Ready(())
})
.await;
}

#[async_std::test]
async fn clear_justification_requests(&mut self) {
let client = Arc::new(TestClientBuilder::with_default_backend().build_with_longest_chain().0);

async fn clear_justification_requests() {
// build `ChainSyncInterface` provider and set no expecations for it (i.e., it cannot be
// called)
let chain_sync_service =
Expand All @@ -281,22 +127,22 @@ async fn clear_justification_requests(&mut self) {
chain_sync.expect_clear_justification_requests().once().returning(|| ());

set_default_expecations_no_peers(&mut chain_sync);
let (mut network, _) = make_network(chain_sync, chain_sync_service, client);
let mut network = TestNetworkBuilder::new()
.with_chain_sync((chain_sync, chain_sync_service))
.build();

// send "request justifiction" message and poll the network
network.service().clear_justification_requests();

futures::future::poll_fn(|cx| {
let _ = network.poll_unpin(cx);
let _ = network.network().poll_unpin(cx);
Poll::Ready(())
})
.await;
}

#[async_std::test]
async fn set_sync_fork_request() {
let client = Arc::new(TestClientBuilder::with_default_backend().build_with_longest_chain().0);

// build `ChainSync` and set default expectations for it
let mut chain_sync =
Box::new(MockChainSync::<substrate_test_runtime_client::runtime::Block>::new());
Expand All @@ -320,13 +166,15 @@ async fn set_sync_fork_request() {
.once()
.returning(|_, _, _| ());

let (mut network, _) = make_network(chain_sync, Box::new(chain_sync_service), client);
let mut network = TestNetworkBuilder::new()
.with_chain_sync((chain_sync, Box::new(chain_sync_service)))
.build();

// send "set sync fork request" message and poll the network
network.service().set_sync_fork_request(copy_peers, hash, number);

futures::future::poll_fn(|cx| {
let _ = network.poll_unpin(cx);
let _ = network.network().poll_unpin(cx);
Poll::Ready(())
})
.await;
Expand Down Expand Up @@ -362,13 +210,16 @@ async fn on_block_finalized() {
.returning(|_, _| ());

set_default_expecations_no_peers(&mut chain_sync);
let (mut network, _) = make_network(chain_sync, chain_sync_service, client);
let mut network = TestNetworkBuilder::new()
.with_client(client)
.with_chain_sync((chain_sync, chain_sync_service))
.build();

// send "set sync fork request" message and poll the network
network.on_block_finalized(hash, header);
network.network().on_block_finalized(hash, header);

futures::future::poll_fn(|cx| {
let _ = network.poll_unpin(cx);
let _ = network.network().poll_unpin(cx);
Poll::Ready(())
})
.await;
Expand Down
Loading