@@ -62,3 +62,54 @@ fn write_last_height_return_error_when_previous_last_height_is_equal() {
6262 Err ( HeightVotedStorageError :: InconsistentStorageState { error_msg: _ } )
6363 ) ;
6464}
65+
66+ #[ test]
67+ fn revert_height_when_no_last_height_in_storage_does_nothing ( ) {
68+ let mut storage = get_voted_height_storage ( get_new_storage_config ( ) ) ;
69+ assert ! ( storage. get_prev_voted_height( ) . unwrap( ) . is_none( ) ) ;
70+ storage. revert_height ( BlockNumber ( 1 ) ) . unwrap ( ) ;
71+ assert ! ( storage. get_prev_voted_height( ) . unwrap( ) . is_none( ) ) ;
72+ }
73+
74+ #[ test]
75+ fn revert_height_when_last_height_in_storage_is_lower_than_height_to_revert_to_does_nothing ( ) {
76+ const HEIGHT_TO_REVERT_TO : BlockNumber = BlockNumber ( 2 ) ;
77+ // Storage has a lower height than what we revert (so should be a no-op)
78+ let last_height_in_storage = HEIGHT_TO_REVERT_TO . prev ( ) . unwrap ( ) ;
79+
80+ let mut storage = get_voted_height_storage ( get_new_storage_config ( ) ) ;
81+ storage. set_prev_voted_height ( last_height_in_storage) . unwrap ( ) ;
82+ storage. revert_height ( HEIGHT_TO_REVERT_TO ) . unwrap ( ) ;
83+ assert_eq ! ( storage. get_prev_voted_height( ) . unwrap( ) , Some ( last_height_in_storage) ) ;
84+ }
85+
86+ #[ test]
87+ fn revert_height_when_last_height_in_storage_is_higher_than_revert_height_reverts_the_given_height ( )
88+ {
89+ const HEIGH_TO_REVERT_TO : BlockNumber = BlockNumber ( 2 ) ;
90+ // Storage has a higher height than what we're reverting.
91+ let last_height_in_storage = HEIGH_TO_REVERT_TO . unchecked_next ( ) . unchecked_next ( ) ;
92+
93+ let mut storage = get_voted_height_storage ( get_new_storage_config ( ) ) ;
94+ storage. set_prev_voted_height ( last_height_in_storage) . unwrap ( ) ;
95+ storage. revert_height ( HEIGH_TO_REVERT_TO ) . unwrap ( ) ;
96+ assert_eq ! ( storage. get_prev_voted_height( ) . unwrap( ) , Some ( HEIGH_TO_REVERT_TO . prev( ) . unwrap( ) ) ) ;
97+ }
98+
99+ #[ test]
100+ fn revert_height_when_last_height_in_storage_is_equal_to_revert_height_reverts_the_given_height ( ) {
101+ const HEIGHT_TO_REVERT_TO : BlockNumber = BlockNumber ( 2 ) ;
102+
103+ let mut storage = get_voted_height_storage ( get_new_storage_config ( ) ) ;
104+ storage. set_prev_voted_height ( HEIGHT_TO_REVERT_TO ) . unwrap ( ) ;
105+ storage. revert_height ( HEIGHT_TO_REVERT_TO ) . unwrap ( ) ;
106+ assert_eq ! ( storage. get_prev_voted_height( ) . unwrap( ) , Some ( HEIGHT_TO_REVERT_TO . prev( ) . unwrap( ) ) ) ;
107+ }
108+
109+ #[ test]
110+ fn revert_height_to_0_clears_the_last_height_in_storage ( ) {
111+ let mut storage = get_voted_height_storage ( get_new_storage_config ( ) ) ;
112+ storage. set_prev_voted_height ( BlockNumber ( 5 ) ) . unwrap ( ) ;
113+ storage. revert_height ( BlockNumber ( 0 ) ) . unwrap ( ) ;
114+ assert ! ( storage. get_prev_voted_height( ) . unwrap( ) . is_none( ) ) ;
115+ }
0 commit comments