|
| 1 | +use consensus::{Step, VoteOn}; |
| 2 | +use std::cmp::Ordering; |
| 3 | + |
| 4 | +pub struct VoteRegressionChecker { |
| 5 | + last_vote: Option<VoteOn>, |
| 6 | +} |
| 7 | + |
| 8 | +impl VoteRegressionChecker { |
| 9 | + pub fn new() -> VoteRegressionChecker { |
| 10 | + VoteRegressionChecker { |
| 11 | + last_vote: None, |
| 12 | + } |
| 13 | + } |
| 14 | + |
| 15 | + pub fn check(&mut self, vote_on: &VoteOn) -> bool { |
| 16 | + assert!(match vote_on.step.step { |
| 17 | + Step::Propose | Step::Prevote | Step::Precommit => true, |
| 18 | + _ => false, |
| 19 | + }); |
| 20 | + |
| 21 | + let monotonic = if let Some(last_vote) = &self.last_vote { |
| 22 | + match last_vote.step.cmp(&vote_on.step) { |
| 23 | + Ordering::Less => true, |
| 24 | + Ordering::Greater => false, |
| 25 | + Ordering::Equal => last_vote.block_hash == vote_on.block_hash, |
| 26 | + } |
| 27 | + } else { |
| 28 | + true |
| 29 | + }; |
| 30 | + |
| 31 | + if monotonic { |
| 32 | + self.last_vote = Some(vote_on.clone()); |
| 33 | + } |
| 34 | + monotonic |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +#[cfg(test)] |
| 39 | +mod tests { |
| 40 | + use super::*; |
| 41 | + use consensus::VoteStep; |
| 42 | + use primitives::H256; |
| 43 | + |
| 44 | + #[test] |
| 45 | + fn test_initial_set() { |
| 46 | + let mut checker = VoteRegressionChecker::new(); |
| 47 | + |
| 48 | + let random_step = VoteStep::new(100, 10, Step::Prevote); |
| 49 | + let random_hash = Some(H256::random()); |
| 50 | + assert!(checker.check(&VoteOn { |
| 51 | + step: random_step, |
| 52 | + block_hash: random_hash |
| 53 | + })) |
| 54 | + } |
| 55 | + |
| 56 | + #[test] |
| 57 | + #[should_panic] |
| 58 | + fn test_disallow_commit() { |
| 59 | + let mut checker = VoteRegressionChecker::new(); |
| 60 | + |
| 61 | + let random_commit_step = VoteStep::new(100, 10, Step::Commit); |
| 62 | + let random_hash = Some(H256::random()); |
| 63 | + assert!(checker.check(&VoteOn { |
| 64 | + step: random_commit_step, |
| 65 | + block_hash: random_hash |
| 66 | + })) |
| 67 | + } |
| 68 | + |
| 69 | + #[test] |
| 70 | + fn test_allow_height_increase() { |
| 71 | + let mut checker = VoteRegressionChecker::new(); |
| 72 | + |
| 73 | + checker.check(&VoteOn { |
| 74 | + step: VoteStep::new(100, 10, Step::Prevote), |
| 75 | + block_hash: Some(H256::from(1)), |
| 76 | + }); |
| 77 | + |
| 78 | + assert!(checker.check(&VoteOn { |
| 79 | + step: VoteStep::new(101, 10, Step::Prevote), |
| 80 | + block_hash: Some(H256::from(2)) |
| 81 | + })) |
| 82 | + } |
| 83 | + |
| 84 | + #[test] |
| 85 | + fn test_disallow_height_decrease() { |
| 86 | + let mut checker = VoteRegressionChecker::new(); |
| 87 | + |
| 88 | + checker.check(&VoteOn { |
| 89 | + step: VoteStep::new(100, 10, Step::Prevote), |
| 90 | + block_hash: Some(H256::from(1)), |
| 91 | + }); |
| 92 | + |
| 93 | + assert!(!checker.check(&VoteOn { |
| 94 | + step: VoteStep::new(99, 10, Step::Prevote), |
| 95 | + block_hash: Some(H256::from(2)) |
| 96 | + })) |
| 97 | + } |
| 98 | + |
| 99 | + #[test] |
| 100 | + fn test_allow_view_increase() { |
| 101 | + let mut checker = VoteRegressionChecker::new(); |
| 102 | + |
| 103 | + checker.check(&VoteOn { |
| 104 | + step: VoteStep::new(100, 10, Step::Prevote), |
| 105 | + block_hash: Some(H256::from(1)), |
| 106 | + }); |
| 107 | + |
| 108 | + assert!(checker.check(&VoteOn { |
| 109 | + step: VoteStep::new(100, 11, Step::Prevote), |
| 110 | + block_hash: Some(H256::from(2)) |
| 111 | + })) |
| 112 | + } |
| 113 | + |
| 114 | + #[test] |
| 115 | + fn test_disallow_view_decrease() { |
| 116 | + let mut checker = VoteRegressionChecker::new(); |
| 117 | + |
| 118 | + checker.check(&VoteOn { |
| 119 | + step: VoteStep::new(100, 10, Step::Prevote), |
| 120 | + block_hash: Some(H256::from(1)), |
| 121 | + }); |
| 122 | + |
| 123 | + assert!(!checker.check(&VoteOn { |
| 124 | + step: VoteStep::new(100, 9, Step::Prevote), |
| 125 | + block_hash: Some(H256::from(2)) |
| 126 | + })) |
| 127 | + } |
| 128 | + |
| 129 | + #[test] |
| 130 | + fn test_allow_step_increased() { |
| 131 | + let mut checker = VoteRegressionChecker::new(); |
| 132 | + |
| 133 | + checker.check(&VoteOn { |
| 134 | + step: VoteStep::new(100, 10, Step::Prevote), |
| 135 | + block_hash: Some(H256::from(1)), |
| 136 | + }); |
| 137 | + |
| 138 | + assert!(checker.check(&VoteOn { |
| 139 | + step: VoteStep::new(100, 10, Step::Precommit), |
| 140 | + block_hash: Some(H256::from(2)) |
| 141 | + })) |
| 142 | + } |
| 143 | + |
| 144 | + #[test] |
| 145 | + fn test_disallow_step_decreased() { |
| 146 | + let mut checker = VoteRegressionChecker::new(); |
| 147 | + |
| 148 | + checker.check(&VoteOn { |
| 149 | + step: VoteStep::new(100, 10, Step::Prevote), |
| 150 | + block_hash: Some(H256::from(1)), |
| 151 | + }); |
| 152 | + |
| 153 | + assert!(!checker.check(&VoteOn { |
| 154 | + step: VoteStep::new(100, 10, Step::Propose), |
| 155 | + block_hash: Some(H256::from(2)) |
| 156 | + })) |
| 157 | + } |
| 158 | + |
| 159 | + #[test] |
| 160 | + fn test_allow_same_hash() { |
| 161 | + let mut checker = VoteRegressionChecker::new(); |
| 162 | + |
| 163 | + let block_hash = Some(H256::random()); |
| 164 | + checker.check(&VoteOn { |
| 165 | + step: VoteStep::new(100, 10, Step::Prevote), |
| 166 | + block_hash, |
| 167 | + }); |
| 168 | + |
| 169 | + assert!(checker.check(&VoteOn { |
| 170 | + step: VoteStep::new(100, 10, Step::Prevote), |
| 171 | + block_hash, |
| 172 | + })) |
| 173 | + } |
| 174 | + |
| 175 | + #[test] |
| 176 | + fn test_disallow_hash_change() { |
| 177 | + let mut checker = VoteRegressionChecker::new(); |
| 178 | + |
| 179 | + checker.check(&VoteOn { |
| 180 | + step: VoteStep::new(100, 10, Step::Prevote), |
| 181 | + block_hash: Some(H256::from(1)), |
| 182 | + }); |
| 183 | + |
| 184 | + assert!(!checker.check(&VoteOn { |
| 185 | + step: VoteStep::new(100, 10, Step::Prevote), |
| 186 | + block_hash: Some(H256::from(2)) |
| 187 | + })) |
| 188 | + } |
| 189 | +} |
0 commit comments