16
16
// You should have received a copy of the GNU General Public License
17
17
// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
18
19
- use crate :: { config, NetworkWorker } ;
19
+ use crate :: { config, ChainSyncInterface , NetworkWorker } ;
20
20
21
21
use futures:: prelude:: * ;
22
22
use libp2p:: PeerId ;
@@ -35,7 +35,7 @@ use sc_network_common::{
35
35
use sc_network_light:: light_client_requests:: handler:: LightClientRequestHandler ;
36
36
use sc_network_sync:: {
37
37
block_request_handler:: BlockRequestHandler , mock:: MockChainSync ,
38
- state_request_handler:: StateRequestHandler ,
38
+ service :: mock :: MockChainSyncInterface , state_request_handler:: StateRequestHandler ,
39
39
} ;
40
40
use sp_core:: H256 ;
41
41
use sp_runtime:: {
@@ -56,6 +56,7 @@ const PROTOCOL_NAME: &str = "/foo";
56
56
57
57
fn make_network (
58
58
chain_sync : Box < dyn ChainSyncT < substrate_test_runtime_client:: runtime:: Block > > ,
59
+ chain_sync_service : Box < dyn ChainSyncInterface < substrate_test_runtime_client:: runtime:: Block > > ,
59
60
client : Arc < substrate_test_runtime_client:: TestClient > ,
60
61
) -> ( TestNetworkWorker , Arc < substrate_test_runtime_client:: TestClient > ) {
61
62
let network_config = config:: NetworkConfiguration {
@@ -174,6 +175,7 @@ fn make_network(
174
175
fork_id,
175
176
import_queue,
176
177
chain_sync,
178
+ chain_sync_service,
177
179
metrics_registry : None ,
178
180
block_request_protocol_config,
179
181
state_request_protocol_config,
@@ -193,7 +195,7 @@ fn set_default_expecations_no_peers(
193
195
chain_sync. expect_state_request ( ) . returning ( || None ) ;
194
196
chain_sync. expect_justification_requests ( ) . returning ( || Box :: new ( iter:: empty ( ) ) ) ;
195
197
chain_sync. expect_warp_sync_request ( ) . returning ( || None ) ;
196
- chain_sync. expect_poll_block_announce_validation ( ) . returning ( |_| Poll :: Pending ) ;
198
+ chain_sync. expect_poll ( ) . returning ( |_| Poll :: Pending ) ;
197
199
chain_sync. expect_status ( ) . returning ( || SyncStatus {
198
200
state : SyncState :: Idle ,
199
201
best_seen_block : None ,
@@ -207,11 +209,18 @@ fn set_default_expecations_no_peers(
207
209
#[ async_std:: test]
208
210
async fn normal_network_poll_no_peers ( ) {
209
211
let client = Arc :: new ( TestClientBuilder :: with_default_backend ( ) . build_with_longest_chain ( ) . 0 ) ;
212
+
213
+ // build `ChainSync` and set default expectations for it
210
214
let mut chain_sync =
211
215
Box :: new ( MockChainSync :: < substrate_test_runtime_client:: runtime:: Block > :: new ( ) ) ;
212
216
set_default_expecations_no_peers ( & mut chain_sync) ;
213
217
214
- let ( mut network, _) = make_network ( chain_sync, client) ;
218
+ // build `ChainSyncInterface` provider and set no expecations for it (i.e., it cannot be
219
+ // called)
220
+ let chain_sync_service =
221
+ Box :: new ( MockChainSyncInterface :: < substrate_test_runtime_client:: runtime:: Block > :: new ( ) ) ;
222
+
223
+ let ( mut network, _) = make_network ( chain_sync, chain_sync_service, client) ;
215
224
216
225
// poll the network once
217
226
futures:: future:: poll_fn ( |cx| {
@@ -224,6 +233,13 @@ async fn normal_network_poll_no_peers() {
224
233
#[ async_std:: test]
225
234
async fn request_justification ( ) {
226
235
let client = Arc :: new ( TestClientBuilder :: with_default_backend ( ) . build_with_longest_chain ( ) . 0 ) ;
236
+
237
+ // build `ChainSyncInterface` provider and set no expecations for it (i.e., it cannot be
238
+ // called)
239
+ let chain_sync_service =
240
+ Box :: new ( MockChainSyncInterface :: < substrate_test_runtime_client:: runtime:: Block > :: new ( ) ) ;
241
+
242
+ // build `ChainSync` and verify that call to `request_justification()` is made
227
243
let mut chain_sync =
228
244
Box :: new ( MockChainSync :: < substrate_test_runtime_client:: runtime:: Block > :: new ( ) ) ;
229
245
@@ -237,7 +253,7 @@ async fn request_justification() {
237
253
. returning ( |_, _| ( ) ) ;
238
254
239
255
set_default_expecations_no_peers ( & mut chain_sync) ;
240
- let ( mut network, _) = make_network ( chain_sync, client) ;
256
+ let ( mut network, _) = make_network ( chain_sync, chain_sync_service , client) ;
241
257
242
258
// send "request justifiction" message and poll the network
243
259
network. service ( ) . request_justification ( & hash, number) ;
@@ -252,13 +268,20 @@ async fn request_justification() {
252
268
#[ async_std:: test]
253
269
async fn clear_justification_requests ( & mut self ) {
254
270
let client = Arc :: new ( TestClientBuilder :: with_default_backend ( ) . build_with_longest_chain ( ) . 0 ) ;
271
+
272
+ // build `ChainSyncInterface` provider and set no expecations for it (i.e., it cannot be
273
+ // called)
274
+ let chain_sync_service =
275
+ Box :: new ( MockChainSyncInterface :: < substrate_test_runtime_client:: runtime:: Block > :: new ( ) ) ;
276
+
277
+ // build `ChainSync` and verify that call to `clear_justification_requests()` is made
255
278
let mut chain_sync =
256
279
Box :: new ( MockChainSync :: < substrate_test_runtime_client:: runtime:: Block > :: new ( ) ) ;
257
280
258
281
chain_sync. expect_clear_justification_requests ( ) . once ( ) . returning ( || ( ) ) ;
259
282
260
283
set_default_expecations_no_peers ( & mut chain_sync) ;
261
- let ( mut network, _) = make_network ( chain_sync, client) ;
284
+ let ( mut network, _) = make_network ( chain_sync, chain_sync_service , client) ;
262
285
263
286
// send "request justifiction" message and poll the network
264
287
network. service ( ) . clear_justification_requests ( ) ;
@@ -273,24 +296,31 @@ async fn clear_justification_requests(&mut self) {
273
296
#[ async_std:: test]
274
297
async fn set_sync_fork_request ( ) {
275
298
let client = Arc :: new ( TestClientBuilder :: with_default_backend ( ) . build_with_longest_chain ( ) . 0 ) ;
299
+
300
+ // build `ChainSync` and set default expectations for it
276
301
let mut chain_sync =
277
302
Box :: new ( MockChainSync :: < substrate_test_runtime_client:: runtime:: Block > :: new ( ) ) ;
303
+ set_default_expecations_no_peers ( & mut chain_sync) ;
304
+
305
+ // build `ChainSyncInterface` provider and verify that the `set_sync_fork_request()`
306
+ // call is delegated to `ChainSyncInterface` (which eventually forwards it to `ChainSync`)
307
+ let mut chain_sync_service =
308
+ MockChainSyncInterface :: < substrate_test_runtime_client:: runtime:: Block > :: new ( ) ;
278
309
279
310
let hash = H256 :: random ( ) ;
280
311
let number = 1337u64 ;
281
312
let peers = ( 0 ..3 ) . map ( |_| PeerId :: random ( ) ) . collect :: < Vec < _ > > ( ) ;
282
313
let copy_peers = peers. clone ( ) ;
283
314
284
- chain_sync
315
+ chain_sync_service
285
316
. expect_set_sync_fork_request ( )
286
317
. withf ( move |in_peers, in_hash, in_number| {
287
318
& peers == in_peers && & hash == in_hash && & number == in_number
288
319
} )
289
320
. once ( )
290
321
. returning ( |_, _, _| ( ) ) ;
291
322
292
- set_default_expecations_no_peers ( & mut chain_sync) ;
293
- let ( mut network, _) = make_network ( chain_sync, client) ;
323
+ let ( mut network, _) = make_network ( chain_sync, Box :: new ( chain_sync_service) , client) ;
294
324
295
325
// send "set sync fork request" message and poll the network
296
326
network. service ( ) . set_sync_fork_request ( copy_peers, hash, number) ;
@@ -305,6 +335,12 @@ async fn set_sync_fork_request() {
305
335
#[ async_std:: test]
306
336
async fn on_block_finalized ( ) {
307
337
let client = Arc :: new ( TestClientBuilder :: with_default_backend ( ) . build_with_longest_chain ( ) . 0 ) ;
338
+ // build `ChainSyncInterface` provider and set no expecations for it (i.e., it cannot be
339
+ // called)
340
+ let chain_sync_service =
341
+ Box :: new ( MockChainSyncInterface :: < substrate_test_runtime_client:: runtime:: Block > :: new ( ) ) ;
342
+
343
+ // build `ChainSync` and verify that call to `on_block_finalized()` is made
308
344
let mut chain_sync =
309
345
Box :: new ( MockChainSync :: < substrate_test_runtime_client:: runtime:: Block > :: new ( ) ) ;
310
346
@@ -326,7 +362,7 @@ async fn on_block_finalized() {
326
362
. returning ( |_, _| ( ) ) ;
327
363
328
364
set_default_expecations_no_peers ( & mut chain_sync) ;
329
- let ( mut network, _) = make_network ( chain_sync, client) ;
365
+ let ( mut network, _) = make_network ( chain_sync, chain_sync_service , client) ;
330
366
331
367
// send "set sync fork request" message and poll the network
332
368
network. on_block_finalized ( hash, header) ;
0 commit comments