27
27
use {
28
28
crate :: {
29
29
crds_entry:: CrdsEntry ,
30
+ crds_gossip_pull:: CrdsTimeouts ,
30
31
crds_shards:: CrdsShards ,
31
32
crds_value:: { CrdsData , CrdsValue , CrdsValueLabel } ,
32
33
legacy_contact_info:: LegacyContactInfo as ContactInfo ,
@@ -472,15 +473,12 @@ impl Crds {
472
473
& self ,
473
474
thread_pool : & ThreadPool ,
474
475
now : u64 ,
475
- timeouts : & HashMap < Pubkey , u64 > ,
476
+ timeouts : & CrdsTimeouts ,
476
477
) -> Vec < CrdsValueLabel > {
477
- let default_timeout = * timeouts
478
- . get ( & Pubkey :: default ( ) )
479
- . expect ( "must have default timeout" ) ;
480
478
// Given an index of all crd values associated with a pubkey,
481
479
// returns crds labels of old values to be evicted.
482
480
let evict = |pubkey, index : & IndexSet < usize > | {
483
- let timeout = timeouts. get ( pubkey) . copied ( ) . unwrap_or ( default_timeout ) ;
481
+ let timeout = timeouts[ pubkey] ;
484
482
// If the origin's contact-info hasn't expired yet then preserve
485
483
// all associated values.
486
484
let origin = CrdsValueLabel :: LegacyContactInfo ( * pubkey) ;
@@ -732,7 +730,7 @@ mod tests {
732
730
signature:: { Keypair , Signer } ,
733
731
timing:: timestamp,
734
732
} ,
735
- std:: { collections:: HashSet , iter:: repeat_with, net:: Ipv4Addr } ,
733
+ std:: { collections:: HashSet , iter:: repeat_with, net:: Ipv4Addr , time :: Duration } ,
736
734
} ;
737
735
738
736
#[ test]
@@ -888,17 +886,34 @@ mod tests {
888
886
crds. insert( val. clone( ) , 1 , GossipRoute :: LocalMessage ) ,
889
887
Ok ( ( ) )
890
888
) ;
891
- let mut set = HashMap :: new ( ) ;
892
- set. insert ( Pubkey :: default ( ) , 0 ) ;
893
- assert ! ( crds. find_old_labels( & thread_pool, 0 , & set) . is_empty( ) ) ;
894
- set. insert ( Pubkey :: default ( ) , 1 ) ;
889
+ let pubkey = Pubkey :: new_unique ( ) ;
890
+ let stakes = HashMap :: from ( [ ( Pubkey :: new_unique ( ) , 1u64 ) ] ) ;
891
+ let epoch_duration = Duration :: from_secs ( 48 * 3600 ) ;
892
+ let timeouts = CrdsTimeouts :: new (
893
+ pubkey,
894
+ 0u64 , // default_timeout,
895
+ epoch_duration,
896
+ & stakes,
897
+ ) ;
898
+ assert ! ( crds. find_old_labels( & thread_pool, 0 , & timeouts) . is_empty( ) ) ;
899
+ let timeouts = CrdsTimeouts :: new (
900
+ pubkey,
901
+ 1u64 , // default_timeout,
902
+ epoch_duration,
903
+ & stakes,
904
+ ) ;
895
905
assert_eq ! (
896
- crds. find_old_labels( & thread_pool, 2 , & set ) ,
906
+ crds. find_old_labels( & thread_pool, 2 , & timeouts ) ,
897
907
vec![ val. label( ) ]
898
908
) ;
899
- set. insert ( Pubkey :: default ( ) , 2 ) ;
909
+ let timeouts = CrdsTimeouts :: new (
910
+ pubkey,
911
+ 2u64 , // default_timeout,
912
+ epoch_duration,
913
+ & stakes,
914
+ ) ;
900
915
assert_eq ! (
901
- crds. find_old_labels( & thread_pool, 4 , & set ) ,
916
+ crds. find_old_labels( & thread_pool, 4 , & timeouts ) ,
902
917
vec![ val. label( ) ]
903
918
) ;
904
919
}
@@ -907,24 +922,51 @@ mod tests {
907
922
let thread_pool = ThreadPoolBuilder :: new ( ) . build ( ) . unwrap ( ) ;
908
923
let mut rng = thread_rng ( ) ;
909
924
let mut crds = Crds :: default ( ) ;
910
- let mut timeouts = HashMap :: new ( ) ;
911
925
let val = CrdsValue :: new_rand ( & mut rng, None ) ;
912
- timeouts. insert ( Pubkey :: default ( ) , 3 ) ;
926
+ let mut stakes = HashMap :: from ( [ ( Pubkey :: new_unique ( ) , 1u64 ) ] ) ;
927
+ let timeouts = CrdsTimeouts :: new (
928
+ Pubkey :: new_unique ( ) ,
929
+ 3 , // default_timeout
930
+ Duration :: from_secs ( 48 * 3600 ) , // epoch_duration
931
+ & stakes,
932
+ ) ;
913
933
assert_eq ! (
914
934
crds. insert( val. clone( ) , 0 , GossipRoute :: LocalMessage ) ,
915
935
Ok ( ( ) )
916
936
) ;
917
937
assert ! ( crds. find_old_labels( & thread_pool, 2 , & timeouts) . is_empty( ) ) ;
918
- timeouts. insert ( val. pubkey ( ) , 1 ) ;
938
+ stakes. insert ( val. pubkey ( ) , 1u64 ) ;
939
+ let timeouts = CrdsTimeouts :: new (
940
+ Pubkey :: new_unique ( ) ,
941
+ 1 , // default_timeout
942
+ Duration :: from_millis ( 1 ) , // epoch_duration
943
+ & stakes,
944
+ ) ;
919
945
assert_eq ! (
920
946
crds. find_old_labels( & thread_pool, 2 , & timeouts) ,
921
947
vec![ val. label( ) ]
922
948
) ;
923
- timeouts. insert ( val. pubkey ( ) , u64:: MAX ) ;
949
+ let timeouts = CrdsTimeouts :: new (
950
+ Pubkey :: new_unique ( ) ,
951
+ 3 , // default_timeout
952
+ Duration :: from_secs ( 48 * 3600 ) , // epoch_duration
953
+ & stakes,
954
+ ) ;
924
955
assert ! ( crds. find_old_labels( & thread_pool, 2 , & timeouts) . is_empty( ) ) ;
925
- timeouts. insert ( Pubkey :: default ( ) , 1 ) ;
956
+ let timeouts = CrdsTimeouts :: new (
957
+ Pubkey :: new_unique ( ) ,
958
+ 1 , // default_timeout
959
+ Duration :: from_secs ( 48 * 3600 ) , // epoch_duration
960
+ & stakes,
961
+ ) ;
926
962
assert ! ( crds. find_old_labels( & thread_pool, 2 , & timeouts) . is_empty( ) ) ;
927
- timeouts. remove ( & val. pubkey ( ) ) ;
963
+ stakes. remove ( & val. pubkey ( ) ) ;
964
+ let timeouts = CrdsTimeouts :: new (
965
+ Pubkey :: new_unique ( ) ,
966
+ 1 , // default_timeout
967
+ Duration :: from_secs ( 48 * 3600 ) , // epoch_duration
968
+ & stakes,
969
+ ) ;
928
970
assert_eq ! (
929
971
crds. find_old_labels( & thread_pool, 2 , & timeouts) ,
930
972
vec![ val. label( ) ]
@@ -940,14 +982,19 @@ mod tests {
940
982
crds. insert( val. clone( ) , 1 , GossipRoute :: LocalMessage ) ,
941
983
Ok ( _)
942
984
) ;
943
- let mut set = HashMap :: new ( ) ;
944
- set. insert ( Pubkey :: default ( ) , 1 ) ;
985
+ let stakes = HashMap :: from ( [ ( Pubkey :: new_unique ( ) , 1u64 ) ] ) ;
986
+ let timeouts = CrdsTimeouts :: new (
987
+ Pubkey :: new_unique ( ) ,
988
+ 1 , // default_timeout
989
+ Duration :: from_secs ( 48 * 3600 ) , // epoch_duration
990
+ & stakes,
991
+ ) ;
945
992
assert_eq ! (
946
- crds. find_old_labels( & thread_pool, 2 , & set ) ,
993
+ crds. find_old_labels( & thread_pool, 2 , & timeouts ) ,
947
994
vec![ val. label( ) ]
948
995
) ;
949
996
crds. remove ( & val. label ( ) , /*now=*/ 0 ) ;
950
- assert ! ( crds. find_old_labels( & thread_pool, 2 , & set ) . is_empty( ) ) ;
997
+ assert ! ( crds. find_old_labels( & thread_pool, 2 , & timeouts ) . is_empty( ) ) ;
951
998
}
952
999
#[ test]
953
1000
fn test_find_old_records_staked ( ) {
@@ -961,28 +1008,35 @@ mod tests {
961
1008
crds. insert( val. clone( ) , 1 , GossipRoute :: LocalMessage ) ,
962
1009
Ok ( ( ) )
963
1010
) ;
964
- let mut set = HashMap :: new ( ) ;
1011
+ let mut stakes = HashMap :: from ( [ ( Pubkey :: new_unique ( ) , 1u64 ) ] ) ;
1012
+ let timeouts = CrdsTimeouts :: new (
1013
+ Pubkey :: new_unique ( ) ,
1014
+ 0 , // default_timeout
1015
+ Duration :: from_secs ( 48 * 3600 ) , // epoch_duration
1016
+ & stakes,
1017
+ ) ;
965
1018
//now < timestamp
966
- set. insert ( Pubkey :: default ( ) , 0 ) ;
967
- set. insert ( val. pubkey ( ) , 0 ) ;
968
- assert ! ( crds. find_old_labels( & thread_pool, 0 , & set) . is_empty( ) ) ;
1019
+ assert ! ( crds. find_old_labels( & thread_pool, 0 , & timeouts) . is_empty( ) ) ;
969
1020
970
1021
//pubkey shouldn't expire since its timeout is MAX
971
- set. insert ( val. pubkey ( ) , std:: u64:: MAX ) ;
972
- assert ! ( crds. find_old_labels( & thread_pool, 2 , & set) . is_empty( ) ) ;
973
-
974
- //default has max timeout, but pubkey should still expire
975
- set. insert ( Pubkey :: default ( ) , std:: u64:: MAX ) ;
976
- set. insert ( val. pubkey ( ) , 1 ) ;
977
- assert_eq ! (
978
- crds. find_old_labels( & thread_pool, 2 , & set) ,
979
- vec![ val. label( ) ]
1022
+ stakes. insert ( val. pubkey ( ) , 1u64 ) ;
1023
+ let timeouts = CrdsTimeouts :: new (
1024
+ Pubkey :: new_unique ( ) ,
1025
+ 0 , // default_timeout
1026
+ Duration :: from_secs ( 48 * 3600 ) , // epoch_duration
1027
+ & stakes,
980
1028
) ;
1029
+ assert ! ( crds. find_old_labels( & thread_pool, 2 , & timeouts) . is_empty( ) ) ;
981
1030
982
- set. insert ( val. pubkey ( ) , 2 ) ;
983
- assert ! ( crds. find_old_labels( & thread_pool, 2 , & set) . is_empty( ) ) ;
1031
+ let timeouts = CrdsTimeouts :: new (
1032
+ Pubkey :: new_unique ( ) ,
1033
+ 0 , // default_timeout
1034
+ Duration :: from_millis ( 2 ) , // epoch_duration
1035
+ & stakes,
1036
+ ) ;
1037
+ assert ! ( crds. find_old_labels( & thread_pool, 2 , & timeouts) . is_empty( ) ) ;
984
1038
assert_eq ! (
985
- crds. find_old_labels( & thread_pool, 3 , & set ) ,
1039
+ crds. find_old_labels( & thread_pool, 3 , & timeouts ) ,
986
1040
vec![ val. label( ) ]
987
1041
) ;
988
1042
}
@@ -1353,17 +1407,19 @@ mod tests {
1353
1407
crds. insert( val. clone( ) , 1 , GossipRoute :: LocalMessage ) ,
1354
1408
Ok ( _)
1355
1409
) ;
1356
- let mut set = HashMap :: new ( ) ;
1357
-
1358
- //default has max timeout, but pubkey should still expire
1359
- set. insert ( Pubkey :: default ( ) , std:: u64:: MAX ) ;
1360
- set. insert ( val. pubkey ( ) , 1 ) ;
1410
+ let stakes = HashMap :: from ( [ ( Pubkey :: new_unique ( ) , 1u64 ) ] ) ;
1411
+ let timeouts = CrdsTimeouts :: new (
1412
+ Pubkey :: new_unique ( ) ,
1413
+ 1 , // default_timeout
1414
+ Duration :: from_millis ( 1 ) , // epoch_duration
1415
+ & stakes,
1416
+ ) ;
1361
1417
assert_eq ! (
1362
- crds. find_old_labels( & thread_pool, 2 , & set ) ,
1418
+ crds. find_old_labels( & thread_pool, 2 , & timeouts ) ,
1363
1419
vec![ val. label( ) ]
1364
1420
) ;
1365
1421
crds. remove ( & val. label ( ) , /*now=*/ 0 ) ;
1366
- assert ! ( crds. find_old_labels( & thread_pool, 2 , & set ) . is_empty( ) ) ;
1422
+ assert ! ( crds. find_old_labels( & thread_pool, 2 , & timeouts ) . is_empty( ) ) ;
1367
1423
}
1368
1424
1369
1425
#[ test]
0 commit comments