Skip to content

Commit a674f6d

Browse files
apollo_consensus: unify timeout scheduling requests into single enum variant
1 parent fc6df0b commit a674f6d

File tree

4 files changed

+88
-74
lines changed

4 files changed

+88
-74
lines changed

crates/apollo_consensus/src/manager.rs

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use crate::metrics::{
3939
CONSENSUS_REPROPOSALS,
4040
};
4141
use crate::single_height_consensus::{ShcReturn, SingleHeightConsensus};
42-
use crate::state_machine::{SMRequest, StateMachineEvent};
42+
use crate::state_machine::{SMRequest, StateMachineEvent, Step};
4343
use crate::storage::HeightVotedStorageTrait;
4444
use crate::types::{
4545
BroadcastVoteChannel,
@@ -786,29 +786,24 @@ impl<ContextT: ConsensusContext> MultiHeightManager<ContextT> {
786786
.boxed();
787787
Ok(Some(fut))
788788
}
789-
SMRequest::ScheduleTimeoutPropose(round) => {
790-
let duration = timeouts.get_proposal_timeout(round);
791-
let fut = async move {
792-
tokio::time::sleep(duration).await;
793-
StateMachineEvent::TimeoutPropose(round)
794-
}
795-
.boxed();
796-
Ok(Some(fut))
797-
}
798-
SMRequest::ScheduleTimeoutPrevote(round) => {
799-
let duration = timeouts.get_prevote_timeout(round);
800-
let fut = async move {
801-
tokio::time::sleep(duration).await;
802-
StateMachineEvent::TimeoutPrevote(round)
803-
}
804-
.boxed();
805-
Ok(Some(fut))
806-
}
807-
SMRequest::ScheduleTimeoutPrecommit(round) => {
808-
let duration = timeouts.get_precommit_timeout(round);
789+
SMRequest::ScheduleTimeout(step, round) => {
790+
let (duration, event) = match step {
791+
Step::Propose => (
792+
timeouts.get_proposal_timeout(round),
793+
StateMachineEvent::TimeoutPropose(round),
794+
),
795+
Step::Prevote => (
796+
timeouts.get_prevote_timeout(round),
797+
StateMachineEvent::TimeoutPrevote(round),
798+
),
799+
Step::Precommit => (
800+
timeouts.get_precommit_timeout(round),
801+
StateMachineEvent::TimeoutPrecommit(round),
802+
),
803+
};
809804
let fut = async move {
810805
tokio::time::sleep(duration).await;
811-
StateMachineEvent::TimeoutPrecommit(round)
806+
event
812807
}
813808
.boxed();
814809
Ok(Some(fut))

crates/apollo_consensus/src/single_height_consensus_test.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use test_case::test_case;
77

88
use super::SingleHeightConsensus;
99
use crate::single_height_consensus::ShcReturn;
10-
use crate::state_machine::{SMRequest, StateMachineEvent};
10+
use crate::state_machine::{SMRequest, StateMachineEvent, Step};
1111
use crate::test_utils::{precommit, prevote, TestBlock};
1212
use crate::types::{ProposalCommitment, ValidatorId};
1313
use crate::votes_threshold::QuorumType;
@@ -70,7 +70,7 @@ fn proposer() {
7070
ShcReturn::Requests(r) => r,
7171
_ => panic!("expected requests"),
7272
};
73-
assert!(matches!(reqs.pop_front(), Some(SMRequest::ScheduleTimeoutPrevote { .. })));
73+
assert!(matches!(reqs.pop_front(), Some(SMRequest::ScheduleTimeout(Step::Prevote, _))));
7474
assert!(matches!(
7575
reqs.pop_front(),
7676
Some(SMRequest::BroadcastVote(v)) if v.vote_type == VoteType::Precommit
@@ -143,7 +143,7 @@ fn validator(repeat_proposal: bool) {
143143
ShcReturn::Requests(r) => r,
144144
_ => panic!("expected requests"),
145145
};
146-
assert!(matches!(reqs.pop_front(), Some(SMRequest::ScheduleTimeoutPrevote { .. })));
146+
assert!(matches!(reqs.pop_front(), Some(SMRequest::ScheduleTimeout(Step::Prevote, _))));
147147
assert!(
148148
matches!(reqs.pop_front(), Some(SMRequest::BroadcastVote(v)) if v.vote_type == VoteType::Precommit)
149149
);
@@ -191,7 +191,7 @@ fn vote_twice(same_vote: bool) {
191191
ShcReturn::Requests(r) => r,
192192
_ => panic!("expected requests"),
193193
};
194-
assert!(matches!(reqs.pop_front(), Some(SMRequest::ScheduleTimeoutPrevote { .. })));
194+
assert!(matches!(reqs.pop_front(), Some(SMRequest::ScheduleTimeout(Step::Prevote, _))));
195195
assert!(matches!(
196196
reqs.pop_front(),
197197
Some(SMRequest::BroadcastVote(v)) if v.vote_type == VoteType::Precommit
@@ -350,7 +350,9 @@ fn repropose() {
350350
// Expect ScheduleTimeoutPrevote{round:0} and BroadcastVote(Precommit).
351351
match ret {
352352
ShcReturn::Requests(reqs) => {
353-
assert!(reqs.iter().any(|req| matches!(req, SMRequest::ScheduleTimeoutPrevote(0))));
353+
assert!(
354+
reqs.iter().any(|req| matches!(req, SMRequest::ScheduleTimeout(Step::Prevote, 0)))
355+
);
354356
assert!(reqs.iter().any(|req| matches!(
355357
req,
356358
SMRequest::BroadcastVote(v) if v.vote_type == VoteType::Precommit && v.round == 0
@@ -365,7 +367,10 @@ fn repropose() {
365367
// assert that ret is ScheduleTimeoutPrecommit
366368
match ret {
367369
ShcReturn::Requests(reqs) => {
368-
assert!(reqs.iter().any(|req| matches!(req, SMRequest::ScheduleTimeoutPrecommit(0))));
370+
assert!(
371+
reqs.iter()
372+
.any(|req| matches!(req, SMRequest::ScheduleTimeout(Step::Precommit, 0)))
373+
);
369374
}
370375
_ => panic!("expected requests"),
371376
}

crates/apollo_consensus/src/state_machine.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,8 @@ pub(crate) enum SMRequest {
6767
StartValidateProposal(ProposalInit),
6868
/// Request to broadcast a Prevote or Precommit vote.
6969
BroadcastVote(Vote),
70-
/// Request to schedule a TimeoutPropose.
71-
ScheduleTimeoutPropose(Round),
72-
/// Request to schedule a TimeoutPrevote.
73-
ScheduleTimeoutPrevote(Round),
74-
/// Request to schedule a TimeoutPrecommit.
75-
ScheduleTimeoutPrecommit(Round),
70+
/// Request to schedule a timeout for a specific step and round.
71+
ScheduleTimeout(Step, Round),
7672
/// Decision reached for the given proposal and round.
7773
DecisionReached(ProposalCommitment, Round),
7874
/// Request to re-propose (sent by the leader after advancing to a new round
@@ -511,7 +507,7 @@ impl StateMachine {
511507
}
512508
} else {
513509
info!("START_ROUND_VALIDATOR: Starting round {round} as Validator");
514-
VecDeque::from([SMRequest::ScheduleTimeoutPropose(self.round)])
510+
VecDeque::from([SMRequest::ScheduleTimeout(Step::Propose, self.round)])
515511
};
516512
output.append(&mut self.current_round_upons());
517513
output
@@ -625,7 +621,7 @@ impl StateMachine {
625621
if !self.mixed_prevote_quorum.insert(self.round) {
626622
return VecDeque::new();
627623
}
628-
VecDeque::from([SMRequest::ScheduleTimeoutPrevote(self.round)])
624+
VecDeque::from([SMRequest::ScheduleTimeout(Step::Prevote, self.round)])
629625
}
630626

631627
// LOC 36 in the paper.
@@ -684,7 +680,7 @@ impl StateMachine {
684680
if !self.mixed_precommit_quorum.insert(self.round) {
685681
return VecDeque::new();
686682
}
687-
VecDeque::from([SMRequest::ScheduleTimeoutPrecommit(self.round)])
683+
VecDeque::from([SMRequest::ScheduleTimeout(Step::Precommit, self.round)])
688684
}
689685

690686
// LOC 49 in the paper.

0 commit comments

Comments
 (0)