From 66b74aa6aa4c29d062bcc20314c729ec1c560e3a Mon Sep 17 00:00:00 2001 From: DanS Date: Sun, 29 Nov 2026 10:46:59 -0600 Subject: [PATCH] wallet: give zsweep, consolidation and autoshield txs the normal expiry The automated ops set nExpiryHeight to buildHeight + 15. The wallet only rebroadcasts its unconfirmed transactions every 0-30 minutes (randomised, for privacy), and only those a few minutes older than the last block, while each transaction is relayed to a random half of the node's peers. A tx that missed the miners on its first relay was therefore almost never retried before block +15 (~9 minutes on mainnet), and expired. The op had already logged "Committed" and, for zsweep, added it to amount_swept; the notes reappeared as unspent and were swept again a round later. Nothing was lost, but z_sweepstatus overstated progress, and manual z_sendmany calls from the deposit addresses kept colliding with the re-sweeps ("bad-txns-shielded-requirements-not-met"). Measured on the 2026-11-29 stress fleet (exchange-style node with ~5 peer connections): 38 of 184 sweep txs (21%) never confirmed; the log shows 1810 "ignoring expired tx" lines from the rebroadcast pass. The three ops now use -txexpirydelta (default 200), the same window every other wallet transaction gets, through AutoOpExpiryDelta(). Their NU-straddle guards use the same window, so near a network upgrade they pause for the full expiry window instead of 15 blocks. This applies to v1.2.0 too (its per-op *_EXPIRY_DELTA constants were also 15); it is not a v1.3.0 regression. Co-Authored-By: Claude Opus 5.5 (1M context) --- doc/zsweep-consolidation.md | 3 ++- src/wallet/asyncrpcoperation_autoshieldcoinbase.cpp | 4 ++-- src/wallet/asyncrpcoperation_saplingconsolidation.cpp | 4 ++-- src/wallet/asyncrpcoperation_sweep.cpp | 6 +++--- src/wallet/wallet.h | 7 ++++++- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/doc/zsweep-consolidation.md b/doc/zsweep-consolidation.md index c809989c2..8900cfecf 100644 --- a/doc/zsweep-consolidation.md +++ b/doc/zsweep-consolidation.md @@ -58,7 +58,8 @@ witness rebuilds. Consolidation keeps that in check. so every address keeps at least one spendable note while the round confirms. * Each transaction has exactly 8 shielded outputs: the real one plus 7 zero-value dummy ("Sietch") outputs to random addresses, so it does not shrink the anonymity set. It pays `consolidationtxfee`, or no fee when its notes are worth - no more than the fee, and expires 15 blocks after it is made. + no more than the fee, and expires `-txexpirydelta` blocks (default 200) after it is made, like any other wallet + transaction, so the wallet's periodic rebroadcast can still retry it. * Only the notes a round actually spends are locked while it proves, so sends made meanwhile still see the rest of the wallet. * All spends of one transaction must use one anchor (Sapling tree root). An address's spends use the tip's root, diff --git a/src/wallet/asyncrpcoperation_autoshieldcoinbase.cpp b/src/wallet/asyncrpcoperation_autoshieldcoinbase.cpp index 8a21bde9f..72cf166b6 100644 --- a/src/wallet/asyncrpcoperation_autoshieldcoinbase.cpp +++ b/src/wallet/asyncrpcoperation_autoshieldcoinbase.cpp @@ -294,7 +294,7 @@ bool AsyncRPCOperation_autoshieldcoinbase::main_impl() { // from below), not the stale enqueue-time targetHeight_, so a queue delay // cannot slip a straddling expiry past this guard. auto nextActivationHeight = NextActivationHeight(tipHeight, consensusParams); - if (nextActivationHeight && tipHeight + AUTO_OP_EXPIRY_DELTA >= nextActivationHeight.get()) { + if (nextActivationHeight && tipHeight + AutoOpExpiryDelta() >= nextActivationHeight.get()) { LogPrintf("%s: autoshield tx could expire across a NU activation. Skipping this round.\n", opid); return true; } @@ -414,7 +414,7 @@ bool AsyncRPCOperation_autoshieldcoinbase::main_impl() { // stale enqueue-time height here meant the guard was checking a height the // transaction was not actually signed against. auto builder = TransactionBuilder(consensusParams, tipHeight, pwalletMain); - builder.SetExpiryHeight(tipHeight + AUTO_OP_EXPIRY_DELTA); + builder.SetExpiryHeight(tipHeight + AutoOpExpiryDelta()); builder.SetFee(fee); for (const auto& t : inputs) { diff --git a/src/wallet/asyncrpcoperation_saplingconsolidation.cpp b/src/wallet/asyncrpcoperation_saplingconsolidation.cpp index b9800d244..fac2e4ec0 100644 --- a/src/wallet/asyncrpcoperation_saplingconsolidation.cpp +++ b/src/wallet/asyncrpcoperation_saplingconsolidation.cpp @@ -313,7 +313,7 @@ bool AsyncRPCOperation_saplingconsolidation::main_impl() { // the NU-straddle guard agree with the height the tx is signed for. Mirrors // the autoshield op (commit 65130c312). auto nextActivationHeight = NextActivationHeight(tipHeight, consensusParams); - if (nextActivationHeight && tipHeight + AUTO_OP_EXPIRY_DELTA >= nextActivationHeight.get()) { + if (nextActivationHeight && tipHeight + AutoOpExpiryDelta() >= nextActivationHeight.get()) { LogPrintf("%s: Consolidation txs would be created before a NU activation but may expire after. Skipping this round.\n",opid); setConsolidationResult(ConsolidationTally()); return status; @@ -469,7 +469,7 @@ bool AsyncRPCOperation_saplingconsolidation::main_impl() { const AddressNotes& group = groups[planned.group]; auto builder = TransactionBuilder(consensusParams, tipHeight, pwalletMain); - builder.SetExpiryHeight(tipHeight + AUTO_OP_EXPIRY_DELTA); + builder.SetExpiryHeight(tipHeight + AutoOpExpiryDelta()); CAmount amountToSend = 0; for (const SaplingNoteEntry& entry : planned.notes) diff --git a/src/wallet/asyncrpcoperation_sweep.cpp b/src/wallet/asyncrpcoperation_sweep.cpp index 3787dbfbc..fcc4ccad1 100644 --- a/src/wallet/asyncrpcoperation_sweep.cpp +++ b/src/wallet/asyncrpcoperation_sweep.cpp @@ -192,7 +192,7 @@ bool AsyncRPCOperation_sweep::main_impl() { // targetHeight_, so a queue delay cannot slip a straddling expiry past this // guard. Mirrors the autoshield op (commit 65130c312). auto nextActivationHeight = NextActivationHeight(tipHeight, consensusParams); - if (nextActivationHeight && tipHeight + AUTO_OP_EXPIRY_DELTA >= nextActivationHeight.get()) { + if (nextActivationHeight && tipHeight + AutoOpExpiryDelta() >= nextActivationHeight.get()) { LogPrintf("%s: Sweep txs would be created before a NU activation but may expire after. Skipping this round.\n", getId()); setSweepResult(0, 0, std::vector()); sweepComplete_ = true; // nothing to do this round; back nextSweep off one interval instead of re-dispatching every block @@ -347,12 +347,12 @@ bool AsyncRPCOperation_sweep::main_impl() { buildHeight = (chainActive.Tip() != NULL) ? chainActive.Tip()->GetHeight() : tipHeight; } auto nextActivation = NextActivationHeight(buildHeight, consensusParams); - if (nextActivation && buildHeight + AUTO_OP_EXPIRY_DELTA >= nextActivation.get()) { + if (nextActivation && buildHeight + AutoOpExpiryDelta() >= 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); + builder.SetExpiryHeight(buildHeight + AutoOpExpiryDelta()); LogPrintf("%s: Beginning creating transaction with Sapling output amount=%s\n", getId(), FormatMoney(amountToSend - fee)); // Add Sapling spends, each with the witness it was selected with diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 2330b282b..adfbe3cf8 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -132,7 +132,12 @@ static const CAmount CONSOLIDATION_MIN_FEE = AUTOSHIELD_MIN_FEE; static const CAmount CONSOLIDATION_MAX_FEE = AUTOSHIELD_MAX_FEE; static const int CONSOLIDATION_MIN_DEPTH = 11; // leaves room for an anchor at height N-10 static const int AUTO_OP_TARGET_HEIGHT_OFFSET = 5; // blocks of lookahead when scheduling an async op -static const int AUTO_OP_EXPIRY_DELTA = 15; // NU-straddle expiry window, shared by all three ops +// Expiry window for the transactions zsweep, consolidation and autoshield build, and for their NU-straddle +// guards: the same -txexpirydelta (default 200) as any other wallet transaction. These used a fixed 15 blocks, +// which ran out before the wallet's rebroadcast (every 0-30 min, and only for txs a few minutes older than the +// last block) ever retried a tx the half-of-peers relay had not carried to a miner: on a node with a handful of +// peers about one sweep tx in five expired unmined, while the sweep had already counted it as swept. +inline int AutoOpExpiryDelta() { return (int)expiryDelta; } extern const char * DEFAULT_WALLET_DAT;