Skip to content

Commit

Permalink
wip: Move address from SignedVote into Vote
Browse files Browse the repository at this point in the history
  • Loading branch information
romac committed Oct 30, 2023
1 parent 98d2b63 commit 155fc0c
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 33 deletions.
12 changes: 10 additions & 2 deletions Code/common/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,17 @@ where

/// Build a new prevote vote by the validator with the given address,
/// for the value identified by the given value id, at the given round.
fn new_prevote(round: Round, value_id: Option<ValueId<Self>>) -> Self::Vote;
fn new_prevote(
round: Round,
value_id: Option<ValueId<Self>>,
address: Self::Address,
) -> Self::Vote;

/// Build a new precommit vote by the validator with the given address,
/// for the value identified by the given value id, at the given round.
fn new_precommit(round: Round, value_id: Option<ValueId<Self>>) -> Self::Vote;
fn new_precommit(
round: Round,
value_id: Option<ValueId<Self>>,
address: Self::Address,
) -> Self::Vote;
}
15 changes: 7 additions & 8 deletions Code/common/src/signed_vote.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Context, Signature};
use crate::{Context, Signature, Vote};

// TODO: Do we need to abstract over `SignedVote` as well?

Expand All @@ -8,19 +8,18 @@ where
Ctx: Context,
{
pub vote: Ctx::Vote,
pub address: Ctx::Address,
pub signature: Signature<Ctx>,
}

impl<Ctx> SignedVote<Ctx>
where
Ctx: Context,
{
pub fn new(vote: Ctx::Vote, address: Ctx::Address, signature: Signature<Ctx>) -> Self {
Self {
vote,
address,
signature,
}
pub fn new(vote: Ctx::Vote, signature: Signature<Ctx>) -> Self {
Self { vote, signature }
}

pub fn validator_address(&self) -> &Ctx::Address {
self.vote.validator_address()
}
}
5 changes: 4 additions & 1 deletion Code/common/src/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub enum VoteType {
/// include information about the validator signing it.
pub trait Vote<Ctx>
where
Self: Clone + Debug + PartialEq + Eq,
Self: Clone + Debug + Eq,
Ctx: Context,
{
/// The round for which the vote is for.
Expand All @@ -32,4 +32,7 @@ where

/// The type of vote.
fn vote_type(&self) -> VoteType;

/// Address of the validator who issued this vote
fn validator_address(&self) -> &Ctx::Address;
}
12 changes: 4 additions & 8 deletions Code/consensus/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,8 @@ where
}

RoundMessage::Vote(vote) => {
let address = self
.validator_set
.get_by_public_key(&self.key.expose_secret().verifying_key())?
.address()
.clone();

let signature = Ctx::sign_vote(&vote, self.key.expose_secret());
let signed_vote = SignedVote::new(vote, address, signature);
let signed_vote = SignedVote::new(vote, signature);

Some(Message::Vote(signed_vote))
}
Expand Down Expand Up @@ -194,7 +188,9 @@ where

fn apply_vote(&mut self, signed_vote: SignedVote<Ctx>) -> Option<RoundMessage<Ctx>> {
// TODO: How to handle missing validator?
let validator = self.validator_set.get_by_address(&signed_vote.address)?;
let validator = self
.validator_set
.get_by_address(signed_vote.validator_address())?;

if !Ctx::verify_signed_vote(&signed_vote, validator.public_key()) {
// TODO: How to handle invalid votes?
Expand Down
8 changes: 4 additions & 4 deletions Code/round/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ where
Message::Proposal(Ctx::new_proposal(height, round, value, pol_round))
}

pub fn prevote(round: Round, value_id: Option<ValueId<Ctx>>) -> Self {
Message::Vote(Ctx::new_prevote(round, value_id))
pub fn prevote(round: Round, value_id: Option<ValueId<Ctx>>, address: Ctx::Address) -> Self {
Message::Vote(Ctx::new_prevote(round, value_id, address))
}

pub fn precommit(round: Round, value_id: Option<ValueId<Ctx>>) -> Self {
Message::Vote(Ctx::new_precommit(round, value_id))
pub fn precommit(round: Round, value_id: Option<ValueId<Ctx>>, address: Ctx::Address) -> Self {
Message::Vote(Ctx::new_precommit(round, value_id, address))
}

pub fn timeout(round: Round, step: TimeoutStep) -> Self {
Expand Down
8 changes: 4 additions & 4 deletions Code/test/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ impl Context for TestConsensus {
Proposal::new(height, round, value, pol_round)
}

fn new_prevote(round: Round, value_id: Option<ValueId>) -> Vote {
Vote::new_prevote(round, value_id)
fn new_prevote(round: Round, value_id: Option<ValueId>, address: Address) -> Vote {
Vote::new_prevote(round, value_id, address)
}

fn new_precommit(round: Round, value_id: Option<ValueId>) -> Vote {
Vote::new_precommit(round, value_id)
fn new_precommit(round: Round, value_id: Option<ValueId>, address: Address) -> Vote {
Vote::new_precommit(round, value_id, address)
}
}
18 changes: 12 additions & 6 deletions Code/test/src/vote.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use malachite_common::{Round, SignedVote, VoteType};
use signature::Signer;

use malachite_common::{Round, SignedVote, VoteType};

use crate::{Address, PrivateKey, TestConsensus, ValueId};

/// A vote for a value in a round
Expand All @@ -9,26 +10,29 @@ pub struct Vote {
pub typ: VoteType,
pub round: Round,
pub value: Option<ValueId>,
pub validator_address: Address,
}

impl Vote {
pub fn new_prevote(round: Round, value: Option<ValueId>) -> Self {
pub fn new_prevote(round: Round, value: Option<ValueId>, validator_address: Address) -> Self {
Self {
typ: VoteType::Prevote,
round,
value,
validator_address,
}
}

pub fn new_precommit(round: Round, value: Option<ValueId>) -> Self {
pub fn new_precommit(round: Round, value: Option<ValueId>, address: Address) -> Self {
Self {
typ: VoteType::Precommit,
round,
value,
validator_address: address,
}
}

// TODO: Use the canonical vote
// TODO: Use a canonical vote
pub fn to_bytes(&self) -> Vec<u8> {
let vtpe = match self.typ {
VoteType::Prevote => 0,
Expand All @@ -47,12 +51,10 @@ impl Vote {
}

pub fn signed(self, private_key: &PrivateKey) -> SignedVote<TestConsensus> {
let address = Address::from_public_key(&private_key.public_key());
let signature = private_key.sign(&self.to_bytes());

SignedVote {
vote: self,
address,
signature,
}
}
Expand All @@ -74,4 +76,8 @@ impl malachite_common::Vote<TestConsensus> for Vote {
fn vote_type(&self) -> VoteType {
self.typ
}

fn validator_address(&self) -> &Address {
&self.validator_address
}
}

0 comments on commit 155fc0c

Please sign in to comment.