diff --git a/nomos-core/Cargo.toml b/nomos-core/Cargo.toml index 70ff5784..a297134a 100644 --- a/nomos-core/Cargo.toml +++ b/nomos-core/Cargo.toml @@ -24,3 +24,4 @@ tokio = { version = "1.23", features = ["macros", "rt"] } [features] default = [] raptor = ["raptorq"] +mock = [] diff --git a/nomos-core/src/fountain/mock.rs b/nomos-core/src/fountain/mock.rs new file mode 100644 index 00000000..109013f7 --- /dev/null +++ b/nomos-core/src/fountain/mock.rs @@ -0,0 +1,38 @@ +// std +// crates +use async_trait::async_trait; +use bytes::Bytes; +use futures::{Stream, StreamExt}; +// internal +use crate::fountain::{FountainCode, FountainError}; + +/// Fountain code that does no protocol at all. +/// Just bypasses the raw bytes into a single chunk and reconstruct from it. +pub struct MockFountain; + +#[async_trait] +impl FountainCode for MockFountain { + type Settings = (); + + fn new(_: Self::Settings) -> Self { + Self + } + + fn encode(&self, block: &[u8]) -> Box + Send + Sync + Unpin> { + let data = block.to_vec(); + Box::new(futures::stream::once(Box::pin( + async move { Bytes::from(data) }, + ))) + } + + async fn decode( + &self, + mut stream: impl Stream + Send + Sync + Unpin, + ) -> Result { + if let Some(chunk) = stream.next().await { + Ok(chunk) + } else { + Err("Stream ended before decoding was complete".into()) + } + } +} diff --git a/nomos-core/src/fountain/mod.rs b/nomos-core/src/fountain/mod.rs index cbcbef03..c1720e9f 100644 --- a/nomos-core/src/fountain/mod.rs +++ b/nomos-core/src/fountain/mod.rs @@ -1,3 +1,5 @@ +#[cfg(feature = "mock")] +pub mod mock; #[cfg(feature = "raptor")] pub mod raptorq;