@@ -22,10 +22,12 @@ use snarkvm::{
22
22
narwhal:: { BatchCertificate , BatchHeader , Transmission , TransmissionID } ,
23
23
} ,
24
24
prelude:: { Address , Field , Network , Result , anyhow, bail, ensure} ,
25
+ utilities:: { cfg_into_iter, cfg_sorted_by} ,
25
26
} ;
26
27
27
28
use indexmap:: { IndexMap , IndexSet , map:: Entry } ;
28
29
use parking_lot:: RwLock ;
30
+ use rayon:: iter:: { IntoParallelIterator , ParallelIterator } ;
29
31
use std:: {
30
32
collections:: { HashMap , HashSet } ,
31
33
sync:: {
@@ -327,30 +329,25 @@ impl<N: Network> Storage<N> {
327
329
/// Returns the certificates that have not yet been included in the ledger.
328
330
/// Note that the order of this set is by round and then insertion.
329
331
pub ( crate ) fn get_pending_certificates ( & self ) -> IndexSet < BatchCertificate < N > > {
330
- let mut pending_certificates = IndexSet :: new ( ) ;
331
-
332
332
// Obtain the read locks.
333
333
let rounds = self . rounds . read ( ) ;
334
334
let certificates = self . certificates . read ( ) ;
335
335
336
336
// Iterate over the rounds.
337
- for ( _, certificates_for_round) in rounds. clone ( ) . sorted_by ( |a, _, b, _| a. cmp ( b) ) {
338
- // Iterate over the certificates for the round.
339
- for ( certificate_id, _, _) in certificates_for_round {
340
- // Skip the certificate if it already exists in the ledger.
341
- if self . ledger . contains_certificate ( & certificate_id) . unwrap_or ( false ) {
342
- continue ;
343
- }
344
-
345
- // Add the certificate to the pending certificates.
346
- match certificates. get ( & certificate_id) . cloned ( ) {
347
- Some ( certificate) => pending_certificates. insert ( certificate) ,
348
- None => continue ,
349
- } ;
350
- }
351
- }
352
-
353
- pending_certificates
337
+ cfg_sorted_by ! ( rounds. clone( ) , |a, _, b, _| a. cmp( b) )
338
+ . flat_map ( |( _, certificates_for_round) | {
339
+ // Iterate over the certificates for the round.
340
+ cfg_into_iter ! ( certificates_for_round) . filter_map ( |( certificate_id, _, _) | {
341
+ // Skip the certificate if it already exists in the ledger.
342
+ if self . ledger . contains_certificate ( & certificate_id) . unwrap_or ( false ) {
343
+ None
344
+ } else {
345
+ // Add the certificate to the pending certificates.
346
+ certificates. get ( & certificate_id) . cloned ( )
347
+ }
348
+ } )
349
+ } )
350
+ . collect ( )
354
351
}
355
352
356
353
/// Checks the given `batch_header` for validity, returning the missing transmissions from storage.
0 commit comments