pub(crate) mod binary_port;
pub(crate) mod block_accumulator;
pub(crate) mod block_synchronizer;
pub(crate) mod block_validator;
pub mod consensus;
pub mod contract_runtime;
pub(crate) mod diagnostics_port;
pub(crate) mod event_stream_server;
pub(crate) mod fetcher;
pub(crate) mod gossiper;
pub(crate) mod transaction_buffer;
#[cfg(test)]
pub mod in_memory_network;
pub(crate) mod metrics;
pub(crate) mod network;
pub(crate) mod rest_server;
pub(crate) mod shutdown_trigger;
pub mod storage;
pub(crate) mod sync_leaper;
pub(crate) mod transaction_acceptor;
pub(crate) mod upgrade_watcher;
use datasize::DataSize;
use serde::Deserialize;
use std::fmt::{Debug, Display};
use tracing::info;
use crate::{
effect::{EffectBuilder, Effects},
failpoints::FailpointActivation,
NodeRng,
};
#[cfg_attr(doc, aquamarine::aquamarine)]
#[derive(Clone, PartialEq, Eq, DataSize, Debug, Deserialize, Default)]
pub(crate) enum ComponentState {
#[default]
Uninitialized,
Initializing,
Initialized,
Fatal(String),
}
pub(crate) trait Component<REv> {
type Event;
fn name(&self) -> &str;
fn activate_failpoint(&mut self, _activation: &FailpointActivation) {
}
fn handle_event(
&mut self,
effect_builder: EffectBuilder<REv>,
rng: &mut NodeRng,
event: Self::Event,
) -> Effects<Self::Event>;
}
pub(crate) trait InitializedComponent<REv>: Component<REv> {
fn state(&self) -> &ComponentState;
fn is_uninitialized(&self) -> bool {
self.state() == &ComponentState::Uninitialized
}
fn is_fatal(&self) -> bool {
matches!(self.state(), ComponentState::Fatal(_))
}
fn start_initialization(&mut self) {
if self.is_uninitialized() {
self.set_state(ComponentState::Initializing);
} else {
info!(name = self.name(), "component must be uninitialized");
}
}
fn set_state(&mut self, new_state: ComponentState);
}
pub(crate) trait PortBoundComponent<REv>: InitializedComponent<REv> {
type Error: Display + Debug;
type ComponentEvent;
fn bind(
&mut self,
enabled: bool,
effect_builder: EffectBuilder<REv>,
) -> (Effects<Self::ComponentEvent>, ComponentState) {
if !enabled {
return (Effects::new(), ComponentState::Initialized);
}
match self.listen(effect_builder) {
Ok(effects) => (effects, ComponentState::Initialized),
Err(error) => (Effects::new(), ComponentState::Fatal(format!("{}", error))),
}
}
fn listen(
&mut self,
effect_builder: EffectBuilder<REv>,
) -> Result<Effects<Self::ComponentEvent>, Self::Error>;
}
pub(crate) trait ValidatorBoundComponent<REv>: Component<REv> {
fn handle_validators(
&mut self,
effect_builder: EffectBuilder<REv>,
rng: &mut NodeRng,
) -> Effects<Self::Event>;
}