Skip to content

Commit fc6d0c8

Browse files
committed
add end block
1 parent dbc7d3e commit fc6d0c8

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

src/scanner.rs

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,24 @@ impl Scanner {
8181
}
8282

8383
pub async fn start(&mut self) -> anyhow::Result<()> {
84-
match self.start_block {
85-
Some(start) => {
86-
self.process_historical_blocks(start).await?;
87-
// TODO: Ensure transition to live indexing is properly handled
84+
match (self.start_block, self.end_block) {
85+
(None, Some(end)) => {
86+
info!("Scanning from genesis block 0 to {}", end);
87+
self.process_historical_blocks(0, Some(end)).await?
88+
}
89+
(Some(start), Some(end)) => {
90+
info!("Scanning from block {} to {}", start, end);
91+
self.process_historical_blocks(start, Some(end)).await?
92+
}
93+
(Some(start), None) => {
94+
info!("Scanning from block {} to latest, then switching to live mode", start);
95+
self.process_historical_blocks(start, None).await?;
96+
self.subscribe_to_new_blocks().await?
97+
}
98+
(None, None) => {
99+
info!("Starting in live mode only");
88100
self.subscribe_to_new_blocks().await?
89101
}
90-
None => self.subscribe_to_new_blocks().await?,
91102
}
92103
Ok(())
93104
}
@@ -100,24 +111,31 @@ impl Scanner {
100111
self.tracked_events.push(filter);
101112
}
102113

103-
async fn process_historical_blocks(&mut self, start_block: u64) -> anyhow::Result<()> {
104-
info!(start_block, "starting historical block processing");
114+
async fn process_historical_blocks(
115+
&mut self,
116+
start_block: u64,
117+
end_block: Option<u64>,
118+
) -> anyhow::Result<()> {
119+
info!(start_block, end_block = ?end_block, "starting historical block processing");
105120

106121
let mut current = start_block;
107122
let max_blocks = self.max_blocks_per_filter;
108123

109-
loop {
110-
let chain_head = self.provider.get_block_number().await?;
124+
let target_end = match end_block {
125+
Some(end) => end,
126+
None => self.provider.get_block_number().await?,
127+
};
111128

112-
if current > chain_head {
113-
self.current_head = Some(current);
114-
info!(last_block = current, "Last block processed");
129+
loop {
130+
if current > target_end {
131+
self.current_head = Some(target_end);
132+
info!(last_block = target_end, "Historical processing completed");
115133
break;
116134
}
117135

118-
let to_block = (current + max_blocks).min(chain_head);
136+
let to_block = (current + max_blocks - 1).min(target_end);
119137

120-
info!(from_block = current, to_block, chain_head, "processing historical block range");
138+
info!(from_block = current, to_block, target_end, "processing historical block range");
121139

122140
self.process_block_events(current, to_block).await?;
123141

0 commit comments

Comments
 (0)