diff --git a/crates/sequencing/papyrus_consensus/src/manager_test.rs b/crates/sequencing/papyrus_consensus/src/manager_test.rs index 490aa2523f1..b2470462855 100644 --- a/crates/sequencing/papyrus_consensus/src/manager_test.rs +++ b/crates/sequencing/papyrus_consensus/src/manager_test.rs @@ -26,7 +26,7 @@ use starknet_types_core::felt::Felt; use super::{run_consensus, MultiHeightManager}; use crate::config::TimeoutsConfig; -use crate::test_utils::{precommit, prevote, proposal_init}; +use crate::test_utils::{precommit, prevote, proposal_init, MockTestContext, TestProposalPart}; use crate::types::{ConsensusContext, ConsensusError, ProposalContentId, Round, ValidatorId}; lazy_static! { @@ -39,49 +39,49 @@ lazy_static! { const CHANNEL_SIZE: usize = 10; -mock! { - pub TestContext {} +// mock! { +// pub TestContext {} - #[async_trait] - impl ConsensusContext for TestContext { - type ProposalPart = ProposalPart; +// #[async_trait] +// impl ConsensusContext for TestContext { +// type ProposalPart = ProposalPart; - async fn build_proposal( - &mut self, - init: ProposalInit, - timeout: Duration - ) -> oneshot::Receiver; +// async fn build_proposal( +// &mut self, +// init: ProposalInit, +// timeout: Duration +// ) -> oneshot::Receiver; - async fn validate_proposal( - &mut self, - height: BlockNumber, - round: Round, - proposer: ValidatorId, - timeout: Duration, - content: mpsc::Receiver - ) -> oneshot::Receiver<(ProposalContentId, ProposalFin)>; +// async fn validate_proposal( +// &mut self, +// height: BlockNumber, +// round: Round, +// proposer: ValidatorId, +// timeout: Duration, +// content: mpsc::Receiver +// ) -> oneshot::Receiver<(ProposalContentId, ProposalFin)>; - async fn repropose( - &mut self, - id: ProposalContentId, - init: ProposalInit, - ); +// async fn repropose( +// &mut self, +// id: ProposalContentId, +// init: ProposalInit, +// ); - async fn validators(&self, height: BlockNumber) -> Vec; +// async fn validators(&self, height: BlockNumber) -> Vec; - fn proposer(&self, height: BlockNumber, round: Round) -> ValidatorId; +// fn proposer(&self, height: BlockNumber, round: Round) -> ValidatorId; - async fn broadcast(&mut self, message: ConsensusMessage) -> Result<(), ConsensusError>; +// async fn broadcast(&mut self, message: ConsensusMessage) -> Result<(), ConsensusError>; - async fn decision_reached( - &mut self, - block: ProposalContentId, - precommits: Vec, - ) -> Result<(), ConsensusError>; +// async fn decision_reached( +// &mut self, +// block: ProposalContentId, +// precommits: Vec, +// ) -> Result<(), ConsensusError>; - async fn set_height_and_round(&mut self, height: BlockNumber, round: Round); - } -} +// async fn set_height_and_round(&mut self, height: BlockNumber, round: Round); +// } +// } async fn send(sender: &mut MockBroadcastedMessagesSender, msg: ConsensusMessage) { let broadcasted_message_metadata = @@ -90,8 +90,8 @@ async fn send(sender: &mut MockBroadcastedMessagesSender, msg: } async fn send_proposal( - proposal_receiver_sender: &mut mpsc::Sender>, - content: Vec, + proposal_receiver_sender: &mut mpsc::Sender>, + content: Vec, ) { let (mut proposal_sender, proposal_receiver) = mpsc::channel(CHANNEL_SIZE); proposal_receiver_sender.send(proposal_receiver).await.unwrap(); @@ -130,8 +130,8 @@ async fn manager_multiple_heights_unordered() { send_proposal( &mut proposal_receiver_sender, vec![ - ProposalPart::Init(proposal_init(2, 0, *PROPOSER_ID)), - ProposalPart::Fin(ProposalFin { proposal_content_id: BlockHash(Felt::TWO) }), + TestProposalPart::Init(proposal_init(2, 0, *PROPOSER_ID)), + TestProposalPart::Fin(ProposalFin { proposal_content_id: BlockHash(Felt::TWO) }), ], ) .await; @@ -141,8 +141,8 @@ async fn manager_multiple_heights_unordered() { send_proposal( &mut proposal_receiver_sender, vec![ - ProposalPart::Init(proposal_init(1, 0, *PROPOSER_ID)), - ProposalPart::Fin(ProposalFin { proposal_content_id: BlockHash(Felt::ONE) }), + TestProposalPart::Init(proposal_init(1, 0, *PROPOSER_ID)), + TestProposalPart::Fin(ProposalFin { proposal_content_id: BlockHash(Felt::ONE) }), ], ) .await; @@ -210,7 +210,7 @@ async fn run_consensus_sync() { // Send messages for height 2. send_proposal( &mut proposal_receiver_sender, - vec![ProposalPart::Init(proposal_init(2, 0, *PROPOSER_ID))], + vec![TestProposalPart::Init(proposal_init(2, 0, *PROPOSER_ID))], ) .await; let TestSubscriberChannels { mock_network, subscriber_channels } = @@ -301,7 +301,7 @@ async fn run_consensus_sync_cancellation_safety() { // Send a proposal for height 1. send_proposal( &mut proposal_receiver_sender, - vec![ProposalPart::Init(proposal_init(1, 0, *PROPOSER_ID))], + vec![TestProposalPart::Init(proposal_init(1, 0, *PROPOSER_ID))], ) .await; proposal_handled_rx.await.unwrap(); @@ -333,7 +333,7 @@ async fn test_timeouts() { send_proposal( &mut proposal_receiver_sender, - vec![ProposalPart::Init(proposal_init(1, 0, *PROPOSER_ID))], + vec![TestProposalPart::Init(proposal_init(1, 0, *PROPOSER_ID))], ) .await; send(&mut sender, prevote(None, 1, 0, *VALIDATOR_ID_2)).await; @@ -388,7 +388,7 @@ async fn test_timeouts() { // reach a decision. send_proposal( &mut proposal_receiver_sender, - vec![ProposalPart::Init(proposal_init(1, 1, *PROPOSER_ID))], + vec![TestProposalPart::Init(proposal_init(1, 1, *PROPOSER_ID))], ) .await; send(&mut sender, prevote(Some(Felt::ONE), 1, 1, *PROPOSER_ID)).await; diff --git a/crates/sequencing/papyrus_consensus/src/single_height_consensus_test.rs b/crates/sequencing/papyrus_consensus/src/single_height_consensus_test.rs index 60a9251f1af..95dc37e828f 100644 --- a/crates/sequencing/papyrus_consensus/src/single_height_consensus_test.rs +++ b/crates/sequencing/papyrus_consensus/src/single_height_consensus_test.rs @@ -11,7 +11,7 @@ use super::SingleHeightConsensus; use crate::config::TimeoutsConfig; use crate::single_height_consensus::{ShcEvent, ShcReturn, ShcTask}; use crate::state_machine::StateMachineEvent; -use crate::test_utils::{precommit, prevote, MockProposalPart, MockTestContext, TestBlock}; +use crate::test_utils::{precommit, prevote, MockTestContext, TestBlock, TestProposalPart}; use crate::types::{ConsensusError, ValidatorId}; lazy_static! { @@ -69,7 +69,7 @@ async fn handle_proposal( ) -> ShcReturn { // Send the proposal from the peer. let (mut content_sender, content_receiver) = mpsc::channel(CHANNEL_SIZE); - content_sender.send(MockProposalPart(1)).await.unwrap(); + content_sender.send(TestProposalPart::Init(ProposalInit::default())).await.unwrap(); shc.handle_proposal(context, PROPOSAL_INIT.clone(), content_receiver).await.unwrap() } diff --git a/crates/sequencing/papyrus_consensus/src/test_utils.rs b/crates/sequencing/papyrus_consensus/src/test_utils.rs index 3c92c60631a..30573b1bd58 100644 --- a/crates/sequencing/papyrus_consensus/src/test_utils.rs +++ b/crates/sequencing/papyrus_consensus/src/test_utils.rs @@ -18,37 +18,79 @@ pub struct TestBlock { } #[derive(Debug, PartialEq, Clone)] -pub struct MockProposalPart(pub u64); - -impl From for MockProposalPart { +// pub struct MockProposalPart(pub u64); + +// impl From for MockProposalPart { +// fn from(init: ProposalInit) -> Self { +// MockProposalPart(init.height.0) +// } +// } + +// impl TryFrom for ProposalInit { +// type Error = ProtobufConversionError; +// fn try_from(part: MockProposalPart) -> Result { +// Ok(ProposalInit { +// height: BlockNumber(part.0), +// round: 0, +// proposer: ValidatorId::default(), +// valid_round: None, +// }) +// } +// } + +// impl From for Vec { +// fn from(part: MockProposalPart) -> Vec { +// vec![u8::try_from(part.0).expect("Invalid MockProposalPart conversion")] +// } +// } + +// impl TryFrom> for MockProposalPart { +// type Error = ProtobufConversionError; + +// fn try_from(value: Vec) -> Result { +// Ok(MockProposalPart(value[0].into())) +// } +// } + +pub enum TestProposalPart { + Init(ProposalInit), + Fin(ProposalFin), +} +impl From for TestProposalPart { fn from(init: ProposalInit) -> Self { - MockProposalPart(init.height.0) + TestProposalPart::Init(init) } } -impl TryFrom for ProposalInit { +impl TryFrom for ProposalInit { type Error = ProtobufConversionError; - fn try_from(part: MockProposalPart) -> Result { - Ok(ProposalInit { - height: BlockNumber(part.0), - round: 0, - proposer: ValidatorId::default(), - valid_round: None, - }) + fn try_from(part: TestProposalPart) -> Result { + match part { + TestProposalPart::Init(init) => Ok(init), + _ => Err(ProtobufConversionError::WrongEnumVariant { + type_description: "TestProposalPart", + expected: "Init", + value_as_str: format!("{:?}", part), + }), + } } } -impl From for Vec { - fn from(part: MockProposalPart) -> Vec { - vec![u8::try_from(part.0).expect("Invalid MockProposalPart conversion")] +impl From for Vec { + fn from(part: TestProposalPart) -> Vec { + let init = match part { + TestProposalPart::Init(init) => init, + _ => panic!("Invalid TestProposalPart conversion"), + }; + >::try_from(init).expect("Invalid MockProposalPart conversion") } } -impl TryFrom> for MockProposalPart { +impl TryFrom> for TestProposalPart { type Error = ProtobufConversionError; fn try_from(value: Vec) -> Result { - Ok(MockProposalPart(value[0].into())) + Ok(TestProposalPart::Init(value.try_into()?)) } } @@ -58,7 +100,7 @@ mock! { #[async_trait] impl ConsensusContext for TestContext { - type ProposalPart = MockProposalPart; + type ProposalPart = TestProposalPart; async fn build_proposal( &mut self, @@ -72,7 +114,7 @@ mock! { round: Round, proposer: ValidatorId, timeout: Duration, - content: mpsc::Receiver + content: mpsc::Receiver ) -> oneshot::Receiver<(ProposalContentId, ProposalFin)>; async fn repropose(