wallet/rpc: fix listtransactions with a large count, and read wallet data by reference in more RPCs

The parts of origin/perf/wallet-scan-rpc-costs that dev's note cache did not already cover:

- listtransactions computed count+from in an int, so a large count (e.g. INT_MAX, a common "all"
  idiom) wrapped and returned nothing. It is now clamped. Regtest: `listtransactions "*" 2147483647 1`
  returned 0 entries on the old binary and 150 of 151 here; a normal page is byte-identical.
- Fewer per-entry copies of wallet transactions in listtransactions and gettransaction, and reads by
  reference in other wallet RPCs.
- z_viewtransaction skips a spend whose parent transaction is no longer in the wallet (e.g. after
  -deletetx) instead of failing with map::at; that spend, and outputs only its key could recover,
  are then left out of the reply.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-11-20 20:10:01 -06:00
co-authored by Claude Opus 5.5
parent 93f8b3768b
commit 433892e40b
3 changed files with 24 additions and 9 deletions
+18 -5
View File
@@ -1805,7 +1805,7 @@ void ListTransactions(const CWalletTx& wtx, const string& strAccount, int nMinDe
entry.push_back(Pair("fee", ValueFromAmount(-nFee)));
if (fLong)
WalletTxToJSON(wtx, entry);
entry.push_back(Pair("size", static_cast<uint64_t>(GetSerializeSize(static_cast<CTransaction>(wtx), SER_NETWORK, PROTOCOL_VERSION))));
entry.push_back(Pair("size", static_cast<uint64_t>(GetSerializeSize(static_cast<const CTransaction&>(wtx), SER_NETWORK, PROTOCOL_VERSION))));
ret.push_back(entry);
}
}
@@ -1850,7 +1850,7 @@ void ListTransactions(const CWalletTx& wtx, const string& strAccount, int nMinDe
entry.push_back(Pair("vout", r.vout));
if (fLong)
WalletTxToJSON(wtx, entry);
entry.push_back(Pair("size", static_cast<uint64_t>(GetSerializeSize(static_cast<CTransaction>(wtx), SER_NETWORK, PROTOCOL_VERSION))));
entry.push_back(Pair("size", static_cast<uint64_t>(GetSerializeSize(static_cast<const CTransaction&>(wtx), SER_NETWORK, PROTOCOL_VERSION))));
ret.push_back(entry);
}
}
@@ -1952,6 +1952,10 @@ UniValue listtransactions(const UniValue& params, bool fHelp, const CPubKey& myp
throw JSONRPCError(RPC_INVALID_PARAMETER, "Negative count");
if (nFrom < 0)
throw JSONRPCError(RPC_INVALID_PARAMETER, "Negative from");
// nCount + nFrom wraps negative otherwise (-fwrapv), and the window below then indexes far out of
// bounds. No wallet has INT_MAX entries, so clamping changes no valid reply.
if (nCount > std::numeric_limits<int>::max() - nFrom)
nCount = std::numeric_limits<int>::max() - nFrom;
UniValue ret(UniValue::VARR);
@@ -2240,7 +2244,7 @@ UniValue gettransaction(const UniValue& params, bool fHelp, const CPubKey& mypk)
ListTransactions(wtx, "*", 0, false, details, filter);
entry.push_back(Pair("details", details));
string strHex = EncodeHexTx(static_cast<CTransaction>(wtx));
string strHex = EncodeHexTx(static_cast<const CTransaction&>(wtx));
entry.push_back(Pair("hex", strHex));
return entry;
@@ -5185,8 +5189,17 @@ UniValue z_viewtransaction(const UniValue& params, bool fHelp, const CPubKey& my
continue;
}
auto op = res->second;
auto wtxPrev = pwalletMain->mapWallet.at(op.hash);
auto decrypted = wtxPrev.DecryptSaplingNote(op).get();
// By reference: a CWalletTx copy deep-copies every note's witness list. The nullifier map can
// outlive the tx it points to (DeleteTransactions doesn't prune it); skip such a spend like an
// unknown one instead of failing the whole call with "map::at".
std::map<uint256, CWalletTx>::const_iterator itPrev = pwalletMain->mapWallet.find(op.hash);
if (itPrev == pwalletMain->mapWallet.end())
continue;
const CWalletTx& wtxPrev = itPrev->second;
auto maybeDecrypted = wtxPrev.DecryptSaplingNote(op);
if (!maybeDecrypted)
continue;
auto decrypted = maybeDecrypted.get();
auto notePt = decrypted.first;
auto pa = decrypted.second;