From 82b6504ef451edd0b466196c2c0b9f0f76ce48d6 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 11 Mar 2024 17:48:39 +0100 Subject: [PATCH] chore: use safe math when calculating missing senders (#7099) --- .../provider/src/providers/database/provider.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/crates/storage/provider/src/providers/database/provider.rs b/crates/storage/provider/src/providers/database/provider.rs index fa65b967e6..d4d158396b 100644 --- a/crates/storage/provider/src/providers/database/provider.rs +++ b/crates/storage/provider/src/providers/database/provider.rs @@ -56,7 +56,7 @@ use std::{ sync::{mpsc, Arc}, time::{Duration, Instant}, }; -use tracing::{debug, warn}; +use tracing::{debug, error, warn}; /// A [`DatabaseProvider`] that holds a read-only database transaction. pub type DatabaseProviderRO = DatabaseProvider<::TX>; @@ -542,10 +542,16 @@ impl DatabaseProvider { // NOTE: Transactions are always guaranteed to be in the database whereas // senders might be pruned. if senders.len() != transactions.len() { - senders.reserve(transactions.len() - senders.len()); + if senders.len() > transactions.len() { + error!(target: "providers::db", senders=%senders.len(), transactions=%transactions.len(), + first_tx=%first_transaction, last_tx=%last_transaction, + "unexpected senders and transactions mismatch"); + } + let missing = transactions.len().saturating_sub(senders.len()); + senders.reserve(missing); // Find all missing senders, their corresponding tx numbers and indexes to the original // `senders` vector at which the recovered senders will be inserted. - let mut missing_senders = Vec::with_capacity(transactions.len() - senders.len()); + let mut missing_senders = Vec::with_capacity(missing); { let mut senders = senders.iter().peekable();