1- use std:: sync:: Arc ;
1+ use std:: sync:: { atomic :: Ordering , Arc } ;
22
33use crate :: {
44 grpc:: {
@@ -12,6 +12,7 @@ use crate::{
1212 StakePoolEntry , TechnicalCommitteeDatumRequest , TechnicalCommitteeDatumResponse ,
1313 UtxoEvent , UtxoEventsRequest , UtxoEventsResponse ,
1414 } ,
15+ stats:: { RequestStats , RequestStatsSnapshot } ,
1516 utxo_events:: truncate_by_tx_capacity,
1617 } ,
1718 state:: State ,
@@ -33,14 +34,24 @@ use tonic::{Request, Response, Status};
3334
3435const MAX_EVENTS_PER_TX : usize = 64 ;
3536
37+ #[ derive( Clone ) ]
3638pub struct MidnightStateService {
3739 history : Arc < Mutex < StateHistory < State > > > ,
3840 context : Arc < Context < Message > > ,
41+ stats : Arc < RequestStats > ,
3942}
4043
4144impl MidnightStateService {
4245 pub fn new ( history : Arc < Mutex < StateHistory < State > > > , context : Arc < Context < Message > > ) -> Self {
43- Self { history, context }
46+ Self {
47+ history,
48+ context,
49+ stats : Arc :: new ( RequestStats :: default ( ) ) ,
50+ }
51+ }
52+
53+ pub fn stats ( & self ) -> RequestStatsSnapshot {
54+ self . stats . snapshot ( )
4455 }
4556}
4657
@@ -176,6 +187,7 @@ impl MidnightState for MidnightStateService {
176187 & self ,
177188 request : Request < UtxoEventsRequest > ,
178189 ) -> Result < Response < UtxoEventsResponse > , Status > {
190+ self . stats . utxo_events . fetch_add ( 1 , Ordering :: Relaxed ) ;
179191 let req = request. into_inner ( ) ;
180192
181193 let start_block = req. start_block ;
@@ -251,6 +263,7 @@ impl MidnightState for MidnightStateService {
251263 & self ,
252264 request : Request < TechnicalCommitteeDatumRequest > ,
253265 ) -> Result < Response < TechnicalCommitteeDatumResponse > , Status > {
266+ self . stats . technical_committee_datum . fetch_add ( 1 , Ordering :: Relaxed ) ;
254267 let req = request. into_inner ( ) ;
255268
256269 let technical_committee = {
@@ -281,6 +294,7 @@ impl MidnightState for MidnightStateService {
281294 & self ,
282295 request : Request < CouncilDatumRequest > ,
283296 ) -> Result < Response < CouncilDatumResponse > , Status > {
297+ self . stats . council_datum . fetch_add ( 1 , Ordering :: Relaxed ) ;
284298 let req = request. into_inner ( ) ;
285299
286300 let council = {
@@ -311,6 +325,7 @@ impl MidnightState for MidnightStateService {
311325 & self ,
312326 request : Request < AriadneParametersRequest > ,
313327 ) -> Result < Response < AriadneParametersResponse > , Status > {
328+ self . stats . ariadne_parameters . fetch_add ( 1 , Ordering :: Relaxed ) ;
314329 let req = request. into_inner ( ) ;
315330
316331 let params = {
@@ -341,6 +356,7 @@ impl MidnightState for MidnightStateService {
341356 & self ,
342357 request : Request < BlockByHashRequest > ,
343358 ) -> Result < Response < BlockByHashResponse > , Status > {
359+ self . stats . block_by_hash . fetch_add ( 1 , Ordering :: Relaxed ) ;
344360 let req = request. into_inner ( ) ;
345361 let block_hash = BlockHash :: try_from ( req. block_hash )
346362 . map_err ( |_| Status :: invalid_argument ( "invalid block hash" ) ) ?;
@@ -383,6 +399,7 @@ impl MidnightState for MidnightStateService {
383399 & self ,
384400 request : Request < EpochNonceRequest > ,
385401 ) -> Result < Response < EpochNonceResponse > , Status > {
402+ self . stats . epoch_nonce . fetch_add ( 1 , Ordering :: Relaxed ) ;
386403 let req = request. into_inner ( ) ;
387404
388405 let nonce_opt = {
@@ -400,6 +417,7 @@ impl MidnightState for MidnightStateService {
400417 & self ,
401418 request : Request < EpochCandidatesRequest > ,
402419 ) -> Result < Response < EpochCandidatesResponse > , Status > {
420+ self . stats . epoch_candidates . fetch_add ( 1 , Ordering :: Relaxed ) ;
403421 let req = request. into_inner ( ) ;
404422
405423 let candidates = {
0 commit comments