Skip to content

Commit 764537b

Browse files
committed
fix: query from_block on start instead of calculating it assuming a block time of 12 seconds
1 parent 51ce787 commit 764537b

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

crates/batcher/src/lib.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -504,21 +504,32 @@ impl Batcher {
504504
/// Runs at configurable intervals and checks recent blocks for events (2x the polling interval).
505505
/// When an event is detected, removes user's proofs from queue and resets UserState.
506506
pub async fn poll_balance_unlocked_events(self: Arc<Self>) -> Result<(), BatcherError> {
507-
let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(
507+
let mut interval = tokio::time::interval(Duration::from_secs(
508508
self.balance_unlock_polling_interval_seconds,
509509
));
510+
let mut from_block = self.get_current_block_number().await.map_err(|e| {
511+
BatcherError::EthereumProviderError(format!(
512+
"Failed to get current block number: {:?}",
513+
e
514+
))
515+
})?;
510516

511517
loop {
512518
interval.tick().await;
513519

514-
if let Err(e) = self.process_balance_unlocked_events().await {
515-
error!("Error processing BalanceUnlocked events: {:?}", e);
516-
// Continue polling even if there's an error
520+
match self.process_balance_unlocked_events(from_block).await {
521+
Ok(current_block) => {
522+
from_block = current_block;
523+
}
524+
Err(e) => {
525+
error!("Error processing BalanceUnlocked events: {:?}", e);
526+
// On error, keep from_block unchanged to retry the same range next time
527+
}
517528
}
518529
}
519530
}
520531

521-
async fn process_balance_unlocked_events(&self) -> Result<(), BatcherError> {
532+
async fn process_balance_unlocked_events(&self, from_block: U64) -> Result<U64, BatcherError> {
522533
// Get current block number using HTTP providers
523534
let current_block = self.get_current_block_number().await.map_err(|e| {
524535
BatcherError::EthereumProviderError(format!(
@@ -527,11 +538,6 @@ impl Batcher {
527538
))
528539
})?;
529540

530-
// Calculate the block range based on polling interval
531-
// Formula: interval / 12 * 2 (assuming 12-second block times, look back 2x the interval)
532-
let block_range = (self.balance_unlock_polling_interval_seconds / 12) * 2;
533-
let from_block = current_block.saturating_sub(U64::from(block_range));
534-
535541
// Query events with retry logic
536542
let events = self
537543
.query_balance_unlocked_events(from_block, current_block)
@@ -574,7 +580,7 @@ impl Batcher {
574580
}
575581
}
576582

577-
Ok(())
583+
Ok(current_block)
578584
}
579585

580586
/// Gets the current block number from Ethereum.

0 commit comments

Comments
 (0)