Skip to content

Commit

Permalink
code: Remove signing and signature verification from the driver (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
romac authored Dec 18, 2023
1 parent ecd0c05 commit 5d0577f
Show file tree
Hide file tree
Showing 7 changed files with 287 additions and 385 deletions.
44 changes: 10 additions & 34 deletions Code/driver/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use alloc::vec::Vec;
use core::fmt;

use malachite_common::{
Context, Proposal, Round, SignedVote, Timeout, TimeoutStep, Validator, ValidatorSet, Vote,
Context, Proposal, Round, Timeout, TimeoutStep, Validator, ValidatorSet, Vote,
};
use malachite_round::input::Input as RoundInput;
use malachite_round::output::Output as RoundOutput;
Expand Down Expand Up @@ -136,27 +136,17 @@ where
match round_output {
RoundOutput::NewRound(round) => Output::NewRound(self.height().clone(), round),

RoundOutput::Proposal(proposal) => {
// TODO: sign the proposal
Output::Propose(proposal)
}
RoundOutput::Proposal(proposal) => Output::Propose(proposal),

RoundOutput::Vote(vote) => {
// TODO: Move this outside of the driver
let signed_vote = self.ctx.sign_vote(vote);
Output::Vote(signed_vote)
}
RoundOutput::Vote(vote) => Output::Vote(vote),

RoundOutput::ScheduleTimeout(timeout) => Output::ScheduleTimeout(timeout),

RoundOutput::GetValueAndScheduleTimeout(round, timeout) => {
Output::GetValueAndScheduleTimeout(round, timeout)
}

RoundOutput::Decision(value) => {
// TODO: update the state
Output::Decide(value.round, value.value)
}
RoundOutput::Decision(value) => Output::Decide(value.round, value.value),
}
}

Expand All @@ -166,7 +156,7 @@ where
Input::NewRound(height, round) => self.apply_new_round(height, round).await,
Input::ProposeValue(round, value) => self.apply_propose_value(round, value).await,
Input::Proposal(proposal, validity) => self.apply_proposal(proposal, validity).await,
Input::Vote(signed_vote) => self.apply_vote(signed_vote),
Input::Vote(vote) => self.apply_vote(vote),
Input::TimeoutElapsed(timeout) => self.apply_timeout(timeout),
}
}
Expand Down Expand Up @@ -207,32 +197,18 @@ where
}
}

fn apply_vote(
&mut self,
signed_vote: SignedVote<Ctx>,
) -> Result<Option<RoundOutput<Ctx>>, Error<Ctx>> {
fn apply_vote(&mut self, vote: Ctx::Vote) -> Result<Option<RoundOutput<Ctx>>, Error<Ctx>> {
let validator = self
.validator_set
.get_by_address(signed_vote.validator_address())
.ok_or_else(|| Error::ValidatorNotFound(signed_vote.validator_address().clone()))?;

// TODO: Move this outside of the driver
if !self
.ctx
.verify_signed_vote(&signed_vote, validator.public_key())
{
return Err(Error::InvalidVoteSignature(
signed_vote.clone(),
validator.clone(),
));
}
.get_by_address(vote.validator_address())
.ok_or_else(|| Error::ValidatorNotFound(vote.validator_address().clone()))?;

let vote_round = signed_vote.vote.round();
let vote_round = vote.round();
let current_round = self.round();

let vote_output =
self.vote_keeper
.apply_vote(signed_vote.vote, validator.voting_power(), current_round);
.apply_vote(vote, validator.voting_power(), current_round);

let Some(vote_output) = vote_output else {
return Ok(None);
Expand Down
14 changes: 1 addition & 13 deletions Code/driver/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::fmt;

use malachite_common::{Context, SignedVote, Validator};
use malachite_common::Context;

/// The type of errors that can be yielded by the `Driver`.
#[derive(Clone, Debug)]
Expand All @@ -13,9 +13,6 @@ where

/// Validator not found in validator set
ValidatorNotFound(Ctx::Address),

/// Invalid vote signature
InvalidVoteSignature(SignedVote<Ctx>, Ctx::Validator),
}

impl<Ctx> fmt::Display for Error<Ctx>
Expand All @@ -27,11 +24,6 @@ where
match self {
Error::ProposerNotFound(addr) => write!(f, "Proposer not found: {addr}"),
Error::ValidatorNotFound(addr) => write!(f, "Validator not found: {addr}"),
Error::InvalidVoteSignature(vote, validator) => write!(
f,
"Invalid vote signature by {} on vote {vote:?}",
validator.address()
),
}
}
}
Expand All @@ -45,10 +37,6 @@ where
match (self, other) {
(Error::ProposerNotFound(addr1), Error::ProposerNotFound(addr2)) => addr1 == addr2,
(Error::ValidatorNotFound(addr1), Error::ValidatorNotFound(addr2)) => addr1 == addr2,
(
Error::InvalidVoteSignature(vote1, validator1),
Error::InvalidVoteSignature(vote2, validator2),
) => vote1 == vote2 && validator1 == validator2,
_ => false,
}
}
Expand Down
6 changes: 3 additions & 3 deletions Code/driver/src/input.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use malachite_common::{Context, Round, SignedVote, Timeout};
use malachite_common::{Context, Round, Timeout};

use crate::Validity;

Expand All @@ -17,8 +17,8 @@ where
/// Receive a proposal, of the given validity
Proposal(Ctx::Proposal, Validity),

/// Receive a signed vote
Vote(SignedVote<Ctx>),
/// Receive a vote
Vote(Ctx::Vote),

/// Receive a timeout
TimeoutElapsed(Timeout),
Expand Down
4 changes: 2 additions & 2 deletions Code/driver/src/output.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::fmt;

use malachite_common::{Context, Round, SignedVote, Timeout};
use malachite_common::{Context, Round, Timeout};

/// Messages emitted by the [`Driver`](crate::Driver)
pub enum Output<Ctx>
Expand All @@ -14,7 +14,7 @@ where
Propose(Ctx::Proposal),

/// Broadcast a vote for a value
Vote(SignedVote<Ctx>),
Vote(Ctx::Vote),

/// Decide on a value
Decide(Round, Ctx::Value),
Expand Down
114 changes: 56 additions & 58 deletions Code/test/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,84 +79,82 @@ pub fn proposal_input(
Input::Proposal(proposal, validity)
}

pub fn prevote_output(
round: Round,
addr: &Address,
sk: &PrivateKey,
) -> Option<Output<TestContext>> {
pub fn prevote_output(round: Round, addr: &Address) -> Option<Output<TestContext>> {
let value = Value::new(9999);

Some(Output::Vote(
Vote::new_prevote(Height::new(1), round, NilOrVal::Val(value.id()), *addr).signed(sk),
))
Some(Output::Vote(Vote::new_prevote(
Height::new(1),
round,
NilOrVal::Val(value.id()),
*addr,
)))
}

pub fn prevote_nil_output(
round: Round,
addr: &Address,
sk: &PrivateKey,
) -> Option<Output<TestContext>> {
Some(Output::Vote(
Vote::new_prevote(Height::new(1), round, NilOrVal::Nil, *addr).signed(sk),
))
pub fn prevote_nil_output(round: Round, addr: &Address) -> Option<Output<TestContext>> {
Some(Output::Vote(Vote::new_prevote(
Height::new(1),
round,
NilOrVal::Nil,
*addr,
)))
}

pub fn prevote_input(addr: &Address, sk: &PrivateKey) -> Input<TestContext> {
pub fn prevote_input(addr: &Address) -> Input<TestContext> {
let value = Value::new(9999);

Input::Vote(
Vote::new_prevote(
Height::new(1),
Round::new(0),
NilOrVal::Val(value.id()),
*addr,
)
.signed(sk),
)
Input::Vote(Vote::new_prevote(
Height::new(1),
Round::new(0),
NilOrVal::Val(value.id()),
*addr,
))
}

pub fn prevote_nil_input(addr: &Address, sk: &PrivateKey) -> Input<TestContext> {
Input::Vote(Vote::new_prevote(Height::new(1), Round::new(0), NilOrVal::Nil, *addr).signed(sk))
pub fn prevote_nil_input(addr: &Address) -> Input<TestContext> {
Input::Vote(Vote::new_prevote(
Height::new(1),
Round::new(0),
NilOrVal::Nil,
*addr,
))
}

pub fn prevote_input_at(round: Round, addr: &Address, sk: &PrivateKey) -> Input<TestContext> {
pub fn prevote_input_at(round: Round, addr: &Address) -> Input<TestContext> {
let value = Value::new(9999);

Input::Vote(
Vote::new_prevote(Height::new(1), round, NilOrVal::Val(value.id()), *addr).signed(sk),
)
Input::Vote(Vote::new_prevote(
Height::new(1),
round,
NilOrVal::Val(value.id()),
*addr,
))
}

pub fn precommit_output(
round: Round,
value: Value,
addr: &Address,
sk: &PrivateKey,
) -> Option<Output<TestContext>> {
Some(Output::Vote(
Vote::new_precommit(Height::new(1), round, NilOrVal::Val(value.id()), *addr).signed(sk),
))
pub fn precommit_output(round: Round, value: Value, addr: &Address) -> Option<Output<TestContext>> {
Some(Output::Vote(Vote::new_precommit(
Height::new(1),
round,
NilOrVal::Val(value.id()),
*addr,
)))
}

pub fn precommit_nil_output(
round: Round,
addr: &Address,
sk: &PrivateKey,
) -> Option<Output<TestContext>> {
Some(Output::Vote(
Vote::new_precommit(Height::new(1), round, NilOrVal::Nil, *addr).signed(sk),
))
pub fn precommit_nil_output(round: Round, addr: &Address) -> Option<Output<TestContext>> {
Some(Output::Vote(Vote::new_precommit(
Height::new(1),
round,
NilOrVal::Nil,
*addr,
)))
}

pub fn precommit_input(
round: Round,
value: Value,
addr: &Address,
sk: &PrivateKey,
) -> Input<TestContext> {
Input::Vote(
Vote::new_precommit(Height::new(1), round, NilOrVal::Val(value.id()), *addr).signed(sk),
)
pub fn precommit_input(round: Round, value: Value, addr: &Address) -> Input<TestContext> {
Input::Vote(Vote::new_precommit(
Height::new(1),
round,
NilOrVal::Val(value.id()),
*addr,
))
}

pub fn decide_output(round: Round, value: Value) -> Option<Output<TestContext>> {
Expand Down
Loading

0 comments on commit 5d0577f

Please sign in to comment.