-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Da service network #384
Merged
Merged
Da service network #384
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f4d97ed
Make da backend async
danielSanchezQ 2301f2f
Added remove blob
danielSanchezQ 16a47f7
Added send_blob method to network adapter trait
danielSanchezQ b0d5caf
Added libp2p backend
danielSanchezQ 233318b
Implement attestation stream
danielSanchezQ 11186f2
Implement send methods
danielSanchezQ File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
nomos-services/data-availability/src/network/adapters/libp2p.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// std | ||
use futures::Stream; | ||
use overwatch_rs::DynError; | ||
use std::marker::PhantomData; | ||
// crates | ||
|
||
// internal | ||
use crate::network::NetworkAdapter; | ||
use nomos_core::wire; | ||
use nomos_network::backends::libp2p::{Command, Event, EventKind, Libp2p, Message, TopicHash}; | ||
use nomos_network::{NetworkMsg, NetworkService}; | ||
use overwatch_rs::services::relay::OutboundRelay; | ||
use overwatch_rs::services::ServiceData; | ||
use serde::de::DeserializeOwned; | ||
use serde::Serialize; | ||
use tokio_stream::wrappers::BroadcastStream; | ||
use tokio_stream::StreamExt; | ||
use tracing::log::error; | ||
|
||
pub const NOMOS_DA_TOPIC: &str = "NomosDa"; | ||
|
||
pub struct Libp2pAdapter<B, A> { | ||
network_relay: OutboundRelay<<NetworkService<Libp2p> as ServiceData>::Message>, | ||
_blob: PhantomData<B>, | ||
_attestation: PhantomData<A>, | ||
} | ||
|
||
impl<B, A> Libp2pAdapter<B, A> | ||
where | ||
B: Serialize + DeserializeOwned + Send + Sync + 'static, | ||
A: Serialize + DeserializeOwned + Send + Sync + 'static, | ||
{ | ||
async fn stream_for<E: DeserializeOwned>(&self) -> Box<dyn Stream<Item = E> + Unpin + Send> { | ||
let topic_hash = TopicHash::from_raw(NOMOS_DA_TOPIC); | ||
let (sender, receiver) = tokio::sync::oneshot::channel(); | ||
self.network_relay | ||
.send(NetworkMsg::Subscribe { | ||
kind: EventKind::Message, | ||
sender, | ||
}) | ||
.await | ||
.expect("Network backend should be ready"); | ||
let receiver = receiver.await.unwrap(); | ||
Box::new(Box::pin(BroadcastStream::new(receiver).filter_map( | ||
move |msg| match msg { | ||
Ok(Event::Message(Message { topic, data, .. })) if topic == topic_hash => { | ||
match wire::deserialize::<E>(&data) { | ||
Ok(msg) => Some(msg), | ||
Err(e) => { | ||
error!("Unrecognized Blob message: {e}"); | ||
None | ||
} | ||
} | ||
} | ||
_ => None, | ||
}, | ||
))) | ||
} | ||
|
||
async fn send<E: Serialize>(&self, data: E) -> Result<(), DynError> { | ||
let message = wire::serialize(&data)?.into_boxed_slice(); | ||
self.network_relay | ||
.send(NetworkMsg::Process(Command::Broadcast { | ||
topic: NOMOS_DA_TOPIC.to_string(), | ||
message, | ||
})) | ||
.await | ||
.map_err(|(e, _)| Box::new(e) as DynError) | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl<B, A> NetworkAdapter for Libp2pAdapter<B, A> | ||
where | ||
B: Serialize + DeserializeOwned + Send + Sync + 'static, | ||
A: Serialize + DeserializeOwned + Send + Sync + 'static, | ||
{ | ||
type Backend = Libp2p; | ||
type Blob = B; | ||
type Attestation = A; | ||
|
||
async fn new( | ||
network_relay: OutboundRelay<<NetworkService<Self::Backend> as ServiceData>::Message>, | ||
) -> Self { | ||
network_relay | ||
.send(NetworkMsg::Process(Command::Subscribe( | ||
NOMOS_DA_TOPIC.to_string(), | ||
))) | ||
.await | ||
.expect("Network backend should be ready"); | ||
Self { | ||
network_relay, | ||
_blob: Default::default(), | ||
_attestation: Default::default(), | ||
} | ||
} | ||
|
||
async fn blob_stream(&self) -> Box<dyn Stream<Item = Self::Blob> + Unpin + Send> { | ||
self.stream_for::<Self::Blob>().await | ||
} | ||
|
||
async fn attestation_stream(&self) -> Box<dyn Stream<Item = Self::Attestation> + Unpin + Send> { | ||
self.stream_for::<Self::Attestation>().await | ||
} | ||
|
||
async fn send_attestation(&self, attestation: Self::Attestation) -> Result<(), DynError> { | ||
self.send(attestation).await | ||
} | ||
|
||
async fn send_blob(&self, blob: Self::Blob) -> Result<(), DynError> { | ||
self.send(blob).await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#[cfg(feature = "libp2p")] | ||
pub mod libp2p; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍