From e8ee2f84d79f38ea3a34a179a150ef681a838e0e Mon Sep 17 00:00:00 2001 From: Aleksey Proshutinskiy Date: Thu, 12 Sep 2024 23:46:58 +0300 Subject: [PATCH] fix(listener): fix batch request creation (#2367) --- crates/chain-listener/src/listener.rs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/crates/chain-listener/src/listener.rs b/crates/chain-listener/src/listener.rs index a25156976f..b067811b86 100644 --- a/crates/chain-listener/src/listener.rs +++ b/crates/chain-listener/src/listener.rs @@ -17,7 +17,7 @@ * along with this program. If not, see . */ -use alloy_primitives::{Address, FixedBytes, Uint, U256, U64}; +use alloy_primitives::{Address, FixedBytes, Uint, U256}; use alloy_sol_types::SolEvent; use backoff::Error::Permanent; use std::cmp::min; @@ -1131,6 +1131,11 @@ impl ChainListener { if let Some(ref ccp_client) = self.ccp_client { let batch_requests = self.get_batch_request(); + if batch_requests.is_empty() { + tracing::debug!(target: "chain-listener", "No compute units to poll proofs for. Probably all CUs have reached max proofs count"); + return Ok(()); + } + let proof_batches = if self.is_epoch_ending() { let last_known_proofs = batch_requests .into_iter() @@ -1419,17 +1424,19 @@ impl ChainListener { let mut batch_request = HashMap::new(); for cu_id in self.cc_compute_units.keys() { let sent_proofs_count = self.proof_tracker.get_proof_counter(cu_id); - let proofs_needed = U64::from( - self.max_proofs_per_epoch - .checked_add(-sent_proofs_count) - .unwrap_or(Uint::ZERO), - ) - .as_limbs()[0] as usize; + let proofs_needed = self + .max_proofs_per_epoch + .checked_sub(sent_proofs_count) + .unwrap_or(Uint::ZERO) + .as_limbs()[0]; if proofs_needed > 0 { let request = BatchRequest { last_seen_proof_idx: self.proof_tracker.get_last_submitted_proof_id(cu_id), - proof_batch_size: min(proofs_needed, self.listener_config.max_proof_batch_size), + proof_batch_size: min( + proofs_needed as usize, + self.listener_config.max_proof_batch_size, + ), }; batch_request.insert(*cu_id, request);