aranya-daemon 6.0.0

Daemon process for syncing with Aranya peers and maintaining the DAG
Documentation
//! Aranya syncer used to send/receive graph commands to other peers.

mod client;
mod handle;
mod manager;
mod server;
mod transport;
mod types;

use aranya_runtime::GraphId;
use aranya_util::Addr;

#[cfg(feature = "preview")]
pub(crate) use self::types::HelloSubscription;
pub(super) use self::types::SyncResponse;
pub(crate) use self::{
    client::SyncClient, handle::SyncHandle, manager::SyncManager, server::SyncServer,
    transport::quic, types::SyncPeer,
};

/// The error type which is returned from syncing with peers.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub(crate) enum Error {
    // TODO(nikki): decide if we want to add a generic.
    /// Something went wrong with the transport layer.
    #[error(transparent)]
    Transport(Box<dyn std::error::Error + Send + Sync + 'static>),

    /// Something went wrong in the Aranya Runtime.
    #[error(transparent)]
    Runtime(#[from] aranya_runtime::SyncError),

    /// Something went wrong in the Aranya Client.
    #[error(transparent)]
    AranyaClient(#[from] aranya_runtime::ClientError),

    /// `postcard` was unable to de/serialize a message into a `SyncType`.
    #[error("postcard failed to de/serialize a message")]
    Postcard(#[from] postcard::Error),

    /// Peer sent an empty response.
    #[cfg(feature = "preview")]
    #[error("peer sent empty response")]
    EmptyResponse,

    /// Failed to collect effects from the sink.
    #[error("failed to collect effects")]
    EffectsSink(#[source] Box<dyn std::error::Error + Send + Sync>),

    /// The syncer is in the middle of shutting down at the end of runtime.
    #[error("the sync manager has shut down")]
    SyncerShutdown,

    /// The server received an invalid/unexpected request.
    #[error("received an invalid request")]
    InvalidRequest,

    /// The server told us that an error occurred while processing a request.
    #[error("the server indicated a sync error")]
    Response(String),

    /// Encountered a bug in the program.
    #[error(transparent)]
    Bug(#[from] buggy::Bug),
}

// Implements this error type to allow it being sent over RPC.
impl From<Error> for aranya_daemon_api::Error {
    #[inline]
    fn from(err: Error) -> Self {
        Self::from_err(err)
    }
}

// Allows Infallible types to be desugared properly with ?.
impl From<std::convert::Infallible> for Error {
    fn from(err: std::convert::Infallible) -> Self {
        match err {}
    }
}

impl Error {
    fn transport(err: impl std::error::Error + Send + Sync + 'static) -> Self {
        Self::Transport(Box::new(err))
    }

    /// Returns whether a `ParallelFinalize` error occurred, which needs to be resolved manually.
    fn is_parallel_finalize(&self) -> bool {
        matches!(
            self,
            Self::AranyaClient(aranya_runtime::ClientError::ParallelFinalize)
        )
    }
}

/// A specialized Result type for sync operations.
///
/// This type is used broadly across [`aranya_daemon::sync`] for any operation which may produce an
/// error.
///
/// This type alias is generally used to avoid writing out [`sync::Error`] directly and is otherwise
/// a direct mapping to [`Result`].
///
/// [`aranya_daemon::sync`]: crate::sync
/// [`sync::Error`]: Error
/// [`Result`]: std::result::Result
type Result<T> = core::result::Result<T, Error>;