Skip to content

Commit

Permalink
Rename Executor to Driver
Browse files Browse the repository at this point in the history
  • Loading branch information
romac committed Nov 7, 2023
1 parent 45d4575 commit ae2423c
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 60 deletions.
2 changes: 1 addition & 1 deletion Code/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ resolver = "2"

members = [
"common",
"consensus",
"driver",
"round",
"vote",
"test",
Expand Down
4 changes: 2 additions & 2 deletions Code/consensus/Cargo.toml → Code/driver/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "malachite-consensus"
description = "Malachite consensus engine"
name = "malachite-driver"
description = "Driver for the state machine of Malachite consensus engine"

version.workspace = true
edition.workspace = true
Expand Down
14 changes: 14 additions & 0 deletions Code/driver/src/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use malachite_common::Context;

/// Client for use by the [`Driver`](crate::Driver) to ask
/// for a value to propose and validate proposals.
pub trait Client<Ctx>
where
Ctx: Context,
{
/// Get the value to propose.
fn get_value(&self) -> Ctx::Value;

/// Validate a proposal.
fn validate_proposal(&self, proposal: &Ctx::Proposal) -> bool;
}
42 changes: 7 additions & 35 deletions Code/consensus/src/executor.rs → Code/driver/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,15 @@ use malachite_vote::keeper::Message as VoteMessage;
use malachite_vote::keeper::VoteKeeper;
use malachite_vote::Threshold;

/// Messages that can be received and broadcast by the consensus executor.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Event<Ctx>
where
Ctx: Context,
{
NewRound(Round),
Proposal(Ctx::Proposal),
Vote(SignedVote<Ctx>),
TimeoutElapsed(Timeout),
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Message<Ctx>
where
Ctx: Context,
{
Propose(Ctx::Proposal),
Vote(SignedVote<Ctx>),
Decide(Round, Ctx::Value),
ScheduleTimeout(Timeout),
NewRound(Round),
}

pub trait Client<Ctx>
where
Ctx: Context,
{
fn get_value(&self) -> Ctx::Value;
fn validate_proposal(&self, proposal: &Ctx::Proposal) -> bool;
}
use crate::event::Event;
use crate::message::Message;

/// Driver for the state machine of the Malachite consensus engine.
#[derive(Clone, Debug)]
pub struct Executor<Ctx, Client>
pub struct Driver<Ctx, Client>
where
Ctx: Context,
Client: self::Client<Ctx>,
Client: crate::client::Client<Ctx>,
{
pub client: Client,
pub height: Ctx::Height,
Expand All @@ -63,10 +35,10 @@ where
pub round_states: BTreeMap<Round, RoundState<Ctx>>,
}

impl<Ctx, Client> Executor<Ctx, Client>
impl<Ctx, Client> Driver<Ctx, Client>
where
Ctx: Context,
Client: self::Client<Ctx>,
Client: crate::client::Client<Ctx>,
{
pub fn new(
client: Client,
Expand Down
13 changes: 13 additions & 0 deletions Code/driver/src/event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use malachite_common::{Context, Round, SignedVote, Timeout};

/// 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),
Vote(SignedVote<Ctx>),
TimeoutElapsed(Timeout),
}
12 changes: 10 additions & 2 deletions Code/consensus/src/lib.rs → Code/driver/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Consensus executor
//! Driver for the state machine of the Malachite consensus engine
#![forbid(unsafe_code)]
#![deny(unused_crate_dependencies, trivial_casts, trivial_numeric_casts)]
Expand All @@ -10,4 +10,12 @@
)]
#![cfg_attr(not(test), deny(clippy::unwrap_used, clippy::panic))]

pub mod executor;
mod client;
mod driver;
mod event;
mod message;

pub use client::Client;
pub use driver::Driver;
pub use event::Event;
pub use message::Message;
14 changes: 14 additions & 0 deletions Code/driver/src/message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use malachite_common::{Context, Round, SignedVote, Timeout};

/// Messages emitted by the [`Driver`](crate::Driver)
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Message<Ctx>
where
Ctx: Context,
{
Propose(Ctx::Proposal),
Vote(SignedVote<Ctx>),
Decide(Round, Ctx::Value),
ScheduleTimeout(Timeout),
NewRound(Round),
}
2 changes: 1 addition & 1 deletion Code/round/src/state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ where
/// Apply an event to the current state at the current round.
///
/// This function takes the current state and round, and an event,
/// and returns the next state and an optional message for the executor to act on.
/// and returns the next state and an optional message for the driver to act on.
///
/// Valid transitions result in at least a change to the state and/or an output message.
///
Expand Down
8 changes: 4 additions & 4 deletions Code/test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ repository.workspace = true
license.workspace = true

[dependencies]
malachite-common = { version = "0.1.0", path = "../common" }
malachite-consensus = { version = "0.1.0", path = "../consensus" }
malachite-round = { version = "0.1.0", path = "../round" }
malachite-vote = { version = "0.1.0", path = "../vote" }
malachite-common = { version = "0.1.0", path = "../common" }
malachite-driver = { version = "0.1.0", path = "../driver" }
malachite-round = { version = "0.1.0", path = "../round" }
malachite-vote = { version = "0.1.0", path = "../vote" }

ed25519-consensus.workspace = true
signature.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion Code/test/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use malachite_consensus::executor::Client;
use malachite_driver::Client;

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

Expand Down
28 changes: 14 additions & 14 deletions Code/test/tests/consensus_executor.rs → Code/test/tests/driver.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use malachite_common::{Context, Round, Timeout};
use malachite_consensus::executor::{Event, Executor, Message};
use malachite_driver::{Driver, Event, Message};
use malachite_round::state::{RoundValue, State, Step};

use malachite_test::{
Expand All @@ -26,7 +26,7 @@ fn to_input_msg(output: Message<TestContext>) -> Option<Event<TestContext>> {
}

#[test]
fn executor_steps_proposer() {
fn driver_steps_proposer() {
let value = TestContext::DUMMY_VALUE; // TODO: get value from external source
let value_id = value.id();

Expand All @@ -48,7 +48,7 @@ fn executor_steps_proposer() {

let vs = ValidatorSet::new(vec![v1, v2.clone(), v3.clone()]);
let client = TestClient::new(value.clone(), |_| true);
let mut executor = Executor::new(client, Height::new(1), vs, my_sk.clone(), my_addr);
let mut driver = Driver::new(client, Height::new(1), vs, my_sk.clone(), my_addr);

let proposal = Proposal::new(Height::new(1), Round::new(0), value.clone(), Round::new(-1));

Expand Down Expand Up @@ -196,18 +196,18 @@ fn executor_steps_proposer() {
.input_event
.unwrap_or_else(|| previous_message.unwrap());

let output = executor.execute(execute_message);
let output = driver.execute(execute_message);
assert_eq!(output, step.expected_output);

let new_state = executor.round_state(Round::new(0)).unwrap();
let new_state = driver.round_state(Round::new(0)).unwrap();
assert_eq!(new_state, &step.new_state);

previous_message = output.and_then(to_input_msg);
}
}

#[test]
fn executor_steps_not_proposer() {
fn driver_steps_not_proposer() {
let value = TestContext::DUMMY_VALUE; // TODO: get value from external source
let value_id = value.id();

Expand All @@ -230,7 +230,7 @@ fn executor_steps_not_proposer() {

let vs = ValidatorSet::new(vec![v1.clone(), v2.clone(), v3.clone()]);
let client = TestClient::new(value.clone(), |_| true);
let mut executor = Executor::new(client, Height::new(1), vs, my_sk.clone(), my_addr);
let mut driver = Driver::new(client, Height::new(1), vs, my_sk.clone(), my_addr);

let proposal = Proposal::new(Height::new(1), Round::new(0), value.clone(), Round::new(-1));

Expand Down Expand Up @@ -378,18 +378,18 @@ fn executor_steps_not_proposer() {
.input_event
.unwrap_or_else(|| previous_message.unwrap());

let output = executor.execute(execute_message);
let output = driver.execute(execute_message);
assert_eq!(output, step.expected_output);

let new_state = executor.round_state(Round::new(0)).unwrap();
let new_state = driver.round_state(Round::new(0)).unwrap();
assert_eq!(new_state, &step.new_state);

previous_message = output.and_then(to_input_msg);
}
}

#[test]
fn executor_steps_not_proposer_timeout_multiple_rounds() {
fn driver_steps_not_proposer_timeout_multiple_rounds() {
let value = TestContext::DUMMY_VALUE; // TODO: get value from external source
let value_id = value.id();

Expand All @@ -412,7 +412,7 @@ fn executor_steps_not_proposer_timeout_multiple_rounds() {

let vs = ValidatorSet::new(vec![v1.clone(), v2.clone(), v3.clone()]);
let client = TestClient::new(value.clone(), |_| true);
let mut executor = Executor::new(client, Height::new(1), vs, my_sk.clone(), my_addr);
let mut driver = Driver::new(client, Height::new(1), vs, my_sk.clone(), my_addr);

let steps = vec![
// Start round 0, we, v3, are not the proposer
Expand Down Expand Up @@ -567,11 +567,11 @@ fn executor_steps_not_proposer_timeout_multiple_rounds() {
.input_event
.unwrap_or_else(|| previous_message.unwrap());

let output = executor.execute(execute_message);
let output = driver.execute(execute_message);
assert_eq!(output, step.expected_output, "expected output message");

// TODO - add expected executor round to test and assert before the new_state check below
let new_state = executor.round_state(executor.round).unwrap();
// TODO - add expected driver round to test and assert before the new_state check below
let new_state = driver.round_state(driver.round).unwrap();
assert_eq!(new_state, &step.new_state, "new state");
assert_eq!(output, step.expected_output, "expected output message");

Expand Down

0 comments on commit ae2423c

Please sign in to comment.