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:
@@ -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
|
||||
|
||||
+16
-3
@@ -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.
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user