kona_node_service/actors/
traits.rs

1//! [NodeActor] trait.
2
3use async_trait::async_trait;
4use tokio_util::sync::WaitForCancellationFuture;
5
6/// The communication context used by the actor.
7pub trait CancellableContext: Send {
8    /// Returns a future that resolves when the actor is cancelled.
9    fn cancelled(&self) -> WaitForCancellationFuture<'_>;
10}
11
12/// The [NodeActor] is an actor-like service for the node.
13///
14/// Actors may:
15/// - Handle incoming messages.
16///     - Perform background tasks.
17/// - Emit new events for other actors to process.
18#[async_trait]
19pub trait NodeActor: Send + 'static {
20    /// The error type for the actor.
21    type Error: std::fmt::Debug;
22    /// The communication context used by the actor.
23    /// These are the channels that the actor will use to send messages to other actors.
24    type OutboundData: CancellableContext;
25    /// The inbound communication channels used by the actor.
26    /// These are the channels that the actor will use to receive messages from other actors.
27    type InboundData: Sized;
28    /// The configuration needed to build the actor.
29    type Builder;
30
31    /// Builds the actor.
32    fn build(builder: Self::Builder) -> (Self::InboundData, Self);
33
34    /// Starts the actor.
35    async fn start(self, inbound_context: Self::OutboundData) -> Result<(), Self::Error>;
36}