-
Notifications
You must be signed in to change notification settings - Fork 4
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
feat(storage) L1BlockManager
#552
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "strata-macros" | ||
version = "0.1.0" | ||
edition = "2024" | ||
|
||
[dependencies] | ||
proc-macro2.workspace = true | ||
quote.workspace = true | ||
syn.workspace = true | ||
|
||
[lib] | ||
proc-macro = true |
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,49 @@ | ||
use proc_macro::TokenStream; | ||
use quote::quote; | ||
use syn::{Attribute, ItemTrait, parse_macro_input}; | ||
|
||
#[proc_macro_attribute] | ||
pub fn gen_async_version(_attr: TokenStream, item: TokenStream) -> TokenStream { | ||
// Parse the input tokens into a syntax tree | ||
let input = parse_macro_input!(item as ItemTrait); | ||
|
||
// Extract the trait name | ||
let trait_name = &input.ident; | ||
let async_trait_name = syn::Ident::new(&format!("{}Async", trait_name), trait_name.span()); | ||
|
||
// Generate async methods | ||
let async_methods = input.items.iter().filter_map(|item| { | ||
if let syn::TraitItem::Fn(method) = item { | ||
let sig = &method.sig; | ||
let async_sig = syn::Signature { | ||
asyncness: Some(Default::default()), // Add async keyword | ||
..sig.clone() | ||
}; | ||
|
||
let docs: Vec<&Attribute> = method | ||
.attrs | ||
.iter() | ||
.filter(|attr| attr.path().is_ident("doc")) | ||
.collect(); | ||
|
||
Some(quote! { | ||
#(#docs)* | ||
#async_sig; | ||
}) | ||
} else { | ||
None | ||
} | ||
}); | ||
|
||
// Generate the new async trait | ||
let expanded = quote! { | ||
#input | ||
|
||
pub trait #async_trait_name { | ||
#(#async_methods)* | ||
} | ||
}; | ||
|
||
// Convert the expanded code into a TokenStream and return it | ||
TokenStream::from(expanded) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,124 @@ | ||
use std::sync::Arc; | ||
|
||
use strata_db::{ | ||
traits::{BlockStatus, Database, L1Database, L1DatabaseAsync}, | ||
DbResult, | ||
}; | ||
use strata_mmr::CompactMmr; | ||
use strata_primitives::{ | ||
buf::Buf32, | ||
l1::{L1BlockManifest, L1TxRef}, | ||
}; | ||
use strata_state::{block::L2BlockBundle, header::L2Header, id::L2BlockId, l1::L1Tx}; | ||
use threadpool::ThreadPool; | ||
use tokio::sync::oneshot::{self, error::RecvError}; | ||
use tracing::warn; | ||
|
||
use crate::cache::{self, CacheTable}; | ||
|
||
/// Caching manager of L1 blocks in the block database. | ||
pub struct L1BlockManager<DB> | ||
where | ||
DB: L1Database + Sync + Send + 'static, | ||
{ | ||
pool: ThreadPool, | ||
db: Arc<DB>, | ||
block_cache: CacheTable<L2BlockId, Option<L2BlockBundle>>, | ||
} | ||
|
||
impl<DB> L1BlockManager<DB> | ||
where | ||
DB: L1Database + Sync + Send + 'static, | ||
{ | ||
pub fn new(pool: ThreadPool, db: Arc<DB>) -> Self { | ||
Self { | ||
pool, | ||
db, | ||
block_cache: CacheTable::new(64.try_into().unwrap()), | ||
} | ||
} | ||
} | ||
|
||
impl<DB> L1DatabaseAsync for L1BlockManager<DB> | ||
where | ||
DB: L1Database + Sync + Send + 'static, | ||
{ | ||
async fn put_block_data(&self, idx: u64, mf: L1BlockManifest, txs: Vec<L1Tx>) -> DbResult<()> { | ||
let db = self.db.clone(); | ||
self.pool | ||
.spawn(move || db.put_block_data(idx, mf, txs)) | ||
.await | ||
} | ||
|
||
async fn put_mmr_checkpoint(&self, idx: u64, mmr: CompactMmr) -> DbResult<()> { | ||
let db = self.db.clone(); | ||
self.pool | ||
.spawn(move || db.put_mmr_checkpoint(idx, mmr)) | ||
.await | ||
} | ||
|
||
async fn revert_to_height(&self, idx: u64) -> DbResult<()> { | ||
let db = self.db.clone(); | ||
self.pool.spawn(move || db.revert_to_height(idx)).await | ||
} | ||
|
||
async fn get_chain_tip(&self) -> DbResult<Option<u64>> { | ||
let db = self.db.clone(); | ||
self.pool.spawn(move || db.get_chain_tip()).await | ||
} | ||
|
||
async fn get_block_manifest(&self, idx: u64) -> DbResult<Option<L1BlockManifest>> { | ||
let db = self.db.clone(); | ||
self.pool.spawn(move || db.get_block_manifest(idx)).await | ||
} | ||
|
||
async fn get_blockid_range(&self, start_idx: u64, end_idx: u64) -> DbResult<Vec<Buf32>> { | ||
let db = self.db.clone(); | ||
self.pool | ||
.spawn(move || db.get_blockid_range(start_idx, end_idx)) | ||
.await | ||
} | ||
|
||
async fn get_block_txs(&self, idx: u64) -> DbResult<Option<Vec<L1TxRef>>> { | ||
let db = self.db.clone(); | ||
self.pool.spawn(move || db.get_block_txs(idx)).await | ||
} | ||
|
||
async fn get_tx(&self, tx_ref: L1TxRef) -> DbResult<Option<L1Tx>> { | ||
let db = self.db.clone(); | ||
self.pool.spawn(move || db.get_tx(tx_ref)).await | ||
} | ||
|
||
async fn get_last_mmr_to(&self, idx: u64) -> DbResult<Option<CompactMmr>> { | ||
let db = self.db.clone(); | ||
self.pool.spawn(move || db.get_last_mmr_to(idx)).await | ||
} | ||
|
||
async fn get_txs_from(&self, start_idx: u64) -> DbResult<(Vec<L1Tx>, u64)> { | ||
let db = self.db.clone(); | ||
self.pool.spawn(move || db.get_txs_from(start_idx)).await | ||
} | ||
} | ||
|
||
trait ThreadPoolSpawn { | ||
async fn spawn<T, F>(&self, func: F) -> T | ||
where | ||
F: FnOnce() -> T + Send + 'static, | ||
T: Send + 'static; | ||
} | ||
|
||
impl ThreadPoolSpawn for ThreadPool { | ||
async fn spawn<T, F>(&self, func: F) -> T | ||
where | ||
F: FnOnce() -> T + Send + 'static, | ||
T: Send + 'static, | ||
{ | ||
let (tx, rx) = oneshot::channel(); | ||
self.execute(move || { | ||
if tx.send(func()).is_err() { | ||
warn!("failed to send response") | ||
} | ||
}); | ||
rx.await.expect("Sender was dropped without sending") | ||
} | ||
} |
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,2 +1,3 @@ | ||
pub mod checkpoint; | ||
pub mod l1; | ||
pub mod l2; |
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.
One of the major reasons to have managers was to be free from these Db generics. The
Ops
construct was to facilitate this.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.
What issues would arise from having this generic here if the compiler is going to infer it anyway?
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.
You have to pass it down through everything that wants to interact with the database / manager, which becomes cumbersome.