-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add basic da module and traits * Pipe new blobs and internal message handling * Add and pipe send attestation method * Add blob trait * Make da backend async * Implement mocka backend * Bound blob in da backend to blob trait * Added remove blob * Rename reply to attestation
- Loading branch information
1 parent
a79d6c5
commit 96e3c2d
Showing
7 changed files
with
133 additions
and
14 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use bytes::Bytes; | ||
use std::hash::Hash; | ||
|
||
pub type BlobHasher<T> = fn(&T) -> <T as Blob>::Hash; | ||
|
||
pub trait Blob { | ||
const HASHER: BlobHasher<Self>; | ||
type Hash: Hash + Eq + Clone; | ||
fn hash(&self) -> Self::Hash { | ||
Self::HASHER(self) | ||
} | ||
fn as_bytes(&self) -> Bytes; | ||
} |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod account; | ||
pub mod blob; | ||
pub mod block; | ||
pub mod crypto; | ||
pub mod fountain; | ||
|
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
73 changes: 73 additions & 0 deletions
73
nomos-services/data-availability/src/backend/memory_cache.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,73 @@ | ||
use crate::backend::{DaBackend, DaError}; | ||
use moka::future::{Cache, CacheBuilder}; | ||
use nomos_core::blob::Blob; | ||
use std::time::Duration; | ||
|
||
#[derive(Clone, Copy)] | ||
pub struct BlobCacheSettings { | ||
max_capacity: usize, | ||
evicting_period: Duration, | ||
} | ||
|
||
pub struct BlobCache<H, B>(Cache<H, B>); | ||
|
||
impl<B> BlobCache<B::Hash, B> | ||
where | ||
B: Clone + Blob + Send + Sync + 'static, | ||
B::Hash: Send + Sync + 'static, | ||
{ | ||
pub fn new(settings: BlobCacheSettings) -> Self { | ||
let BlobCacheSettings { | ||
max_capacity, | ||
evicting_period, | ||
} = settings; | ||
let cache = CacheBuilder::new(max_capacity as u64) | ||
.time_to_live(evicting_period) | ||
// can we leverage this to evict really old blobs? | ||
.time_to_idle(evicting_period) | ||
.build(); | ||
Self(cache) | ||
} | ||
|
||
pub async fn add(&self, blob: B) { | ||
self.0.insert(blob.hash(), blob).await | ||
} | ||
|
||
pub async fn remove(&self, hash: &B::Hash) { | ||
self.0.remove(hash).await; | ||
} | ||
|
||
pub fn pending_blobs(&self) -> Box<dyn Iterator<Item = B> + Send> { | ||
// bypass lifetime | ||
let blobs: Vec<_> = self.0.iter().map(|t| t.1).collect(); | ||
Box::new(blobs.into_iter()) | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl<B> DaBackend for BlobCache<B::Hash, B> | ||
where | ||
B: Clone + Blob + Send + Sync + 'static, | ||
B::Hash: Send + Sync + 'static, | ||
{ | ||
type Settings = BlobCacheSettings; | ||
type Blob = B; | ||
|
||
fn new(settings: Self::Settings) -> Self { | ||
BlobCache::new(settings) | ||
} | ||
|
||
async fn add_blob(&self, blob: Self::Blob) -> Result<(), DaError> { | ||
self.add(blob).await; | ||
Ok(()) | ||
} | ||
|
||
async fn remove_blob(&self, blob: &<Self::Blob as Blob>::Hash) -> Result<(), DaError> { | ||
self.remove(blob).await; | ||
Ok(()) | ||
} | ||
|
||
fn pending_blobs(&self) -> Box<dyn Iterator<Item = Self::Blob> + Send> { | ||
BlobCache::pending_blobs(self) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,18 +1,24 @@ | ||
mod memory_cache; | ||
|
||
use nomos_core::blob::Blob; | ||
use overwatch_rs::DynError; | ||
|
||
#[derive(Debug)] | ||
pub enum DaError { | ||
Dyn(DynError), | ||
} | ||
|
||
#[async_trait::async_trait] | ||
pub trait DaBackend { | ||
type Settings: Clone; | ||
|
||
type Blob; | ||
type Blob: Blob; | ||
|
||
fn new(settings: Self::Settings) -> Self; | ||
|
||
fn add_blob(&mut self, blob: Self::Blob) -> Result<(), DaError>; | ||
async fn add_blob(&self, blob: Self::Blob) -> Result<(), DaError>; | ||
|
||
async fn remove_blob(&self, blob: &<Self::Blob as Blob>::Hash) -> Result<(), DaError>; | ||
|
||
fn pending_blobs(&self) -> Box<dyn Iterator<Item = Self::Blob> + Send>; | ||
} |
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
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 |
---|---|---|
@@ -1,25 +1,25 @@ | ||
// std | ||
// crates | ||
use futures::Stream; | ||
use overwatch_rs::DynError; | ||
// internal | ||
use nomos_network::backends::NetworkBackend; | ||
use nomos_network::NetworkService; | ||
use overwatch_rs::services::relay::OutboundRelay; | ||
use overwatch_rs::services::ServiceData; | ||
use overwatch_rs::DynError; | ||
|
||
#[async_trait::async_trait] | ||
pub trait NetworkAdapter { | ||
type Backend: NetworkBackend + 'static; | ||
|
||
type Blob: Send + Sync + 'static; | ||
type Reply: Send + Sync + 'static; | ||
type Attestation: Send + Sync + 'static; | ||
|
||
async fn new( | ||
network_relay: OutboundRelay<<NetworkService<Self::Backend> as ServiceData>::Message>, | ||
) -> Self; | ||
|
||
async fn blob_stream(&self) -> Box<dyn Stream<Item = Self::Blob> + Unpin + Send>; | ||
|
||
async fn send_attestation(&self, attestation: Self::Reply) -> Result<(), DynError>; | ||
async fn send_attestation(&self, attestation: Self::Attestation) -> Result<(), DynError>; | ||
} |