diff options
| author | tv <tv@krebsco.de> | 2026-10-26 21:48:54 +0100 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-10-26 21:48:54 +0100 |
| commit | ecdb3ee944f5c171faac3a71117e2ff27e064295 (patch) | |
| tree | a8e35f4dec4de93c7d53416da43fc4f9a3a381ed /src/transport/mod.rs | |
| parent | 7135c860db4169bfc9a06838cf637350ef68c0c0 (diff) | |
turn all pub into pub(crate)
Diffstat (limited to 'src/transport/mod.rs')
| -rw-r--r-- | src/transport/mod.rs | 128 |
1 files changed, 64 insertions, 64 deletions
diff --git a/src/transport/mod.rs b/src/transport/mod.rs index 0d761e0..c109b0c 100644 --- a/src/transport/mod.rs +++ b/src/transport/mod.rs @@ -9,29 +9,29 @@ use std::future::Future; use std::pin::Pin; use std::sync::atomic::{AtomicUsize, Ordering}; -pub use crate::document::replica::Epoch; +pub(crate) use crate::document::replica::Epoch; -pub mod firewall; -pub mod mycelium; +pub(crate) mod firewall; +pub(crate) mod mycelium; #[cfg(test)] pub(crate) mod sim; -pub mod traffic; +pub(crate) mod traffic; pub(crate) mod wire; /// Random identifier of one run of a node (from start to exit). -pub type RunId = [u8; 16]; +pub(crate) type RunId = [u8; 16]; /// Most heads listed in a keepalive; more are sent as a digest. A head takes 34 bytes on /// the wire, so 32 of them keep a keepalive within one Mycelium chunk (1300 bytes; see /// the size test in `mycelium`). There are rarely more heads than nodes that wrote /// concurrently since the last merging write. -pub const MAX_LISTED_HEADS: usize = 32; +pub(crate) const MAX_LISTED_HEADS: usize = 32; /// A document's heads, compactly: the heads themselves while there are few, otherwise a /// digest of them with some of them. Two summaries are equal exactly when the heads are, /// whatever form each took (nodes with different limits still agree). #[derive(Debug, Clone)] -pub enum HeadsSummary { +pub(crate) enum HeadsSummary { /// Sorted. Heads(Vec<ChangeHash>), Digest { @@ -48,7 +48,7 @@ pub enum HeadsSummary { static SAMPLE_OFFSET: AtomicUsize = AtomicUsize::new(0); impl HeadsSummary { - pub fn of(mut heads: Vec<ChangeHash>) -> Self { + pub(crate) fn of(mut heads: Vec<ChangeHash>) -> Self { heads.sort_unstable(); if heads.len() <= MAX_LISTED_HEADS { return Self::Heads(heads); @@ -68,7 +68,7 @@ impl HeadsSummary { } /// The digest of the heads, computed if they are listed. - pub fn digest(&self) -> [u8; 32] { + pub(crate) fn digest(&self) -> [u8; 32] { match self { Self::Heads(heads) => digest_of(heads), Self::Digest { digest, .. } => *digest, @@ -76,7 +76,7 @@ impl HeadsSummary { } /// The heads, if all are listed. - pub fn heads(&self) -> Option<&[ChangeHash]> { + pub(crate) fn heads(&self) -> Option<&[ChangeHash]> { match self { Self::Heads(heads) => Some(heads), Self::Digest { .. } => None, @@ -84,7 +84,7 @@ impl HeadsSummary { } /// The heads listed: all of them, or a sample. - pub fn listed(&self) -> &[ChangeHash] { + pub(crate) fn listed(&self) -> &[ChangeHash] { match self { Self::Heads(heads) => heads, Self::Digest { sample, .. } => sample, @@ -114,23 +114,23 @@ fn digest_of(sorted_heads: &[ChangeHash]) -> [u8; 32] { /// PING and PONG. Addressed to one peer: `syncs_with_you` is about the recipient. #[derive(Debug, Clone, Serialize, Deserialize)] -pub struct Keepalive { +pub(crate) struct Keepalive { /// Only comparable between nodes on the same epoch. #[serde(with = "crate::transport::wire::codec::heads")] - pub heads: HeadsSummary, + pub(crate) heads: HeadsSummary, #[serde(with = "crate::transport::wire::codec::epoch")] - pub epoch: Epoch, + pub(crate) epoch: Epoch, /// See `SyncEnvelope::epoch_seq`. - pub epoch_seq: u32, + pub(crate) epoch_seq: u32, /// The sender is ready (same meaning as `SyncEnvelope::ready`). - pub ready: bool, + pub(crate) ready: bool, /// The sender would sync with the recipient: counts it as an active member on its /// own epoch. - pub syncs_with_you: bool, + pub(crate) syncs_with_you: bool, } #[derive(Debug)] -pub enum ParsedMessage { +pub(crate) enum ParsedMessage { Hello(HelloRequest), HelloReply(HelloReply), Sync(SyncEnvelope), @@ -142,7 +142,7 @@ pub enum ParsedMessage { /// Content-addressed bytes that any node holding them can serve, in chunks /// (`swarm::blob`). #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] -pub enum BlobId { +pub(crate) enum BlobId { /// An Automerge change, by its hash: e.g. an epoch's snapshot change. Change(#[serde(with = "crate::transport::wire::codec::change_hash")] ChangeHash), /// Bytes by their BLAKE3 hash: e.g. the content of a remake. @@ -164,18 +164,18 @@ impl std::fmt::Display for BlobId { /// A blob request or one of its answers. Each is a message of its own, so no message /// is larger than one chunk. #[derive(Debug, Clone, Serialize, Deserialize)] -pub struct BlobMessage { +pub(crate) struct BlobMessage { /// The sender's epoch (every message carries it). #[serde(with = "crate::transport::wire::codec::epoch")] - pub epoch: Epoch, + pub(crate) epoch: Epoch, /// See `SyncEnvelope::epoch_seq`. - pub epoch_seq: u32, - pub id: BlobId, - pub body: BlobBody, + pub(crate) epoch_seq: u32, + pub(crate) id: BlobId, + pub(crate) body: BlobBody, } #[derive(Clone, Serialize, Deserialize)] -pub enum BlobBody { +pub(crate) enum BlobBody { /// Asks for these chunks; for the manifest if empty. Request { chunks: Vec<u32> }, /// Size and per-chunk BLAKE3 hashes; the last chunk may be shorter. @@ -223,7 +223,7 @@ impl std::fmt::Debug for BlobBody { /// What a message is, without its contents. #[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum MessageKind { +pub(crate) enum MessageKind { Hello, HelloReply, Sync, @@ -233,7 +233,7 @@ pub enum MessageKind { } impl ParsedMessage { - pub fn kind(&self) -> MessageKind { + pub(crate) fn kind(&self) -> MessageKind { match self { ParsedMessage::Hello(_) => MessageKind::Hello, ParsedMessage::HelloReply(_) => MessageKind::HelloReply, @@ -245,7 +245,7 @@ impl ParsedMessage { } /// Every message carries the sender's epoch. - pub fn epoch(&self) -> Epoch { + pub(crate) fn epoch(&self) -> Epoch { match self { ParsedMessage::Hello(hello) => hello.epoch, ParsedMessage::HelloReply(reply) => reply.epoch, @@ -256,7 +256,7 @@ impl ParsedMessage { } /// `SyncEnvelope::epoch_seq`, as every message carries it. - pub fn epoch_seq(&self) -> u32 { + pub(crate) fn epoch_seq(&self) -> u32 { match self { ParsedMessage::Hello(hello) => hello.epoch_seq, ParsedMessage::HelloReply(reply) => reply.epoch_seq, @@ -268,83 +268,83 @@ impl ParsedMessage { } #[derive(Debug, Clone, Serialize, Deserialize)] -pub struct HelloRequest { +pub(crate) struct HelloRequest { /// The sender's swarm, if it knows it (a new node doesn't yet). A receiver in /// another swarm doesn't answer. - pub swarm_id: Option<String>, + pub(crate) swarm_id: Option<String>, #[serde(with = "crate::transport::wire::codec::epoch")] - pub epoch: Epoch, + pub(crate) epoch: Epoch, /// See `SyncEnvelope::epoch_seq`. - pub epoch_seq: u32, + pub(crate) epoch_seq: u32, /// Proof of the swarm key (`swarm::auth`); empty without one. #[serde(with = "serde_bytes")] - pub auth: Vec<u8>, + pub(crate) auth: Vec<u8>, /// The sender is ready (same meaning as `SyncEnvelope::ready`). - pub ready: bool, + pub(crate) ready: bool, /// Random per run of the sender; the reply echoes it. #[serde(with = "crate::transport::wire::codec::run_id")] - pub run_id: RunId, + pub(crate) run_id: RunId, /// The handshake's number: the sender numbers the handshakes it starts in a run, /// with all peers, from 1. A retry carries the same number, so the recipient resets /// its sync session only for a new handshake, not for every resend or late copy. - pub handshake: u32, + pub(crate) handshake: u32, } #[derive(Debug, Clone, Serialize, Deserialize)] -pub struct HelloReply { - pub swarm_id: String, - pub peers: Vec<String>, +pub(crate) struct HelloReply { + pub(crate) swarm_id: String, + pub(crate) peers: Vec<String>, #[serde(with = "crate::transport::wire::codec::epoch")] - pub epoch: Epoch, + pub(crate) epoch: Epoch, /// See `SyncEnvelope::epoch_seq`. - pub epoch_seq: u32, + pub(crate) epoch_seq: u32, /// Proof of the swarm key (`swarm::auth`); empty without one. #[serde(with = "serde_bytes")] - pub auth: Vec<u8>, + pub(crate) auth: Vec<u8>, /// The sender is ready. - pub ready: bool, + pub(crate) ready: bool, /// The `run_id` of the HELLO answered. A reply to a HELLO from an earlier run of the /// recipient (delivered late, or queued while it was down) is ignored. #[serde(with = "crate::transport::wire::codec::run_id")] - pub run_id: RunId, + pub(crate) run_id: RunId, /// How long before this HELLO the sender had last heard from the recipient, if ever /// (in milliseconds): tells a restarted node how long it was away. - pub away_ms: Option<u64>, + pub(crate) away_ms: Option<u64>, /// The sender's own run (as its HELLOs carry it): a new one means it restarted. #[serde(with = "crate::transport::wire::codec::run_id")] - pub sender_run_id: RunId, + pub(crate) sender_run_id: RunId, } #[derive(Debug, Clone, Serialize, Deserialize)] -pub struct SyncEnvelope { - pub swarm_id: String, +pub(crate) struct SyncEnvelope { + pub(crate) swarm_id: String, /// Epoch of the document the message was generated from. #[serde(with = "crate::transport::wire::codec::epoch")] - pub epoch: Epoch, + pub(crate) epoch: Epoch, /// How many times the sender changed epoch in its current run, when it sent this /// (`Replica::epoch_seq`). Messages may arrive late and out of order: the receiver /// ignores an epoch older than the newest it has seen in the same session. - pub epoch_seq: u32, + pub(crate) epoch_seq: u32, #[serde(with = "crate::transport::wire::codec::sync_message")] - pub message: SyncPayload, + pub(crate) message: SyncPayload, /// The sender was ready when it generated the message: its document is the swarm's, /// not one it is still filling (joining, rejoining). Only such a peer can tell a /// catching-up node that it has everything; an empty document says nothing else. - pub ready: bool, + pub(crate) ready: bool, } /// An Automerge sync message in Automerge's own encoding. Decoded only where it is /// applied (`decode`), after the sender proved to be a member and off the consumer: a /// large message from anyone else costs nothing but its bytes. #[derive(Clone)] -pub struct SyncPayload(pub bytes::Bytes); +pub(crate) struct SyncPayload(pub(crate) bytes::Bytes); impl SyncPayload { - pub fn encode(message: Message) -> Self { + pub(crate) fn encode(message: Message) -> Self { Self(message.encode().into()) } - pub fn decode(&self) -> Result<Message> { + pub(crate) fn decode(&self) -> Result<Message> { Message::decode(&self.0) .map_err(|error| anyhow::anyhow!("Automerge decode failed: {error:?}")) } @@ -359,23 +359,23 @@ impl std::fmt::Debug for SyncPayload { /// A message serialized for the wire. Cloning is cheap (shared buffer). #[derive(Clone, Debug)] -pub struct EncodedMessage { - pub peer: String, - pub body: bytes::Bytes, - pub kind: MessageKind, +pub(crate) struct EncodedMessage { + pub(crate) peer: String, + pub(crate) body: bytes::Bytes, + pub(crate) kind: MessageKind, /// Bytes the message takes on the wire (without the transport's own framing). - pub payload_len: usize, + pub(crate) payload_len: usize, } /// Called with the sender, the message and its `payload_len` (as in `EncodedMessage`). -pub type HandlerFn = dyn Fn(String, ParsedMessage, usize) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> +pub(crate) type HandlerFn = dyn Fn(String, ParsedMessage, usize) -> Pin<Box<dyn Future<Output = Result<()>> + Send>> + Send + Sync + 'static; -pub type MessageHandler = Box<HandlerFn>; +pub(crate) type MessageHandler = Box<HandlerFn>; #[async_trait] -pub trait Transport: Send + Sync { +pub(crate) trait Transport: Send + Sync { /// This node's identity on the transport (for Mycelium: its public key). fn local_id(&self) -> &str; |
