Skip to content

Commit 64788fc

Browse files
authored
fix(l1-watcher): substraction underflow when initializing current_block (#333)
* fix(l1-watcher): underflow when initializing current_block
1 parent 3a67f58 commit 64788fc

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

crates/watcher/src/lib.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ where
183183
let watcher = Self {
184184
execution_provider,
185185
unfinalized_blocks: BoundedVec::new(HEADER_CAPACITY),
186-
current_block_number: start_block.unwrap_or(config.start_l1_block) - 1,
186+
current_block_number: start_block.unwrap_or(config.start_l1_block).saturating_sub(1),
187187
l1_state,
188188
sender: tx,
189189
config,
@@ -344,7 +344,7 @@ where
344344
.iter()
345345
.zip(chain.iter())
346346
.find(|(old, new)| old.hash != new.hash)
347-
.map(|(old, _)| old.number - 1);
347+
.map(|(old, _)| old.number.saturating_sub(1));
348348

349349
// set the unfinalized chain.
350350
self.unfinalized_blocks = chain;
@@ -564,12 +564,14 @@ where
564564
break (pos, chain);
565565
}
566566

567-
tracing::trace!(target: "scroll::watcher", number = ?(current_block.number - 1), "fetching block");
567+
tracing::trace!(target: "scroll::watcher", number = ?(current_block.number.saturating_sub(1)), "fetching block");
568568
let block = self
569569
.execution_provider
570-
.get_block((current_block.number - 1).into())
570+
.get_block((current_block.number.saturating_sub(1)).into())
571571
.await?
572-
.ok_or(EthRequestError::MissingBlock(current_block.number - 1))?;
572+
.ok_or_else(|| {
573+
EthRequestError::MissingBlock(current_block.number.saturating_sub(1))
574+
})?;
573575
chain.push(block.header.clone());
574576
current_block = block.header;
575577
};
@@ -653,7 +655,7 @@ where
653655

654656
// skip a block for `from_block` since `self.current_block_number` is the last indexed
655657
// block.
656-
filter = filter.from_block(self.current_block_number + 1).to_block(to_block);
658+
filter = filter.from_block(self.current_block_number.saturating_add(1)).to_block(to_block);
657659

658660
tracing::trace!(target: "scroll::watcher", ?filter, "fetching logs");
659661

0 commit comments

Comments
 (0)