From 7c0702affa892fd881d1aaebf02b6e0790a28ba1 Mon Sep 17 00:00:00 2001 From: DanS Date: Mon, 30 Nov 2026 12:44:29 -0600 Subject: [PATCH] wallet: make zsweep and consolidation take strict turns The previous fix (3e75769a4) let a due sweep give way once to a consolidation round that was running or "due within 5 blocks", and then hold every new consolidation round off until the sweep had run. On a wallet that always has something to sweep this starved consolidation instead: the 5-block "give way" itself set the waiting flag, the next consolidation was refused because a sweep was waiting, the sweep ran, and so on. Measured on the 2026-11-30 soak of 9c7c5835b (exchange-style wallet, zsweep every 5 blocks, consolidation at the default 25): in 15.5 h, 2,375 sweep txs and 0 consolidation txs (273 deferrals); the withdraw address piled up to 5,898 unspent notes and RSS reached 4.7 GB. Now the two ops take strict turns: - a due sweep that finds a consolidation round running sets fSweepWaiting, and no new consolidation round starts until the sweep is dispatched; - a due consolidation round that finds a sweep running sets fConsolidationWaiting, and no new sweep round starts until it is dispatched; - the 5-block blackout is gone (with consolidationinterval=5 it was always true); - whichever op yields clears its own flag, so they can never both be waiting; - each flag is cleared on dispatch, when its op is off, or when it is skipped for a locked wallet. z_consolidationstatus gains waiting_for_sweep, next to z_sweepstatus's waiting_for_consolidation. doc/zsweep-consolidation.md updated. Co-Authored-By: Claude Opus 5.5 (1M context) --- doc/zsweep-consolidation.md | 8 ++++---- src/wallet/rpcwallet.cpp | 3 +++ src/wallet/wallet.cpp | 34 ++++++++++++++++++---------------- src/wallet/wallet.h | 11 ++++++++--- 4 files changed, 33 insertions(+), 23 deletions(-) diff --git a/doc/zsweep-consolidation.md b/doc/zsweep-consolidation.md index ab33d99ee..9ca294b75 100644 --- a/doc/zsweep-consolidation.md +++ b/doc/zsweep-consolidation.md @@ -129,10 +129,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. 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. + and `z_consolidate` refuses any other address too. Sweep and consolidation never run at the same time, and they take + strict turns: a round that comes due while the other op is running goes next, and the other op starts no new + round until it has run, so neither can starve the other. `z_sweepstatus` shows `"waiting_for_consolidation": true` + and `z_consolidationstatus` shows `"waiting_for_sweep": true` while a round is waiting for its turn. ### Copyright diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 9e41c2e52..ea022475f 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -3300,6 +3300,8 @@ UniValue z_consolidationstatus(const UniValue& params, bool fHelp, const CPubKey "{\n" " \"consolidation\" : true|false, (boolean) whether periodic consolidation (-consolidation) is on\n" " \"running\" : true|false, (boolean) whether a round is in flight\n" + " \"waiting_for_sweep\" : true|false, (boolean) a due round is waiting for a zsweep round to end;\n" + " no new sweep round starts until it has run\n" " \"amount_consolidated\" : n, (numeric) value of the notes created since startup, in puposhis\n" " \"next_consolidation\" : n, (numeric) height of the next periodic round\n" " \"consolidationinterval\" : n, (numeric) blocks between periodic rounds\n" @@ -3348,6 +3350,7 @@ UniValue z_consolidationstatus(const UniValue& params, bool fHelp, const CPubKey UniValue ret(UniValue::VOBJ); ret.push_back(Pair("consolidation", pwalletMain->fSaplingConsolidationEnabled)); ret.push_back(Pair("running", pwalletMain->fConsolidationRunning)); + ret.push_back(Pair("waiting_for_sweep", pwalletMain->fConsolidationWaiting)); ret.push_back(Pair("amount_consolidated", (int64_t)stats.amountConsolidated)); ret.push_back(Pair("next_consolidation", pwalletMain->nextConsolidation)); ret.push_back(Pair("consolidationinterval", pwalletMain->consolidationInterval)); diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 591a75f18..d1ca4643c 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -671,22 +671,19 @@ void CWallet::RunSaplingSweep(int blockHeight) { return; } - // 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); + // Strict turns with consolidation (see fSweepWaiting / fConsolidationWaiting). This replaces a + // "consolidation is due within 5 blocks" blackout, which with consolidationinterval=5 was always true. + //Don't Run While consolidation is running: wait, and take the next turn. + if (fConsolidationRunning) { + LogPrintf("%s: not sweeping since consolidation is currently running at height=%d; the sweep goes next\n", __func__, 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; no new consolidation round starts until this sweep has run\n", __func__, blockHeight); - fSweepWaiting = true; + //A consolidation round that came due while a sweep was running goes first. + if (fConsolidationWaiting && fSaplingConsolidationEnabled) { + LogPrintf("%s: not sweeping since a consolidation round is waiting for its turn at height=%d\n", __func__, blockHeight); + fSweepWaiting = false; // never both waiting: that would leave neither op able to start return; } @@ -730,6 +727,7 @@ void CWallet::RunSaplingConsolidation(int blockHeight) { LOCK(cs_wallet); if (!fSaplingConsolidationEnabled) { + fConsolidationWaiting = false; return; } @@ -749,20 +747,23 @@ void CWallet::RunSaplingConsolidation(int blockHeight) { if (IsLocked()) { LogPrintf("%s: wallet locked; not consolidating at height=%d\n", __func__, blockHeight); nextConsolidation = consolidationInterval + blockHeight; + fConsolidationWaiting = false; return; } LogPrintf("%s: consolidation enabled at blockHeight=%d fSweepRunning=%d\n", __func__, blockHeight, fSweepRunning ); + //Don't Run While a sweep is running: wait, and take the next turn (nextConsolidation stays put). if (fSweepRunning) { - LogPrintf("%s: not consolidating since sweep is currently running at height=%d\n", __func__, blockHeight); + LogPrintf("%s: not consolidating since sweep is currently running at height=%d; consolidation goes next\n", __func__, blockHeight); + fConsolidationWaiting = true; 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. + //A sweep that came due while this op was running goes first. if (fSweepWaiting && fSweepEnabled) { - LogPrintf("%s: not consolidating since a due sweep is waiting at height=%d\n", __func__, blockHeight); + LogPrintf("%s: not consolidating since a due sweep is waiting for its turn at height=%d\n", __func__, blockHeight); + fConsolidationWaiting = false; // never both waiting return; } @@ -773,6 +774,7 @@ void CWallet::RunSaplingConsolidation(int blockHeight) { LogPrintf("%s: creating consolidation operation at blockHeight=%d\n", __func__, blockHeight); fConsolidationRunning = true; + fConsolidationWaiting = false; std::shared_ptr q = getAsyncRPCQueue(); std::shared_ptr lastOperation = q->getOperationForId(saplingConsolidationOperationId); if (lastOperation != nullptr) { diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index b9a2523eb..baa9f63c2 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -908,10 +908,15 @@ 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. + // Strict turns between zsweep and consolidation: an op that came due while the other was running + // goes next. fSweepWaiting is set when a due sweep finds a consolidation round running, and keeps a + // new consolidation round from starting; fConsolidationWaiting is the same the other way round, and + // keeps a new sweep round from starting. Each is cleared when its own op is dispatched (or turned + // off, or skipped for a locked wallet). Without them the two starved each other: the sweep lost every + // block to a 5-block "consolidation is due soon" blackout, and once that was relaxed, back-to-back + // sweep rounds left consolidation no block at all. bool fSweepWaiting = false; + bool fConsolidationWaiting = false; // Automatic coinbase shielding (t->z). The real default is computed in // init.cpp: ON only for known-recoverable seed provenance (CREATED/RESTORED),