wallet: stop consolidation from starving zsweep

RunSaplingSweep returned early, and retried the next block, whenever a
consolidation round was running or due within 5 blocks, but kept no memory of
having given way. With consolidationinterval=5 the "due within 5 blocks" test is
always true, and with longer intervals a long consolidation round could fill the
remaining blocks, so on a busy wallet the sweep could go a whole run without
dispatching while deposits piled up.

Measured on the 2026-11-29 stress fleet (exchange-style wallet, zsweepinterval=5,
consolidationinterval=5, 150 deposit notes): in 20 minutes consolidation ran
4 rounds / 11 txs and the sweep 0, with 45 "not sweeping since consolidation"
lines; the deposit notes never moved.

A due sweep now gives way once and records it in fSweepWaiting. While that is
set, RunSaplingConsolidation starts no new round (nextConsolidation is left
alone, so that round runs right after the sweep), and the sweep skips the
5-block blackout. The flag is cleared when the sweep is dispatched, disabled, or
skipped for a locked wallet, so it cannot hold consolidation off once there is
no sweep to wait for. z_sweepstatus reports it as waiting_for_consolidation.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-11-29 10:48:26 -06:00
co-authored by Claude Opus 5.5
parent 4538a8cd76
commit 3e75769a48
4 changed files with 32 additions and 6 deletions
+21 -4
View File
@@ -640,6 +640,7 @@ void CWallet::RunSaplingSweep(int blockHeight) {
}
if (!fSweepEnabled) {
fSweepWaiting = false;
return;
}
@@ -666,18 +667,26 @@ void CWallet::RunSaplingSweep(int blockHeight) {
LogPrintf("%s: wallet locked; not sweeping at height=%d. Unlock it with walletpassphrase "
"for zsweep to resume\n", __func__, blockHeight);
nextSweep = sweepInterval + blockHeight;
fSweepWaiting = false;
return;
}
//Don't Run if consolidation will run soon.
if (fSaplingConsolidationEnabled && nextConsolidation - 5 <= blockHeight) {
LogPrintf("%s: not sweeping since next consolidation is within 5 blocks, nextConsolidation=%d , blockHeight=%d\n", __func__, nextConsolidation, blockHeight);
// Yield to consolidation once, then take priority. Both checks used to return without any memory of
// having done so: with a consolidation interval of 5 the "within 5 blocks" test is always true, and
// otherwise a long consolidation round filled the remaining blocks, so the sweep could go a whole run
// without dispatching while deposits piled up. fSweepWaiting makes RunSaplingConsolidation hold off its
// next round, and lets the sweep past the blackout below on a later block.
//Don't Run if consolidation will run soon, unless this sweep has already waited for consolidation.
if (!fSweepWaiting && fSaplingConsolidationEnabled && nextConsolidation - 5 <= blockHeight) {
LogPrintf("%s: not sweeping since next consolidation is within 5 blocks, nextConsolidation=%d , blockHeight=%d; the sweep goes first next time\n", __func__, nextConsolidation, blockHeight);
fSweepWaiting = true;
return;
}
//Don't Run While consolidation is running.
if (fConsolidationRunning) {
LogPrintf("%s: not sweeping since consolidation is currently running at height=%d\n", __func__, blockHeight);
LogPrintf("%s: not sweeping since consolidation is currently running at height=%d; no new consolidation round starts until this sweep has run\n", __func__, blockHeight);
fSweepWaiting = true;
return;
}
@@ -688,6 +697,7 @@ void CWallet::RunSaplingSweep(int blockHeight) {
}
fSweepRunning = true;
fSweepWaiting = false;
std::shared_ptr<AsyncRPCQueue> q = getAsyncRPCQueue();
std::shared_ptr<AsyncRPCOperation> lastOperation = q->getOperationForId(saplingSweepOperationId);
@@ -749,6 +759,13 @@ void CWallet::RunSaplingConsolidation(int blockHeight) {
return;
}
// A due sweep already gave way to this op once (see fSweepWaiting): let it run before the next round.
// nextConsolidation is left as it is, so this round starts as soon as that sweep finishes.
if (fSweepWaiting && fSweepEnabled) {
LogPrintf("%s: not consolidating since a due sweep is waiting at height=%d\n", __func__, blockHeight);
return;
}
if (fAutoShieldRunning) {
LogPrintf("%s: not consolidating since autoshield is currently running at height=%d\n", __func__, blockHeight);
return;