wallet: let an in-flight zsweep round finish, and build each tx at the current tip

RunSaplingSweep re-dispatched a sweep on every block while a round was still running. That never
stopped the running op -- AsyncRPCOperation::cancel() is not virtual, so through the queue's base
pointer it only cancels an op that has not started, and the sweep's own cancel() override is never
reached -- it just queued a fresh round behind it, which then repeated note selection over the
whole wallet and was itself superseded a block later. Leave an in-flight op alone, as
RunSaplingConsolidation already does, and log that it is still running and since which height.

A round can outlive the block it started at (a busy deposit wallet takes seconds per sweep
transaction), but since v1.3.0 every transaction in a round was built and expired against the tip
read once when the round began, so late transactions in a long round could be expiring, or already
expired, by the time they were committed. Read the tip for each transaction, and re-check the
NU-straddle guard there.

Regtest, same fixture as dev (3 deposit addresses x 7 notes, -zsweepmaxinputs=5, a block every
~3 s while each build takes ~7 s): the same 7 sweep transactions and amount as dev, with "still
running; not re-dispatching" where dev queued a redundant round on every block.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-11-18 11:08:57 -06:00
co-authored by Claude Opus 5.5
parent df17fd4b1a
commit 2e78769817
3 changed files with 33 additions and 5 deletions
+16 -3
View File
@@ -625,13 +625,14 @@ void CWallet::RunSaplingSweep(int blockHeight) {
// consolidation waits on sweep, and autoshield shares the same gate, so a
// wedged sweep silently disables coinbase shielding forever.
// Only honour the baton while a sweep operation genuinely is in flight.
bool fSweepInFlight = false;
if (fSweepRunning) {
std::shared_ptr<AsyncRPCQueue> sweepQueue = getAsyncRPCQueue();
std::shared_ptr<AsyncRPCOperation> inFlightSweep =
(sweepQueue != nullptr) ? sweepQueue->getOperationForId(saplingSweepOperationId) : nullptr;
bool inFlight = (inFlightSweep != nullptr) &&
(inFlightSweep->isReady() || inFlightSweep->isExecuting());
if (!inFlight) {
fSweepInFlight = (inFlightSweep != nullptr) &&
(inFlightSweep->isReady() || inFlightSweep->isExecuting());
if (!fSweepInFlight) {
LogPrintf("%s: clearing stale fSweepRunning at blockHeight=%d (no sweep operation in flight)\n",
__func__, blockHeight);
fSweepRunning = false;
@@ -642,6 +643,17 @@ void CWallet::RunSaplingSweep(int blockHeight) {
return;
}
// Self-guard: let an in-flight round finish. Re-dispatching on every block, as this used to, never
// stopped the running op -- AsyncRPCOperation::cancel() is not virtual, so through the queue's base
// pointer it only cancels an op that has not started -- it just queued a fresh round behind it,
// which then repeated note selection over the whole wallet for nothing. (On a single async worker
// a replacement could not get past a hung op anyway.)
if (fSweepInFlight) {
LogPrintf("%s: sweep operation %s (enqueued at height %d) is still running at blockHeight=%d; "
"not re-dispatching\n", __func__, saplingSweepOperationId, sweepDispatchHeight, blockHeight);
return;
}
if (nextSweep > blockHeight) {
LogPrintf("%s: Not time to sweep yet at blockHeight=%d nextSweep=%d\n", __func__, blockHeight, nextSweep);
return;
@@ -680,6 +692,7 @@ void CWallet::RunSaplingSweep(int blockHeight) {
pendingSaplingSweepTxs.clear();
std::shared_ptr<AsyncRPCOperation> operation(new AsyncRPCOperation_sweep(blockHeight + AUTO_OP_TARGET_HEIGHT_OFFSET));
saplingSweepOperationId = operation->getId();
sweepDispatchHeight = blockHeight;
if (!q->addOperation(operation)) {
// Queue is closing (shutdown). Release the flag we just set, or it stays
// set with no operation in flight and blocks every later round.