-
Notifications
You must be signed in to change notification settings - Fork 15
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 backend #381
Da service backend #381
Changes from 8 commits
afa56d1
8221c66
df31926
db2ad3b
7995abb
ffd460a
44ce2fb
87d4d99
0de183c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
} | ||
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; | ||
|
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) | ||
Comment on lines
+26
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As discussed in https://github.com/logos-co/nomos-node/pull/376/files#r1317083701, the expectation is that the outer service retrieves pending blobs from this service, does something with those blobs, and after that, requests removing those blobs? If so, it seems it's okay to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. So the idea here is that. Either a blob is pending for the whole life until is added into a block. Or is removed. (that is why is using the same evicting period as the time to live). But it would make sense that if it is not included it dies as well after some time. |
||
.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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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) | ||
} | ||
} |
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>; | ||
} |
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.
This may be expanded later if needed when we know more about how would it looks.