aranya_runtime/sync/
mod.rsuse core::{convert::Infallible, fmt};
use aranya_buggy::Bug;
use postcard::Error as PostcardError;
use serde::{Deserialize, Serialize};
use crate::{
command::{Command, CommandId, Priority},
storage::{StorageError, MAX_COMMAND_LENGTH},
Address, Prior,
};
mod requester;
mod responder;
pub use requester::SyncRequester;
pub use responder::{PeerCache, SyncResponder};
pub const PEER_HEAD_MAX: usize = 10;
const COMMAND_SAMPLE_MAX: usize = 100;
const REQUEST_MISSING_MAX: usize = 100;
pub const COMMAND_RESPONSE_MAX: usize = 100;
const SEGMENT_BUFFER_MAX: usize = 100;
pub const MAX_SYNC_MESSAGE_SIZE: usize = 1024 + MAX_COMMAND_LENGTH * COMMAND_RESPONSE_MAX;
#[derive(Serialize, Deserialize, Debug)]
pub struct CommandMeta {
id: CommandId,
priority: Priority,
parent: Prior<Address>,
policy_length: u32,
length: u32,
max_cut: usize,
}
#[derive(Debug)]
pub enum SyncError {
UnexpectedMessage,
SessionMismatch,
MissingSyncResponse,
SessionState,
StorageError,
NotReady,
SerilizeError,
CommandOverflow,
Bug(Bug),
}
impl fmt::Display for SyncError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UnexpectedMessage => write!(f, "unexpected message"),
Self::SessionMismatch => write!(f, "session mismatch"),
Self::MissingSyncResponse => write!(f, "missing sync response"),
Self::SessionState => write!(f, "session state error"),
Self::StorageError => write!(f, "storage error"),
Self::NotReady => write!(f, "not ready"),
Self::SerilizeError => write!(f, "serialize error"),
Self::CommandOverflow => write!(f, "too many commands sent"),
Self::Bug(bug) => write!(f, "{bug}"),
}
}
}
impl core::error::Error for SyncError {}
impl From<Bug> for SyncError {
fn from(error: Bug) -> Self {
SyncError::Bug(error)
}
}
impl From<Infallible> for SyncError {
fn from(error: Infallible) -> Self {
match error {}
}
}
impl From<StorageError> for SyncError {
fn from(_error: StorageError) -> Self {
SyncError::StorageError
}
}
impl From<PostcardError> for SyncError {
fn from(_error: PostcardError) -> Self {
SyncError::SerilizeError
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct SyncCommand<'a> {
priority: Priority,
id: CommandId,
parent: Prior<Address>,
policy: Option<&'a [u8]>,
data: &'a [u8],
max_cut: usize,
}
impl<'a> Command for SyncCommand<'a> {
fn priority(&self) -> Priority {
self.priority.clone()
}
fn id(&self) -> CommandId {
self.id
}
fn parent(&self) -> Prior<Address> {
self.parent
}
fn policy(&self) -> Option<&'a [u8]> {
self.policy
}
fn bytes(&self) -> &'a [u8] {
self.data
}
fn max_cut(&self) -> Result<usize, Bug> {
Ok(self.max_cut)
}
}