Skip to content

Commit f62de3b

Browse files
committed
refactor indexer
1 parent 67f8477 commit f62de3b

File tree

2 files changed

+48
-34
lines changed

2 files changed

+48
-34
lines changed

crates/indexer/src/action.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use super::{IndexerError, IndexerEvent};
2+
use std::{
3+
fmt,
4+
future::Future,
5+
pin::Pin,
6+
task::{Context, Poll},
7+
};
8+
9+
/// A future that resolves to a tuple of the block info and the block import outcome.
10+
type PendingIndexerFuture =
11+
Pin<Box<dyn Future<Output = Result<IndexerEvent, IndexerError>> + Send>>;
12+
13+
pub(super) enum IndexerAction {
14+
HandleReorg(PendingIndexerFuture),
15+
HandleBatchCommit(PendingIndexerFuture),
16+
HandleBatchFinalization(PendingIndexerFuture),
17+
HandleL1Message(PendingIndexerFuture),
18+
}
19+
20+
impl IndexerAction {
21+
/// Polls the future to completion.
22+
pub(super) fn poll(
23+
&mut self,
24+
cx: &mut Context<'_>,
25+
) -> Poll<Result<IndexerEvent, IndexerError>> {
26+
match self {
27+
Self::HandleReorg(fut) |
28+
Self::HandleBatchCommit(fut) |
29+
Self::HandleBatchFinalization(fut) |
30+
Self::HandleL1Message(fut) => fut.as_mut().poll(cx),
31+
}
32+
}
33+
}
34+
35+
impl fmt::Debug for IndexerAction {
36+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37+
match self {
38+
Self::HandleReorg(_) => write!(f, "HandleReorg"),
39+
Self::HandleBatchCommit(_) => write!(f, "HandleBatchCommit"),
40+
Self::HandleBatchFinalization(_) => write!(f, "HandleBatchFinalization"),
41+
Self::HandleL1Message(_) => write!(f, "HandleL1Message"),
42+
}
43+
}
44+
}

crates/indexer/src/lib.rs

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,29 @@ use rollup_node_watcher::L1Notification;
66
use scroll_db::{Database, DatabaseOperations};
77
use std::{
88
collections::VecDeque,
9-
fmt,
10-
future::Future,
119
pin::Pin,
1210
sync::Arc,
1311
task::{Context, Poll},
1412
};
1513

14+
mod action;
15+
use action::IndexerAction;
16+
1617
mod event;
1718
pub use event::IndexerEvent;
1819

1920
mod error;
2021
use error::IndexerError;
2122

22-
/// A future that resolves to a tuple of the block info and the block import outcome.
23-
type PendingIndexerFuture =
24-
Pin<Box<dyn Future<Output = Result<IndexerEvent, IndexerError>> + Send>>;
25-
26-
enum IndexerAction {
27-
HandleReorg(PendingIndexerFuture),
28-
HandleBatchCommit(PendingIndexerFuture),
29-
HandleBatchFinalization(PendingIndexerFuture),
30-
HandleL1Message(PendingIndexerFuture),
31-
}
32-
3323
/// The indexer is responsible for indexing data relevant to the L1.
24+
#[derive(Debug)]
3425
pub struct Indexer {
3526
/// A reference to the database used to persist the indexed data.
3627
database: Arc<Database>,
3728
/// A queue of pending futures.
3829
pending_futures: VecDeque<IndexerAction>,
3930
}
4031

41-
impl IndexerAction {
42-
fn poll(&mut self, cx: &mut Context<'_>) -> Poll<Result<IndexerEvent, IndexerError>> {
43-
match self {
44-
Self::HandleReorg(fut) |
45-
Self::HandleBatchCommit(fut) |
46-
Self::HandleBatchFinalization(fut) |
47-
Self::HandleL1Message(fut) => fut.as_mut().poll(cx),
48-
}
49-
}
50-
}
51-
5232
impl Indexer {
5333
/// Creates a new indexer with the given [`Database`].
5434
pub fn new(database: Arc<Database>) -> Self {
@@ -149,16 +129,6 @@ impl Stream for Indexer {
149129
}
150130
}
151131

152-
impl fmt::Debug for Indexer {
153-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
154-
f.debug_struct("Indexer")
155-
.field("database", &"Arc<Database>") // Hide actual DB details
156-
.field("cmd_rx", &"mpsc::UnboundedReceiver<IndexerCommand>") // Hide channel details
157-
.field("pending_futures_len", &self.pending_futures.len()) // Only print the queue length
158-
.finish()
159-
}
160-
}
161-
162132
#[cfg(test)]
163133
mod test {
164134
use super::*;

0 commit comments

Comments
 (0)