@@ -7,7 +7,9 @@ use super::{
7
7
use crate :: {
8
8
blockcfg:: { Block , FragmentId , Header , HeaderHash } ,
9
9
blockchain:: Checkpoints ,
10
- intercom:: { self , BlockMsg , ExplorerMsg , NetworkMsg , PropagateMsg , TransactionMsg } ,
10
+ intercom:: {
11
+ self , BlockMsg , ExplorerMsg , NetworkMsg , NotifierMsg , PropagateMsg , TransactionMsg ,
12
+ } ,
11
13
metrics:: { Metrics , MetricsBackend } ,
12
14
network:: p2p:: Address ,
13
15
utils:: {
@@ -58,6 +60,7 @@ pub struct Process {
58
60
pub network_msgbox : MessageBox < NetworkMsg > ,
59
61
pub fragment_msgbox : MessageBox < TransactionMsg > ,
60
62
pub explorer_msgbox : Option < MessageBox < ExplorerMsg > > ,
63
+ pub notifier_msgbox : MessageBox < NotifierMsg > ,
61
64
pub garbage_collection_interval : Duration ,
62
65
}
63
66
@@ -92,6 +95,7 @@ impl Process {
92
95
let blockchain_tip = self . blockchain_tip . clone ( ) ;
93
96
let network_msg_box = self . network_msgbox . clone ( ) ;
94
97
let explorer_msg_box = self . explorer_msgbox . clone ( ) ;
98
+ let event_notifier_msg_box = self . notifier_msgbox . clone ( ) ;
95
99
let tx_msg_box = self . fragment_msgbox . clone ( ) ;
96
100
let stats_counter = self . stats_counter . clone ( ) ;
97
101
@@ -117,6 +121,7 @@ impl Process {
117
121
tx_msg_box,
118
122
network_msg_box,
119
123
explorer_msg_box,
124
+ event_notifier_msg_box,
120
125
leadership_block,
121
126
stats_counter,
122
127
)
@@ -163,6 +168,7 @@ impl Process {
163
168
tx_msg_box,
164
169
network_msg_box,
165
170
explorer_msg_box,
171
+ event_notifier_msg_box,
166
172
get_next_block_scheduler,
167
173
handle,
168
174
stats_counter,
@@ -195,6 +201,8 @@ impl Process {
195
201
let explorer = self . explorer_msgbox . clone ( ) ;
196
202
let tx_msg_box = self . fragment_msgbox . clone ( ) ;
197
203
204
+ let notifier = self . notifier_msgbox . clone ( ) ;
205
+
198
206
info. run_periodic_fallible (
199
207
"branch reprocessing" ,
200
208
BRANCH_REPROCESSING_INTERVAL ,
@@ -204,6 +212,7 @@ impl Process {
204
212
tip. clone ( ) ,
205
213
explorer. clone ( ) ,
206
214
tx_msg_box. clone ( ) ,
215
+ notifier. clone ( ) ,
207
216
)
208
217
} ,
209
218
)
@@ -294,6 +303,7 @@ async fn reprocess_tip(
294
303
tip : Tip ,
295
304
explorer_msg_box : Option < MessageBox < ExplorerMsg > > ,
296
305
tx_msg_box : MessageBox < TransactionMsg > ,
306
+ notifier_msg_box : MessageBox < NotifierMsg > ,
297
307
) -> Result < ( ) , Error > {
298
308
let branches: Vec < Arc < Ref > > = blockchain. branches ( ) . branches ( ) . await ;
299
309
@@ -311,6 +321,7 @@ async fn reprocess_tip(
311
321
Arc :: clone ( other) ,
312
322
explorer_msg_box. clone ( ) ,
313
323
Some ( tx_msg_box. clone ( ) ) ,
324
+ Some ( notifier_msg_box. clone ( ) ) ,
314
325
)
315
326
. await ?
316
327
}
@@ -333,6 +344,7 @@ pub async fn process_new_ref(
333
344
candidate : Arc < Ref > ,
334
345
explorer_msg_box : Option < MessageBox < ExplorerMsg > > ,
335
346
mut tx_msg_box : Option < MessageBox < TransactionMsg > > ,
347
+ notifier_msg_box : Option < MessageBox < NotifierMsg > > ,
336
348
) -> Result < ( ) , Error > {
337
349
let candidate_hash = candidate. hash ( ) ;
338
350
let tip_ref = tip. get_ref ( ) . await ;
@@ -411,6 +423,15 @@ pub async fn process_new_ref(
411
423
. await
412
424
. unwrap_or_else ( |err| {
413
425
tracing:: error!( "cannot send new tip to explorer: {}" , err)
426
+ } )
427
+ }
428
+
429
+ if let Some ( mut msg_box) = notifier_msg_box {
430
+ msg_box
431
+ . send ( NotifierMsg :: NewTip ( candidate_hash) )
432
+ . await
433
+ . unwrap_or_else ( |err| {
434
+ tracing:: error!( "cannot notify new block to subscribers: {}" , err)
414
435
} ) ;
415
436
}
416
437
}
@@ -426,15 +447,18 @@ async fn process_and_propagate_new_ref(
426
447
mut network_msg_box : MessageBox < NetworkMsg > ,
427
448
explorer_msg_box : Option < MessageBox < ExplorerMsg > > ,
428
449
tx_msg_box : MessageBox < TransactionMsg > ,
450
+ notifier_msg_box : MessageBox < NotifierMsg > ,
429
451
) -> chain:: Result < ( ) > {
430
452
let header = new_block_ref. header ( ) . clone ( ) ;
431
453
tracing:: debug!( "processing the new block and propagating" ) ;
454
+
432
455
process_new_ref (
433
456
blockchain,
434
457
tip,
435
458
new_block_ref,
436
459
explorer_msg_box,
437
460
Some ( tx_msg_box) ,
461
+ Some ( notifier_msg_box) ,
438
462
)
439
463
. await ?;
440
464
@@ -454,6 +478,7 @@ async fn process_leadership_block(
454
478
tx_msg_box : MessageBox < TransactionMsg > ,
455
479
network_msg_box : MessageBox < NetworkMsg > ,
456
480
explorer_msg_box : Option < MessageBox < ExplorerMsg > > ,
481
+ mut notifier_msg_box : MessageBox < crate :: notifier:: Message > ,
457
482
leadership_block : LeadershipBlock ,
458
483
stats_counter : Metrics ,
459
484
) -> chain:: Result < ( ) > {
@@ -467,12 +492,23 @@ async fn process_leadership_block(
467
492
network_msg_box,
468
493
explorer_msg_box. clone ( ) ,
469
494
tx_msg_box,
495
+ notifier_msg_box. clone ( ) ,
470
496
)
471
497
. await ?;
472
498
473
499
// Track block as new new tip block
474
500
stats_counter. set_tip_block ( & block, & new_block_ref) ;
475
501
502
+ if let Err ( err) = notifier_msg_box
503
+ . send ( NotifierMsg :: NewBlock ( block. clone ( ) ) )
504
+ . await
505
+ {
506
+ tracing:: error!(
507
+ "Cannot propagate block to blockchain event notifier: {}" ,
508
+ err
509
+ )
510
+ }
511
+
476
512
if let Some ( mut msg_box) = explorer_msg_box {
477
513
msg_box. send ( ExplorerMsg :: NewBlock ( block) ) . await ?;
478
514
}
@@ -547,6 +583,7 @@ async fn process_network_blocks(
547
583
tx_msg_box : MessageBox < TransactionMsg > ,
548
584
network_msg_box : MessageBox < NetworkMsg > ,
549
585
mut explorer_msg_box : Option < MessageBox < ExplorerMsg > > ,
586
+ mut notifier_msg_box : MessageBox < NotifierMsg > ,
550
587
mut get_next_block_scheduler : GetNextBlockScheduler ,
551
588
handle : intercom:: RequestStreamHandle < Block , ( ) > ,
552
589
stats_counter : Metrics ,
@@ -564,6 +601,7 @@ async fn process_network_blocks(
564
601
& blockchain,
565
602
block. clone ( ) ,
566
603
explorer_msg_box. as_mut ( ) ,
604
+ & mut notifier_msg_box,
567
605
& mut get_next_block_scheduler,
568
606
)
569
607
. await ;
@@ -603,6 +641,7 @@ async fn process_network_blocks(
603
641
network_msg_box,
604
642
explorer_msg_box,
605
643
tx_msg_box,
644
+ notifier_msg_box,
606
645
)
607
646
. await ?;
608
647
@@ -620,6 +659,7 @@ async fn process_network_block(
620
659
blockchain : & Blockchain ,
621
660
block : Block ,
622
661
explorer_msg_box : Option < & mut MessageBox < ExplorerMsg > > ,
662
+ event_notifier_msg_box : & mut MessageBox < NotifierMsg > ,
623
663
get_next_block_scheduler : & mut GetNextBlockScheduler ,
624
664
) -> Result < Option < Arc < Ref > > , chain:: Error > {
625
665
get_next_block_scheduler
@@ -650,7 +690,14 @@ async fn process_network_block(
650
690
Err ( Error :: MissingParentBlock ( parent_hash) )
651
691
}
652
692
PreCheckedHeader :: HeaderWithCache { parent_ref, .. } => {
653
- let r = check_and_apply_block ( blockchain, parent_ref, block, explorer_msg_box) . await ;
693
+ let r = check_and_apply_block (
694
+ blockchain,
695
+ parent_ref,
696
+ block,
697
+ explorer_msg_box,
698
+ event_notifier_msg_box,
699
+ )
700
+ . await ;
654
701
r
655
702
}
656
703
}
@@ -661,6 +708,7 @@ async fn check_and_apply_block(
661
708
parent_ref : Arc < Ref > ,
662
709
block : Block ,
663
710
explorer_msg_box : Option < & mut MessageBox < ExplorerMsg > > ,
711
+ event_notifier_msg_box : & mut MessageBox < NotifierMsg > ,
664
712
) -> Result < Option < Arc < Ref > > , chain:: Error > {
665
713
let explorer_enabled = explorer_msg_box. is_some ( ) ;
666
714
let post_checked = blockchain
@@ -679,6 +727,8 @@ async fn check_and_apply_block(
679
727
} else {
680
728
None
681
729
} ;
730
+ let block_for_subscribers = block. clone ( ) ;
731
+
682
732
let applied_block = blockchain
683
733
. apply_and_store_block ( post_checked, block)
684
734
. await ?;
@@ -695,6 +745,13 @@ async fn check_and_apply_block(
695
745
. try_send ( ExplorerMsg :: NewBlock ( block_for_explorer. take ( ) . unwrap ( ) ) )
696
746
. unwrap_or_else ( |err| tracing:: error!( "cannot add block to explorer: {}" , err) ) ;
697
747
}
748
+
749
+ event_notifier_msg_box
750
+ . try_send ( NotifierMsg :: NewBlock ( block_for_subscribers) )
751
+ . unwrap_or_else ( |err| {
752
+ tracing:: error!( "cannot notify new block to subscribers: {}" , err)
753
+ } ) ;
754
+
698
755
Ok ( Some ( block_ref) )
699
756
} else {
700
757
tracing:: debug!(
0 commit comments