33//! This crate provides a simple implementation of a derivation pipeline that transforms a batch
44//! into payload attributes for block building.
55
6- #![ cfg_attr( not( feature = "std" ) , no_std) ]
7-
86mod data_source;
97
10- pub use error:: DerivationPipelineError ;
118mod error;
9+ pub use error:: DerivationPipelineError ;
1210
13- # [ cfg ( not ( feature = "std" ) ) ]
14- extern crate alloc as std ;
11+ mod metrics ;
12+ pub use metrics :: DerivationPipelineMetrics ;
1513
1614use crate :: data_source:: CodecDataSource ;
17- use std:: { boxed:: Box , collections:: VecDeque , sync:: Arc , vec:: Vec } ;
15+ use std:: { boxed:: Box , collections:: VecDeque , sync:: Arc , time :: Instant , vec:: Vec } ;
1816
1917use alloy_primitives:: { Address , B256 } ;
2018use alloy_rpc_types_engine:: PayloadAttributes ;
@@ -61,6 +59,8 @@ pub struct DerivationPipeline<P> {
6159 attributes_queue : VecDeque < ScrollPayloadAttributesWithBatchInfo > ,
6260 /// The waker for the pipeline.
6361 waker : Option < Waker > ,
62+ /// The metrics of the pipeline.
63+ metrics : DerivationPipelineMetrics ,
6464}
6565
6666impl < P > DerivationPipeline < P >
7676 pipeline_futures : Default :: default ( ) ,
7777 attributes_queue : Default :: default ( ) ,
7878 waker : None ,
79+ metrics : DerivationPipelineMetrics :: default ( ) ,
7980 }
8081 }
8182
@@ -93,10 +94,13 @@ where
9394 /// futures.
9495 fn handle_next_batch ( & mut self ) -> Option < DerivationPipelineFuture > {
9596 let database = self . database . clone ( ) ;
97+ let metrics = self . metrics . clone ( ) ;
9698 let provider = self . l1_provider . clone ( ) ;
9799
98100 if let Some ( info) = self . batch_queue . pop_front ( ) {
99101 let fut = Box :: pin ( async move {
102+ let derive_start = Instant :: now ( ) ;
103+
100104 // get the batch commit data.
101105 let batch = database
102106 . get_batch_by_index ( info. index )
@@ -107,6 +111,12 @@ where
107111 // derive the attributes and attach the corresponding batch info.
108112 let attrs =
109113 derive ( batch, provider, database) . await . map_err ( |err| ( info. clone ( ) , err) ) ?;
114+
115+ // update metrics.
116+ metrics. derived_blocks . increment ( attrs. len ( ) as u64 ) ;
117+ let execution_duration = derive_start. elapsed ( ) . as_secs_f64 ( ) ;
118+ metrics. blocks_per_second . set ( attrs. len ( ) as f64 / execution_duration) ;
119+
110120 Ok ( attrs. into_iter ( ) . map ( |attr| ( attr, * info) . into ( ) ) . collect ( ) )
111121 } ) ;
112122 return Some ( fut) ;
@@ -415,14 +425,14 @@ mod tests {
415425 break
416426 }
417427 }
418- let expected = ScrollPayloadAttributes {
419- payload_attributes : PayloadAttributes {
428+ let expected = ScrollPayloadAttributes {
429+ payload_attributes : PayloadAttributes {
420430 timestamp : 1696935657 ,
421431 ..Default :: default ( )
422432 } ,
423433 transactions : Some ( vec ! [ bytes!( "f88c8202658417d7840082a4f294530000000000000000000000000000000000000280a4bede39b500000000000000000000000000000000000000000000000000000001669aa2f583104ec4a07461e6555f927393ebdf5f183738450c3842bc3b86a1db7549d9bee21fadd0b1a06d7ba96897bd9fb8e838a327d3ca34be66da11955f10d1fb2264949071e9e8cd" ) ] ) ,
424434 no_tx_pool : true ,
425- block_data_hint : Some ( BlockDataHint { extra_data : bytes ! ( "d883050000846765746888676f312e31392e31856c696e757800000000000000909feed55823062d606d517c3f971201615962991dbc2c1ef5897b3b2c9bd0b93b5bae6e5cedbddfc4bcbbe731477beaca8b3eff111de7ac67a388850405265200" ) , difficulty : U256 :: from ( 2 ) } ) ,
435+ block_data_hint : Some ( BlockDataHint { extra_data : bytes ! ( "d883050000846765746888676f312e31392e31856c696e757800000000000000909feed55823062d606d517c3f971201615962991dbc2c1ef5897b3b2c9bd0b93b5bae6e5cedbddfc4bcbbe731477beaca8b3eff111de7ac67a388850405265200" ) , difficulty : U256 :: from ( 2 ) } ) ,
426436 } ;
427437 assert_eq ! ( attribute, expected) ;
428438
@@ -478,20 +488,20 @@ mod tests {
478488 let attribute =
479489 attributes. iter ( ) . find ( |a| a. payload_attributes . timestamp == 1696935384 ) . unwrap ( ) ;
480490
481- let expected = ScrollPayloadAttributes {
482- payload_attributes : PayloadAttributes {
491+ let expected = ScrollPayloadAttributes {
492+ payload_attributes : PayloadAttributes {
483493 timestamp : 1696935384 ,
484494 ..Default :: default ( )
485495 } ,
486496 transactions : Some ( vec ! [ bytes!( "7ef901b7218302904094781e90f1c8fc4611c9b7497c3b47f99ef6969cbc80b901848ef1332e0000000000000000000000007f2b8c31f88b6006c382775eea88297ec1e3e9050000000000000000000000006ea73e05adc79974b931123675ea8f78ffdacdf0000000000000000000000000000000000000000000000000006a94d74f430000000000000000000000000000000000000000000000000000000000000000002100000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000a4232e8748000000000000000000000000ca266224613396a0e8d4c2497dbc4f33dd6cdeff000000000000000000000000ca266224613396a0e8d4c2497dbc4f33dd6cdeff000000000000000000000000000000000000000000000000006a94d74f4300000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000947885bcbd5cecef1336b5300fb5186a12ddd8c478" ) , bytes!( "7ef901b7228302904094781e90f1c8fc4611c9b7497c3b47f99ef6969cbc80b901848ef1332e0000000000000000000000007f2b8c31f88b6006c382775eea88297ec1e3e9050000000000000000000000006ea73e05adc79974b931123675ea8f78ffdacdf000000000000000000000000000000000000000000000000000470de4df820000000000000000000000000000000000000000000000000000000000000000002200000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000a4232e8748000000000000000000000000982fe4a7cbd74bb3422ebe46333c3e8046c12c7f000000000000000000000000982fe4a7cbd74bb3422ebe46333c3e8046c12c7f00000000000000000000000000000000000000000000000000470de4df8200000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000947885bcbd5cecef1336b5300fb5186a12ddd8c478" ) ] ) ,
487497 no_tx_pool : true ,
488- block_data_hint : None
498+ block_data_hint : None ,
489499 } ;
490500 assert_eq ! ( attribute, & expected) ;
491501
492502 let attribute = attributes. last ( ) . unwrap ( ) ;
493- let expected = ScrollPayloadAttributes {
494- payload_attributes : PayloadAttributes {
503+ let expected = ScrollPayloadAttributes {
504+ payload_attributes : PayloadAttributes {
495505 timestamp : 1696935657 ,
496506 ..Default :: default ( )
497507 } ,
0 commit comments