From ae2423cf47b7323c62d0abdf04a63942345aed5e Mon Sep 17 00:00:00 2001 From: Romain Ruetschi Date: Tue, 7 Nov 2023 15:17:13 +0100 Subject: [PATCH] Rename `Executor` to `Driver` --- Code/Cargo.toml | 2 +- Code/{consensus => driver}/Cargo.toml | 4 +- Code/driver/src/client.rs | 14 +++++++ .../src/executor.rs => driver/src/driver.rs} | 42 ++++--------------- Code/driver/src/event.rs | 13 ++++++ Code/{consensus => driver}/src/lib.rs | 12 +++++- Code/driver/src/message.rs | 14 +++++++ Code/round/src/state_machine.rs | 2 +- Code/test/Cargo.toml | 8 ++-- Code/test/src/client.rs | 2 +- .../{consensus_executor.rs => driver.rs} | 28 ++++++------- 11 files changed, 81 insertions(+), 60 deletions(-) rename Code/{consensus => driver}/Cargo.toml (78%) create mode 100644 Code/driver/src/client.rs rename Code/{consensus/src/executor.rs => driver/src/driver.rs} (91%) create mode 100644 Code/driver/src/event.rs rename Code/{consensus => driver}/src/lib.rs (58%) create mode 100644 Code/driver/src/message.rs rename Code/test/tests/{consensus_executor.rs => driver.rs} (95%) diff --git a/Code/Cargo.toml b/Code/Cargo.toml index beb3909f2..b96fe3227 100644 --- a/Code/Cargo.toml +++ b/Code/Cargo.toml @@ -3,7 +3,7 @@ resolver = "2" members = [ "common", - "consensus", + "driver", "round", "vote", "test", diff --git a/Code/consensus/Cargo.toml b/Code/driver/Cargo.toml similarity index 78% rename from Code/consensus/Cargo.toml rename to Code/driver/Cargo.toml index 9144e823a..bf2b34458 100644 --- a/Code/consensus/Cargo.toml +++ b/Code/driver/Cargo.toml @@ -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 diff --git a/Code/driver/src/client.rs b/Code/driver/src/client.rs new file mode 100644 index 000000000..1e240d730 --- /dev/null +++ b/Code/driver/src/client.rs @@ -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 +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; +} diff --git a/Code/consensus/src/executor.rs b/Code/driver/src/driver.rs similarity index 91% rename from Code/consensus/src/executor.rs rename to Code/driver/src/driver.rs index e7ec64f89..fad28b96e 100644 --- a/Code/consensus/src/executor.rs +++ b/Code/driver/src/driver.rs @@ -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 -where - Ctx: Context, -{ - NewRound(Round), - Proposal(Ctx::Proposal), - Vote(SignedVote), - TimeoutElapsed(Timeout), -} - -#[derive(Clone, Debug, PartialEq, Eq)] -pub enum Message -where - Ctx: Context, -{ - Propose(Ctx::Proposal), - Vote(SignedVote), - Decide(Round, Ctx::Value), - ScheduleTimeout(Timeout), - NewRound(Round), -} - -pub trait Client -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 +pub struct Driver where Ctx: Context, - Client: self::Client, + Client: crate::client::Client, { pub client: Client, pub height: Ctx::Height, @@ -63,10 +35,10 @@ where pub round_states: BTreeMap>, } -impl Executor +impl Driver where Ctx: Context, - Client: self::Client, + Client: crate::client::Client, { pub fn new( client: Client, diff --git a/Code/driver/src/event.rs b/Code/driver/src/event.rs new file mode 100644 index 000000000..060e330ce --- /dev/null +++ b/Code/driver/src/event.rs @@ -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 +where + Ctx: Context, +{ + NewRound(Round), + Proposal(Ctx::Proposal), + Vote(SignedVote), + TimeoutElapsed(Timeout), +} diff --git a/Code/consensus/src/lib.rs b/Code/driver/src/lib.rs similarity index 58% rename from Code/consensus/src/lib.rs rename to Code/driver/src/lib.rs index 9f5b7756d..ce115e1e7 100644 --- a/Code/consensus/src/lib.rs +++ b/Code/driver/src/lib.rs @@ -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)] @@ -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; diff --git a/Code/driver/src/message.rs b/Code/driver/src/message.rs new file mode 100644 index 000000000..e4ebde55d --- /dev/null +++ b/Code/driver/src/message.rs @@ -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 +where + Ctx: Context, +{ + Propose(Ctx::Proposal), + Vote(SignedVote), + Decide(Round, Ctx::Value), + ScheduleTimeout(Timeout), + NewRound(Round), +} diff --git a/Code/round/src/state_machine.rs b/Code/round/src/state_machine.rs index 4dda1a1b1..595af5bc0 100644 --- a/Code/round/src/state_machine.rs +++ b/Code/round/src/state_machine.rs @@ -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. /// diff --git a/Code/test/Cargo.toml b/Code/test/Cargo.toml index e7964c2e7..6b536bce3 100644 --- a/Code/test/Cargo.toml +++ b/Code/test/Cargo.toml @@ -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 diff --git a/Code/test/src/client.rs b/Code/test/src/client.rs index 4e05c2345..cc66f2381 100644 --- a/Code/test/src/client.rs +++ b/Code/test/src/client.rs @@ -1,4 +1,4 @@ -use malachite_consensus::executor::Client; +use malachite_driver::Client; use crate::{Proposal, TestContext, Value}; diff --git a/Code/test/tests/consensus_executor.rs b/Code/test/tests/driver.rs similarity index 95% rename from Code/test/tests/consensus_executor.rs rename to Code/test/tests/driver.rs index 67d54afa9..144484b58 100644 --- a/Code/test/tests/consensus_executor.rs +++ b/Code/test/tests/driver.rs @@ -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::{ @@ -26,7 +26,7 @@ fn to_input_msg(output: Message) -> Option> { } #[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(); @@ -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)); @@ -196,10 +196,10 @@ 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); @@ -207,7 +207,7 @@ fn executor_steps_proposer() { } #[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(); @@ -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)); @@ -378,10 +378,10 @@ 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); @@ -389,7 +389,7 @@ fn executor_steps_not_proposer() { } #[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(); @@ -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 @@ -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");