Skip to content
This repository has been archived by the owner on Dec 26, 2024. It is now read-only.

Commit

Permalink
feat(consensus): add Vote message to proto
Browse files Browse the repository at this point in the history
  • Loading branch information
matan-starkware committed Jun 26, 2024
1 parent a2a31b3 commit c8bb9c8
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 7 deletions.
15 changes: 15 additions & 0 deletions crates/papyrus_protobuf/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,22 @@ pub struct Proposal {
pub block_hash: BlockHash,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub enum VoteType {
Prevote,
Precommit,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Vote {
pub vote_type: VoteType,
pub height: u64,
pub block_hash: BlockHash,
pub voter: ContractAddress,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub enum ConsensusMessage {
Proposal(Proposal),
Vote(Vote),
}
67 changes: 64 additions & 3 deletions crates/papyrus_protobuf/src/converters/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use starknet_api::block::BlockHash;
use starknet_api::hash::StarkHash;
use starknet_api::transaction::Transaction;

use crate::consensus::{ConsensusMessage, Proposal};
use crate::consensus::{ConsensusMessage, Proposal, Vote, VoteType};
use crate::converters::ProtobufConversionError;
use crate::{auto_impl_into_and_try_from_vec_u8, protobuf};

Expand All @@ -20,7 +20,7 @@ impl TryFrom<protobuf::Proposal> for Proposal {
.collect::<Result<Vec<Transaction>, ProtobufConversionError>>()?;

let height = value.height;
let contract_address = value
let proposer = value
.proposer
.ok_or(ProtobufConversionError::MissingField { field_description: "proposer" })?
.try_into()?;
Expand All @@ -30,7 +30,7 @@ impl TryFrom<protobuf::Proposal> for Proposal {
.try_into()?;
let block_hash = BlockHash(block_hash);

Ok(Proposal { height, proposer: contract_address, transactions, block_hash })
Ok(Proposal { height, proposer, transactions, block_hash })
}
}

Expand All @@ -47,6 +47,63 @@ impl From<Proposal> for protobuf::Proposal {
}
}

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

fn try_from(value: protobuf::vote::VoteType) -> Result<Self, Self::Error> {
match value {
protobuf::vote::VoteType::Prevote => Ok(VoteType::Prevote),
protobuf::vote::VoteType::Precommit => Ok(VoteType::Precommit),
}
}
}

impl From<VoteType> for protobuf::vote::VoteType {
fn from(value: VoteType) -> Self {
match value {
VoteType::Prevote => protobuf::vote::VoteType::Prevote,
VoteType::Precommit => protobuf::vote::VoteType::Precommit,
}
}
}

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

fn try_from(value: protobuf::Vote) -> Result<Self, Self::Error> {
let vote_type = protobuf::vote::VoteType::try_from(value.vote_type)?.try_into()?;

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

Ok(Vote { vote_type, height, block_hash, voter })
}
}

impl From<Vote> for protobuf::Vote {
fn from(value: Vote) -> Self {
let vote_type = match value.vote_type {
VoteType::Prevote => protobuf::vote::VoteType::Prevote,
VoteType::Precommit => protobuf::vote::VoteType::Precommit,
};

protobuf::Vote {
vote_type: vote_type as i32,
height: value.height,
block_hash: Some(value.block_hash.0.into()),
voter: Some(value.voter.into()),
}
}
}

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

Expand All @@ -59,6 +116,7 @@ impl TryFrom<protobuf::ConsensusMessage> for ConsensusMessage {

match message {
Message::Proposal(proposal) => Ok(ConsensusMessage::Proposal(proposal.try_into()?)),
Message::Vote(vote) => Ok(ConsensusMessage::Vote(vote.try_into()?)),
}
}
}
Expand All @@ -69,6 +127,9 @@ impl From<ConsensusMessage> for protobuf::ConsensusMessage {
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())),
},
}
}
}
Expand Down
23 changes: 20 additions & 3 deletions crates/papyrus_protobuf/src/proto/p2p/proto/consensus.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,31 @@ import "p2p/proto/transaction.proto";
import "p2p/proto/common.proto";

message Proposal {
uint64 height = 1;
Address proposer = 2;
uint64 height = 1;
Address proposer = 2;
repeated Transaction transactions = 3;
Hash block_hash = 4;
Hash block_hash = 4;
}

message Vote {
enum VoteType {
Prevote = 0;
Precommit = 1;
};

// We use a type field to distinguish between prevotes and precommits instead of different
// messages, to make sure the data, and therefore the signatures, are unambiguous between
// Prevote and Precommit.
VoteType vote_type = 2;
uint64 height = 3;
// This is optional since a vote can be NIL.
optional Hash block_hash = 4;
Address voter = 5;
}

message ConsensusMessage {
oneof message {
Proposal proposal = 1;
Vote vote = 2;
}
}
5 changes: 4 additions & 1 deletion crates/sequencing/papyrus_consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ where
.await
.expect("Failed to receive a message from network")
.0
.expect("Network receiver closed unexpectedly");
.expect("Network receiver closed unexpectedly")
else {
todo!("Handle votes");
};
let (proposal_init, content_receiver, fin_receiver) = ProposalWrapper(proposal).into();

shc.handle_proposal(proposal_init, content_receiver, fin_receiver)
Expand Down

0 comments on commit c8bb9c8

Please sign in to comment.