@@ -208,13 +208,15 @@ where
208208 }
209209
210210 /// The pids of peers which have not promised a higher ballot than mine.
211- pub fn get_preparable_peers ( & self ) -> Vec < NodeId > {
212- self . promises_meta
211+ pub ( crate ) fn get_preparable_peers ( & self , peers : & [ NodeId ] ) -> Vec < NodeId > {
212+ peers
213213 . iter ( )
214- . enumerate ( )
215- . filter_map ( |( idx, x) | match x {
216- PromiseState :: NotPromised => Some ( ( idx + 1 ) as NodeId ) ,
217- _ => None ,
214+ . filter_map ( |pid| {
215+ let idx = Self :: pid_to_idx ( * pid) ;
216+ match self . promises_meta . get ( idx) . unwrap ( ) {
217+ PromiseState :: NotPromised => Some ( * pid) ,
218+ _ => None ,
219+ }
218220 } )
219221 . collect ( )
220222 }
@@ -467,3 +469,34 @@ pub(crate) struct AcceptedMetaData<T: Entry> {
467469 #[ cfg( feature = "unicache" ) ]
468470 pub entries : Vec < T :: EncodeResult > ,
469471}
472+
473+ #[ cfg( not( feature = "unicache" ) ) ]
474+ #[ cfg( test) ]
475+ mod tests {
476+ use super :: * ; // Import functions and types from this module
477+ use crate :: storage:: NoSnapshot ;
478+ #[ test]
479+ fn preparable_peers_test ( ) {
480+ type Value = ( ) ;
481+
482+ impl Entry for Value {
483+ type Snapshot = NoSnapshot ;
484+ }
485+
486+ let nodes = vec ! [ 6 , 7 , 8 ] ;
487+ let quorum = Quorum :: Majority ( 2 ) ;
488+ let max_pid = 8 ;
489+ let leader_state =
490+ LeaderState :: < Value > :: with ( Ballot :: with ( 1 , 1 , 1 , max_pid) , max_pid as usize , quorum) ;
491+ let prep_peers = leader_state. get_preparable_peers ( & nodes) ;
492+ assert_eq ! ( prep_peers, nodes) ;
493+
494+ let nodes = vec ! [ 7 , 1 , 100 , 4 , 6 ] ;
495+ let quorum = Quorum :: Majority ( 3 ) ;
496+ let max_pid = 100 ;
497+ let leader_state =
498+ LeaderState :: < Value > :: with ( Ballot :: with ( 1 , 1 , 1 , max_pid) , max_pid as usize , quorum) ;
499+ let prep_peers = leader_state. get_preparable_peers ( & nodes) ;
500+ assert_eq ! ( prep_peers, nodes) ;
501+ }
502+ }
0 commit comments