Skip to content

Commit

Permalink
chore(sequencing): remove ConsensusMessage and Proposal from proto
Browse files Browse the repository at this point in the history
  • Loading branch information
guy-starkware committed Dec 24, 2024
1 parent 173cb96 commit bd1211a
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 174 deletions.
27 changes: 0 additions & 27 deletions crates/papyrus_protobuf/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,6 @@ use starknet_api::transaction::Transaction;

use crate::converters::ProtobufConversionError;

// TODO(guyn): remove this once we integrate ProposalPart everywhere.
#[derive(Debug, Default, Hash, Clone, Eq, PartialEq)]
pub struct Proposal {
pub height: u64,
pub round: u32,
pub proposer: ContractAddress,
pub transactions: Vec<Transaction>,
pub block_hash: BlockHash,
pub valid_round: Option<u32>,
}

#[derive(Debug, Default, Hash, Clone, Eq, PartialEq)]
pub enum VoteType {
Prevote,
Expand All @@ -31,22 +20,6 @@ pub struct Vote {
pub voter: ContractAddress,
}

// TODO: remove this once we are sure everything works using just Vote.
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub enum ConsensusMessage {
Proposal(Proposal), // To be deprecated
Vote(Vote),
}

impl ConsensusMessage {
pub fn height(&self) -> u64 {
match self {
ConsensusMessage::Proposal(proposal) => proposal.height,
ConsensusMessage::Vote(vote) => vote.height,
}
}
}

#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub enum StreamMessageBody<T> {
Content(T),
Expand Down
80 changes: 0 additions & 80 deletions crates/papyrus_protobuf/src/converters/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ use starknet_api::hash::StarkHash;
use starknet_api::transaction::Transaction;

use crate::consensus::{
ConsensusMessage,
Proposal,
ProposalFin,
ProposalInit,
ProposalPart,
Expand All @@ -23,51 +21,6 @@ use crate::consensus::{
use crate::converters::ProtobufConversionError;
use crate::{auto_impl_into_and_try_from_vec_u8, protobuf};

// TODO(guyn): remove this once we integrate ProposalPart everywhere.
impl TryFrom<protobuf::Proposal> for Proposal {
type Error = ProtobufConversionError;

fn try_from(value: protobuf::Proposal) -> Result<Self, Self::Error> {
let transactions = value
.transactions
.into_iter()
.map(|tx| tx.try_into())
.collect::<Result<Vec<Transaction>, ProtobufConversionError>>()?;

let height = value.height;
let round = value.round;
let proposer = value
.proposer
.ok_or(ProtobufConversionError::MissingField { field_description: "proposer" })?
.try_into()?;
let block_hash: StarkHash = value
.block_hash
.ok_or(ProtobufConversionError::MissingField { field_description: "block_hash" })?
.try_into()?;
let block_hash = BlockHash(block_hash);
let valid_round = value.valid_round;

Ok(Proposal { height, round, proposer, transactions, block_hash, valid_round })
}
}

impl From<Proposal> for protobuf::Proposal {
fn from(value: Proposal) -> Self {
let transactions = value.transactions.into_iter().map(Into::into).collect();

protobuf::Proposal {
height: value.height,
round: value.round,
proposer: Some(value.proposer.into()),
transactions,
block_hash: Some(value.block_hash.0.into()),
valid_round: value.valid_round,
}
}
}

auto_impl_into_and_try_from_vec_u8!(Proposal, protobuf::Proposal);

impl TryFrom<protobuf::vote::VoteType> for VoteType {
type Error = ProtobufConversionError;

Expand Down Expand Up @@ -304,36 +257,3 @@ impl From<ProposalPart> for protobuf::ProposalPart {
}

auto_impl_into_and_try_from_vec_u8!(ProposalPart, protobuf::ProposalPart);

// TODO(guyn): remove this once we are happy with how proposals are sent separate from votes.
impl TryFrom<protobuf::ConsensusMessage> for ConsensusMessage {
type Error = ProtobufConversionError;

fn try_from(value: protobuf::ConsensusMessage) -> Result<Self, Self::Error> {
use protobuf::consensus_message::Message;

let Some(message) = value.message else {
return Err(ProtobufConversionError::MissingField { field_description: "message" });
};

match message {
Message::Proposal(proposal) => Ok(ConsensusMessage::Proposal(proposal.try_into()?)),
Message::Vote(vote) => Ok(ConsensusMessage::Vote(vote.try_into()?)),
}
}
}

impl From<ConsensusMessage> for protobuf::ConsensusMessage {
fn from(value: ConsensusMessage) -> Self {
match value {
ConsensusMessage::Proposal(proposal) => protobuf::ConsensusMessage {
message: Some(protobuf::consensus_message::Message::Proposal(proposal.into())),
},
ConsensusMessage::Vote(vote) => protobuf::ConsensusMessage {
message: Some(protobuf::consensus_message::Message::Vote(vote.into())),
},
}
}
}

auto_impl_into_and_try_from_vec_u8!(ConsensusMessage, protobuf::ConsensusMessage);
36 changes: 2 additions & 34 deletions crates/papyrus_protobuf/src/converters/consensus_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ use starknet_api::transaction::{
};

use crate::consensus::{
ConsensusMessage, // TODO: remove this
Proposal, // TODO: remove this
ProposalFin,
ProposalInit,
ProposalPart,
Expand Down Expand Up @@ -66,23 +64,6 @@ fn convert_stream_message_to_vec_u8_and_back() {
assert_eq!(stream_message, res_data);
}

// TODO(guyn): this can be removed once ConsensusMessage is taken out.
#[test]
fn convert_consensus_message_to_vec_u8_and_back() {
let mut rng = get_rng();

// Test that we can convert a ConsensusMessage to bytes and back.
let mut message = ConsensusMessage::get_test_instance(&mut rng);

if let ConsensusMessage::Proposal(proposal) = &mut message {
add_gas_values_to_transaction(&mut proposal.transactions);
}

let bytes_data: Vec<u8> = message.clone().into();
let res_data = ConsensusMessage::try_from(bytes_data).unwrap();
assert_eq!(message, res_data);
}

#[test]
fn convert_vote_to_vec_u8_and_back() {
let mut rng = get_rng();
Expand All @@ -94,19 +75,6 @@ fn convert_vote_to_vec_u8_and_back() {
assert_eq!(vote, res_data);
}

#[test]
fn convert_proposal_to_vec_u8_and_back() {
let mut rng = get_rng();

let mut proposal = Proposal::get_test_instance(&mut rng);

add_gas_values_to_transaction(&mut proposal.transactions);

let bytes_data: Vec<u8> = proposal.clone().into();
let res_data = Proposal::try_from(bytes_data).unwrap();
assert_eq!(proposal, res_data);
}

#[test]
fn convert_proposal_init_to_vec_u8_and_back() {
let mut rng = get_rng();
Expand Down Expand Up @@ -162,7 +130,7 @@ fn stream_message_display() {
let mut rng = get_rng();
let stream_id = 42;
let message_id = 127;
let proposal = Proposal::get_test_instance(&mut rng);
let proposal = ProposalPart::get_test_instance(&mut rng);
let proposal_bytes: Vec<u8> = proposal.clone().into();
let proposal_length = proposal_bytes.len();
let content = StreamMessageBody::Content(proposal);
Expand All @@ -177,7 +145,7 @@ fn stream_message_display() {
)
);

let content: StreamMessageBody<Proposal> = StreamMessageBody::Fin;
let content: StreamMessageBody<ProposalPart> = StreamMessageBody::Fin;
let message = StreamMessage { message: content, stream_id, message_id };
let txt = message.to_string();
assert_eq!(
Expand Down
15 changes: 0 additions & 15 deletions crates/papyrus_protobuf/src/converters/test_instances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use starknet_api::core::ContractAddress;
use starknet_api::transaction::Transaction;

use crate::consensus::{
ConsensusMessage, // TODO: remove this
Proposal, // TODO: remove this
ProposalFin,
ProposalInit,
ProposalPart,
Expand All @@ -18,19 +16,6 @@ use crate::consensus::{
};

auto_impl_get_test_instance! {
// TODO(guyn): remove this once we integrate ProposalPart everywhere.
pub enum ConsensusMessage {
Proposal(Proposal) = 0,
Vote(Vote) = 1,
}
pub struct Proposal {
pub height: u64,
pub round: u32,
pub proposer: ContractAddress,
pub transactions: Vec<Transaction>,
pub block_hash: BlockHash,
pub valid_round: Option<u32>,
}
pub struct Vote {
pub vote_type: VoteType,
pub height: u64,
Expand Down
18 changes: 0 additions & 18 deletions crates/papyrus_protobuf/src/proto/p2p/proto/consensus.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,6 @@ syntax = "proto3";
import "p2p/proto/transaction.proto";
import "p2p/proto/common.proto";

// To be deprecated
message Proposal {
uint64 height = 1;
uint32 round = 2;
Address proposer = 3;
repeated Transaction transactions = 4;
Hash block_hash = 5;
optional uint32 valid_round = 6;
}

message Vote {
enum VoteType {
Prevote = 0;
Expand All @@ -29,14 +19,6 @@ message Vote {
Address voter = 6;
}

// TODO(guyn): remove this after we have integrated streams for the proposal
message ConsensusMessage {
oneof message {
Proposal proposal = 1;
Vote vote = 2;
}
}

message StreamMessage {
oneof message {
bytes content = 1;
Expand Down

0 comments on commit bd1211a

Please sign in to comment.