|
| 1 | +use vetomint::*; |
| 2 | + |
| 3 | +/// A very normal, desirable, and expected scenario. |
| 4 | +#[test] |
| 5 | +fn normal_2() { |
| 6 | + let mut height_info = HeightInfo { |
| 7 | + validators: vec![2, 2, 2, 2, 2], |
| 8 | + this_node_index: Some(0), |
| 9 | + timestamp: 0, |
| 10 | + consensus_params: ConsensusParams { |
| 11 | + timeout_ms: 100, |
| 12 | + repeat_round_for_first_leader: 1, |
| 13 | + }, |
| 14 | + initial_block_candidate: 0, |
| 15 | + }; |
| 16 | + let mut proposer = Vetomint::new(height_info.clone()); |
| 17 | + let mut nodes = Vec::new(); |
| 18 | + for i in 1..=4 { |
| 19 | + height_info.this_node_index = Some(i); |
| 20 | + nodes.push(Vetomint::new(height_info.clone())); |
| 21 | + } |
| 22 | + let response = proposer.progress(ConsensusEvent::Start, 0); |
| 23 | + assert_eq!( |
| 24 | + response, |
| 25 | + vec![ |
| 26 | + ConsensusResponse::BroadcastProposal { |
| 27 | + proposal: 0, |
| 28 | + valid_round: None, |
| 29 | + round: 0, |
| 30 | + }, |
| 31 | + ConsensusResponse::BroadcastPrevote { |
| 32 | + proposal: Some(0), |
| 33 | + round: 0 |
| 34 | + } |
| 35 | + ] |
| 36 | + ); |
| 37 | + for node in nodes.iter_mut() { |
| 38 | + let response = node.progress(ConsensusEvent::Start, 0); |
| 39 | + assert_eq!(response, vec![]); |
| 40 | + } |
| 41 | + |
| 42 | + for node in nodes.iter_mut() { |
| 43 | + let response = node.progress( |
| 44 | + ConsensusEvent::BlockProposalReceived { |
| 45 | + proposal: 0, |
| 46 | + valid: true, |
| 47 | + valid_round: None, |
| 48 | + proposer: 0, |
| 49 | + round: 0, |
| 50 | + favor: true, |
| 51 | + }, |
| 52 | + 1, |
| 53 | + ); |
| 54 | + assert_eq!( |
| 55 | + response, |
| 56 | + vec![ConsensusResponse::BroadcastPrevote { |
| 57 | + proposal: Some(0), |
| 58 | + round: 0, |
| 59 | + }] |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + let mut nodes = vec![vec![proposer], nodes].concat(); |
| 64 | + |
| 65 | + for (i, node) in nodes.iter_mut().enumerate() { |
| 66 | + let response = node.progress( |
| 67 | + ConsensusEvent::Prevote { |
| 68 | + proposal: Some(0), |
| 69 | + signer: (i + 1) % 5, |
| 70 | + round: 0, |
| 71 | + }, |
| 72 | + 2, |
| 73 | + ); |
| 74 | + assert_eq!(response, Vec::new()); |
| 75 | + let response = node.progress( |
| 76 | + ConsensusEvent::Prevote { |
| 77 | + proposal: Some(0), |
| 78 | + signer: (i + 2) % 5, |
| 79 | + round: 0, |
| 80 | + }, |
| 81 | + 2, |
| 82 | + ); |
| 83 | + assert_eq!(response, Vec::new()); |
| 84 | + let response = node.progress( |
| 85 | + ConsensusEvent::Prevote { |
| 86 | + proposal: Some(0), |
| 87 | + signer: (i + 3) % 5, |
| 88 | + round: 0, |
| 89 | + }, |
| 90 | + 2, |
| 91 | + ); |
| 92 | + assert_eq!( |
| 93 | + response, |
| 94 | + vec![ConsensusResponse::BroadcastPrecommit { |
| 95 | + proposal: Some(0), |
| 96 | + round: 0, |
| 97 | + }] |
| 98 | + ); |
| 99 | + let response = node.progress( |
| 100 | + ConsensusEvent::Prevote { |
| 101 | + proposal: Some(0), |
| 102 | + signer: (i + 4) % 5, |
| 103 | + round: 0, |
| 104 | + }, |
| 105 | + 2, |
| 106 | + ); |
| 107 | + assert_eq!(response, Vec::new()); |
| 108 | + } |
| 109 | + |
| 110 | + for (i, node) in nodes.iter_mut().enumerate() { |
| 111 | + let response = node.progress( |
| 112 | + ConsensusEvent::Precommit { |
| 113 | + proposal: Some(0), |
| 114 | + signer: (i + 1) % 5, |
| 115 | + round: 0, |
| 116 | + }, |
| 117 | + 3, |
| 118 | + ); |
| 119 | + assert_eq!(response, Vec::new()); |
| 120 | + let response = node.progress( |
| 121 | + ConsensusEvent::Precommit { |
| 122 | + proposal: Some(0), |
| 123 | + signer: (i + 2) % 5, |
| 124 | + round: 0, |
| 125 | + }, |
| 126 | + 3, |
| 127 | + ); |
| 128 | + assert_eq!(response, Vec::new()); |
| 129 | + let response = node.progress( |
| 130 | + ConsensusEvent::Precommit { |
| 131 | + proposal: Some(0), |
| 132 | + signer: (i + 3) % 5, |
| 133 | + round: 0, |
| 134 | + }, |
| 135 | + 3, |
| 136 | + ); |
| 137 | + assert_eq!( |
| 138 | + response, |
| 139 | + vec![ConsensusResponse::FinalizeBlock { |
| 140 | + proposal: 0, |
| 141 | + proof: (0..5).into_iter().filter(|x| *x != (i + 4) % 5).collect(), |
| 142 | + }] |
| 143 | + ); |
| 144 | + } |
| 145 | +} |
0 commit comments