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 of9c7c5835b(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) <noreply@anthropic.com>
This commit is contained in:
+18
-16
@@ -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<AsyncRPCQueue> q = getAsyncRPCQueue();
|
||||
std::shared_ptr<AsyncRPCOperation> lastOperation = q->getOperationForId(saplingConsolidationOperationId);
|
||||
if (lastOperation != nullptr) {
|
||||
|
||||
Reference in New Issue
Block a user