Skip to content

Commit

Permalink
feat: Migrate node implementation trait
Browse files Browse the repository at this point in the history
  • Loading branch information
nmccarty committed Feb 15, 2022
1 parent 738b7b5 commit e10b761
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 44 deletions.
2 changes: 2 additions & 0 deletions phaselock-types/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Common traits for the `PhaseLock` protocol
pub mod block_contents;
pub mod network;
pub mod node_implementation;
pub mod state;
pub mod stateful_handler;
pub mod storage;

pub use block_contents::BlockContents;
Expand Down
41 changes: 41 additions & 0 deletions phaselock-types/src/traits/node_implementation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//! Composite trait for node behavior
//!
//! This module defines the [`NodeImplementation`] trait, which is a composite trait used for
//! describing the overall behavior of a node, as a composition of implementations of the node trait.
use std::fmt::Debug;

use crate::{
message::Message,
traits::{
network::NetworkingImplementation, stateful_handler::StatefulHandler, storage::Storage,
BlockContents,
},
};

/// Node implementation aggregate trait
///
/// This trait exists to collect multiple behavior implementations into one type, to allow
/// `PhaseLock` to avoid annoying numbers of type arguments and type patching.
///
/// It is recommended you implement this trait on a zero sized type, as `PhaseLock`does not actually
/// store or keep a reference to any value implementing this trait.
pub trait NodeImplementation<const N: usize>: Send + Sync + Debug + Clone + 'static {
/// Block type for this consensus implementation
type Block: BlockContents<N> + 'static;
/// State type for this consensus implementation
type State: crate::traits::State<N, Block = Self::Block>;
/// Storage type for this consensus implementation
type Storage: Storage<Self::Block, Self::State, N> + Clone;
/// Networking type for this consensus implementation
type Networking: NetworkingImplementation<
Message<
Self::Block,
<<Self as NodeImplementation<N>>::Block as BlockContents<N>>::Transaction,
Self::State,
N,
>,
> + Clone;
/// Stateful call back handler for this consensus implementation
type StatefulHandler: StatefulHandler<N, Block = Self::Block, State = Self::State>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
use std::{fmt::Debug, marker::PhantomData};

use super::{BlockContents, State};
use crate::traits::{BlockContents, State};

/// Trait for a stateful event handler
///
/// The [`PhaseLock`](crate::PhaseLock) instance will keep around the provided value of this type,
/// and call the `notify` method every time a series of blocks are committed.
/// The `PhaseLock` instance will keep around the provided value of this type, and call the `notify`
/// method every time a series of blocks are committed.
///
/// A do-nothing implementation ([`Stateless`]) is provided, as a convince for implementations that
/// do not need this functionality.
Expand All @@ -17,8 +17,8 @@ pub trait StatefulHandler<const N: usize>: Send + Sync + Debug + 'static {
/// State type for this consensus implementation
type State: State<N, Block = Self::Block>;

/// The [`PhaseLock`](crate::PhaseLock) implementation will call this method, with the series of
/// blocks and states that are being committed, whenever a commit action takes place.
/// The `PhaseLock` implementation will call this method, with the series of blocks and states
/// that are being committed, whenever a commit action takes place.
///
/// The provided states and blocks are guaranteed to be in ascending order of age (newest to
/// oldest).
Expand Down
5 changes: 2 additions & 3 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@
pub mod election;
mod networking;
mod node_implementation;
mod stateful_handler;
mod storage;

pub use election::Election;
pub use networking::{BoxedFuture, NetworkError, NetworkReliability, NetworkingImplementation};
pub use node_implementation::NodeImplementation;
pub use phaselock_types::traits::stateful_handler::StatefulHandler;
pub use phaselock_types::traits::BlockContents;
pub use phaselock_types::traits::State;
pub use stateful_handler::StatefulHandler;
pub use storage::{Storage, StorageResult};

/// Module for publicly usable implementations of the traits
pub mod implementations {
pub use super::networking::memory_network::{DummyReliability, MasterMap, MemoryNetwork};
pub use super::networking::w_network::WNetwork;
pub use super::stateful_handler::Stateless;
pub use super::storage::memory_storage::MemoryStorage;
pub use phaselock_types::traits::stateful_handler::Stateless;
}

/// Dummy testing implementations
Expand Down
37 changes: 1 addition & 36 deletions src/traits/node_implementation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,4 @@
//! This module defines the [`NodeImplementation`] trait, which is a composite trait used for
//! describing the overall behavior of a node, as a composition of implementations of the node trait.
use std::fmt::Debug;

use crate::{
traits::{BlockContents, NetworkingImplementation, Storage},
types::Message,
};

use super::StatefulHandler;

/// Node implementation aggregate trait
///
/// This trait exists to collect multiple behavior implementations into one type, to allow
/// [`PhaseLock`](crate::PhaseLock) to avoid annoying numbers of type arguments and type patching.
///
/// It is recommended you implement this trait on a zero sized type, as
/// [`PhaseLock`](crate::PhaseLock) does not actually store or keep a reference to any value
/// implementing this trait.
pub trait NodeImplementation<const N: usize>: Send + Sync + Debug + Clone + 'static {
/// Block type for this consensus implementation
type Block: BlockContents<N> + 'static;
/// State type for this consensus implementation
type State: crate::traits::State<N, Block = Self::Block>;
/// Storage type for this consensus implementation
type Storage: Storage<Self::Block, Self::State, N> + Clone;
/// Networking type for this consensus implementation
type Networking: NetworkingImplementation<
Message<
Self::Block,
<<Self as NodeImplementation<N>>::Block as BlockContents<N>>::Transaction,
Self::State,
N,
>,
> + Clone;
/// Stateful call back handler for this consensus implementation
type StatefulHandler: StatefulHandler<N, Block = Self::Block, State = Self::State>;
}
pub use phaselock_types::traits::node_implementation::NodeImplementation;

0 comments on commit e10b761

Please sign in to comment.