Decrement sapling note witnesses

This commit is contained in:
Eirik Ogilvie-Wigley
2018-07-17 11:53:24 -06:00
committed by Simon
parent be43b7469d
commit 9d804cc619

View File

@@ -868,28 +868,27 @@ void CWallet::IncrementNoteWitnesses(const CBlockIndex* pindex,
// of the wallet.dat is maintained). // of the wallet.dat is maintained).
} }
void CWallet::DecrementNoteWitnesses(const CBlockIndex* pindex) template<typename NoteDataMap>
void DecrementNoteWitnesses(NoteDataMap& noteDataMap, int indexHeight, int64_t nWitnessCacheSize)
{ {
LOCK(cs_wallet); for (auto& item : noteDataMap) {
for (std::pair<const uint256, CWalletTx>& wtxItem : mapWallet) { auto* nd = &(item.second);
for (mapSproutNoteData_t::value_type& item : wtxItem.second.mapSproutNoteData) { // Only decrement witnesses that are not above the current height
SproutNoteData* nd = &(item.second); if (nd->witnessHeight <= indexHeight) {
// Only increment witnesses that are not above the current height
if (nd->witnessHeight <= pindex->nHeight) {
// Check the validity of the cache // Check the validity of the cache
// See comment below (this would be invalid if there was a // See comment below (this would be invalid if there were a
// prior decrement). // prior decrement).
assert(nWitnessCacheSize >= nd->witnesses.size()); assert(nWitnessCacheSize >= nd->witnesses.size());
// Witnesses being decremented should always be either -1 // Witnesses being decremented should always be either -1
// (never incremented or decremented) or equal to pindex // (never incremented or decremented) or equal to the height
assert((nd->witnessHeight == -1) || // of the block being removed (indexHeight)
(nd->witnessHeight == pindex->nHeight)); assert((nd->witnessHeight == -1) || (nd->witnessHeight == indexHeight));
if (nd->witnesses.size() > 0) { if (nd->witnesses.size() > 0) {
nd->witnesses.pop_front(); nd->witnesses.pop_front();
} }
// pindex is the block being removed, so the new witness cache // indexHeight is the height of the block being removed, so
// height is one below it. // the new witness cache height is one below it.
nd->witnessHeight = pindex->nHeight - 1; nd->witnessHeight = indexHeight - 1;
} }
// Check the validity of the cache // Check the validity of the cache
// Technically if there are notes witnessed above the current // Technically if there are notes witnessed above the current
@@ -901,13 +900,21 @@ void CWallet::DecrementNoteWitnesses(const CBlockIndex* pindex)
// We don't set nWitnessCacheSize to zero at the start of the // We don't set nWitnessCacheSize to zero at the start of the
// reindex because the on-disk blocks had already resulted in a // reindex because the on-disk blocks had already resulted in a
// chain that didn't trigger the assertion below. // chain that didn't trigger the assertion below.
if (nd->witnessHeight < pindex->nHeight) { if (nd->witnessHeight < indexHeight) {
// Subtract 1 to compare to what nWitnessCacheSize will be after // Subtract 1 to compare to what nWitnessCacheSize will be after
// decrementing. // decrementing.
assert((nWitnessCacheSize - 1) >= nd->witnesses.size()); assert((nWitnessCacheSize - 1) >= nd->witnesses.size());
} }
} }
} }
void CWallet::DecrementNoteWitnesses(const CBlockIndex* pindex)
{
LOCK(cs_wallet);
for (std::pair<const uint256, CWalletTx>& wtxItem : mapWallet) {
::DecrementNoteWitnesses(wtxItem.second.mapSproutNoteData, pindex->nHeight, nWitnessCacheSize);
::DecrementNoteWitnesses(wtxItem.second.mapSaplingNoteData, pindex->nHeight, nWitnessCacheSize);
}
nWitnessCacheSize -= 1; nWitnessCacheSize -= 1;
// TODO: If nWitnessCache is zero, we need to regenerate the caches (#1302) // TODO: If nWitnessCache is zero, we need to regenerate the caches (#1302)
assert(nWitnessCacheSize > 0); assert(nWitnessCacheSize > 0);