@@ -2,6 +2,7 @@ import type { EpochCacheInterface } from '@aztec/epoch-cache';
2
2
import { createLibp2pComponentLogger , createLogger } from '@aztec/foundation/log' ;
3
3
import { SerialQueue } from '@aztec/foundation/queue' ;
4
4
import { RunningPromise } from '@aztec/foundation/running-promise' ;
5
+ import { Timer } from '@aztec/foundation/timer' ;
5
6
import type { AztecAsyncKVStore } from '@aztec/kv-store' ;
6
7
import { protocolContractTreeRoot } from '@aztec/protocol-contracts' ;
7
8
import type { L2BlockSource } from '@aztec/stdlib/block' ;
@@ -65,6 +66,7 @@ import { reqGoodbyeHandler } from '../reqresp/protocols/goodbye.js';
65
66
import { pingHandler , reqRespBlockHandler , reqRespTxHandler , statusHandler } from '../reqresp/protocols/index.js' ;
66
67
import { ReqResp } from '../reqresp/reqresp.js' ;
67
68
import type { P2PBlockReceivedCallback , P2PService , PeerDiscoveryService } from '../service.js' ;
69
+ import { P2PInstrumentation } from './instrumentation.js' ;
68
70
69
71
interface ValidationResult {
70
72
name : string ;
@@ -107,6 +109,8 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
107
109
108
110
private gossipSubEventHandler : ( e : CustomEvent < GossipsubMessage > ) => void ;
109
111
112
+ private instrumentation : P2PInstrumentation ;
113
+
110
114
constructor (
111
115
private clientType : T ,
112
116
private config : P2PConfig ,
@@ -122,6 +126,8 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
122
126
) {
123
127
super ( telemetry , 'LibP2PService' ) ;
124
128
129
+ this . instrumentation = new P2PInstrumentation ( telemetry , 'LibP2PService' ) ;
130
+
125
131
this . msgIdSeenValidators [ TopicType . tx ] = new MessageSeenValidator ( config . seenMessageCacheSize ) ;
126
132
this . msgIdSeenValidators [ TopicType . block_proposal ] = new MessageSeenValidator ( config . seenMessageCacheSize ) ;
127
133
this . msgIdSeenValidators [ TopicType . block_attestation ] = new MessageSeenValidator ( config . seenMessageCacheSize ) ;
@@ -501,25 +507,33 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
501
507
}
502
508
503
509
protected preValidateReceivedMessage ( msg : Message , msgId : string , source : PeerId ) {
504
- const getValidator = ( ) => {
505
- if ( msg . topic === this . topicStrings [ TopicType . tx ] ) {
506
- return this . msgIdSeenValidators [ TopicType . tx ] ;
507
- }
508
- if ( msg . topic === this . topicStrings [ TopicType . block_attestation ] ) {
509
- return this . msgIdSeenValidators [ TopicType . block_attestation ] ;
510
- }
511
- if ( msg . topic === this . topicStrings [ TopicType . block_proposal ] ) {
512
- return this . msgIdSeenValidators [ TopicType . block_proposal ] ;
513
- }
514
- this . logger . error ( `Received message on unknown topic: ${ msg . topic } ` ) ;
515
- } ;
510
+ let topicType : TopicType | undefined ;
511
+
512
+ switch ( msg . topic ) {
513
+ case this . topicStrings [ TopicType . tx ] :
514
+ topicType = TopicType . tx ;
515
+ break ;
516
+ case this . topicStrings [ TopicType . block_attestation ] :
517
+ topicType = TopicType . block_attestation ;
518
+ break ;
519
+ case this . topicStrings [ TopicType . block_proposal ] :
520
+ topicType = TopicType . block_proposal ;
521
+ break ;
522
+ default :
523
+ this . logger . error ( `Received message on unknown topic: ${ msg . topic } ` ) ;
524
+ break ;
525
+ }
516
526
517
- const validator = getValidator ( ) ;
527
+ const validator = topicType ? this . msgIdSeenValidators [ topicType ] : undefined ;
518
528
519
529
if ( ! validator || ! validator . addMessage ( msgId ) ) {
530
+ this . instrumentation . incMessagePrevalidationStatus ( false , topicType ) ;
520
531
this . node . services . pubsub . reportMessageValidationResult ( msgId , source . toString ( ) , TopicValidatorResult . Ignore ) ;
521
532
return false ;
522
533
}
534
+
535
+ this . instrumentation . incMessagePrevalidationStatus ( true , topicType ) ;
536
+
523
537
return true ;
524
538
}
525
539
@@ -559,14 +573,20 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
559
573
validationFunc : ( ) => Promise < { result : boolean ; obj : T } > ,
560
574
msgId : string ,
561
575
source : PeerId ,
576
+ topicType : TopicType ,
562
577
) : Promise < { result : boolean ; obj : T | undefined } > {
563
578
let resultAndObj : { result : boolean ; obj : T | undefined } = { result : false , obj : undefined } ;
579
+ const timer = new Timer ( ) ;
564
580
try {
565
581
resultAndObj = await validationFunc ( ) ;
566
582
} catch ( err ) {
567
583
this . logger . error ( `Error deserialising and validating message ` , err ) ;
568
584
}
569
585
586
+ if ( resultAndObj . result ) {
587
+ this . instrumentation . recordMessageValidation ( topicType , timer ) ;
588
+ }
589
+
570
590
this . node . services . pubsub . reportMessageValidationResult (
571
591
msgId ,
572
592
source . toString ( ) ,
@@ -582,7 +602,7 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
582
602
return { result, obj : tx } ;
583
603
} ;
584
604
585
- const { result, obj : tx } = await this . validateReceivedMessage < Tx > ( validationFunc , msgId , source ) ;
605
+ const { result, obj : tx } = await this . validateReceivedMessage < Tx > ( validationFunc , msgId , source , TopicType . tx ) ;
586
606
if ( ! result || ! tx ) {
587
607
return ;
588
608
}
@@ -613,6 +633,7 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
613
633
validationFunc ,
614
634
msgId ,
615
635
source ,
636
+ TopicType . block_attestation ,
616
637
) ;
617
638
if ( ! result || ! attestation ) {
618
639
return ;
@@ -640,7 +661,12 @@ export class LibP2PService<T extends P2PClientType = P2PClientType.Full> extends
640
661
return { result, obj : block } ;
641
662
} ;
642
663
643
- const { result, obj : block } = await this . validateReceivedMessage < BlockProposal > ( validationFunc , msgId , source ) ;
664
+ const { result, obj : block } = await this . validateReceivedMessage < BlockProposal > (
665
+ validationFunc ,
666
+ msgId ,
667
+ source ,
668
+ TopicType . block_proposal ,
669
+ ) ;
644
670
if ( ! result || ! block ) {
645
671
return ;
646
672
}
0 commit comments