1- use std:: collections:: { BTreeMap , BTreeSet } ;
1+ use std:: {
2+ collections:: { BTreeMap , BTreeSet } ,
3+ time:: Duration ,
4+ } ;
25
36use serde:: Serialize ;
47use sim_core:: {
@@ -9,6 +12,24 @@ use sim_core::{
912
1013use super :: { EndorserBlockId , InputBlockId , OutputEvent , VoteBundleId } ;
1114
15+ #[ derive( PartialEq , Eq , PartialOrd , Ord ) ]
16+ enum TransactionStatus {
17+ Created ,
18+ InIb ,
19+ InEb ,
20+ OnChain ,
21+ }
22+
23+ #[ derive( Serialize , Default ) ]
24+ #[ serde( rename_all = "camelCase" ) ]
25+ struct TransactionCounts {
26+ timestamp : Timestamp ,
27+ created : u64 ,
28+ in_ib : u64 ,
29+ in_eb : u64 ,
30+ on_chain : u64 ,
31+ }
32+
1233#[ derive( Default ) ]
1334pub struct TraceAggregator {
1435 current_time : Timestamp ,
@@ -17,15 +38,19 @@ pub struct TraceAggregator {
1738 ibs : BTreeMap < InputBlockId , InputBlock > ,
1839 ebs : BTreeMap < EndorserBlockId , EndorsementBlock > ,
1940 rbs : Vec < Block > ,
41+ tx_counts : Vec < TransactionCounts > ,
2042 nodes : BTreeMap < Node , NodeAggregatedData > ,
2143 bytes : BTreeMap < MessageId , u64 > ,
44+ tx_statuses : BTreeMap < TransactionId , TransactionStatus > ,
2245 leios_txs : BTreeSet < TransactionId > ,
2346 praos_txs : BTreeSet < TransactionId > ,
2447}
2548
2649impl TraceAggregator {
2750 pub fn new ( ) -> Self {
28- Self :: default ( )
51+ let mut me = Self :: default ( ) ;
52+ me. tx_counts . push ( TransactionCounts :: default ( ) ) ;
53+ me
2954 }
3055
3156 pub fn process ( & mut self , event : OutputEvent ) -> Option < AggregatedData > {
@@ -42,6 +67,7 @@ impl TraceAggregator {
4267 bytes : size_bytes,
4368 } ,
4469 ) ;
70+ self . tx_statuses . insert ( id, TransactionStatus :: Created ) ;
4571 self . track_data_generated ( MessageId :: TX ( id) , publisher, size_bytes) ;
4672 }
4773 Event :: TXSent { id, sender, .. } => {
@@ -73,6 +99,15 @@ impl TraceAggregator {
7399 . collect ( ) ,
74100 } ,
75101 ) ;
102+ for tx in transactions {
103+ let status = self
104+ . tx_statuses
105+ . entry ( tx)
106+ . or_insert ( TransactionStatus :: InIb ) ;
107+ if * status == TransactionStatus :: Created {
108+ * status = TransactionStatus :: InIb ;
109+ }
110+ }
76111 self . track_data_generated ( MessageId :: IB ( id) , producer, size_bytes) ;
77112 }
78113 Event :: IBSent { id, sender, .. } => {
@@ -108,6 +143,19 @@ impl TraceAggregator {
108143 . collect ( ) ,
109144 } ,
110145 ) ;
146+ for tx in input_blocks
147+ . iter ( )
148+ . map ( |ib| self . ibs . get ( & ib. id ) . unwrap ( ) )
149+ . flat_map ( |ib| ib. txs . iter ( ) )
150+ {
151+ let status = self
152+ . tx_statuses
153+ . entry ( tx. id )
154+ . or_insert ( TransactionStatus :: InEb ) ;
155+ if matches ! ( status, TransactionStatus :: Created | TransactionStatus :: InIb ) {
156+ * status = TransactionStatus :: InEb ;
157+ }
158+ }
111159 self . track_data_generated ( MessageId :: EB ( id) , producer, size_bytes) ;
112160 }
113161 Event :: EBSent { id, sender, .. } => {
@@ -140,6 +188,7 @@ impl TraceAggregator {
140188 ..
141189 } => {
142190 for id in & transactions {
191+ self . tx_statuses . insert ( * id, TransactionStatus :: OnChain ) ;
143192 self . praos_txs . insert ( * id) ;
144193 }
145194 for tx in endorsement
@@ -149,6 +198,7 @@ impl TraceAggregator {
149198 . flat_map ( |eb| & eb. ibs )
150199 . flat_map ( |ib| ib. txs . iter ( ) )
151200 {
201+ self . tx_statuses . insert ( tx. id , TransactionStatus :: OnChain ) ;
152202 self . leios_txs . insert ( tx. id ) ;
153203 }
154204 self . rbs . push ( Block {
@@ -180,6 +230,10 @@ impl TraceAggregator {
180230 let new_chunk = ( event. time_s - Timestamp :: zero ( ) ) . as_millis ( ) / 250 ;
181231 self . current_time = event. time_s ;
182232 if current_chunk != new_chunk {
233+ if new_chunk % 4 == 0 {
234+ let timestamp = Duration :: from_secs ( ( new_chunk / 4 ) as u64 ) . into ( ) ;
235+ self . tx_counts . push ( self . produce_tx_counts ( timestamp) ) ;
236+ }
183237 Some ( self . produce_message ( ) )
184238 } else {
185239 None
@@ -194,6 +248,22 @@ impl TraceAggregator {
194248 }
195249 }
196250
251+ fn produce_tx_counts ( & self , timestamp : Timestamp ) -> TransactionCounts {
252+ let mut tx_counts = TransactionCounts {
253+ timestamp,
254+ ..TransactionCounts :: default ( )
255+ } ;
256+ for status in self . tx_statuses . values ( ) {
257+ match status {
258+ TransactionStatus :: Created => tx_counts. created += 1 ,
259+ TransactionStatus :: InIb => tx_counts. in_ib += 1 ,
260+ TransactionStatus :: InEb => tx_counts. in_eb += 1 ,
261+ TransactionStatus :: OnChain => tx_counts. on_chain += 1 ,
262+ }
263+ }
264+ tx_counts
265+ }
266+
197267 fn produce_message ( & mut self ) -> AggregatedData {
198268 let nodes_updated = std:: mem:: take ( & mut self . nodes_updated ) ;
199269 AggregatedData {
@@ -204,6 +274,7 @@ impl TraceAggregator {
204274 leios_tx_on_chain : self . leios_txs . len ( ) as u64 ,
205275 } ,
206276 blocks : std:: mem:: take ( & mut self . rbs ) ,
277+ transactions : std:: mem:: take ( & mut self . tx_counts ) ,
207278 last_nodes_updated : nodes_updated. into_iter ( ) . collect ( ) ,
208279 }
209280 }
@@ -243,6 +314,7 @@ pub struct AggregatedData {
243314 nodes : BTreeMap < Node , NodeAggregatedData > ,
244315 global : GlobalAggregatedData ,
245316 blocks : Vec < Block > ,
317+ transactions : Vec < TransactionCounts > ,
246318 last_nodes_updated : Vec < Node > ,
247319}
248320
0 commit comments