Skip to content

Commit 13c7dc1

Browse files
apollo_consensus: use SM.id in SHC; remove local id
1 parent fe45e45 commit 13c7dc1

File tree

2 files changed

+33
-27
lines changed

2 files changed

+33
-27
lines changed

crates/apollo_consensus/src/single_height_consensus.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ use crate::votes_threshold::QuorumType;
4747
/// blocking further calls to itself.
4848
#[derive(Debug, PartialEq)]
4949
#[cfg_attr(test, derive(EnumAsInner))]
50-
pub enum ShcReturn {
50+
pub(crate) enum ShcReturn {
5151
Tasks(Vec<ShcTask>),
5252
Decision(Decision),
5353
}
5454

5555
/// A task which should be run without blocking calls to SHC.
5656
#[derive(Debug)]
5757
#[cfg_attr(test, derive(EnumAsInner))]
58-
pub enum ShcTask {
58+
pub(crate) enum ShcTask {
5959
TimeoutPropose(Duration, StateMachineEvent),
6060
TimeoutPrevote(Duration, StateMachineEvent),
6161
TimeoutPrecommit(Duration, StateMachineEvent),
@@ -95,7 +95,7 @@ impl PartialEq for ShcTask {
9595
}
9696

9797
impl ShcTask {
98-
pub async fn run(self) -> StateMachineEvent {
98+
pub(crate) async fn run(self) -> StateMachineEvent {
9999
trace!("Running task: {:?}", self);
100100
match self {
101101
ShcTask::TimeoutPropose(duration, event)
@@ -135,7 +135,6 @@ impl ShcTask {
135135
pub(crate) struct SingleHeightConsensus {
136136
height: BlockNumber,
137137
validators: Vec<ValidatorId>,
138-
id: ValidatorId,
139138
timeouts: TimeoutsConfig,
140139
state_machine: StateMachine,
141140
proposals: HashMap<Round, Option<ProposalCommitment>>,
@@ -161,7 +160,6 @@ impl SingleHeightConsensus {
161160
Self {
162161
height,
163162
validators,
164-
id,
165163
timeouts,
166164
state_machine,
167165
proposals: HashMap::new(),
@@ -230,7 +228,7 @@ impl SingleHeightConsensus {
230228
}
231229

232230
#[instrument(skip_all)]
233-
pub async fn handle_event<ContextT: ConsensusContext>(
231+
pub(crate) async fn handle_event<ContextT: ConsensusContext>(
234232
&mut self,
235233
context: &mut ContextT,
236234
event: StateMachineEvent,
@@ -467,8 +465,12 @@ impl SingleHeightConsensus {
467465

468466
// TODO(Matan): Figure out how to handle failed proposal building. I believe this should be
469467
// handled by applying timeoutPropose when we are the leader.
470-
let init =
471-
ProposalInit { height: self.height, round, proposer: self.id, valid_round: None };
468+
let init = ProposalInit {
469+
height: self.height,
470+
round,
471+
proposer: self.state_machine.id(),
472+
valid_round: None,
473+
};
472474
CONSENSUS_BUILD_PROPOSAL_TOTAL.increment(1);
473475
// TODO(Asmaa): Reconsider: we should keep the builder's timeout bounded independently of
474476
// the consensus proposal timeout. We currently use the base (round 0) proposal
@@ -506,7 +508,7 @@ impl SingleHeightConsensus {
506508
let init = ProposalInit {
507509
height: self.height,
508510
round,
509-
proposer: self.id,
511+
proposer: self.state_machine.id(),
510512
valid_round: Some(valid_round),
511513
};
512514
CONSENSUS_REPROPOSALS.increment(1);
@@ -542,9 +544,9 @@ impl SingleHeightConsensus {
542544
height: self.height.0,
543545
round,
544546
proposal_commitment: proposal_id,
545-
voter: self.id,
547+
voter: self.state_machine.id(),
546548
};
547-
if let Some(old) = votes.insert((round, self.id), vote.clone()) {
549+
if let Some(old) = votes.insert((round, self.state_machine.id()), vote.clone()) {
548550
return Err(ConsensusError::InternalInconsistency(format!(
549551
"State machine should not send repeat votes: old={old:?}, new={vote:?}"
550552
)));

crates/apollo_consensus/src/state_machine.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::votes_threshold::{QuorumType, VotesThreshold, ROUND_SKIP_THRESHOLD};
2727

2828
/// Events which the state machine sends/receives.
2929
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
30-
pub enum StateMachineEvent {
30+
pub(crate) enum StateMachineEvent {
3131
/// Sent by the state machine when a block is required to propose (ProposalCommitment is always
3232
/// None). While waiting for the response of GetProposal, the state machine will buffer all
3333
/// other events. The caller *must* respond with a valid proposal id for this height to the
@@ -53,7 +53,7 @@ pub enum StateMachineEvent {
5353
}
5454

5555
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
56-
pub enum Step {
56+
pub(crate) enum Step {
5757
Propose,
5858
Prevote,
5959
Precommit,
@@ -65,7 +65,7 @@ pub enum Step {
6565
///
6666
/// Each height is begun with a call to `start`, with no further calls to it.
6767
#[derive(Serialize, Deserialize)]
68-
pub struct StateMachine {
68+
pub(crate) struct StateMachine {
6969
id: ValidatorId,
7070
round: Round,
7171
step: Step,
@@ -91,7 +91,7 @@ pub struct StateMachine {
9191

9292
impl StateMachine {
9393
/// total_weight - the total voting weight of all validators for this height.
94-
pub fn new(
94+
pub(crate) fn new(
9595
id: ValidatorId,
9696
total_weight: u64,
9797
is_observer: bool,
@@ -120,22 +120,26 @@ impl StateMachine {
120120
}
121121
}
122122

123-
pub fn round(&self) -> Round {
123+
pub(crate) fn round(&self) -> Round {
124124
self.round
125125
}
126126

127-
pub fn total_weight(&self) -> u64 {
127+
pub(crate) fn total_weight(&self) -> u64 {
128128
self.total_weight
129129
}
130130

131-
pub fn quorum(&self) -> &VotesThreshold {
131+
pub(crate) fn quorum(&self) -> &VotesThreshold {
132132
&self.quorum
133133
}
134134

135+
pub(crate) fn id(&self) -> ValidatorId {
136+
self.id
137+
}
138+
135139
/// Starts the state machine, effectively calling `StartRound(0)` from the paper. This is
136140
/// needed to trigger the first leader to propose.
137141
/// See [`GetProposal`](StateMachineEvent::GetProposal)
138-
pub fn start<LeaderFn>(&mut self, leader_fn: &LeaderFn) -> VecDeque<StateMachineEvent>
142+
pub(crate) fn start<LeaderFn>(&mut self, leader_fn: &LeaderFn) -> VecDeque<StateMachineEvent>
139143
where
140144
LeaderFn: Fn(Round) -> ValidatorId,
141145
{
@@ -151,7 +155,7 @@ impl StateMachine {
151155
/// events back to the state machine, as it makes sure to handle them before returning.
152156
// This means that the StateMachine handles events the same regardless of whether it was sent by
153157
// self or a peer. This is in line with the Algorithm 1 in the paper and keeps the code simpler.
154-
pub fn handle_event<LeaderFn>(
158+
pub(crate) fn handle_event<LeaderFn>(
155159
&mut self,
156160
event: StateMachineEvent,
157161
leader_fn: &LeaderFn,
@@ -178,7 +182,7 @@ impl StateMachine {
178182
self.handle_enqueued_events(leader_fn)
179183
}
180184

181-
fn handle_enqueued_events<LeaderFn>(
185+
pub(crate) fn handle_enqueued_events<LeaderFn>(
182186
&mut self,
183187
leader_fn: &LeaderFn,
184188
) -> VecDeque<StateMachineEvent>
@@ -219,7 +223,7 @@ impl StateMachine {
219223
output_events
220224
}
221225

222-
fn handle_event_internal<LeaderFn>(
226+
pub(crate) fn handle_event_internal<LeaderFn>(
223227
&mut self,
224228
event: StateMachineEvent,
225229
leader_fn: &LeaderFn,
@@ -258,7 +262,7 @@ impl StateMachine {
258262
}
259263
}
260264

261-
fn handle_get_proposal(
265+
pub(crate) fn handle_get_proposal(
262266
&mut self,
263267
proposal_id: Option<ProposalCommitment>,
264268
round: u32,
@@ -271,7 +275,7 @@ impl StateMachine {
271275
}
272276

273277
// A proposal from a peer (or self) node.
274-
fn handle_proposal<LeaderFn>(
278+
pub(crate) fn handle_proposal<LeaderFn>(
275279
&mut self,
276280
proposal_id: Option<ProposalCommitment>,
277281
round: u32,
@@ -286,7 +290,7 @@ impl StateMachine {
286290
self.map_round_to_upons(round, leader_fn)
287291
}
288292

289-
fn handle_timeout_propose(&mut self, round: u32) -> VecDeque<StateMachineEvent> {
293+
pub(crate) fn handle_timeout_propose(&mut self, round: u32) -> VecDeque<StateMachineEvent> {
290294
if self.step != Step::Propose || round != self.round {
291295
return VecDeque::new();
292296
};
@@ -301,7 +305,7 @@ impl StateMachine {
301305
}
302306

303307
// A prevote from a peer (or self) node.
304-
fn handle_prevote<LeaderFn>(
308+
pub(crate) fn handle_prevote<LeaderFn>(
305309
&mut self,
306310
proposal_id: Option<ProposalCommitment>,
307311
round: u32,
@@ -316,7 +320,7 @@ impl StateMachine {
316320
self.map_round_to_upons(round, leader_fn)
317321
}
318322

319-
fn handle_timeout_prevote(&mut self, round: u32) -> VecDeque<StateMachineEvent> {
323+
pub(crate) fn handle_timeout_prevote(&mut self, round: u32) -> VecDeque<StateMachineEvent> {
320324
if self.step != Step::Prevote || round != self.round {
321325
return VecDeque::new();
322326
};

0 commit comments

Comments
 (0)