sc-network 0.23.0

Substrate network protocol
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// This file is part of Substrate.

// Copyright (C) 2022 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// 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,
	service::tests::{TestNetworkBuilder, BLOCK_ANNOUNCE_PROTO_NAME},
};

use futures::prelude::*;
use libp2p::PeerId;
use sc_block_builder::BlockBuilderProvider;
use sc_client_api::HeaderBackend;
use sc_consensus::JustificationSyncLink;
use sc_network_common::{
	config::{MultiaddrWithPeerId, ProtocolId, SetConfig},
	protocol::{event::Event, role::Roles, ProtocolName},
	service::NetworkSyncForkRequest,
	sync::{SyncState, SyncStatus},
};
use sc_network_sync::{mock::MockChainSync, service::mock::MockChainSyncInterface, ChainSync};
use sp_core::H256;
use sp_runtime::{
	generic::BlockId,
	traits::{Block as BlockT, Header as _},
};
use std::{
	sync::{Arc, RwLock},
	task::Poll,
	time::Duration,
};
use substrate_test_runtime_client::{TestClientBuilder, TestClientBuilderExt as _};

fn set_default_expecations_no_peers(
	chain_sync: &mut MockChainSync<substrate_test_runtime_client::runtime::Block>,
) {
	chain_sync.expect_poll().returning(|_| Poll::Pending);
	chain_sync.expect_status().returning(|| SyncStatus {
		state: SyncState::Idle,
		best_seen_block: None,
		num_peers: 0u32,
		queued_blocks: 0u32,
		state_sync: None,
		warp_sync: None,
	});
}

#[tokio::test]
async fn normal_network_poll_no_peers() {
	// build `ChainSync` and set default expectations for it
	let mut chain_sync =
		Box::new(MockChainSync::<substrate_test_runtime_client::runtime::Block>::new());
	set_default_expecations_no_peers(&mut chain_sync);

	// build `ChainSyncInterface` provider and set no expecations for it (i.e., it cannot be
	// called)
	let chain_sync_service =
		Box::new(MockChainSyncInterface::<substrate_test_runtime_client::runtime::Block>::new());

	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.network().poll_unpin(cx);
		Poll::Ready(())
	})
	.await;
}

#[tokio::test]
async fn request_justification() {
	let hash = H256::random();
	let number = 1337u64;

	// build `ChainSyncInterface` provider and and expect
	// `JustificationSyncLink::request_justification() to be called once
	let mut chain_sync_service =
		Box::new(MockChainSyncInterface::<substrate_test_runtime_client::runtime::Block>::new());

	chain_sync_service
		.expect_justification_sync_link_request_justification()
		.withf(move |in_hash, in_number| &hash == in_hash && &number == in_number)
		.once()
		.returning(|_, _| ());

	// build `ChainSync` and set default expecations for it
	let mut chain_sync = MockChainSync::<substrate_test_runtime_client::runtime::Block>::new();

	set_default_expecations_no_peers(&mut chain_sync);
	let mut network = TestNetworkBuilder::new()
		.with_chain_sync((Box::new(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.network().poll_unpin(cx);
		Poll::Ready(())
	})
	.await;
}

#[tokio::test]
async fn clear_justification_requests() {
	// build `ChainSyncInterface` provider and expect
	// `JustificationSyncLink::clear_justification_requests()` to be called
	let mut chain_sync_service =
		Box::new(MockChainSyncInterface::<substrate_test_runtime_client::runtime::Block>::new());

	chain_sync_service
		.expect_justification_sync_link_clear_justification_requests()
		.once()
		.returning(|| ());

	// build `ChainSync` and set default expecations for it
	let mut chain_sync =
		Box::new(MockChainSync::<substrate_test_runtime_client::runtime::Block>::new());

	set_default_expecations_no_peers(&mut chain_sync);
	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.network().poll_unpin(cx);
		Poll::Ready(())
	})
	.await;
}

#[tokio::test]
async fn set_sync_fork_request() {
	// build `ChainSync` and set default expectations for it
	let mut chain_sync =
		Box::new(MockChainSync::<substrate_test_runtime_client::runtime::Block>::new());
	set_default_expecations_no_peers(&mut chain_sync);

	// build `ChainSyncInterface` provider and verify that the `set_sync_fork_request()`
	// call is delegated to `ChainSyncInterface` (which eventually forwards it to `ChainSync`)
	let mut chain_sync_service =
		MockChainSyncInterface::<substrate_test_runtime_client::runtime::Block>::new();

	let hash = H256::random();
	let number = 1337u64;
	let peers = (0..3).map(|_| PeerId::random()).collect::<Vec<_>>();
	let copy_peers = peers.clone();

	chain_sync_service
		.expect_set_sync_fork_request()
		.withf(move |in_peers, in_hash, in_number| {
			&peers == in_peers && &hash == in_hash && &number == in_number
		})
		.once()
		.returning(|_, _, _| ());

	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.network().poll_unpin(cx);
		Poll::Ready(())
	})
	.await;
}

#[tokio::test]
async fn on_block_finalized() {
	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 =
		Box::new(MockChainSyncInterface::<substrate_test_runtime_client::runtime::Block>::new());

	// build `ChainSync` and verify that call to `on_block_finalized()` is made
	let mut chain_sync =
		Box::new(MockChainSync::<substrate_test_runtime_client::runtime::Block>::new());

	let at = client.header(client.info().best_hash).unwrap().unwrap().hash();
	let block = client
		.new_block_at(&BlockId::Hash(at), Default::default(), false)
		.unwrap()
		.build()
		.unwrap()
		.block;
	let header = block.header.clone();
	let block_number = *header.number();
	let hash = block.hash();

	chain_sync
		.expect_on_block_finalized()
		.withf(move |in_hash, in_number| &hash == in_hash && &block_number == in_number)
		.once()
		.returning(|_, _| ());

	set_default_expecations_no_peers(&mut chain_sync);
	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.network().on_block_finalized(hash, header);

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

// report from mock import queue that importing a justification was not successful
// and verify that connection to the peer is closed
#[tokio::test]
async fn invalid_justification_imported() {
	struct DummyImportQueueHandle;

	impl
		sc_consensus::import_queue::ImportQueueService<
			substrate_test_runtime_client::runtime::Block,
		> for DummyImportQueueHandle
	{
		fn import_blocks(
			&mut self,
			_origin: sp_consensus::BlockOrigin,
			_blocks: Vec<
				sc_consensus::IncomingBlock<substrate_test_runtime_client::runtime::Block>,
			>,
		) {
		}

		fn import_justifications(
			&mut self,
			_who: sc_consensus::import_queue::RuntimeOrigin,
			_hash: substrate_test_runtime_client::runtime::Hash,
			_number: sp_runtime::traits::NumberFor<substrate_test_runtime_client::runtime::Block>,
			_justifications: sp_runtime::Justifications,
		) {
		}
	}

	struct DummyImportQueue(
		Arc<
			RwLock<
				Option<(
					PeerId,
					substrate_test_runtime_client::runtime::Hash,
					sp_runtime::traits::NumberFor<substrate_test_runtime_client::runtime::Block>,
				)>,
			>,
		>,
		DummyImportQueueHandle,
	);

	#[async_trait::async_trait]
	impl sc_consensus::ImportQueue<substrate_test_runtime_client::runtime::Block> for DummyImportQueue {
		fn poll_actions(
			&mut self,
			_cx: &mut futures::task::Context,
			link: &mut dyn sc_consensus::Link<substrate_test_runtime_client::runtime::Block>,
		) {
			if let Some((peer, hash, number)) = *self.0.read().unwrap() {
				link.justification_imported(peer, &hash, number, false);
			}
		}

		fn service(
			&self,
		) -> Box<
			dyn sc_consensus::import_queue::ImportQueueService<
				substrate_test_runtime_client::runtime::Block,
			>,
		> {
			Box::new(DummyImportQueueHandle {})
		}

		fn service_ref(
			&mut self,
		) -> &mut dyn sc_consensus::import_queue::ImportQueueService<
			substrate_test_runtime_client::runtime::Block,
		> {
			&mut self.1
		}

		async fn run(
			self,
			_link: Box<dyn sc_consensus::Link<substrate_test_runtime_client::runtime::Block>>,
		) {
		}
	}

	let justification_info = Arc::new(RwLock::new(None));
	let listen_addr = config::build_multiaddr![Memory(rand::random::<u64>())];

	let (service1, mut event_stream1) = TestNetworkBuilder::new()
		.with_import_queue(Box::new(DummyImportQueue(
			justification_info.clone(),
			DummyImportQueueHandle {},
		)))
		.with_listen_addresses(vec![listen_addr.clone()])
		.build()
		.start_network();

	let (service2, mut event_stream2) = TestNetworkBuilder::new()
		.with_set_config(SetConfig {
			reserved_nodes: vec![MultiaddrWithPeerId {
				multiaddr: listen_addr,
				peer_id: service1.local_peer_id,
			}],
			..Default::default()
		})
		.build()
		.start_network();

	async fn wait_for_events(stream: &mut (impl Stream<Item = Event> + std::marker::Unpin)) {
		let mut notif_received = false;
		let mut sync_received = false;
		while !notif_received || !sync_received {
			match stream.next().await.unwrap() {
				Event::NotificationStreamOpened { .. } => notif_received = true,
				Event::SyncConnected { .. } => sync_received = true,
				_ => {},
			};
		}
	}

	wait_for_events(&mut event_stream1).await;
	wait_for_events(&mut event_stream2).await;

	{
		let mut info = justification_info.write().unwrap();
		*info = Some((service2.local_peer_id, H256::random(), 1337u64));
	}

	let wait_disconnection = async {
		while !std::matches!(event_stream1.next().await, Some(Event::SyncDisconnected { .. })) {}
	};

	if tokio::time::timeout(Duration::from_secs(5), wait_disconnection).await.is_err() {
		panic!("did not receive disconnection event in time");
	}
}

#[tokio::test]
async fn disconnect_peer_using_chain_sync_handle() {
	let client = Arc::new(TestClientBuilder::with_default_backend().build_with_longest_chain().0);
	let listen_addr = config::build_multiaddr![Memory(rand::random::<u64>())];

	let import_queue = Box::new(sc_consensus::import_queue::mock::MockImportQueueHandle::new());
	let (chain_sync_network_provider, chain_sync_network_handle) =
		sc_network_sync::service::network::NetworkServiceProvider::new();
	let handle_clone = chain_sync_network_handle.clone();

	let (chain_sync, chain_sync_service, _) = ChainSync::new(
		sc_network_common::sync::SyncMode::Full,
		client.clone(),
		ProtocolId::from("test-protocol-name"),
		&Some(String::from("test-fork-id")),
		Roles::from(&config::Role::Full),
		Box::new(sp_consensus::block_validation::DefaultBlockAnnounceValidator),
		1u32,
		None,
		None,
		chain_sync_network_handle.clone(),
		import_queue,
		ProtocolName::from("block-request"),
		ProtocolName::from("state-request"),
		None,
	)
	.unwrap();

	let (node1, mut event_stream1) = TestNetworkBuilder::new()
		.with_listen_addresses(vec![listen_addr.clone()])
		.with_chain_sync((Box::new(chain_sync), Box::new(chain_sync_service)))
		.with_chain_sync_network((chain_sync_network_provider, chain_sync_network_handle))
		.with_client(client.clone())
		.build()
		.start_network();

	let (node2, mut event_stream2) = TestNetworkBuilder::new()
		.with_set_config(SetConfig {
			reserved_nodes: vec![MultiaddrWithPeerId {
				multiaddr: listen_addr,
				peer_id: node1.local_peer_id,
			}],
			..Default::default()
		})
		.with_client(client.clone())
		.build()
		.start_network();

	async fn wait_for_events(stream: &mut (impl Stream<Item = Event> + std::marker::Unpin)) {
		let mut notif_received = false;
		let mut sync_received = false;
		while !notif_received || !sync_received {
			match stream.next().await.unwrap() {
				Event::NotificationStreamOpened { .. } => notif_received = true,
				Event::SyncConnected { .. } => sync_received = true,
				_ => {},
			};
		}
	}

	wait_for_events(&mut event_stream1).await;
	wait_for_events(&mut event_stream2).await;

	handle_clone.disconnect_peer(node2.local_peer_id, BLOCK_ANNOUNCE_PROTO_NAME.into());

	let wait_disconnection = async {
		while !std::matches!(event_stream1.next().await, Some(Event::SyncDisconnected { .. })) {}
	};

	if tokio::time::timeout(Duration::from_secs(5), wait_disconnection).await.is_err() {
		panic!("did not receive disconnection event in time");
	}
}