wallet: drop the witness history of notes spent beyond reorg reach
BuildWitnessCache stops extending a note once its spend is deeper than WITNESS_CACHE_SIZE (MAX_REORG_LENGTH + 10 = 109): no reorg can un-spend it any more. But the up-to-109 witness snapshots the note had by then were never released. Every spent note the wallet ever had kept them, in memory and in wallet.dat, and SetBestChain rewrote them all on every save. On a wallet that receives a steady stream of deposits and sweeps them, memory and wallet.dat grow with the number of notes ever received. Measured in the 2026-11-29/30 stress soak (exchange-style wallet, ~360 deposit notes an hour swept to a withdraw address): RSS rose ~120-170 MB an hour to 2.9 GB over 18 h, wallet.dat reached 855 MB for ~6,500 transactions, and the per-block log reported ~6,200 witnessed notes. With deposits stopped, RSS stopped climbing, so this is history growth, not a leak. Now such a note keeps only its newest witness (front()). That one is at witnessHeight, gives the position its nullifier is derived from, and is the root VerifyAndSetInitialWitness validates at startup, so a trimmed note loads and validates exactly as before. The list is never emptied: a note with no witnesses loses its nullifier in UpdateSaplingNullifierNoteMapWithTx and would then count as unspent. A deeper-than-cache reorg (only possible after raising -maxreorg) already pops or clears such notes' single remaining witness and reseeds them. The trim runs in the per-block BuildWitnessCache pass, which already evaluates each note's spend depth, so it costs no extra wallet walk. It bumps nWitnessGeneration so the next SetBestChain or the shutdown save writes the shorter lists, and logs how many notes it trimmed. New gtest WalletWitnessTrim covers: deeply spent notes keep exactly their newest witness (root, position, height, nullifier unchanged); unspent and in-reach notes, notes without a nullifier, and empty lists are untouched; the serialized note shrinks by more than 20x. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
// Copyright (c) 2024-2026 The DragonX developers
|
||||
// Distributed under the GPLv3 software license, see the accompanying
|
||||
// file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "random.h"
|
||||
#include "uint256.h"
|
||||
#include "wallet/wallet.h"
|
||||
#include "zcash/IncrementalMerkleTree.hpp"
|
||||
|
||||
namespace {
|
||||
|
||||
libzcash::SaplingIncomingViewingKey TestIvk()
|
||||
{
|
||||
return libzcash::SaplingIncomingViewingKey(uint256S("0202020202020202020202020202020202020202020202020202020202020202"));
|
||||
}
|
||||
|
||||
// A note with a witness for every one of nSnapshots blocks, newest at the front, the way
|
||||
// BuildWitnessCache leaves it: each snapshot is the previous one plus one more commitment.
|
||||
SaplingNoteData NoteWithHistory(int nSnapshots, int tipHeight, bool fNullifier = true)
|
||||
{
|
||||
SaplingNoteData nd = fNullifier ? SaplingNoteData(TestIvk(), GetRandHash()) : SaplingNoteData(TestIvk());
|
||||
SaplingMerkleTree tree;
|
||||
tree.append(GetRandHash()); // the note's own commitment
|
||||
nd.witnesses.push_front(tree.witness());
|
||||
for (int i = 1; i < nSnapshots; i++) {
|
||||
nd.witnesses.push_front(nd.witnesses.front());
|
||||
nd.witnesses.front().append(GetRandHash());
|
||||
}
|
||||
nd.witnessHeight = tipHeight;
|
||||
return nd;
|
||||
}
|
||||
|
||||
const unsigned int CACHE = 109; // WITNESS_CACHE_SIZE on DragonX: MAX_REORG_LENGTH (99) + 10
|
||||
|
||||
} // namespace
|
||||
|
||||
// Spent deeper than the cache: only the newest witness survives, unchanged, and the note keeps the
|
||||
// position and height its nullifier and startup validation depend on.
|
||||
TEST(WalletWitnessTrim, DeeplySpentNoteKeepsOnlyNewestWitness)
|
||||
{
|
||||
SaplingNoteData nd = NoteWithHistory(CACHE, 5000);
|
||||
const uint256 rootBefore = nd.witnesses.front().root();
|
||||
const uint64_t posBefore = nd.witnesses.front().position();
|
||||
const boost::optional<uint256> nfBefore = nd.nullifier;
|
||||
|
||||
EXPECT_TRUE(TrimDeeplySpentNoteWitnesses(nd, CACHE + 1, CACHE));
|
||||
ASSERT_EQ(nd.witnesses.size(), 1u);
|
||||
EXPECT_EQ(nd.witnesses.front().root(), rootBefore);
|
||||
EXPECT_EQ(nd.witnesses.front().position(), posBefore);
|
||||
EXPECT_EQ(nd.witnessHeight, 5000);
|
||||
EXPECT_TRUE(nd.nullifier == nfBefore);
|
||||
|
||||
// Already trimmed: nothing more to do.
|
||||
EXPECT_FALSE(TrimDeeplySpentNoteWitnesses(nd, CACHE + 50, CACHE));
|
||||
EXPECT_EQ(nd.witnesses.size(), 1u);
|
||||
}
|
||||
|
||||
// Unspent (depth 0) and spent within reorg reach: a reorg may still need the older snapshots.
|
||||
TEST(WalletWitnessTrim, UnspentAndShallowSpentNotesAreUntouched)
|
||||
{
|
||||
SaplingNoteData unspent = NoteWithHistory(CACHE, 5000);
|
||||
EXPECT_FALSE(TrimDeeplySpentNoteWitnesses(unspent, 0, CACHE));
|
||||
EXPECT_EQ(unspent.witnesses.size(), CACHE);
|
||||
|
||||
SaplingNoteData shallow = NoteWithHistory(CACHE, 5000);
|
||||
EXPECT_FALSE(TrimDeeplySpentNoteWitnesses(shallow, CACHE, CACHE)); // depth == cache size: still in reach
|
||||
EXPECT_EQ(shallow.witnesses.size(), CACHE);
|
||||
}
|
||||
|
||||
// A note without a nullifier is never trimmed (it is not known to be spent), and an empty list stays
|
||||
// empty: trimming must never be what leaves a note with no witnesses.
|
||||
TEST(WalletWitnessTrim, NoNullifierOrNoWitnessesIsLeftAlone)
|
||||
{
|
||||
SaplingNoteData noNf = NoteWithHistory(CACHE, 5000, false);
|
||||
EXPECT_FALSE(TrimDeeplySpentNoteWitnesses(noNf, CACHE + 1, CACHE));
|
||||
EXPECT_EQ(noNf.witnesses.size(), CACHE);
|
||||
|
||||
SaplingNoteData empty(TestIvk(), GetRandHash());
|
||||
EXPECT_FALSE(TrimDeeplySpentNoteWitnesses(empty, CACHE + 1, CACHE));
|
||||
EXPECT_TRUE(empty.witnesses.empty());
|
||||
}
|
||||
|
||||
// The serialized note shrinks with the list: what SetBestChain rewrites for every spent note.
|
||||
TEST(WalletWitnessTrim, SerializedSizeShrinks)
|
||||
{
|
||||
SaplingNoteData nd = NoteWithHistory(CACHE, 5000);
|
||||
const size_t before = ::GetSerializeSize(nd, SER_DISK, CLIENT_VERSION);
|
||||
ASSERT_TRUE(TrimDeeplySpentNoteWitnesses(nd, CACHE + 1, CACHE));
|
||||
const size_t after = ::GetSerializeSize(nd, SER_DISK, CLIENT_VERSION);
|
||||
EXPECT_LT(after * 20, before);
|
||||
}
|
||||
Reference in New Issue
Block a user