Skip to content

Commit

Permalink
Verify votes signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
romac committed Oct 27, 2023
1 parent f6f72aa commit 723a389
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 28 deletions.
13 changes: 6 additions & 7 deletions Code/common/src/consensus.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::public_key::PrivateKey;
use crate::{
Address, Height, Proposal, PublicKey, Round, Validator, ValidatorSet, Value, ValueId, Vote,
Address, Height, PrivateKey, Proposal, PublicKey, Round, Signature, SignedVote, Validator,
ValidatorSet, Value, ValueId, Vote,
};

/// This trait allows to abstract over the various datatypes
Expand All @@ -13,7 +13,7 @@ where
type Height: Height;
type Proposal: Proposal<Self>;
type PrivateKey: PrivateKey<PublicKey = Self::PublicKey>;
type PublicKey: PublicKey<Signature = <Self::PrivateKey as PrivateKey>::Signature>;
type PublicKey: PublicKey<Signature = Signature<Self>>;
type Validator: Validator<Self>;
type ValidatorSet: ValidatorSet<Self>;
type Value: Value;
Expand All @@ -22,10 +22,9 @@ where
// FIXME: Remove altogether
const DUMMY_VALUE: Self::Value;

fn sign_vote(
vote: &Self::Vote,
private_key: &Self::PrivateKey,
) -> <Self::PrivateKey as PrivateKey>::Signature;
fn sign_vote(vote: &Self::Vote, private_key: &Self::PrivateKey) -> Signature<Self>;

fn verify_signed_vote(signed_vote: &SignedVote<Self>, public_key: &Self::PublicKey) -> bool;

/// Build a new proposal for the given value at the given height, round and POL round.
fn new_proposal(
Expand Down
3 changes: 3 additions & 0 deletions Code/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@ mod height;
mod proposal;
mod public_key;
mod round;
mod signed_vote;
mod timeout;
mod validator_set;
mod value;
mod vote;

/// Type alias to make it easier to refer the `ValueId` type of a given `Consensus` engine.
pub type ValueId<C> = <<C as Consensus>::Value as Value>::Id;
pub type Signature<C> = <<C as Consensus>::PrivateKey as PrivateKey>::Signature;

pub use consensus::Consensus;
pub use height::Height;
pub use proposal::Proposal;
pub use public_key::{PrivateKey, PublicKey};
pub use round::Round;
pub use signed_vote::SignedVote;
pub use timeout::{Timeout, TimeoutStep};
pub use validator_set::{Address, Validator, ValidatorSet, VotingPower};
pub use value::Value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use malachite_common::{Consensus, PublicKey};
use crate::{Consensus, PublicKey};

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

Expand Down
24 changes: 10 additions & 14 deletions Code/consensus/src/executor.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::collections::BTreeMap;

use malachite_common::{
Consensus, PrivateKey, Proposal, Round, Timeout, TimeoutStep, Validator, ValidatorSet, Value,
Vote, VoteType,
Consensus, PrivateKey, Proposal, Round, SignedVote, Timeout, TimeoutStep, Validator,
ValidatorSet, Value, Vote, VoteType,
};
use malachite_round::events::Event as RoundEvent;
use malachite_round::message::Message as RoundMessage;
Expand All @@ -11,7 +11,6 @@ use malachite_vote::count::Threshold;
use malachite_vote::keeper::VoteKeeper;

use crate::message::Message;
use crate::signed_vote::SignedVote;

#[derive(Clone, Debug)]
pub struct Executor<C>
Expand Down Expand Up @@ -168,22 +167,19 @@ where
}

fn apply_vote(&mut self, signed_vote: SignedVote<C>) -> Option<RoundMessage<C>> {
let Some(validator) = self.validator_set.get_by_address(&signed_vote.address) else {
// TODO: Is this the correct behavior? How to log such "errors"?
return None;
};
// TODO: How to handle missing validator?
let validator = self.validator_set.get_by_address(&signed_vote.address)?;

// TODO: Verify the vote's signature
if !C::verify_signed_vote(&signed_vote, validator.public_key()) {
// TODO: How to handle invalid votes?
return None;
}

let round = signed_vote.vote.round();

let event = match self
let event = self
.votes
.apply_vote(signed_vote.vote, validator.voting_power())
{
Some(event) => event,
None => return None,
};
.apply_vote(signed_vote.vote, validator.voting_power())?;

self.apply_event(round, event)
}
Expand Down
1 change: 0 additions & 1 deletion Code/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@

pub mod executor;
pub mod message;
pub mod signed_vote;
4 changes: 1 addition & 3 deletions Code/consensus/src/message.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use malachite_common::{Consensus, Round, Timeout};

use crate::signed_vote::SignedVote;
use malachite_common::{Consensus, Round, SignedVote, Timeout};

/// Messages that can be received and broadcast by the consensus executor.
#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down
8 changes: 8 additions & 0 deletions Code/test/src/consensus.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use malachite_common::Consensus;
use malachite_common::Round;
use malachite_common::SignedVote;

use crate::height::*;
use crate::proposal::*;
Expand Down Expand Up @@ -29,6 +30,13 @@ impl Consensus for TestConsensus {
private_key.sign(&vote.to_bytes())
}

fn verify_signed_vote(signed_vote: &SignedVote<Self>, public_key: &Ed25519PublicKey) -> bool {
use signature::Verifier;
public_key
.verify(&signed_vote.vote.to_bytes(), &signed_vote.signature)
.is_ok()
}

fn new_proposal(height: Height, round: Round, value: Value, pol_round: Round) -> Proposal {
Proposal::new(height, round, value, pol_round)
}
Expand Down
3 changes: 1 addition & 2 deletions Code/test/src/vote.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use malachite_common::{Round, VoteType};
use malachite_consensus::signed_vote::SignedVote;
use malachite_common::{Round, SignedVote, VoteType};
use signature::Signer;

use crate::{Address, Ed25519PrivateKey, TestConsensus, ValueId};
Expand Down

0 comments on commit 723a389

Please sign in to comment.