Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update gather block #44

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 24 additions & 32 deletions crates/katana/core/src/service/messaging/starknet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,46 +204,38 @@ impl<EF: katana_executor::ExecutorFactory + Send + Sync> Messenger for StarknetM
let chain_latest_block: u64 = match self.provider.block_number().await {
Ok(n) => n,
Err(_) => {
warn!(
target: LOG_TARGET,
"Couldn't fetch settlement chain last block number. \nSkipped, retry at the \
next tick."
);
warn!(target: LOG_TARGET, "Couldn't fetch settlement chain last block number");
return Err(Error::SendError);
}
};

if from_block > chain_latest_block {
// Nothing to fetch, we can skip waiting the next tick.
return Ok((chain_latest_block, vec![]));
}

// +1 as the from_block counts as 1 block fetched.
let to_block = if from_block + max_blocks + 1 < chain_latest_block {
from_block + max_blocks
} else {
chain_latest_block
};


// Instead of skipping blocks, process them sequentially
let to_block = std::cmp::min(from_block + max_blocks, chain_latest_block);

let mut l1_handler_txs: Vec<L1HandlerTx> = vec![];

self.fetch_events(BlockId::Number(from_block), BlockId::Number(to_block))
.await
.map_err(|_| Error::SendError)
.unwrap()
.iter()
.for_each(|e| {
debug!(
target: LOG_TARGET,
event = ?e,
"Converting event into L1HandlerTx."
);

if let Ok(tx) = l1_handler_tx_from_event(e, chain_id) {
l1_handler_txs.push(tx)

// Process each block individually to ensure none are missed
for block_num in from_block..=to_block {
match self.fetch_events(BlockId::Number(block_num), BlockId::Number(block_num)).await {
Ok(events) => {
events.iter().for_each(|e| {
if let Ok(tx) = l1_handler_tx_from_event(e, chain_id) {
l1_handler_txs.push(tx)
}
});
}
});

Err(e) => {
warn!(target: LOG_TARGET, "Error fetching block {}: {}", block_num, e);
// Return the last successfully processed block
return Ok((block_num - 1, l1_handler_txs));
}
}
}

Ok((to_block, l1_handler_txs))
}

Expand Down
Loading