Skip to content

Commit

Permalink
code: Rename Event to Input and Message to Output (#93)
Browse files Browse the repository at this point in the history
* Rename `Message` to `Output` across the project

* Cleanup doc comments

* Rename `Event` to `Input` across the project

* Cleanup
  • Loading branch information
romac authored Nov 30, 2023
1 parent 7563350 commit 4c5a1a7
Show file tree
Hide file tree
Showing 21 changed files with 639 additions and 598 deletions.
1 change: 0 additions & 1 deletion Code/QUESTIONS.md

This file was deleted.

11 changes: 0 additions & 11 deletions Code/TODO.md

This file was deleted.

72 changes: 36 additions & 36 deletions Code/driver/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ use malachite_common::{
Context, Proposal, Round, SignedVote, Timeout, TimeoutStep, Validator, ValidatorSet, Value,
Vote, VoteType,
};
use malachite_round::events::Event as RoundEvent;
use malachite_round::message::Message as RoundMessage;
use malachite_round::input::Input as RoundEvent;
use malachite_round::output::Output as RoundOutput;
use malachite_round::state::{State as RoundState, Step};
use malachite_round::state_machine::Info;
use malachite_vote::keeper::Message as VoteMessage;
use malachite_vote::keeper::Output as VoteMessage;
use malachite_vote::keeper::VoteKeeper;
use malachite_vote::Threshold;
use malachite_vote::ThresholdParams;

use crate::event::Event;
use crate::message::Message;
use crate::input::Input;
use crate::output::Output;
use crate::Error;
use crate::ProposerSelector;
use crate::Validity;
Expand Down Expand Up @@ -81,55 +81,55 @@ where
Ok(proposer)
}

pub async fn execute(&mut self, msg: Event<Ctx>) -> Result<Option<Message<Ctx>>, Error<Ctx>> {
let round_msg = match self.apply(msg).await? {
pub async fn execute(&mut self, msg: Input<Ctx>) -> Result<Option<Output<Ctx>>, Error<Ctx>> {
let round_output = match self.apply(msg).await? {
Some(msg) => msg,
None => return Ok(None),
};

let msg = match round_msg {
RoundMessage::NewRound(round) => Message::NewRound(self.height().clone(), round),
let output = match round_output {
RoundOutput::NewRound(round) => Output::NewRound(self.height().clone(), round),

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

RoundMessage::Vote(vote) => {
RoundOutput::Vote(vote) => {
let signed_vote = self.ctx.sign_vote(vote);
Message::Vote(signed_vote)
Output::Vote(signed_vote)
}

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

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

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

Ok(Some(msg))
Ok(Some(output))
}

async fn apply(&mut self, event: Event<Ctx>) -> Result<Option<RoundMessage<Ctx>>, Error<Ctx>> {
match event {
Event::NewRound(height, round) => self.apply_new_round(height, round).await,
Event::ProposeValue(round, value) => self.apply_propose_value(round, value).await,
Event::Proposal(proposal, validity) => self.apply_proposal(proposal, validity).await,
Event::Vote(signed_vote) => self.apply_vote(signed_vote),
Event::TimeoutElapsed(timeout) => self.apply_timeout(timeout),
async fn apply(&mut self, input: Input<Ctx>) -> Result<Option<RoundOutput<Ctx>>, Error<Ctx>> {
match input {
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::TimeoutElapsed(timeout) => self.apply_timeout(timeout),
}
}

async fn apply_new_round(
&mut self,
height: Ctx::Height,
round: Round,
) -> Result<Option<RoundMessage<Ctx>>, Error<Ctx>> {
) -> Result<Option<RoundOutput<Ctx>>, Error<Ctx>> {
if self.height() == &height {
// If it's a new round for same height, just reset the round, keep the valid and locked values
self.round_state.round = round;
Expand All @@ -143,15 +143,15 @@ where
&mut self,
round: Round,
value: Ctx::Value,
) -> Result<Option<RoundMessage<Ctx>>, Error<Ctx>> {
) -> Result<Option<RoundOutput<Ctx>>, Error<Ctx>> {
self.apply_event(round, RoundEvent::ProposeValue(value))
}

async fn apply_proposal(
&mut self,
proposal: Ctx::Proposal,
validity: Validity,
) -> Result<Option<RoundMessage<Ctx>>, Error<Ctx>> {
) -> Result<Option<RoundOutput<Ctx>>, Error<Ctx>> {
// Check that there is an ongoing round
if self.round_state.round == Round::Nil {
return Ok(None);
Expand Down Expand Up @@ -241,7 +241,7 @@ where
fn apply_vote(
&mut self,
signed_vote: SignedVote<Ctx>,
) -> Result<Option<RoundMessage<Ctx>>, Error<Ctx>> {
) -> Result<Option<RoundOutput<Ctx>>, Error<Ctx>> {
let validator = self
.validator_set
.get_by_address(signed_vote.validator_address())
Expand Down Expand Up @@ -279,7 +279,7 @@ where
self.apply_event(vote_round, round_event)
}

fn apply_timeout(&mut self, timeout: Timeout) -> Result<Option<RoundMessage<Ctx>>, Error<Ctx>> {
fn apply_timeout(&mut self, timeout: Timeout) -> Result<Option<RoundOutput<Ctx>>, Error<Ctx>> {
let event = match timeout.step {
TimeoutStep::Propose => RoundEvent::TimeoutPropose,
TimeoutStep::Prevote => RoundEvent::TimeoutPrevote,
Expand All @@ -294,7 +294,7 @@ where
&mut self,
event_round: Round,
event: RoundEvent<Ctx>,
) -> Result<Option<RoundMessage<Ctx>>, Error<Ctx>> {
) -> Result<Option<RoundOutput<Ctx>>, Error<Ctx>> {
let round_state = core::mem::take(&mut self.round_state);
let proposer = self.get_proposer(round_state.round)?;

Expand All @@ -319,13 +319,13 @@ where
};

// Apply the event to the round state machine
let transition = round_state.apply_event(&data, mux_event);
let transition = round_state.apply(&data, mux_event);

// Update state
self.round_state = transition.next_state;

// Return message, if any
Ok(transition.message)
// Return output, if any
Ok(transition.output)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Code/driver/src/event.rs → Code/driver/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::Validity;

/// Events that can be received by the [`Driver`](crate::Driver).
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Event<Ctx>
pub enum Input<Ctx>
where
Ctx: Context,
{
Expand Down
8 changes: 4 additions & 4 deletions Code/driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ extern crate alloc;

mod driver;
mod error;
mod event;
mod message;
mod input;
mod output;
mod proposer;
mod util;

pub use driver::Driver;
pub use error::Error;
pub use event::Event;
pub use message::Message;
pub use input::Input;
pub use output::Output;
pub use proposer::ProposerSelector;
pub use util::Validity;

Expand Down
93 changes: 0 additions & 93 deletions Code/driver/src/message.rs

This file was deleted.

93 changes: 93 additions & 0 deletions Code/driver/src/output.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use core::fmt;

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

/// Messages emitted by the [`Driver`](crate::Driver)
pub enum Output<Ctx>
where
Ctx: Context,
{
/// Start a new round
NewRound(Ctx::Height, Round),

/// Broadcast a proposal
Propose(Ctx::Proposal),

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

/// Decide on a value
Decide(Round, Ctx::Value),

/// Schedule a timeout
ScheduleTimeout(Timeout),

/// Ask for a value to propose and schedule a timeout
GetValueAndScheduleTimeout(Round, Timeout),
}

// NOTE: We have to derive these instances manually, otherwise
// the compiler would infer a Clone/Debug/PartialEq/Eq bound on `Ctx`,
// which may not hold for all contexts.

impl<Ctx: Context> Clone for Output<Ctx> {
#[cfg_attr(coverage_nightly, coverage(off))]
fn clone(&self) -> Self {
match self {
Output::NewRound(height, round) => Output::NewRound(height.clone(), *round),
Output::Propose(proposal) => Output::Propose(proposal.clone()),
Output::Vote(signed_vote) => Output::Vote(signed_vote.clone()),
Output::Decide(round, value) => Output::Decide(*round, value.clone()),
Output::ScheduleTimeout(timeout) => Output::ScheduleTimeout(*timeout),
Output::GetValueAndScheduleTimeout(round, timeout) => {
Output::GetValueAndScheduleTimeout(*round, *timeout)
}
}
}
}

impl<Ctx: Context> fmt::Debug for Output<Ctx> {
#[cfg_attr(coverage_nightly, coverage(off))]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Output::NewRound(height, round) => write!(f, "NewRound({:?}, {:?})", height, round),
Output::Propose(proposal) => write!(f, "Propose({:?})", proposal),
Output::Vote(signed_vote) => write!(f, "Vote({:?})", signed_vote),
Output::Decide(round, value) => write!(f, "Decide({:?}, {:?})", round, value),
Output::ScheduleTimeout(timeout) => write!(f, "ScheduleTimeout({:?})", timeout),
Output::GetValueAndScheduleTimeout(round, timeout) => {
write!(f, "GetValueAndScheduleTimeout({:?}, {:?})", round, timeout)
}
}
}
}

impl<Ctx: Context> PartialEq for Output<Ctx> {
#[cfg_attr(coverage_nightly, coverage(off))]
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Output::NewRound(height, round), Output::NewRound(other_height, other_round)) => {
height == other_height && round == other_round
}
(Output::Propose(proposal), Output::Propose(other_proposal)) => {
proposal == other_proposal
}
(Output::Vote(signed_vote), Output::Vote(other_signed_vote)) => {
signed_vote == other_signed_vote
}
(Output::Decide(round, value), Output::Decide(other_round, other_value)) => {
round == other_round && value == other_value
}
(Output::ScheduleTimeout(timeout), Output::ScheduleTimeout(other_timeout)) => {
timeout == other_timeout
}
(
Output::GetValueAndScheduleTimeout(round, timeout),
Output::GetValueAndScheduleTimeout(other_round, other_timeout),
) => round == other_round && timeout == other_timeout,
_ => false,
}
}
}

impl<Ctx: Context> Eq for Output<Ctx> {}
Loading

0 comments on commit 4c5a1a7

Please sign in to comment.