22
33#[ cfg( feature = "dev-context-only-utils" ) ]
44use arbitrary:: Arbitrary ;
5+ #[ cfg( test) ]
6+ use arbitrary:: Unstructured ;
57#[ cfg( feature = "serde" ) ]
68use serde_derive:: { Deserialize , Serialize } ;
79#[ cfg( feature = "frozen-abi" ) ]
1315 solana_rent:: Rent ,
1416 std:: { collections:: VecDeque , fmt:: Debug } ,
1517} ;
16- #[ cfg( test) ]
17- use { arbitrary:: Unstructured , solana_epoch_schedule:: MAX_LEADER_SCHEDULE_EPOCH_OFFSET } ;
1818
1919mod vote_state_0_23_5;
2020pub mod vote_state_1_14_11;
@@ -399,15 +399,9 @@ pub mod serde_tower_sync {
399399#[ cfg( test) ]
400400mod tests {
401401 use {
402- super :: * ,
403- crate :: { error:: VoteError , state:: vote_state_0_23_5:: VoteState0_23_5 } ,
404- bincode:: serialized_size,
405- core:: mem:: MaybeUninit ,
406- itertools:: Itertools ,
407- rand:: Rng ,
408- solana_clock:: Clock ,
409- solana_hash:: Hash ,
410- solana_instruction:: error:: InstructionError ,
402+ super :: * , crate :: state:: vote_state_0_23_5:: VoteState0_23_5 , bincode:: serialized_size,
403+ core:: mem:: MaybeUninit , itertools:: Itertools , rand:: Rng , solana_clock:: Clock ,
404+ solana_hash:: Hash , solana_instruction:: error:: InstructionError ,
411405 } ;
412406
413407 // Test helper to create a VoteStateV4 with random data for testing
@@ -825,316 +819,6 @@ mod tests {
825819 }
826820 }
827821
828- #[ test]
829- fn test_vote_state_epoch_credits ( ) {
830- let mut vote_state = VoteStateV3 :: default ( ) ;
831-
832- assert_eq ! ( vote_state. credits( ) , 0 ) ;
833- assert_eq ! ( vote_state. epoch_credits( ) . clone( ) , vec![ ] ) ;
834-
835- let mut expected = vec ! [ ] ;
836- let mut credits = 0 ;
837- let epochs = ( MAX_EPOCH_CREDITS_HISTORY + 2 ) as u64 ;
838- for epoch in 0 ..epochs {
839- for _j in 0 ..epoch {
840- vote_state. increment_credits ( epoch, 1 ) ;
841- credits += 1 ;
842- }
843- expected. push ( ( epoch, credits, credits - epoch) ) ;
844- }
845-
846- while expected. len ( ) > MAX_EPOCH_CREDITS_HISTORY {
847- expected. remove ( 0 ) ;
848- }
849-
850- assert_eq ! ( vote_state. credits( ) , credits) ;
851- assert_eq ! ( vote_state. epoch_credits( ) . clone( ) , expected) ;
852- }
853-
854- #[ test]
855- fn test_vote_state_epoch0_no_credits ( ) {
856- let mut vote_state = VoteStateV3 :: default ( ) ;
857-
858- assert_eq ! ( vote_state. epoch_credits( ) . len( ) , 0 ) ;
859- vote_state. increment_credits ( 1 , 1 ) ;
860- assert_eq ! ( vote_state. epoch_credits( ) . len( ) , 1 ) ;
861-
862- vote_state. increment_credits ( 2 , 1 ) ;
863- assert_eq ! ( vote_state. epoch_credits( ) . len( ) , 2 ) ;
864- }
865-
866- #[ test]
867- fn test_vote_state_increment_credits ( ) {
868- let mut vote_state = VoteStateV3 :: default ( ) ;
869-
870- let credits = ( MAX_EPOCH_CREDITS_HISTORY + 2 ) as u64 ;
871- for i in 0 ..credits {
872- vote_state. increment_credits ( i, 1 ) ;
873- }
874- assert_eq ! ( vote_state. credits( ) , credits) ;
875- assert ! ( vote_state. epoch_credits( ) . len( ) <= MAX_EPOCH_CREDITS_HISTORY ) ;
876- }
877-
878- #[ test]
879- fn test_vote_process_timestamp ( ) {
880- let ( slot, timestamp) = ( 15 , 1_575_412_285 ) ;
881- let mut vote_state = VoteStateV3 {
882- last_timestamp : BlockTimestamp { slot, timestamp } ,
883- ..VoteStateV3 :: default ( )
884- } ;
885-
886- assert_eq ! (
887- vote_state. process_timestamp( slot - 1 , timestamp + 1 ) ,
888- Err ( VoteError :: TimestampTooOld )
889- ) ;
890- assert_eq ! (
891- vote_state. last_timestamp,
892- BlockTimestamp { slot, timestamp }
893- ) ;
894- assert_eq ! (
895- vote_state. process_timestamp( slot + 1 , timestamp - 1 ) ,
896- Err ( VoteError :: TimestampTooOld )
897- ) ;
898- assert_eq ! (
899- vote_state. process_timestamp( slot, timestamp + 1 ) ,
900- Err ( VoteError :: TimestampTooOld )
901- ) ;
902- assert_eq ! ( vote_state. process_timestamp( slot, timestamp) , Ok ( ( ) ) ) ;
903- assert_eq ! (
904- vote_state. last_timestamp,
905- BlockTimestamp { slot, timestamp }
906- ) ;
907- assert_eq ! ( vote_state. process_timestamp( slot + 1 , timestamp) , Ok ( ( ) ) ) ;
908- assert_eq ! (
909- vote_state. last_timestamp,
910- BlockTimestamp {
911- slot: slot + 1 ,
912- timestamp
913- }
914- ) ;
915- assert_eq ! (
916- vote_state. process_timestamp( slot + 2 , timestamp + 1 ) ,
917- Ok ( ( ) )
918- ) ;
919- assert_eq ! (
920- vote_state. last_timestamp,
921- BlockTimestamp {
922- slot: slot + 2 ,
923- timestamp: timestamp + 1
924- }
925- ) ;
926-
927- // Test initial vote
928- vote_state. last_timestamp = BlockTimestamp :: default ( ) ;
929- assert_eq ! ( vote_state. process_timestamp( 0 , timestamp) , Ok ( ( ) ) ) ;
930- }
931-
932- #[ test]
933- fn test_get_and_update_authorized_voter ( ) {
934- let original_voter = Pubkey :: new_unique ( ) ;
935- let mut vote_state = VoteStateV3 :: new (
936- & VoteInit {
937- node_pubkey : original_voter,
938- authorized_voter : original_voter,
939- authorized_withdrawer : original_voter,
940- commission : 0 ,
941- } ,
942- & Clock :: default ( ) ,
943- ) ;
944-
945- assert_eq ! ( vote_state. authorized_voters. len( ) , 1 ) ;
946- assert_eq ! (
947- * vote_state. authorized_voters. first( ) . unwrap( ) . 1 ,
948- original_voter
949- ) ;
950-
951- // If no new authorized voter was set, the same authorized voter
952- // is locked into the next epoch
953- assert_eq ! (
954- vote_state. get_and_update_authorized_voter( 1 ) . unwrap( ) ,
955- original_voter
956- ) ;
957-
958- // Try to get the authorized voter for epoch 5, implies
959- // the authorized voter for epochs 1-4 were unchanged
960- assert_eq ! (
961- vote_state. get_and_update_authorized_voter( 5 ) . unwrap( ) ,
962- original_voter
963- ) ;
964-
965- // Authorized voter for expired epoch 0..5 should have been
966- // purged and no longer queryable
967- assert_eq ! ( vote_state. authorized_voters. len( ) , 1 ) ;
968- for i in 0 ..5 {
969- assert ! ( vote_state
970- . authorized_voters
971- . get_authorized_voter( i)
972- . is_none( ) ) ;
973- }
974-
975- // Set an authorized voter change at slot 7
976- let new_authorized_voter = Pubkey :: new_unique ( ) ;
977- vote_state
978- . set_new_authorized_voter ( & new_authorized_voter, 5 , 7 , |_| Ok ( ( ) ) )
979- . unwrap ( ) ;
980-
981- // Try to get the authorized voter for epoch 6, unchanged
982- assert_eq ! (
983- vote_state. get_and_update_authorized_voter( 6 ) . unwrap( ) ,
984- original_voter
985- ) ;
986-
987- // Try to get the authorized voter for epoch 7 and onwards, should
988- // be the new authorized voter
989- for i in 7 ..10 {
990- assert_eq ! (
991- vote_state. get_and_update_authorized_voter( i) . unwrap( ) ,
992- new_authorized_voter
993- ) ;
994- }
995- assert_eq ! ( vote_state. authorized_voters. len( ) , 1 ) ;
996- }
997-
998- #[ test]
999- fn test_set_new_authorized_voter ( ) {
1000- let original_voter = Pubkey :: new_unique ( ) ;
1001- let epoch_offset = 15 ;
1002- let mut vote_state = VoteStateV3 :: new (
1003- & VoteInit {
1004- node_pubkey : original_voter,
1005- authorized_voter : original_voter,
1006- authorized_withdrawer : original_voter,
1007- commission : 0 ,
1008- } ,
1009- & Clock :: default ( ) ,
1010- ) ;
1011-
1012- assert ! ( vote_state. prior_voters. last( ) . is_none( ) ) ;
1013-
1014- let new_voter = Pubkey :: new_unique ( ) ;
1015- // Set a new authorized voter
1016- vote_state
1017- . set_new_authorized_voter ( & new_voter, 0 , epoch_offset, |_| Ok ( ( ) ) )
1018- . unwrap ( ) ;
1019-
1020- assert_eq ! ( vote_state. prior_voters. idx, 0 ) ;
1021- assert_eq ! (
1022- vote_state. prior_voters. last( ) ,
1023- Some ( & ( original_voter, 0 , epoch_offset) )
1024- ) ;
1025-
1026- // Trying to set authorized voter for same epoch again should fail
1027- assert_eq ! (
1028- vote_state. set_new_authorized_voter( & new_voter, 0 , epoch_offset, |_| Ok ( ( ) ) ) ,
1029- Err ( VoteError :: TooSoonToReauthorize . into( ) )
1030- ) ;
1031-
1032- // Setting the same authorized voter again should succeed
1033- vote_state
1034- . set_new_authorized_voter ( & new_voter, 2 , 2 + epoch_offset, |_| Ok ( ( ) ) )
1035- . unwrap ( ) ;
1036-
1037- // Set a third and fourth authorized voter
1038- let new_voter2 = Pubkey :: new_unique ( ) ;
1039- vote_state
1040- . set_new_authorized_voter ( & new_voter2, 3 , 3 + epoch_offset, |_| Ok ( ( ) ) )
1041- . unwrap ( ) ;
1042- assert_eq ! ( vote_state. prior_voters. idx, 1 ) ;
1043- assert_eq ! (
1044- vote_state. prior_voters. last( ) ,
1045- Some ( & ( new_voter, epoch_offset, 3 + epoch_offset) )
1046- ) ;
1047-
1048- let new_voter3 = Pubkey :: new_unique ( ) ;
1049- vote_state
1050- . set_new_authorized_voter ( & new_voter3, 6 , 6 + epoch_offset, |_| Ok ( ( ) ) )
1051- . unwrap ( ) ;
1052- assert_eq ! ( vote_state. prior_voters. idx, 2 ) ;
1053- assert_eq ! (
1054- vote_state. prior_voters. last( ) ,
1055- Some ( & ( new_voter2, 3 + epoch_offset, 6 + epoch_offset) )
1056- ) ;
1057-
1058- // Check can set back to original voter
1059- vote_state
1060- . set_new_authorized_voter ( & original_voter, 9 , 9 + epoch_offset, |_| Ok ( ( ) ) )
1061- . unwrap ( ) ;
1062-
1063- // Run with these voters for a while, check the ranges of authorized
1064- // voters is correct
1065- for i in 9 ..epoch_offset {
1066- assert_eq ! (
1067- vote_state. get_and_update_authorized_voter( i) . unwrap( ) ,
1068- original_voter
1069- ) ;
1070- }
1071- for i in epoch_offset..3 + epoch_offset {
1072- assert_eq ! (
1073- vote_state. get_and_update_authorized_voter( i) . unwrap( ) ,
1074- new_voter
1075- ) ;
1076- }
1077- for i in 3 + epoch_offset..6 + epoch_offset {
1078- assert_eq ! (
1079- vote_state. get_and_update_authorized_voter( i) . unwrap( ) ,
1080- new_voter2
1081- ) ;
1082- }
1083- for i in 6 + epoch_offset..9 + epoch_offset {
1084- assert_eq ! (
1085- vote_state. get_and_update_authorized_voter( i) . unwrap( ) ,
1086- new_voter3
1087- ) ;
1088- }
1089- for i in 9 + epoch_offset..=10 + epoch_offset {
1090- assert_eq ! (
1091- vote_state. get_and_update_authorized_voter( i) . unwrap( ) ,
1092- original_voter
1093- ) ;
1094- }
1095- }
1096-
1097- #[ test]
1098- fn test_authorized_voter_is_locked_within_epoch ( ) {
1099- let original_voter = Pubkey :: new_unique ( ) ;
1100- let mut vote_state = VoteStateV3 :: new (
1101- & VoteInit {
1102- node_pubkey : original_voter,
1103- authorized_voter : original_voter,
1104- authorized_withdrawer : original_voter,
1105- commission : 0 ,
1106- } ,
1107- & Clock :: default ( ) ,
1108- ) ;
1109-
1110- // Test that it's not possible to set a new authorized
1111- // voter within the same epoch, even if none has been
1112- // explicitly set before
1113- let new_voter = Pubkey :: new_unique ( ) ;
1114- assert_eq ! (
1115- vote_state. set_new_authorized_voter( & new_voter, 1 , 1 , |_| Ok ( ( ) ) ) ,
1116- Err ( VoteError :: TooSoonToReauthorize . into( ) )
1117- ) ;
1118-
1119- assert_eq ! ( vote_state. get_authorized_voter( 1 ) , Some ( original_voter) ) ;
1120-
1121- // Set a new authorized voter for a future epoch
1122- assert_eq ! (
1123- vote_state. set_new_authorized_voter( & new_voter, 1 , 2 , |_| Ok ( ( ) ) ) ,
1124- Ok ( ( ) )
1125- ) ;
1126-
1127- // Test that it's not possible to set a new authorized
1128- // voter within the same epoch, even if none has been
1129- // explicitly set before
1130- assert_eq ! (
1131- vote_state. set_new_authorized_voter( & original_voter, 3 , 3 , |_| Ok ( ( ) ) ) ,
1132- Err ( VoteError :: TooSoonToReauthorize . into( ) )
1133- ) ;
1134-
1135- assert_eq ! ( vote_state. get_authorized_voter( 3 ) , Some ( new_voter) ) ;
1136- }
1137-
1138822 #[ test]
1139823 fn test_vote_state_v3_size_of ( ) {
1140824 let vote_state = VoteStateV3 :: get_max_sized_vote_state ( ) ;
@@ -1151,31 +835,6 @@ mod tests {
1151835 assert ! ( size < VoteStateV4 :: size_of( ) as u64 ) ; // v4 is smaller than the max size
1152836 }
1153837
1154- #[ test]
1155- fn test_vote_state_max_size ( ) {
1156- let mut max_sized_data = vec ! [ 0 ; VoteStateV3 :: size_of( ) ] ;
1157- let vote_state = VoteStateV3 :: get_max_sized_vote_state ( ) ;
1158- let ( start_leader_schedule_epoch, _) = vote_state. authorized_voters . last ( ) . unwrap ( ) ;
1159- let start_current_epoch =
1160- start_leader_schedule_epoch - MAX_LEADER_SCHEDULE_EPOCH_OFFSET + 1 ;
1161-
1162- let mut vote_state = Some ( vote_state) ;
1163- for i in start_current_epoch..start_current_epoch + 2 * MAX_LEADER_SCHEDULE_EPOCH_OFFSET {
1164- vote_state. as_mut ( ) . map ( |vote_state| {
1165- vote_state. set_new_authorized_voter (
1166- & Pubkey :: new_unique ( ) ,
1167- i,
1168- i + MAX_LEADER_SCHEDULE_EPOCH_OFFSET ,
1169- |_| Ok ( ( ) ) ,
1170- )
1171- } ) ;
1172-
1173- let versioned = VoteStateVersions :: new_v3 ( vote_state. take ( ) . unwrap ( ) ) ;
1174- VoteStateV3 :: serialize ( & versioned, & mut max_sized_data) . unwrap ( ) ;
1175- vote_state = Some ( versioned. try_convert_to_v3 ( ) . unwrap ( ) ) ;
1176- }
1177- }
1178-
1179838 #[ test]
1180839 fn test_default_vote_state_is_uninitialized ( ) {
1181840 // The default `VoteStateV3` is stored to de-initialize a zero-balance vote account,
0 commit comments