Merge branch 'fix/trim-spent-witnesses' into dev
This commit is contained in:
+23
-1
@@ -1454,6 +1454,17 @@ void ClearSingleNoteWitnessCache(NoteData* nd)
|
||||
nd->witnessRootValidated = false;
|
||||
}
|
||||
|
||||
bool TrimDeeplySpentNoteWitnesses(SaplingNoteData& nd, unsigned int nSpendDepth, unsigned int nCacheSize)
|
||||
{
|
||||
// Never empty the list: UpdateSaplingNullifierNoteMapWithTx drops the nullifier of a note with no
|
||||
// witnesses, and a spent note without a nullifier is counted as unspent by GetFilteredNotes.
|
||||
if (!nd.nullifier || nSpendDepth <= nCacheSize || nd.witnesses.size() <= 1)
|
||||
return false;
|
||||
while (nd.witnesses.size() > 1)
|
||||
nd.witnesses.pop_back(); // oldest snapshots are at the back; front() stays the one at witnessHeight
|
||||
return true;
|
||||
}
|
||||
|
||||
// A note without a nullifier is never extended by BuildWitnessCache (its pending-note gate requires one),
|
||||
// so it doesn't pull the rebuild's start height down.
|
||||
int CWallet::SaplingWitnessMinimumHeight(const boost::optional<uint256>& nullifier, int nWitnessHeight, int nMinimumHeight)
|
||||
@@ -1754,6 +1765,7 @@ void CWallet::BuildWitnessCache(const CBlockIndex* pindex, bool witnessOnly)
|
||||
// cost changes from O(blocks * walletSize) to O(blocks + activeNotes).
|
||||
struct PendingNote { int startWitnessHeight; SaplingNoteData* nd; };
|
||||
std::vector<PendingNote> pending;
|
||||
int nNotesTrimmed = 0;
|
||||
for (std::pair<const uint256, CWalletTx>& wtxItem : mapWallet) {
|
||||
if (wtxItem.second.mapSaplingNoteData.empty())
|
||||
continue;
|
||||
@@ -1765,14 +1777,24 @@ void CWallet::BuildWitnessCache(const CBlockIndex* pindex, bool witnessOnly)
|
||||
continue;
|
||||
if (nd->witnesses.empty()) // cannot extend (front() would be UB); the original gate also never matched these in practice
|
||||
continue;
|
||||
if (GetSaplingSpendDepth(*nd->nullifier) > WITNESS_CACHE_SIZE)
|
||||
const unsigned int nSpendDepth = GetSaplingSpendDepth(*nd->nullifier);
|
||||
if (nSpendDepth > WITNESS_CACHE_SIZE) {
|
||||
// Spent beyond reorg reach: no longer extended, so its older snapshots are dead weight.
|
||||
if (TrimDeeplySpentNoteWitnesses(*nd, nSpendDepth, WITNESS_CACHE_SIZE))
|
||||
nNotesTrimmed++;
|
||||
continue;
|
||||
}
|
||||
// Only notes that still lag the build target need extension. The lowest such witnessHeight
|
||||
// is exactly startHeight-1 (startHeight = nMinimumHeight+1 from SaplingWitnessMinimumHeight).
|
||||
if (nd->witnessHeight >= startHeight - 1 && nd->witnessHeight <= pindex->GetHeight() - 1)
|
||||
pending.push_back({ nd->witnessHeight, nd });
|
||||
}
|
||||
}
|
||||
if (nNotesTrimmed > 0) {
|
||||
nWitnessGeneration++; // the next SetBestChain (or the shutdown save) writes the shorter lists
|
||||
LogPrintf("%s: dropped the older witnesses of %d note(s) spent more than %u blocks ago\n",
|
||||
__func__, nNotesTrimmed, WITNESS_CACHE_SIZE);
|
||||
}
|
||||
std::sort(pending.begin(), pending.end(),
|
||||
[](const PendingNote& a, const PendingNote& b) { return a.startWitnessHeight < b.startWitnessHeight; });
|
||||
|
||||
|
||||
Reference in New Issue
Block a user