mod.rs
1 // Copyright (c) 2025-2026 ACDC Network 2 // This file is part of the alphaos library. 3 // 4 // Alpha Chain | Delta Chain Protocol 5 // International Monetary Graphite. 6 // 7 // Derived from Aleo (https://aleo.org) and ProvableHQ (https://provable.com). 8 // They built world-class ZK infrastructure. We installed the EASY button. 9 // Their cryptography: elegant. Our modifications: bureaucracy-compatible. 10 // Original brilliance: theirs. Robert's Rules: ours. Bugs: definitely ours. 11 // 12 // Original Aleo/ProvableHQ code subject to Apache 2.0 https://www.apache.org/licenses/LICENSE-2.0 13 // All modifications and new work: CC0 1.0 Universal Public Domain Dedication. 14 // No rights reserved. No permission required. No warranty. No refunds. 15 // 16 // https://creativecommons.org/publicdomain/zero/1.0/ 17 // SPDX-License-Identifier: CC0-1.0 18 19 //! Opt-in protocols available to the node; each protocol is expected to spawn its own task that runs throughout the 20 //! node's lifetime and handles a specific functionality. The communication with these tasks is done via dedicated 21 //! handler objects. 22 23 use std::{io, net::SocketAddr}; 24 25 use once_cell::race::OnceBox; 26 use tokio::sync::{mpsc, oneshot}; 27 28 use crate::connections::Connection; 29 30 mod disconnect; 31 mod handshake; 32 mod on_connect; 33 mod reading; 34 mod writing; 35 36 pub use disconnect::Disconnect; 37 pub use handshake::Handshake; 38 pub use on_connect::OnConnect; 39 pub use reading::Reading; 40 pub use writing::Writing; 41 42 #[derive(Default)] 43 pub(crate) struct Protocols { 44 pub(crate) handshake: OnceBox<ProtocolHandler<Connection, io::Result<Connection>>>, 45 pub(crate) reading: OnceBox<ProtocolHandler<Connection, io::Result<Connection>>>, 46 pub(crate) writing: OnceBox<writing::WritingHandler>, 47 pub(crate) on_connect: OnceBox<ProtocolHandler<SocketAddr, ()>>, 48 pub(crate) disconnect: OnceBox<ProtocolHandler<SocketAddr, ()>>, 49 } 50 51 /// An object sent to a protocol handler task; the task assumes control of a protocol-relevant item `T`, 52 /// and when it's done with it, it returns it (possibly in a wrapper object) or another relevant object 53 /// to the callsite via the counterpart [`oneshot::Receiver`]. 54 pub(crate) type ReturnableItem<T, U> = (T, oneshot::Sender<U>); 55 56 pub(crate) type ReturnableConnection = ReturnableItem<Connection, io::Result<Connection>>; 57 58 pub(crate) struct ProtocolHandler<T, U>(mpsc::UnboundedSender<ReturnableItem<T, U>>); 59 60 pub(crate) trait Protocol<T, U> { 61 fn trigger(&self, item: ReturnableItem<T, U>); 62 } 63 64 impl<T, U> Protocol<T, U> for ProtocolHandler<T, U> { 65 fn trigger(&self, item: ReturnableItem<T, U>) { 66 // ignore errors; they can only happen if a disconnect interrupts the protocol setup process 67 let _ = self.0.send(item); 68 } 69 }