wallet: stop zsweep reporting success on a locked wallet

On an encrypted wallet that is locked, the sweep op could get no spending key, skipped every
address without a word, and finished "Created 0 transactions ... status=1": a successful round that
swept nothing, every interval, with no hint anywhere. Every restart locks an encrypted wallet, so an
exchange that restarted its node to upgrade saw its deposit -> withdraw sweep simply stop.

- RunSaplingSweep skips the round while the wallet is locked, logs why, and looks again an interval
  later, as consolidation and autoshield already do.
- The op fails with RPC_WALLET_UNLOCK_NEEDED if the wallet is locked when it starts, and fails the
  round with that error if the wallet is locked part-way through (a walletpassphrase timeout).
- An address the wallet has only a viewing key for is now logged (zrpcunsafe) rather than skipped
  silently.

Regtest, dev vs this branch on the same encrypted fixture: dev logs "Created 0 transactions ...
status=success" while locked; this logs "wallet locked; not sweeping ... Unlock it with
walletpassphrase", and locking mid-round ends it "status=failed, error=Wallet was locked during
the sweep ...". After unlocking, both sweep everything.

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 2e78769817
commit 70dd6c6f8b
2 changed files with 29 additions and 0 deletions
+20
View File
@@ -128,6 +128,15 @@ bool AsyncRPCOperation_sweep::main_impl() {
return true;
}
// A locked wallet yields no spending keys, so every address below would be skipped and the round
// would report success having swept nothing. RunSaplingSweep already declines to enqueue while
// locked; this catches a wallet locked between enqueue and execution, and makes the reason
// visible in z_getoperationstatus.
if (pwalletMain->IsLocked()) {
LogPrintf("%s: wallet is locked; nothing can be swept. Unlock it with walletpassphrase\n", getId());
throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, "Wallet is locked; unlock it with walletpassphrase for zsweep to sign");
}
std::vector<SaplingNoteEntry> saplingEntries;
libzcash::SaplingPaymentAddress sweepAddress;
std::map<libzcash::SaplingPaymentAddress, std::vector<SaplingNoteEntry>> mapAddresses;
@@ -342,6 +351,17 @@ bool AsyncRPCOperation_sweep::main_impl() {
status = false;
break;
}
} else if (pwalletMain->IsLocked()) {
// Locked part-way through the round (a walletpassphrase timeout): nothing further can be
// signed. Fail the round rather than report the unswept addresses as done.
LogPrintf("%s: wallet was locked during the sweep; stopping. Unlock it with walletpassphrase\n", getId());
set_error_code(RPC_WALLET_UNLOCK_NEEDED);
set_error_message("Wallet was locked during the sweep; unlock it with walletpassphrase for zsweep to sign");
status = false;
break;
} else {
// A viewing-key-only address: its notes are visible but cannot be spent from this wallet.
LogPrint("zrpcunsafe", "%s: no spending key for %s; not sweeping it\n", getId(), EncodePaymentAddress(addr));
}
}
+9
View File
@@ -660,6 +660,15 @@ void CWallet::RunSaplingSweep(int blockHeight) {
}
LogPrintf("%s: Sweep enabled at blockHeight=%d nextSweep=%d\n", __func__, blockHeight, nextSweep);
// Nothing can be signed while the wallet is locked: every address would be skipped and the round
// would report success with nothing swept. Say so, and look again an interval later.
if (IsLocked()) {
LogPrintf("%s: wallet locked; not sweeping at height=%d. Unlock it with walletpassphrase "
"for zsweep to resume\n", __func__, blockHeight);
nextSweep = sweepInterval + blockHeight;
return;
}
//Don't Run if consolidation will run soon.
if (fSaplingConsolidationEnabled && nextConsolidation - 5 <= blockHeight) {
LogPrintf("%s: not sweeping since next consolidation is within 5 blocks, nextConsolidation=%d , blockHeight=%d\n", __func__, nextConsolidation, blockHeight);