Skip to content

Commit

Permalink
ref: async trait functions
Browse files Browse the repository at this point in the history
  • Loading branch information
zeapoz committed Oct 5, 2023
1 parent 707e35c commit e2ff37f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
async-trait = "0.1.73"
clap = { version = "4.4.0", features = ["string"] }
ethers = "1"
eyre = "0.6.8"
Expand Down
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ async fn main() -> Result<()> {
let fetcher = L1Fetcher::new(http_url)?;
let processor = TreeProcessor::new(&db_dir)?;
let (tx, rx) = mpsc::channel::<Vec<CommitBlockInfoV1>>(5);
processor.run(rx);

tokio::spawn(async move {
processor.run(rx).await;
});

fetcher.fetch(tx, Some(U64([*start_block])), None).await?;
}
Expand Down
6 changes: 4 additions & 2 deletions src/processor/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use tokio::{sync::mpsc, task::JoinHandle};
use async_trait::async_trait;
use tokio::sync::mpsc;

use crate::types::CommitBlockInfoV1;

pub mod tree;

#[async_trait]
pub trait Processor {
fn run(self, rx: mpsc::Receiver<Vec<CommitBlockInfoV1>>) -> JoinHandle<()>;
async fn run(self, rx: mpsc::Receiver<Vec<CommitBlockInfoV1>>);
}
5 changes: 3 additions & 2 deletions src/processor/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod tree_wrapper;

use std::path::Path;

use async_trait::async_trait;
use eyre::Result;
use tokio::{sync::mpsc, task::JoinHandle};

Expand Down Expand Up @@ -44,9 +45,9 @@ impl TreeProcessor<'static> {
}
}

#[async_trait]
impl Processor for TreeProcessor<'static> {
fn run(mut self, mut rx: mpsc::Receiver<Vec<CommitBlockInfoV1>>) -> JoinHandle<()> {
tokio::spawn(async move {
async fn run(mut self, mut rx: mpsc::Receiver<Vec<CommitBlockInfoV1>>) {
while let Some(blocks) = rx.recv().await {
for block in blocks {
// Check if we've already processed this block.
Expand Down

0 comments on commit e2ff37f

Please sign in to comment.