diff --git a/src/wallet/asyncrpcoperation_sweep.cpp b/src/wallet/asyncrpcoperation_sweep.cpp index 75a5b20c2..981141099 100644 --- a/src/wallet/asyncrpcoperation_sweep.cpp +++ b/src/wallet/asyncrpcoperation_sweep.cpp @@ -253,8 +253,21 @@ bool AsyncRPCOperation_sweep::main_impl() { fee = 0; } - auto builder = TransactionBuilder(consensusParams, tipHeight, pwalletMain); - builder.SetExpiryHeight(tipHeight + AUTO_OP_EXPIRY_DELTA); + // Build and expire against the tip as it is now, not as it was when the round began: a round + // over many addresses can outlive the block it started at, and a transaction built against + // a stale height can be expiring, or already expired, by the time it is committed. + int buildHeight; + { + LOCK(cs_main); + buildHeight = (chainActive.Tip() != NULL) ? chainActive.Tip()->GetHeight() : tipHeight; + } + auto nextActivation = NextActivationHeight(buildHeight, consensusParams); + if (nextActivation && buildHeight + AUTO_OP_EXPIRY_DELTA >= nextActivation.get()) { + LogPrintf("%s: Sweep txs would now be created before a NU activation but may expire after. Stopping this round.\n", getId()); + break; + } + auto builder = TransactionBuilder(consensusParams, buildHeight, pwalletMain); + builder.SetExpiryHeight(buildHeight + AUTO_OP_EXPIRY_DELTA); LogPrintf("%s: Beginning creating transaction with Sapling output amount=%s\n", getId(), FormatMoney(amountToSend - fee)); // Select Sapling notes diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index c83396fda..941fb08dc 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -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 sweepQueue = getAsyncRPCQueue(); std::shared_ptr 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 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. diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 6e14a5f39..9b16ba6f3 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -930,6 +930,8 @@ public: // Id of the in-flight sweep op; read by the op to confirm it is still the // current one before mutating scheduler state. AsyncRPCOperationId saplingSweepOperationId; + // Block height at which that op was enqueued (for the log while RunSaplingSweep waits on it). + int sweepDispatchHeight = 0; int amountSwept = 0; // What consolidation rounds have done since startup. Guarded by cs_wallet. SaplingConsolidationStats consolidationStats;