Skip to content

Commit

Permalink
Move Transition struct into its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
romac committed Oct 31, 2023
1 parent ee55d30 commit 26a5573
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 37 deletions.
1 change: 1 addition & 0 deletions Code/round/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ pub mod events;
pub mod message;
pub mod state;
pub mod state_machine;
pub mod transition;
2 changes: 1 addition & 1 deletion Code/round/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::events::Event;
use crate::state_machine::Transition;
use crate::transition::Transition;

use malachite_common::{Context, Round};

Expand Down
37 changes: 1 addition & 36 deletions Code/round/src/state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,7 @@ use malachite_common::{Context, Proposal, Round, TimeoutStep, Value, ValueId};
use crate::events::Event;
use crate::message::Message;
use crate::state::{State, Step};

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Transition<Ctx>
where
Ctx: Context,
{
pub next_state: State<Ctx>,
pub message: Option<Message<Ctx>>,
pub valid: bool,
}

impl<Ctx> Transition<Ctx>
where
Ctx: Context,
{
pub fn to(next_state: State<Ctx>) -> Self {
Self {
next_state,
message: None,
valid: true,
}
}

pub fn invalid(next_state: State<Ctx>) -> Self {
Self {
next_state,
message: None,
valid: false,
}
}

pub fn with_message(mut self, message: Message<Ctx>) -> Self {
self.message = Some(message);
self
}
}
use crate::transition::Transition;

/// Check that a proposal has a valid Proof-Of-Lock round
fn is_valid_pol_round<Ctx>(state: &State<Ctx>, pol_round: Round) -> bool
Expand Down
40 changes: 40 additions & 0 deletions Code/round/src/transition.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use malachite_common::Context;

use crate::message::Message;
use crate::state::State;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Transition<Ctx>
where
Ctx: Context,
{
pub next_state: State<Ctx>,
pub message: Option<Message<Ctx>>,
pub valid: bool,
}

impl<Ctx> Transition<Ctx>
where
Ctx: Context,
{
pub fn to(next_state: State<Ctx>) -> Self {
Self {
next_state,
message: None,
valid: true,
}
}

pub fn invalid(next_state: State<Ctx>) -> Self {
Self {
next_state,
message: None,
valid: false,
}
}

pub fn with_message(mut self, message: Message<Ctx>) -> Self {
self.message = Some(message);
self
}
}

0 comments on commit 26a5573

Please sign in to comment.