From 3e75769a48819d82ea74d02f8f756a5344aa6962 Mon Sep 17 00:00:00 2001 From: DanS Date: Sun, 29 Nov 2026 10:48:26 -0600 Subject: [PATCH] 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) --- doc/zsweep-consolidation.md | 6 ++++-- src/wallet/rpcwallet.cpp | 3 +++ src/wallet/wallet.cpp | 25 +++++++++++++++++++++---- src/wallet/wallet.h | 4 ++++ 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/doc/zsweep-consolidation.md b/doc/zsweep-consolidation.md index c809989c2..757271088 100644 --- a/doc/zsweep-consolidation.md +++ b/doc/zsweep-consolidation.md @@ -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 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 - sweep is not started when a consolidation round is due within 5 blocks. + and `z_consolidate` refuses any other address too. Sweep and consolidation never run at the same time. A due sweep + 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 diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 496541fff..9e41c2e52 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -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" " \"wallet_locked\" : true|false, (boolean) the wallet is encrypted and locked, so nothing can be swept\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" " \"next_zsweep\" : n, (numeric) height of the next round\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("running", pwalletMain->fSweepRunning)); 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("next_zsweep", pwalletMain->nextSweep)); ret.push_back(Pair("zsweepinterval", pwalletMain->sweepInterval)); diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 439133e7e..591a75f18 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -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 q = getAsyncRPCQueue(); std::shared_ptr 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; diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 2330b282b..6697ec4ec 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -903,6 +903,10 @@ public: bool fSweepEnabled = false; bool fSweepExternalEnabled = 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 // init.cpp: ON only for known-recoverable seed provenance (CREATED/RESTORED),