Skip to content

Commit aa8ee60

Browse files
committed
refactor reset of considered_txs cache
1 parent 2c4eb5a commit aa8ee60

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

stackslib/src/core/mempool.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,11 +1441,22 @@ impl MemPoolDB {
14411441

14421442
#[cfg_attr(test, mutants::skip)]
14431443
pub fn reset_mempool_caches(&mut self) -> Result<(), db_error> {
1444+
self.reset_nonce_cache()?;
1445+
self.reset_considered_txs_cache()?;
1446+
Ok(())
1447+
}
1448+
1449+
#[cfg_attr(test, mutants::skip)]
1450+
pub fn reset_considered_txs_cache(&mut self) -> Result<(), db_error> {
1451+
debug!("reset considered txs cache");
1452+
self.db.execute("DELETE FROM considered_txs", NO_PARAMS)?;
1453+
Ok(())
1454+
}
1455+
1456+
#[cfg_attr(test, mutants::skip)]
1457+
pub fn reset_nonce_cache(&mut self) -> Result<(), db_error> {
14441458
debug!("reset nonce cache");
1445-
// Delete all rows from the nonces table
14461459
self.db.execute("DELETE FROM nonces", NO_PARAMS)?;
1447-
// Also delete all rows from the considered_txs table
1448-
self.db.execute("DELETE FROM considered_txs", NO_PARAMS)?;
14491460
Ok(())
14501461
}
14511462

testnet/stacks-node/src/nakamoto_node/miner.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,8 @@ pub struct BlockMinerThread {
227227
burn_tip_at_start: ConsensusHash,
228228
/// flag to indicate an abort driven from the relayer
229229
abort_flag: Arc<AtomicBool>,
230-
/// Should the nonce cache be reset before mining the next block?
231-
reset_nonce_cache: bool,
230+
/// Should the nonce and considered transactions cache be reset before mining the next block?
231+
reset_mempool_caches: bool,
232232
/// Storage for persisting non-confidential miner information
233233
miner_db: MinerDB,
234234
}
@@ -264,7 +264,7 @@ impl BlockMinerThread {
264264
abort_flag: Arc::new(AtomicBool::new(false)),
265265
tenure_cost: ExecutionCost::ZERO,
266266
tenure_budget: ExecutionCost::ZERO,
267-
reset_nonce_cache: true,
267+
reset_mempool_caches: true,
268268
miner_db: MinerDB::open_with_config(&rt.config)?,
269269
})
270270
}
@@ -528,13 +528,23 @@ impl BlockMinerThread {
528528
thread::sleep(Duration::from_millis(ABORT_TRY_AGAIN_MS));
529529
return Ok(());
530530
}
531-
532-
if self.reset_nonce_cache {
531+
if self.reset_mempool_caches
532+
|| self.config.miner.mempool_walk_strategy
533+
== MemPoolWalkStrategy::NextNonceWithHighestFeeRate
534+
{
533535
let mut mem_pool = self
534536
.config
535537
.connect_mempool_db()
536538
.expect("Database failure opening mempool");
537-
mem_pool.reset_mempool_caches()?;
539+
540+
if self.reset_mempool_caches {
541+
mem_pool.reset_mempool_caches()?;
542+
} else {
543+
// Even if the nonce cache is still valid, NextNonceWithHighestFeeRate strategy
544+
// needs to reset this cache after each block. This prevents skipping transactions
545+
// that were previously considered, but not included in previous blocks.
546+
mem_pool.reset_considered_txs_cache()?;
547+
}
538548
}
539549

540550
let Some(new_block) = self.mine_block_and_handle_result(coordinator)? else {
@@ -619,13 +629,7 @@ impl BlockMinerThread {
619629
"Miner did not find any transactions to mine, sleeping for {:?}",
620630
self.config.miner.empty_mempool_sleep_time
621631
);
622-
// For NextNonceWithHighestFeeRate strategy, keep reset_nonce_cache = true
623-
// to ensure considered_txs table is cleared for each block attempt
624-
if self.config.miner.mempool_walk_strategy
625-
!= MemPoolWalkStrategy::NextNonceWithHighestFeeRate
626-
{
627-
self.reset_nonce_cache = false;
628-
}
632+
self.reset_mempool_caches = false;
629633

630634
// Pause the miner to wait for transactions to arrive
631635
let now = Instant::now();
@@ -740,13 +744,7 @@ impl BlockMinerThread {
740744
);
741745

742746
// We successfully mined, so the mempool caches are valid.
743-
// For NextNonceWithHighestFeeRate strategy, keep reset_nonce_cache = true
744-
// to ensure considered_txs table is cleared for each block attempt
745-
if self.config.miner.mempool_walk_strategy
746-
!= MemPoolWalkStrategy::NextNonceWithHighestFeeRate
747-
{
748-
self.reset_nonce_cache = false;
749-
}
747+
self.reset_mempool_caches = false;
750748
}
751749

752750
// update mined-block counters and mined-tenure counters
@@ -1456,7 +1454,7 @@ impl BlockMinerThread {
14561454
// If we attempt to build a block, we should reset the nonce cache.
14571455
// In the special case where no transactions are found, this flag will
14581456
// be reset to false.
1459-
self.reset_nonce_cache = true;
1457+
self.reset_mempool_caches = true;
14601458

14611459
let replay_transactions = if self.config.miner.replay_transactions {
14621460
coordinator

0 commit comments

Comments
 (0)