diff --git a/crates/sequencing/papyrus_consensus/src/manager_test.rs b/crates/sequencing/papyrus_consensus/src/manager_test.rs index 7dc642be35..233a486288 100644 --- a/crates/sequencing/papyrus_consensus/src/manager_test.rs +++ b/crates/sequencing/papyrus_consensus/src/manager_test.rs @@ -16,7 +16,14 @@ use starknet_types_core::felt::Felt; use super::{run_consensus, MultiHeightManager}; use crate::test_utils::{precommit, prevote, proposal}; -use crate::types::{ConsensusBlock, ConsensusContext, ConsensusError, ProposalInit, ValidatorId}; +use crate::types::{ + ConsensusBlock, + ConsensusContext, + ConsensusError, + ProposalInit, + Round, + ValidatorId, +}; lazy_static! { static ref VALIDATOR_ID: ValidatorId = 1_u32.into(); @@ -64,7 +71,7 @@ mock! { async fn validators(&self, height: BlockNumber) -> Vec; - fn proposer(&self, validators: &[ValidatorId], height: BlockNumber) -> ValidatorId; + fn proposer(&self, height: BlockNumber, round: Round) -> ValidatorId; async fn broadcast(&mut self, message: ConsensusMessage) -> Result<(), ConsensusError>; diff --git a/crates/sequencing/papyrus_consensus/src/papyrus_consensus_context.rs b/crates/sequencing/papyrus_consensus/src/papyrus_consensus_context.rs index 87d2e09253..841d7c6f1e 100644 --- a/crates/sequencing/papyrus_consensus/src/papyrus_consensus_context.rs +++ b/crates/sequencing/papyrus_consensus/src/papyrus_consensus_context.rs @@ -19,7 +19,14 @@ use starknet_api::core::ContractAddress; use starknet_api::transaction::Transaction; use tracing::{debug, debug_span, info, warn, Instrument}; -use crate::types::{ConsensusBlock, ConsensusContext, ConsensusError, ProposalInit, ValidatorId}; +use crate::types::{ + ConsensusBlock, + ConsensusContext, + ConsensusError, + ProposalInit, + Round, + ValidatorId, +}; use crate::ProposalWrapper; // TODO: add debug messages and span to the tasks. @@ -181,7 +188,7 @@ impl ConsensusContext for PapyrusConsensusContext { self.validators.clone() } - fn proposer(&self, _validators: &[ValidatorId], _height: BlockNumber) -> ValidatorId { + fn proposer(&self, _height: BlockNumber, _round: Round) -> ValidatorId { *self.validators.first().expect("validators should have at least 2 validators") } diff --git a/crates/sequencing/papyrus_consensus/src/single_height_consensus.rs b/crates/sequencing/papyrus_consensus/src/single_height_consensus.rs index fd74787f06..e4c6e2ee03 100644 --- a/crates/sequencing/papyrus_consensus/src/single_height_consensus.rs +++ b/crates/sequencing/papyrus_consensus/src/single_height_consensus.rs @@ -56,8 +56,7 @@ impl SingleHeightConsensus { context: &mut ContextT, ) -> Result>, ConsensusError> { info!("Starting consensus with validators {:?}", self.validators); - let leader_fn = - |_round: Round| -> ValidatorId { context.proposer(&self.validators, self.height) }; + let leader_fn = |round: Round| -> ValidatorId { context.proposer(self.height, round) }; let events = self.state_machine.start(&leader_fn); self.handle_state_machine_events(context, events).await } @@ -80,7 +79,7 @@ impl SingleHeightConsensus { "Received proposal: height={}, round={}, proposer={:?}", init.height.0, init.round, init.proposer ); - let proposer_id = context.proposer(&self.validators, self.height); + let proposer_id = context.proposer(self.height, init.round); if init.height != self.height { let msg = format!("invalid height: expected {:?}, got {:?}", self.height, init.height); return Err(ConsensusError::InvalidProposal(proposer_id, self.height, msg)); @@ -131,8 +130,7 @@ impl SingleHeightConsensus { block_id: Option, ) -> Result>, ConsensusError> { let sm_proposal = StateMachineEvent::Proposal(block_id, init.round); - let leader_fn = - |_round: Round| -> ValidatorId { context.proposer(&self.validators, self.height) }; + let leader_fn = |round: Round| -> ValidatorId { context.proposer(self.height, round) }; let sm_events = self.state_machine.handle_event(sm_proposal, &leader_fn); self.handle_state_machine_events(context, sm_events).await } @@ -193,8 +191,7 @@ impl SingleHeightConsensus { } } } - let leader_fn = - |_round: Round| -> ValidatorId { context.proposer(&self.validators, self.height) }; + let leader_fn = |round: Round| -> ValidatorId { context.proposer(self.height, round) }; let sm_events = self.state_machine.handle_event(sm_vote, &leader_fn); self.handle_state_machine_events(context, sm_events).await } @@ -268,8 +265,7 @@ impl SingleHeightConsensus { fin_sender.send(id).expect("Failed to send ProposalFin to Peering."); let old = self.proposals.insert(round, Some(block)); assert!(old.is_none(), "There should be no entry for this round."); - let leader_fn = - |_round: Round| -> ValidatorId { context.proposer(&self.validators, self.height) }; + let leader_fn = |round: Round| -> ValidatorId { context.proposer(self.height, round) }; self.state_machine.handle_event(StateMachineEvent::GetProposal(Some(id), round), &leader_fn) } diff --git a/crates/sequencing/papyrus_consensus/src/test_utils.rs b/crates/sequencing/papyrus_consensus/src/test_utils.rs index e34ddc8936..fa80505961 100644 --- a/crates/sequencing/papyrus_consensus/src/test_utils.rs +++ b/crates/sequencing/papyrus_consensus/src/test_utils.rs @@ -4,7 +4,14 @@ use mockall::mock; use papyrus_protobuf::consensus::{ConsensusMessage, Proposal, Vote, VoteType}; use starknet_api::block::{BlockHash, BlockNumber}; -use crate::types::{ConsensusBlock, ConsensusContext, ConsensusError, ProposalInit, ValidatorId}; +use crate::types::{ + ConsensusBlock, + ConsensusContext, + ConsensusError, + ProposalInit, + Round, + ValidatorId, +}; /// Define a consensus block which can be used to enable auto mocking Context. #[derive(Debug, PartialEq, Clone)] @@ -47,7 +54,7 @@ mock! { async fn validators(&self, height: BlockNumber) -> Vec; - fn proposer(&self, validators: &[ValidatorId], height: BlockNumber) -> ValidatorId; + fn proposer(&self, height: BlockNumber, round: Round) -> ValidatorId; async fn broadcast(&mut self, message: ConsensusMessage) -> Result<(), ConsensusError>; diff --git a/crates/sequencing/papyrus_consensus/src/types.rs b/crates/sequencing/papyrus_consensus/src/types.rs index 621563417e..b69de86150 100644 --- a/crates/sequencing/papyrus_consensus/src/types.rs +++ b/crates/sequencing/papyrus_consensus/src/types.rs @@ -122,7 +122,7 @@ pub trait ConsensusContext { async fn validators(&self, height: BlockNumber) -> Vec; /// Calculates the ID of the Proposer based on the inputs. - fn proposer(&self, validators: &[ValidatorId], height: BlockNumber) -> ValidatorId; + fn proposer(&self, height: BlockNumber, round: Round) -> ValidatorId; async fn broadcast(&mut self, message: ConsensusMessage) -> Result<(), ConsensusError>;