Skip to content

Commit

Permalink
chore(sequencing): remove ProposalChunk
Browse files Browse the repository at this point in the history
  • Loading branch information
guy-starkware committed Dec 10, 2024
1 parent b800ae7 commit a5d57e5
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 39 deletions.
2 changes: 0 additions & 2 deletions crates/sequencing/papyrus_consensus/src/manager_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use papyrus_protobuf::consensus::{
};
use papyrus_test_utils::{get_rng, GetTestInstance};
use starknet_api::block::{BlockHash, BlockNumber};
use starknet_api::transaction::Transaction;
use starknet_types_core::felt::Felt;

use super::{run_consensus, MultiHeightManager};
Expand Down Expand Up @@ -52,7 +51,6 @@ mock! {

#[async_trait]
impl ConsensusContext for TestContext {
type ProposalChunk = Transaction;
type ProposalPart = ProposalPart;

async fn build_proposal(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, DEFAULT_VALIDATOR_ID};

lazy_static! {
Expand Down Expand Up @@ -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(1)).await.unwrap();

shc.handle_proposal(context, PROPOSAL_INIT.clone(), content_receiver).await.unwrap()
}
Expand Down
55 changes: 28 additions & 27 deletions crates/sequencing/papyrus_consensus/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@ use papyrus_protobuf::converters::ProtobufConversionError;
use starknet_api::block::{BlockHash, BlockNumber};
use starknet_types_core::felt::Felt;

use crate::types::{
ConsensusContext,
ConsensusError,
ProposalContentId,
Round,
ValidatorId,
DEFAULT_VALIDATOR_ID,
};
use crate::types::{ConsensusContext, ConsensusError, ProposalContentId, Round, ValidatorId};

/// Define a consensus block which can be used to enable auto mocking Context.
#[derive(Debug, PartialEq, Clone)]
Expand All @@ -25,36 +18,45 @@ pub struct TestBlock {
}

#[derive(Debug, PartialEq, Clone)]
pub struct MockProposalPart(pub u64);

impl From<ProposalInit> for MockProposalPart {
pub enum TestProposalPart {
Init(ProposalInit),
Fin(ProposalFin),
}
impl From<ProposalInit> for TestProposalPart {
fn from(init: ProposalInit) -> Self {
MockProposalPart(init.height.0)
TestProposalPart::Init(init)
}
}

impl TryFrom<MockProposalPart> for ProposalInit {
impl TryFrom<TestProposalPart> for ProposalInit {
type Error = ProtobufConversionError;
fn try_from(part: MockProposalPart) -> Result<Self, Self::Error> {
Ok(ProposalInit {
height: BlockNumber(part.0),
proposer: DEFAULT_VALIDATOR_ID.into(),
..Default::default()
})
fn try_from(part: TestProposalPart) -> Result<Self, Self::Error> {
match part {
TestProposalPart::Init(init) => Ok(init),
_ => Err(ProtobufConversionError::WrongEnumVariant {
type_description: "TestProposalPart",
expected: "Init",
value_as_str: format!("{:?}", part),
}),
}
}
}

impl From<MockProposalPart> for Vec<u8> {
fn from(part: MockProposalPart) -> Vec<u8> {
vec![u8::try_from(part.0).expect("Invalid MockProposalPart conversion")]
impl From<TestProposalPart> for Vec<u8> {
fn from(part: TestProposalPart) -> Vec<u8> {
let init = match part {
TestProposalPart::Init(init) => init,
_ => panic!("Invalid TestProposalPart conversion"),
};
<Vec<u8>>::try_from(init).expect("Invalid MockProposalPart conversion")
}
}

impl TryFrom<Vec<u8>> for MockProposalPart {
impl TryFrom<Vec<u8>> for TestProposalPart {
type Error = ProtobufConversionError;

fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
Ok(MockProposalPart(value[0].into()))
Ok(TestProposalPart::Init(value.try_into()?))
}
}

Expand All @@ -64,8 +66,7 @@ mock! {

#[async_trait]
impl ConsensusContext for TestContext {
type ProposalChunk = u32;
type ProposalPart = MockProposalPart;
type ProposalPart = TestProposalPart;

async fn build_proposal(
&mut self,
Expand All @@ -79,7 +80,7 @@ mock! {
round: Round,
proposer: ValidatorId,
timeout: Duration,
content: mpsc::Receiver<MockProposalPart>
content: mpsc::Receiver<TestProposalPart>
) -> oneshot::Receiver<(ProposalContentId, ProposalFin)>;

async fn repropose(
Expand Down
6 changes: 2 additions & 4 deletions crates/sequencing/papyrus_consensus/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ pub const DEFAULT_VALIDATOR_ID: u64 = 100;
/// Interface for consensus to call out to the node.
#[async_trait]
pub trait ConsensusContext {
/// The chunks of content returned when iterating the proposal.
// In practice I expect this to match the type sent to the network
// (papyrus_protobuf::ConsensusMessage), and not to be specific to just the block's content.
type ProposalChunk; // TODO(guyn): deprecate this (and replace by ProposalPart)
/// The parts of the proposal that are streamed in.
/// Must contain at least the ProposalInit and ProposalFin.
type ProposalPart: TryFrom<Vec<u8>, Error = ProtobufConversionError>
+ Into<Vec<u8>>
+ TryInto<ProposalInit, Error = ProtobufConversionError>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ impl PapyrusConsensusContext {

#[async_trait]
impl ConsensusContext for PapyrusConsensusContext {
type ProposalChunk = Transaction;
type ProposalPart = ProposalPart;

async fn build_proposal(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ use starknet_api::block::{
NonzeroGasPrice,
};
use starknet_api::executable_transaction::Transaction as ExecutableTransaction;
use starknet_api::transaction::Transaction;
use starknet_batcher_types::batcher_types::{
DecisionReachedInput,
GetProposalContent,
Expand Down Expand Up @@ -134,8 +133,6 @@ impl SequencerConsensusContext {

#[async_trait]
impl ConsensusContext for SequencerConsensusContext {
// TODO(guyn): Switch to ProposalPart when done with the streaming integration.
type ProposalChunk = Vec<Transaction>;
type ProposalPart = ProposalPart;

async fn build_proposal(
Expand Down

0 comments on commit a5d57e5

Please sign in to comment.