aranya_runtime/sync/
mod.rs1use aranya_buggy::Bug;
4use postcard::Error as PostcardError;
5use serde::{Deserialize, Serialize};
6
7use crate::{
8 command::{Command, CommandId, Priority},
9 storage::{StorageError, MAX_COMMAND_LENGTH},
10 Address, Prior,
11};
12
13mod requester;
14mod responder;
15
16pub use requester::SyncRequester;
17pub use responder::{PeerCache, SyncResponder};
18
19pub const PEER_HEAD_MAX: usize = 10;
23
24const COMMAND_SAMPLE_MAX: usize = 100;
26
27const REQUEST_MISSING_MAX: usize = 100;
30
31pub const COMMAND_RESPONSE_MAX: usize = 100;
33
34const SEGMENT_BUFFER_MAX: usize = 100;
36
37pub const MAX_SYNC_MESSAGE_SIZE: usize = 1024 + MAX_COMMAND_LENGTH * COMMAND_RESPONSE_MAX;
41
42#[derive(Serialize, Deserialize, Debug)]
44pub struct CommandMeta {
45 id: CommandId,
46 priority: Priority,
47 parent: Prior<Address>,
48 policy_length: u32,
49 length: u32,
50 max_cut: usize,
51}
52
53#[derive(Debug, thiserror::Error)]
55pub enum SyncError {
56 #[error("sync session ID does not match")]
57 SessionMismatch,
58 #[error("missing sync response")]
59 MissingSyncResponse,
60 #[error("syncer state not valid for this message")]
61 SessionState,
62 #[error("syncer not ready for operation")]
63 NotReady,
64 #[error("too many commands sent")]
65 CommandOverflow,
66 #[error("storage error: {0}")]
67 Storage(#[from] StorageError),
68 #[error("serialize error: {0}")]
69 Serialize(#[from] PostcardError),
70 #[error(transparent)]
71 Bug(#[from] Bug),
72}
73
74#[derive(Serialize, Deserialize, Debug)]
76pub struct SyncCommand<'a> {
77 priority: Priority,
78 id: CommandId,
79 parent: Prior<Address>,
80 policy: Option<&'a [u8]>,
81 data: &'a [u8],
82 max_cut: usize,
83}
84
85impl<'a> Command for SyncCommand<'a> {
86 fn priority(&self) -> Priority {
87 self.priority.clone()
88 }
89
90 fn id(&self) -> CommandId {
91 self.id
92 }
93
94 fn parent(&self) -> Prior<Address> {
95 self.parent
96 }
97
98 fn policy(&self) -> Option<&'a [u8]> {
99 self.policy
100 }
101
102 fn bytes(&self) -> &'a [u8] {
103 self.data
104 }
105
106 fn max_cut(&self) -> Result<usize, Bug> {
107 Ok(self.max_cut)
108 }
109}