Skip to content

Commit 01b5d0f

Browse files
authored
Merge pull request #3421 from ProvableHQ/process_mempool_periodically
Periodically process the unconfirmed transmissions in the memory pool
2 parents aab8a83 + 58c33ec commit 01b5d0f

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

node/consensus/src/lib.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ extern crate tracing;
2121
use snarkos_account::Account;
2222
use snarkos_node_bft::{
2323
BFT,
24+
MAX_BATCH_DELAY_IN_MS,
2425
Primary,
2526
helpers::{
2627
ConsensusReceiver,
@@ -49,7 +50,7 @@ use colored::Colorize;
4950
use indexmap::IndexMap;
5051
use lru::LruCache;
5152
use parking_lot::Mutex;
52-
use std::{future::Future, net::SocketAddr, num::NonZeroUsize, sync::Arc};
53+
use std::{future::Future, net::SocketAddr, num::NonZeroUsize, sync::Arc, time::Duration};
5354
use tokio::{
5455
sync::{OnceCell, oneshot},
5556
task::JoinHandle,
@@ -296,7 +297,7 @@ impl<N: Network> Consensus<N> {
296297
.lock()
297298
.insert(TransmissionID::Solution(solution.id(), checksum), timestamp);
298299
}
299-
// Process the unconfirmed solution.
300+
// Queue the unconfirmed solution.
300301
{
301302
let solution_id = solution.id();
302303

@@ -316,6 +317,12 @@ impl<N: Network> Consensus<N> {
316317
}
317318
}
318319

320+
// Try to process the unconfirmed solutions in the memory pool.
321+
self.process_unconfirmed_solutions().await
322+
}
323+
324+
/// Processes unconfirmed transactions in the memory pool.
325+
pub async fn process_unconfirmed_solutions(&self) -> Result<()> {
319326
// If the memory pool of this node is full, return early.
320327
let num_unconfirmed_solutions = self.num_unconfirmed_solutions();
321328
let num_unconfirmed_transmissions = self.num_unconfirmed_transmissions();
@@ -365,7 +372,7 @@ impl<N: Network> Consensus<N> {
365372
.lock()
366373
.insert(TransmissionID::Transaction(transaction.id(), checksum), timestamp);
367374
}
368-
// Process the unconfirmed transaction.
375+
// Queue the unconfirmed transaction.
369376
{
370377
let transaction_id = transaction.id();
371378

@@ -391,8 +398,14 @@ impl<N: Network> Consensus<N> {
391398
} else if self.transactions_queue.lock().executions.put(transaction_id, transaction).is_some() {
392399
bail!("Transaction '{}' exists in the memory pool", fmt_id(transaction_id));
393400
}
401+
402+
// Try to process the unconfirmed transactions in the memory pool.
403+
self.process_unconfirmed_transactions().await
394404
}
405+
}
395406

407+
/// Processes unconfirmed transactions in the memory pool.
408+
pub async fn process_unconfirmed_transactions(&self) -> Result<()> {
396409
// If the memory pool of this node is full, return early.
397410
let num_unconfirmed_transmissions = self.num_unconfirmed_transmissions();
398411
if num_unconfirmed_transmissions >= Primary::<N>::MAX_TRANSMISSIONS_TOLERANCE {
@@ -455,6 +468,23 @@ impl<N: Network> Consensus<N> {
455468
self_.process_bft_subdag(committed_subdag, transmissions, callback).await;
456469
}
457470
});
471+
472+
// Process the unconfirmed transactions in the memory pool.
473+
let self_ = self.clone();
474+
self.spawn(async move {
475+
loop {
476+
// Sleep briefly.
477+
tokio::time::sleep(Duration::from_millis(MAX_BATCH_DELAY_IN_MS)).await;
478+
// Process the unconfirmed transactions in the memory pool.
479+
if let Err(e) = self_.process_unconfirmed_transactions().await {
480+
warn!("Cannot process unconfirmed transactions - {e}");
481+
}
482+
// Process the unconfirmed solutions in the memory pool.
483+
if let Err(e) = self_.process_unconfirmed_solutions().await {
484+
warn!("Cannot process unconfirmed solutions - {e}");
485+
}
486+
}
487+
});
458488
}
459489

460490
/// Processes the committed subdag and transmissions from the BFT.

0 commit comments

Comments
 (0)