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
+4 -2
View File
@@ -128,8 +128,10 @@ wallet: `N addresses have more than 25 spendable notes; consider -consolidation=
1. With zsweep on, consolidation only works on the sweep address: a `consolidatesaplingaddress` other than the 1. With zsweep on, consolidation only works on the sweep address: a `consolidatesaplingaddress` other than the
sweep address is refused at startup (with `consolidation=0` it is replaced by the sweep address, with a warning), sweep address is refused at startup (with `consolidation=0` it is replaced by the sweep address, with a warning),
and `z_consolidate` refuses any other address too. Sweep and consolidation never run at the same time, and a and `z_consolidate` refuses any other address too. Sweep and consolidation never run at the same time. A due sweep
sweep is not started when a consolidation round is due within 5 blocks. gives way once, to a consolidation round that is running or due within 5 blocks; after that no new consolidation
round starts until the sweep has run, so neither can starve the other. `z_sweepstatus` shows
`"waiting_for_consolidation": true` while a sweep is waiting.
### Copyright ### Copyright
+3
View File
@@ -3536,6 +3536,8 @@ UniValue z_sweepstatus(const UniValue& params, bool fHelp, const CPubKey& mypk)
" \"running\" : true|false, (boolean) whether a sweep round is in flight\n" " \"running\" : true|false, (boolean) whether a sweep round is in flight\n"
" \"wallet_locked\" : true|false, (boolean) the wallet is encrypted and locked, so nothing can be swept\n" " \"wallet_locked\" : true|false, (boolean) the wallet is encrypted and locked, so nothing can be swept\n"
" until it is unlocked with walletpassphrase\n" " until it is unlocked with walletpassphrase\n"
" \"waiting_for_consolidation\" : true|false, (boolean) a due round is waiting for a consolidation round to end;\n"
" no new consolidation round starts until it has run\n"
" \"amount_swept\" : x.xxx, (numeric) DRGX sent to the sweep address since startup\n" " \"amount_swept\" : x.xxx, (numeric) DRGX sent to the sweep address since startup\n"
" \"next_zsweep\" : n, (numeric) height of the next round\n" " \"next_zsweep\" : n, (numeric) height of the next round\n"
" \"zsweepinterval\" : n, (numeric) blocks between rounds\n" " \"zsweepinterval\" : n, (numeric) blocks between rounds\n"
@@ -3556,6 +3558,7 @@ UniValue z_sweepstatus(const UniValue& params, bool fHelp, const CPubKey& mypk)
ret.push_back(Pair("zsweep", pwalletMain->fSweepEnabled)); ret.push_back(Pair("zsweep", pwalletMain->fSweepEnabled));
ret.push_back(Pair("running", pwalletMain->fSweepRunning)); ret.push_back(Pair("running", pwalletMain->fSweepRunning));
ret.push_back(Pair("wallet_locked", pwalletMain->IsLocked())); ret.push_back(Pair("wallet_locked", pwalletMain->IsLocked()));
ret.push_back(Pair("waiting_for_consolidation", pwalletMain->fSweepWaiting));
ret.push_back(Pair("amount_swept", ValueFromAmount(pwalletMain->amountSwept))); ret.push_back(Pair("amount_swept", ValueFromAmount(pwalletMain->amountSwept)));
ret.push_back(Pair("next_zsweep", pwalletMain->nextSweep)); ret.push_back(Pair("next_zsweep", pwalletMain->nextSweep));
ret.push_back(Pair("zsweepinterval", pwalletMain->sweepInterval)); ret.push_back(Pair("zsweepinterval", pwalletMain->sweepInterval));
+21 -4
View File
@@ -640,6 +640,7 @@ void CWallet::RunSaplingSweep(int blockHeight) {
} }
if (!fSweepEnabled) { if (!fSweepEnabled) {
fSweepWaiting = false;
return; return;
} }
@@ -666,18 +667,26 @@ void CWallet::RunSaplingSweep(int blockHeight) {
LogPrintf("%s: wallet locked; not sweeping at height=%d. Unlock it with walletpassphrase " LogPrintf("%s: wallet locked; not sweeping at height=%d. Unlock it with walletpassphrase "
"for zsweep to resume\n", __func__, blockHeight); "for zsweep to resume\n", __func__, blockHeight);
nextSweep = sweepInterval + blockHeight; nextSweep = sweepInterval + blockHeight;
fSweepWaiting = false;
return; return;
} }
//Don't Run if consolidation will run soon. // Yield to consolidation once, then take priority. Both checks used to return without any memory of
if (fSaplingConsolidationEnabled && nextConsolidation - 5 <= blockHeight) { // having done so: with a consolidation interval of 5 the "within 5 blocks" test is always true, and
LogPrintf("%s: not sweeping since next consolidation is within 5 blocks, nextConsolidation=%d , blockHeight=%d\n", __func__, nextConsolidation, blockHeight); // 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; return;
} }
//Don't Run While consolidation is running. //Don't Run While consolidation is running.
if (fConsolidationRunning) { 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; return;
} }
@@ -688,6 +697,7 @@ void CWallet::RunSaplingSweep(int blockHeight) {
} }
fSweepRunning = true; fSweepRunning = true;
fSweepWaiting = false;
std::shared_ptr<AsyncRPCQueue> q = getAsyncRPCQueue(); std::shared_ptr<AsyncRPCQueue> q = getAsyncRPCQueue();
std::shared_ptr<AsyncRPCOperation> lastOperation = q->getOperationForId(saplingSweepOperationId); std::shared_ptr<AsyncRPCOperation> lastOperation = q->getOperationForId(saplingSweepOperationId);
@@ -749,6 +759,13 @@ void CWallet::RunSaplingConsolidation(int blockHeight) {
return; 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) { if (fAutoShieldRunning) {
LogPrintf("%s: not consolidating since autoshield is currently running at height=%d\n", __func__, blockHeight); LogPrintf("%s: not consolidating since autoshield is currently running at height=%d\n", __func__, blockHeight);
return; return;
+4
View File
@@ -903,6 +903,10 @@ public:
bool fSweepEnabled = false; bool fSweepEnabled = false;
bool fSweepExternalEnabled = false; bool fSweepExternalEnabled = false;
bool fSweepRunning = false; bool fSweepRunning = false;
// A due sweep was held back by consolidation (running, or about to run). While set, consolidation
// starts no new round and the sweep skips its pre-consolidation blackout, so the sweep goes as soon
// as the running round ends instead of losing every block to consolidation. Cleared on dispatch.
bool fSweepWaiting = false;
// Automatic coinbase shielding (t->z). The real default is computed in // Automatic coinbase shielding (t->z). The real default is computed in
// init.cpp: ON only for known-recoverable seed provenance (CREATED/RESTORED), // init.cpp: ON only for known-recoverable seed provenance (CREATED/RESTORED),