@@ -28,7 +28,8 @@ use crate::{
28
28
schema:: v1:: { StateRequest , StateResponse } ,
29
29
service:: { self , chain_sync:: ToServiceCommand } ,
30
30
warp:: WarpSyncParams ,
31
- BlockRequestEvent , ChainSync , ClientError , SyncingService ,
31
+ BlockRequestAction , ChainSync , ClientError , ImportBlocksAction , ImportJustificationsAction ,
32
+ OnBlockResponse , SyncingService ,
32
33
} ;
33
34
34
35
use codec:: { Decode , Encode } ;
@@ -41,7 +42,8 @@ use futures_timer::Delay;
41
42
use libp2p:: { request_response:: OutboundFailure , PeerId } ;
42
43
use log:: { debug, trace} ;
43
44
use prometheus_endpoint:: {
44
- register, Gauge , GaugeVec , MetricSource , Opts , PrometheusError , Registry , SourcedGauge , U64 ,
45
+ register, Counter , Gauge , GaugeVec , MetricSource , Opts , PrometheusError , Registry ,
46
+ SourcedGauge , U64 ,
45
47
} ;
46
48
use prost:: Message ;
47
49
use schnellru:: { ByLength , LruMap } ;
@@ -135,6 +137,8 @@ struct Metrics {
135
137
queued_blocks : Gauge < U64 > ,
136
138
fork_targets : Gauge < U64 > ,
137
139
justifications : GaugeVec < U64 > ,
140
+ import_queue_blocks_submitted : Counter < U64 > ,
141
+ import_queue_justifications_submitted : Counter < U64 > ,
138
142
}
139
143
140
144
impl Metrics {
@@ -164,6 +168,20 @@ impl Metrics {
164
168
) ?;
165
169
register ( g, r) ?
166
170
} ,
171
+ import_queue_blocks_submitted : {
172
+ let c = Counter :: new (
173
+ "substrate_sync_import_queue_blocks_submitted" ,
174
+ "Number of blocks submitted to the import queue." ,
175
+ ) ?;
176
+ register ( c, r) ?
177
+ } ,
178
+ import_queue_justifications_submitted : {
179
+ let c = Counter :: new (
180
+ "substrate_sync_import_queue_justifications_submitted" ,
181
+ "Number of justifications submitted to the import queue." ,
182
+ ) ?;
183
+ register ( c, r) ?
184
+ } ,
167
185
} )
168
186
}
169
187
}
@@ -311,6 +329,9 @@ pub struct SyncingEngine<B: BlockT, Client> {
311
329
312
330
/// Protocol name used to send out warp sync requests
313
331
warp_sync_protocol_name : Option < ProtocolName > ,
332
+
333
+ /// Handle to import queue.
334
+ import_queue : Box < dyn ImportQueueService < B > > ,
314
335
}
315
336
316
337
impl < B : BlockT , Client > SyncingEngine < B , Client >
@@ -436,9 +457,7 @@ where
436
457
max_parallel_downloads,
437
458
max_blocks_per_request,
438
459
warp_sync_config,
439
- metrics_registry,
440
460
network_service. clone ( ) ,
441
- import_queue,
442
461
) ?;
443
462
444
463
let ( tx, service_rx) = tracing_unbounded ( "mpsc_chain_sync" , 100_000 ) ;
@@ -501,6 +520,7 @@ where
501
520
block_downloader,
502
521
state_request_protocol_name,
503
522
warp_sync_protocol_name,
523
+ import_queue,
504
524
} ,
505
525
SyncingService :: new ( tx, num_connected, is_major_syncing) ,
506
526
block_announce_config,
@@ -728,13 +748,13 @@ where
728
748
ToServiceCommand :: BlocksProcessed ( imported, count, results) => {
729
749
for result in self . chain_sync . on_blocks_processed ( imported, count, results) {
730
750
match result {
731
- Ok ( event ) => match event {
732
- BlockRequestEvent :: SendRequest { peer_id, request } => {
751
+ Ok ( action ) => match action {
752
+ BlockRequestAction :: SendRequest { peer_id, request } => {
733
753
// drop obsolete pending response first
734
754
self . pending_responses . remove ( & peer_id) ;
735
755
self . send_block_request ( peer_id, request) ;
736
756
} ,
737
- BlockRequestEvent :: RemoveStale { peer_id } => {
757
+ BlockRequestAction :: RemoveStale { peer_id } => {
738
758
self . pending_responses . remove ( & peer_id) ;
739
759
} ,
740
760
} ,
@@ -922,7 +942,10 @@ where
922
942
}
923
943
}
924
944
925
- self . chain_sync . peer_disconnected ( & peer_id) ;
945
+ if let Some ( import_blocks_action) = self . chain_sync . peer_disconnected ( & peer_id) {
946
+ self . import_blocks ( import_blocks_action)
947
+ }
948
+
926
949
self . pending_responses . remove ( & peer_id) ;
927
950
self . event_streams . retain ( |stream| {
928
951
stream. unbounded_send ( SyncEvent :: PeerDisconnected ( peer_id) ) . is_ok ( )
@@ -1181,10 +1204,14 @@ where
1181
1204
PeerRequest :: Block ( req) => {
1182
1205
match self . block_downloader . block_response_into_blocks ( & req, resp) {
1183
1206
Ok ( blocks) => {
1184
- if let Some ( ( peer_id, new_req) ) =
1185
- self . chain_sync . on_block_response ( peer_id, req, blocks)
1186
- {
1187
- self . send_block_request ( peer_id, new_req) ;
1207
+ match self . chain_sync . on_block_response ( peer_id, req, blocks) {
1208
+ OnBlockResponse :: SendBlockRequest { peer_id, request } =>
1209
+ self . send_block_request ( peer_id, request) ,
1210
+ OnBlockResponse :: ImportBlocks ( import_blocks_action) =>
1211
+ self . import_blocks ( import_blocks_action) ,
1212
+ OnBlockResponse :: ImportJustifications ( action) =>
1213
+ self . import_justifications ( action) ,
1214
+ OnBlockResponse :: Nothing => { } ,
1188
1215
}
1189
1216
} ,
1190
1217
Err ( BlockResponseError :: DecodeFailed ( e) ) => {
@@ -1230,7 +1257,11 @@ where
1230
1257
} ,
1231
1258
} ;
1232
1259
1233
- self . chain_sync . on_state_response ( peer_id, response) ;
1260
+ if let Some ( import_blocks_action) =
1261
+ self . chain_sync . on_state_response ( peer_id, response)
1262
+ {
1263
+ self . import_blocks ( import_blocks_action) ;
1264
+ }
1234
1265
} ,
1235
1266
PeerRequest :: WarpProof => {
1236
1267
self . chain_sync . on_warp_sync_response ( peer_id, EncodedProof ( resp) ) ;
@@ -1337,4 +1368,24 @@ where
1337
1368
} ,
1338
1369
}
1339
1370
}
1371
+
1372
+ /// Import blocks.
1373
+ fn import_blocks ( & mut self , ImportBlocksAction { origin, blocks } : ImportBlocksAction < B > ) {
1374
+ if let Some ( metrics) = & self . metrics {
1375
+ metrics. import_queue_blocks_submitted . inc ( ) ;
1376
+ }
1377
+
1378
+ self . import_queue . import_blocks ( origin, blocks) ;
1379
+ }
1380
+
1381
+ /// Import justifications.
1382
+ fn import_justifications ( & mut self , action : ImportJustificationsAction < B > ) {
1383
+ if let Some ( metrics) = & self . metrics {
1384
+ metrics. import_queue_justifications_submitted . inc ( ) ;
1385
+ }
1386
+
1387
+ let ImportJustificationsAction { peer_id, hash, number, justifications } = action;
1388
+
1389
+ self . import_queue . import_justifications ( peer_id, hash, number, justifications) ;
1390
+ }
1340
1391
}
0 commit comments