Skip to content

Commit

Permalink
Merge branch 'main' into anca/vote_height
Browse files Browse the repository at this point in the history
  • Loading branch information
romac committed Nov 14, 2023
2 parents 910ac07 + d39e933 commit 14c5b28
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 71 deletions.
3 changes: 0 additions & 3 deletions Code/common/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ where
type Vote: Vote<Self>;
type SigningScheme: SigningScheme; // TODO: Do we need to support multiple signing schemes?

// FIXME: Remove altogether
const DUMMY_VALUE: Self::Value;

/// Sign the given vote our private key.
fn sign_vote(&self, vote: Self::Vote) -> SignedVote<Self>;

Expand Down
21 changes: 11 additions & 10 deletions Code/driver/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::event::Event;
use crate::message::Message;
use crate::Error;
use crate::ProposerSelector;
use crate::Validity;

/// Driver for the state machine of the Malachite consensus engine at a given height.
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -73,10 +74,6 @@ where
self.env.get_value(self.height.clone(), round).await
}

async fn validate_proposal(&self, proposal: &Ctx::Proposal) -> bool {
self.env.validate_proposal(proposal).await
}

pub async fn execute(&mut self, msg: Event<Ctx>) -> Result<Option<Message<Ctx>>, Error<Ctx>> {
let round_msg = match self.apply(msg).await? {
Some(msg) => msg,
Expand Down Expand Up @@ -114,7 +111,9 @@ where
async fn apply(&mut self, msg: Event<Ctx>) -> Result<Option<RoundMessage<Ctx>>, Error<Ctx>> {
match msg {
Event::NewRound(round) => self.apply_new_round(round).await,
Event::Proposal(proposal) => Ok(self.apply_proposal(proposal).await),
Event::Proposal(proposal, validity) => {
Ok(self.apply_proposal(proposal, validity).await)
}
Event::Vote(signed_vote) => self.apply_vote(signed_vote),
Event::TimeoutElapsed(timeout) => Ok(self.apply_timeout(timeout)),
}
Expand Down Expand Up @@ -156,7 +155,11 @@ where
Ok(self.apply_event(round, event))
}

async fn apply_proposal(&mut self, proposal: Ctx::Proposal) -> Option<RoundMessage<Ctx>> {
async fn apply_proposal(
&mut self,
proposal: Ctx::Proposal,
validity: Validity,
) -> Option<RoundMessage<Ctx>> {
// Check that there is an ongoing round
let Some(round_state) = self.round_states.get(&self.round) else {
// TODO: Add logging
Expand All @@ -180,14 +183,12 @@ where

// TODO: Verify proposal signature (make some of these checks part of message validation)

let is_valid = self.validate_proposal(&proposal).await;

match proposal.pol_round() {
Round::Nil => {
// Is it possible to get +2/3 prevotes before the proposal?
// Do we wait for our own prevote to check the threshold?
let round = proposal.round();
let event = if is_valid {
let event = if validity.is_valid() {
RoundEvent::Proposal(proposal)
} else {
RoundEvent::ProposalInvalid
Expand All @@ -203,7 +204,7 @@ where
) =>
{
let round = proposal.round();
let event = if is_valid {
let event = if validity.is_valid() {
RoundEvent::Proposal(proposal)
} else {
RoundEvent::ProposalInvalid
Expand Down
3 changes: 0 additions & 3 deletions Code/driver/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,4 @@ where
/// If `None` is returned, the driver will understand this
/// as an error and will not propose a value.
async fn get_value(&self, height: Ctx::Height, round: Round) -> Option<Ctx::Value>;

/// Validate a proposal.
async fn validate_proposal(&self, proposal: &Ctx::Proposal) -> bool;
}
4 changes: 3 additions & 1 deletion Code/driver/src/event.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use malachite_common::{Context, Round, SignedVote, Timeout};

use crate::Validity;

/// Events that can be received by the [`Driver`](crate::Driver).
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Event<Ctx>
where
Ctx: Context,
{
NewRound(Round),
Proposal(Ctx::Proposal),
Proposal(Ctx::Proposal, Validity),
Vote(SignedVote<Ctx>),
TimeoutElapsed(Timeout),
}
2 changes: 2 additions & 0 deletions Code/driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ mod error;
mod event;
mod message;
mod proposer;
mod util;

pub use driver::Driver;
pub use env::Env;
pub use error::Error;
pub use event::Event;
pub use message::Message;
pub use proposer::ProposerSelector;
pub use util::Validity;

// Re-export `#[async_trait]` macro for convenience.
pub use async_trait::async_trait;
15 changes: 15 additions & 0 deletions Code/driver/src/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// Wether or not a proposal is valid.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Validity {
/// The proposal is valid.
Valid,
/// The proposal is invalid.
Invalid,
}

impl Validity {
/// Returns `true` if the proposal is valid.
pub fn is_valid(self) -> bool {
self == Validity::Valid
}
}
2 changes: 0 additions & 2 deletions Code/test/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ impl Context for TestContext {
type Vote = Vote;
type SigningScheme = Ed25519;

const DUMMY_VALUE: Self::Value = Value::new(9999);

fn sign_vote(&self, vote: Self::Vote) -> SignedVote<Self> {
use signature::Signer;
let signature = self.private_key.sign(&vote.to_bytes());
Expand Down
13 changes: 2 additions & 11 deletions Code/test/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,16 @@ use async_trait::async_trait;
use malachite_common::Round;
use malachite_driver::Env;

use crate::{Height, Proposal, TestContext, Value};
use crate::{Height, TestContext, Value};

pub struct TestEnv {
get_value: Box<dyn Fn(Height, Round) -> Option<Value> + Send + Sync>,
is_valid: Box<dyn Fn(&Proposal) -> bool + Send + Sync>,
}

impl TestEnv {
pub fn new(
get_value: impl Fn(Height, Round) -> Option<Value> + Send + Sync + 'static,
is_valid: impl Fn(&Proposal) -> bool + Send + Sync + 'static,
) -> Self {
pub fn new(get_value: impl Fn(Height, Round) -> Option<Value> + Send + Sync + 'static) -> Self {
Self {
get_value: Box::new(get_value),
is_valid: Box::new(is_valid),
}
}
}
Expand All @@ -27,8 +22,4 @@ impl Env<TestContext> for TestEnv {
async fn get_value(&self, height: Height, round: Round) -> Option<Value> {
(self.get_value)(height, round)
}

async fn validate_proposal(&self, proposal: &Proposal) -> bool {
(self.is_valid)(proposal)
}
}
Loading

0 comments on commit 14c5b28

Please sign in to comment.