Skip to content

Commit

Permalink
chore: unify the two types of ProposalInit structs
Browse files Browse the repository at this point in the history
  • Loading branch information
guy-starkware committed Nov 4, 2024
1 parent b04f2e0 commit 41c6f9c
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 49 deletions.
30 changes: 15 additions & 15 deletions crates/papyrus_protobuf/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub struct StreamMessage<T: Into<Vec<u8>> + TryFrom<Vec<u8>, Error = ProtobufCon
#[derive(Debug, Clone, PartialEq)]
pub struct ProposalInit {
/// The height of the consensus (block number).
pub height: u64,
pub height: BlockNumber,
/// The current round of the consensus.
pub round: u32,
/// The last round that was valid.
Expand Down Expand Up @@ -129,16 +129,16 @@ where
pub struct ProposalWrapper(pub Proposal);

impl From<ProposalWrapper>
for (
(BlockNumber, u32, ContractAddress, Option<u32>),
mpsc::Receiver<Transaction>,
oneshot::Receiver<BlockHash>,
)
for (ProposalInit, mpsc::Receiver<Transaction>, oneshot::Receiver<BlockHash>)
{
fn from(val: ProposalWrapper) -> Self {
let transactions: Vec<Transaction> = val.0.transactions.into_iter().collect();
let proposal_init =
(BlockNumber(val.0.height), val.0.round, val.0.proposer, val.0.valid_round);
let proposal_init = ProposalInit {
height: BlockNumber(val.0.height),
round: val.0.round,
proposer: val.0.proposer,
valid_round: val.0.valid_round,
};
let (mut content_sender, content_receiver) = mpsc::channel(transactions.len());
for tx in transactions {
content_sender.try_send(tx).expect("Send should succeed");
Expand All @@ -153,15 +153,15 @@ impl From<ProposalWrapper>
}

impl From<ProposalWrapper>
for (
(BlockNumber, u32, ContractAddress, Option<u32>),
mpsc::Receiver<Vec<ExecutableTransaction>>,
oneshot::Receiver<BlockHash>,
)
for (ProposalInit, mpsc::Receiver<Vec<ExecutableTransaction>>, oneshot::Receiver<BlockHash>)
{
fn from(val: ProposalWrapper) -> Self {
let proposal_init =
(BlockNumber(val.0.height), val.0.round, val.0.proposer, val.0.valid_round);
let proposal_init = ProposalInit {
height: BlockNumber(val.0.height),
round: val.0.round,
proposer: val.0.proposer,
valid_round: val.0.valid_round,
};

let (_, content_receiver) = mpsc::channel(0);
// This should only be used for Milestone 1, and then removed once streaming is supported.
Expand Down
6 changes: 3 additions & 3 deletions crates/papyrus_protobuf/src/converters/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod consensus_test;
use std::convert::{TryFrom, TryInto};

use prost::Message;
use starknet_api::block::BlockHash;
use starknet_api::block::{BlockHash, BlockNumber};
use starknet_api::hash::StarkHash;
use starknet_api::transaction::Transaction;

Expand Down Expand Up @@ -209,14 +209,14 @@ impl TryFrom<protobuf::ProposalInit> for ProposalInit {
.proposer
.ok_or(ProtobufConversionError::MissingField { field_description: "proposer" })?
.try_into()?;
Ok(ProposalInit { height, round, valid_round, proposer })
Ok(ProposalInit { height: BlockNumber(height), round, valid_round, proposer })
}
}

impl From<ProposalInit> for protobuf::ProposalInit {
fn from(value: ProposalInit) -> Self {
protobuf::ProposalInit {
height: value.height,
height: value.height.0,
round: value.round,
valid_round: value.valid_round,
proposer: Some(value.proposer.into()),
Expand Down
14 changes: 5 additions & 9 deletions crates/sequencing/papyrus_consensus/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ use futures::stream::FuturesUnordered;
use futures::{Stream, StreamExt};
use papyrus_common::metrics::{PAPYRUS_CONSENSUS_HEIGHT, PAPYRUS_CONSENSUS_SYNC_COUNT};
use papyrus_network::network_manager::BroadcastTopicClientTrait;
use papyrus_protobuf::consensus::{ConsensusMessage, ProposalWrapper};
use papyrus_protobuf::consensus::{ConsensusMessage, ProposalInit, ProposalWrapper};
use starknet_api::block::{BlockHash, BlockNumber};
use starknet_api::core::ContractAddress;
use tracing::{debug, info, instrument};

use crate::config::TimeoutsConfig;
Expand Down Expand Up @@ -42,11 +41,8 @@ pub async fn run_consensus<ContextT, SyncReceiverT>(
where
ContextT: ConsensusContext,
SyncReceiverT: Stream<Item = BlockNumber> + Unpin,
ProposalWrapper: Into<(
(BlockNumber, u32, ContractAddress, Option<u32>),
mpsc::Receiver<ContextT::ProposalChunk>,
oneshot::Receiver<BlockHash>,
)>,
ProposalWrapper:
Into<(ProposalInit, mpsc::Receiver<ContextT::ProposalChunk>, oneshot::Receiver<BlockHash>)>,
{
info!(
"Running consensus, start_height={}, validator_id={}, consensus_delay={}, timeouts={:?}",
Expand Down Expand Up @@ -112,7 +108,7 @@ impl MultiHeightManager {
where
ContextT: ConsensusContext,
ProposalWrapper: Into<(
(BlockNumber, u32, ContractAddress, Option<u32>),
ProposalInit,
mpsc::Receiver<ContextT::ProposalChunk>,
oneshot::Receiver<BlockHash>,
)>,
Expand Down Expand Up @@ -169,7 +165,7 @@ impl MultiHeightManager {
where
ContextT: ConsensusContext,
ProposalWrapper: Into<(
(BlockNumber, u32, ContractAddress, Option<u32>),
ProposalInit,
mpsc::Receiver<ContextT::ProposalChunk>,
oneshot::Receiver<BlockHash>,
)>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::collections::{HashMap, VecDeque};
use std::time::Duration;

use futures::channel::{mpsc, oneshot};
use papyrus_protobuf::consensus::{ConsensusMessage, Vote, VoteType};
use papyrus_protobuf::consensus::{ConsensusMessage, ProposalInit, Vote, VoteType};
use starknet_api::block::BlockNumber;
use tracing::{debug, info, instrument, trace, warn};

Expand All @@ -18,7 +18,6 @@ use crate::types::{
ConsensusError,
Decision,
ProposalContentId,
ProposalInit,
Round,
ValidatorId,
};
Expand Down
17 changes: 1 addition & 16 deletions crates/sequencing/papyrus_consensus/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use papyrus_network::network_manager::{
GenericReceiver,
};
use papyrus_network_types::network_types::BroadcastedMessageMetadata;
use papyrus_protobuf::consensus::{ConsensusMessage, Vote};
use papyrus_protobuf::consensus::{ConsensusMessage, ProposalInit, Vote};
use papyrus_protobuf::converters::ProtobufConversionError;
use starknet_api::block::{BlockHash, BlockNumber};
use starknet_api::core::ContractAddress;
Expand Down Expand Up @@ -118,21 +118,6 @@ impl Debug for Decision {
}
}

#[derive(PartialEq, Debug, Default, Clone)]
pub struct ProposalInit {
pub height: BlockNumber,
pub round: Round,
pub proposer: ValidatorId,
pub valid_round: Option<Round>,
}

// TODO(Guy): Remove after implementing broadcast streams.
impl From<(BlockNumber, u32, ContractAddress, Option<u32>)> for ProposalInit {
fn from(val: (BlockNumber, u32, ContractAddress, Option<u32>)) -> Self {
ProposalInit { height: val.0, round: val.1, proposer: val.2, valid_round: val.3 }
}
}

pub struct BroadcastConsensusMessageChannel {
pub broadcasted_messages_receiver: GenericReceiver<(
Result<ConsensusMessage, ProtobufConversionError>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ use papyrus_consensus::types::{
ConsensusContext,
ConsensusError,
ProposalContentId,
ProposalInit,
Round,
ValidatorId,
};
use papyrus_network::network_manager::{BroadcastTopicClient, BroadcastTopicClientTrait};
use papyrus_protobuf::consensus::{ConsensusMessage, Proposal, Vote};
use papyrus_protobuf::consensus::{ConsensusMessage, Proposal, ProposalInit, Vote};
use papyrus_storage::body::BodyStorageReader;
use papyrus_storage::header::HeaderStorageReader;
use papyrus_storage::{StorageError, StorageReader};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ use papyrus_consensus::types::{
ConsensusContext,
ConsensusError,
ProposalContentId,
ProposalInit,
Round,
ValidatorId,
};
use papyrus_protobuf::consensus::{ConsensusMessage, Vote};
use papyrus_protobuf::consensus::{ConsensusMessage, ProposalInit, Vote};
use starknet_api::block::{BlockHash, BlockHashAndNumber, BlockNumber};
use starknet_api::executable_transaction::Transaction;
use starknet_batcher_types::batcher_types::{
Expand Down

0 comments on commit 41c6f9c

Please sign in to comment.