Rename nullifier caches and maps to indicate sprout nullifiers

This commit is contained in:
Eirik Ogilvie-Wigley
2018-04-25 11:26:38 -06:00
parent cab341e1a8
commit 9669920f75
11 changed files with 43 additions and 38 deletions

View File

@@ -52,7 +52,7 @@ bool CCoinsView::BatchWrite(CCoinsMap &mapCoins,
const uint256 &hashBlock, const uint256 &hashBlock,
const uint256 &hashAnchor, const uint256 &hashAnchor,
CAnchorsMap &mapAnchors, CAnchorsMap &mapAnchors,
CNullifiersMap &mapNullifiers, CNullifiersMap &mapSproutNullifiers,
CNullifiersMap &mapSaplingNullifiers) { return false; } CNullifiersMap &mapSaplingNullifiers) { return false; }
bool CCoinsView::GetStats(CCoinsStats &stats) const { return false; } bool CCoinsView::GetStats(CCoinsStats &stats) const { return false; }
@@ -70,8 +70,8 @@ bool CCoinsViewBacked::BatchWrite(CCoinsMap &mapCoins,
const uint256 &hashBlock, const uint256 &hashBlock,
const uint256 &hashAnchor, const uint256 &hashAnchor,
CAnchorsMap &mapAnchors, CAnchorsMap &mapAnchors,
CNullifiersMap &mapNullifiers, CNullifiersMap &mapSproutNullifiers,
CNullifiersMap &mapSaplingNullifiers) { return base->BatchWrite(mapCoins, hashBlock, hashAnchor, mapAnchors, mapNullifiers, mapSaplingNullifiers); } CNullifiersMap &mapSaplingNullifiers) { return base->BatchWrite(mapCoins, hashBlock, hashAnchor, mapAnchors, mapSproutNullifiers, mapSaplingNullifiers); }
bool CCoinsViewBacked::GetStats(CCoinsStats &stats) const { return base->GetStats(stats); } bool CCoinsViewBacked::GetStats(CCoinsStats &stats) const { return base->GetStats(stats); }
CCoinsKeyHasher::CCoinsKeyHasher() : salt(GetRandHash()) {} CCoinsKeyHasher::CCoinsKeyHasher() : salt(GetRandHash()) {}
@@ -86,7 +86,7 @@ CCoinsViewCache::~CCoinsViewCache()
size_t CCoinsViewCache::DynamicMemoryUsage() const { size_t CCoinsViewCache::DynamicMemoryUsage() const {
return memusage::DynamicUsage(cacheCoins) + return memusage::DynamicUsage(cacheCoins) +
memusage::DynamicUsage(cacheAnchors) + memusage::DynamicUsage(cacheAnchors) +
memusage::DynamicUsage(cacheNullifiers) + memusage::DynamicUsage(cacheSproutNullifiers) +
memusage::DynamicUsage(cacheSaplingNullifiers) + memusage::DynamicUsage(cacheSaplingNullifiers) +
cachedCoinsUsage; cachedCoinsUsage;
} }
@@ -137,7 +137,7 @@ bool CCoinsViewCache::GetNullifier(const uint256 &nullifier, NullifierType type)
CNullifiersMap* cacheToUse; CNullifiersMap* cacheToUse;
switch (type) { switch (type) {
case SPROUT_NULLIFIER: case SPROUT_NULLIFIER:
cacheToUse = &cacheNullifiers; cacheToUse = &cacheSproutNullifiers;
break; break;
case SAPLING_NULLIFIER: case SAPLING_NULLIFIER:
cacheToUse = &cacheSaplingNullifiers; cacheToUse = &cacheSaplingNullifiers;
@@ -213,7 +213,7 @@ void CCoinsViewCache::PopAnchor(const uint256 &newrt) {
void CCoinsViewCache::SetNullifiers(const CTransaction& tx, bool spent) { void CCoinsViewCache::SetNullifiers(const CTransaction& tx, bool spent) {
for (const JSDescription &joinsplit : tx.vjoinsplit) { for (const JSDescription &joinsplit : tx.vjoinsplit) {
for (const uint256 &nullifier : joinsplit.nullifiers) { for (const uint256 &nullifier : joinsplit.nullifiers) {
std::pair<CNullifiersMap::iterator, bool> ret = cacheNullifiers.insert(std::make_pair(nullifier, CNullifiersCacheEntry())); std::pair<CNullifiersMap::iterator, bool> ret = cacheSproutNullifiers.insert(std::make_pair(nullifier, CNullifiersCacheEntry()));
ret.first->second.entered = spent; ret.first->second.entered = spent;
ret.first->second.flags |= CNullifiersCacheEntry::DIRTY; ret.first->second.flags |= CNullifiersCacheEntry::DIRTY;
} }
@@ -316,7 +316,7 @@ bool CCoinsViewCache::BatchWrite(CCoinsMap &mapCoins,
const uint256 &hashBlockIn, const uint256 &hashBlockIn,
const uint256 &hashAnchorIn, const uint256 &hashAnchorIn,
CAnchorsMap &mapAnchors, CAnchorsMap &mapAnchors,
CNullifiersMap &mapNullifiers, CNullifiersMap &mapSproutNullifiers,
CNullifiersMap &mapSaplingNullifiers) { CNullifiersMap &mapSaplingNullifiers) {
assert(!hasModifier); assert(!hasModifier);
for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end();) { for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end();) {
@@ -379,7 +379,7 @@ bool CCoinsViewCache::BatchWrite(CCoinsMap &mapCoins,
mapAnchors.erase(itOld); mapAnchors.erase(itOld);
} }
::BatchWriteNullifiers(mapNullifiers, cacheNullifiers); ::BatchWriteNullifiers(mapSproutNullifiers, cacheSproutNullifiers);
::BatchWriteNullifiers(mapSaplingNullifiers, cacheSaplingNullifiers); ::BatchWriteNullifiers(mapSaplingNullifiers, cacheSaplingNullifiers);
hashAnchor = hashAnchorIn; hashAnchor = hashAnchorIn;
@@ -388,10 +388,10 @@ bool CCoinsViewCache::BatchWrite(CCoinsMap &mapCoins,
} }
bool CCoinsViewCache::Flush() { bool CCoinsViewCache::Flush() {
bool fOk = base->BatchWrite(cacheCoins, hashBlock, hashAnchor, cacheAnchors, cacheNullifiers, cacheSaplingNullifiers); bool fOk = base->BatchWrite(cacheCoins, hashBlock, hashAnchor, cacheAnchors, cacheSproutNullifiers, cacheSaplingNullifiers);
cacheCoins.clear(); cacheCoins.clear();
cacheAnchors.clear(); cacheAnchors.clear();
cacheNullifiers.clear(); cacheSproutNullifiers.clear();
cacheSaplingNullifiers.clear(); cacheSaplingNullifiers.clear();
cachedCoinsUsage = 0; cachedCoinsUsage = 0;
return fOk; return fOk;

View File

@@ -351,7 +351,7 @@ public:
const uint256 &hashBlock, const uint256 &hashBlock,
const uint256 &hashAnchor, const uint256 &hashAnchor,
CAnchorsMap &mapAnchors, CAnchorsMap &mapAnchors,
CNullifiersMap &mapNullifiers, CNullifiersMap &mapSproutNullifiers,
CNullifiersMap &mapSaplingNullifiers); CNullifiersMap &mapSaplingNullifiers);
//! Calculate statistics about the unspent transaction output set //! Calculate statistics about the unspent transaction output set
@@ -381,7 +381,7 @@ public:
const uint256 &hashBlock, const uint256 &hashBlock,
const uint256 &hashAnchor, const uint256 &hashAnchor,
CAnchorsMap &mapAnchors, CAnchorsMap &mapAnchors,
CNullifiersMap &mapNullifiers, CNullifiersMap &mapSproutNullifiers,
CNullifiersMap &mapSaplingNullifiers); CNullifiersMap &mapSaplingNullifiers);
bool GetStats(CCoinsStats &stats) const; bool GetStats(CCoinsStats &stats) const;
}; };
@@ -425,7 +425,7 @@ protected:
mutable CCoinsMap cacheCoins; mutable CCoinsMap cacheCoins;
mutable uint256 hashAnchor; mutable uint256 hashAnchor;
mutable CAnchorsMap cacheAnchors; mutable CAnchorsMap cacheAnchors;
mutable CNullifiersMap cacheNullifiers; mutable CNullifiersMap cacheSproutNullifiers;
mutable CNullifiersMap cacheSaplingNullifiers; mutable CNullifiersMap cacheSaplingNullifiers;
/* Cached dynamic memory usage for the inner CCoins objects. */ /* Cached dynamic memory usage for the inner CCoins objects. */
@@ -447,7 +447,7 @@ public:
const uint256 &hashBlock, const uint256 &hashBlock,
const uint256 &hashAnchor, const uint256 &hashAnchor,
CAnchorsMap &mapAnchors, CAnchorsMap &mapAnchors,
CNullifiersMap &mapNullifiers, CNullifiersMap &mapSproutNullifiers,
CNullifiersMap &mapSaplingNullifiers); CNullifiersMap &mapSaplingNullifiers);
@@ -459,7 +459,7 @@ public:
// the new current root. // the new current root.
void PopAnchor(const uint256 &rt); void PopAnchor(const uint256 &rt);
// Marks a nullifiers for a given transaction as spent or not. // Marks nullifiers for a given transaction as spent or not.
void SetNullifiers(const CTransaction& tx, bool spent); void SetNullifiers(const CTransaction& tx, bool spent);
/** /**

View File

@@ -56,7 +56,7 @@ public:
const uint256 &hashBlock, const uint256 &hashBlock,
const uint256 &hashAnchor, const uint256 &hashAnchor,
CAnchorsMap &mapAnchors, CAnchorsMap &mapAnchors,
CNullifiersMap &mapNullifiers, CNullifiersMap &mapSproutNullifiers,
CNullifiersMap &mapSaplingNullifiers) { CNullifiersMap &mapSaplingNullifiers) {
return false; return false;
} }

View File

@@ -51,7 +51,7 @@ public:
const uint256 &hashBlock, const uint256 &hashBlock,
const uint256 &hashAnchor, const uint256 &hashAnchor,
CAnchorsMap &mapAnchors, CAnchorsMap &mapAnchors,
CNullifiersMap &mapNullifiers, CNullifiersMap &mapSproutNullifiers,
CNullifiersMap saplingNullifiersMap) { CNullifiersMap saplingNullifiersMap) {
return false; return false;
} }

View File

@@ -1270,13 +1270,15 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa
} }
BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) { BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) {
BOOST_FOREACH(const uint256 &nf, joinsplit.nullifiers) { BOOST_FOREACH(const uint256 &nf, joinsplit.nullifiers) {
if (pool.nullifierExists(nf, SPROUT_NULLIFIER)) if (pool.nullifierExists(nf, SPROUT_NULLIFIER)) {
return false; return false;
}
} }
} }
for (const SpendDescription &spendDescription : tx.vShieldedSpend) { for (const SpendDescription &spendDescription : tx.vShieldedSpend) {
if (pool.nullifierExists(spendDescription.nullifier, SAPLING_NULLIFIER)) if (pool.nullifierExists(spendDescription.nullifier, SAPLING_NULLIFIER)) {
return false; return false;
}
} }
} }

View File

@@ -28,7 +28,7 @@ class CCoinsViewTest : public CCoinsView
uint256 hashBestAnchor_; uint256 hashBestAnchor_;
std::map<uint256, CCoins> map_; std::map<uint256, CCoins> map_;
std::map<uint256, ZCIncrementalMerkleTree> mapAnchors_; std::map<uint256, ZCIncrementalMerkleTree> mapAnchors_;
std::map<uint256, bool> mapNullifiers_; std::map<uint256, bool> mapSproutNullifiers_;
std::map<uint256, bool> mapSaplingNullifiers_; std::map<uint256, bool> mapSaplingNullifiers_;
public: public:
@@ -57,7 +57,7 @@ public:
const std::map<uint256, bool>* mapToUse; const std::map<uint256, bool>* mapToUse;
switch (type) { switch (type) {
case SPROUT_NULLIFIER: case SPROUT_NULLIFIER:
mapToUse = &mapNullifiers_; mapToUse = &mapSproutNullifiers_;
break; break;
case SAPLING_NULLIFIER: case SAPLING_NULLIFIER:
mapToUse = &mapSaplingNullifiers_; mapToUse = &mapSaplingNullifiers_;
@@ -116,7 +116,7 @@ public:
const uint256& hashBlock, const uint256& hashBlock,
const uint256& hashAnchor, const uint256& hashAnchor,
CAnchorsMap& mapAnchors, CAnchorsMap& mapAnchors,
CNullifiersMap& mapNullifiers, CNullifiersMap& mapSproutNullifiers,
CNullifiersMap& mapSaplingNullifiers) CNullifiersMap& mapSaplingNullifiers)
{ {
for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end(); ) { for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end(); ) {
@@ -139,7 +139,7 @@ public:
mapAnchors.erase(it++); mapAnchors.erase(it++);
} }
BatchWriteNullifiers(mapNullifiers, mapNullifiers_); BatchWriteNullifiers(mapSproutNullifiers, mapSproutNullifiers_);
BatchWriteNullifiers(mapSaplingNullifiers, mapSaplingNullifiers_); BatchWriteNullifiers(mapSaplingNullifiers, mapSaplingNullifiers_);
mapCoins.clear(); mapCoins.clear();
@@ -162,7 +162,7 @@ public:
// Manually recompute the dynamic usage of the whole data, and compare it. // Manually recompute the dynamic usage of the whole data, and compare it.
size_t ret = memusage::DynamicUsage(cacheCoins) + size_t ret = memusage::DynamicUsage(cacheCoins) +
memusage::DynamicUsage(cacheAnchors) + memusage::DynamicUsage(cacheAnchors) +
memusage::DynamicUsage(cacheNullifiers) + memusage::DynamicUsage(cacheSproutNullifiers) +
memusage::DynamicUsage(cacheSaplingNullifiers); memusage::DynamicUsage(cacheSaplingNullifiers);
for (CCoinsMap::iterator it = cacheCoins.begin(); it != cacheCoins.end(); it++) { for (CCoinsMap::iterator it = cacheCoins.begin(); it != cacheCoins.end(); it++) {
ret += it->second.coins.DynamicMemoryUsage(); ret += it->second.coins.DynamicMemoryUsage();

View File

@@ -109,7 +109,7 @@ bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins,
const uint256 &hashBlock, const uint256 &hashBlock,
const uint256 &hashAnchor, const uint256 &hashAnchor,
CAnchorsMap &mapAnchors, CAnchorsMap &mapAnchors,
CNullifiersMap &mapNullifiers, CNullifiersMap &mapSproutNullifiers,
CNullifiersMap &mapSaplingNullifiers) { CNullifiersMap &mapSaplingNullifiers) {
CDBBatch batch(db); CDBBatch batch(db);
size_t count = 0; size_t count = 0;
@@ -140,7 +140,7 @@ bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins,
mapAnchors.erase(itOld); mapAnchors.erase(itOld);
} }
::BatchWriteNullifiers(batch, mapNullifiers, DB_NULLIFIER); ::BatchWriteNullifiers(batch, mapSproutNullifiers, DB_NULLIFIER);
::BatchWriteNullifiers(batch, mapSaplingNullifiers, DB_SAPLING_NULLIFIER); ::BatchWriteNullifiers(batch, mapSaplingNullifiers, DB_SAPLING_NULLIFIER);
if (!hashBlock.IsNull()) if (!hashBlock.IsNull())

View File

@@ -45,7 +45,7 @@ public:
const uint256 &hashBlock, const uint256 &hashBlock,
const uint256 &hashAnchor, const uint256 &hashAnchor,
CAnchorsMap &mapAnchors, CAnchorsMap &mapAnchors,
CNullifiersMap &mapNullifiers, CNullifiersMap &mapSproutNullifiers,
CNullifiersMap &mapSaplingNullifiers); CNullifiersMap &mapSaplingNullifiers);
bool GetStats(CCoinsStats &stats) const; bool GetStats(CCoinsStats &stats) const;
}; };

View File

@@ -107,7 +107,7 @@ bool CTxMemPool::addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry,
mapNextTx[tx.vin[i].prevout] = CInPoint(&tx, i); mapNextTx[tx.vin[i].prevout] = CInPoint(&tx, i);
BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) { BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) {
BOOST_FOREACH(const uint256 &nf, joinsplit.nullifiers) { BOOST_FOREACH(const uint256 &nf, joinsplit.nullifiers) {
mapNullifiers[nf] = &tx; mapSproutNullifiers[nf] = &tx;
} }
} }
for (const SpendDescription &spendDescription : tx.vShieldedSpend) { for (const SpendDescription &spendDescription : tx.vShieldedSpend) {
@@ -121,6 +121,7 @@ bool CTxMemPool::addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry,
return true; return true;
} }
void CTxMemPool::remove(const CTransaction &origTx, std::list<CTransaction>& removed, bool fRecursive) void CTxMemPool::remove(const CTransaction &origTx, std::list<CTransaction>& removed, bool fRecursive)
{ {
// Remove transaction from memory pool // Remove transaction from memory pool
@@ -159,7 +160,7 @@ void CTxMemPool::remove(const CTransaction &origTx, std::list<CTransaction>& rem
mapNextTx.erase(txin.prevout); mapNextTx.erase(txin.prevout);
BOOST_FOREACH(const JSDescription& joinsplit, tx.vjoinsplit) { BOOST_FOREACH(const JSDescription& joinsplit, tx.vjoinsplit) {
BOOST_FOREACH(const uint256& nf, joinsplit.nullifiers) { BOOST_FOREACH(const uint256& nf, joinsplit.nullifiers) {
mapNullifiers.erase(nf); mapSproutNullifiers.erase(nf);
} }
} }
for (const SpendDescription &spendDescription : tx.vShieldedSpend) { for (const SpendDescription &spendDescription : tx.vShieldedSpend) {
@@ -248,11 +249,12 @@ void CTxMemPool::removeConflicts(const CTransaction &tx, std::list<CTransaction>
BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) { BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) {
BOOST_FOREACH(const uint256 &nf, joinsplit.nullifiers) { BOOST_FOREACH(const uint256 &nf, joinsplit.nullifiers) {
std::map<uint256, const CTransaction*>::iterator it = mapNullifiers.find(nf); std::map<uint256, const CTransaction*>::iterator it = mapSproutNullifiers.find(nf);
if (it != mapNullifiers.end()) { if (it != mapSproutNullifiers.end()) {
const CTransaction &txConflict = *it->second; const CTransaction &txConflict = *it->second;
if (txConflict != tx) if (txConflict != tx) {
remove(txConflict, removed, true); remove(txConflict, removed, true);
}
} }
} }
} }
@@ -260,8 +262,9 @@ void CTxMemPool::removeConflicts(const CTransaction &tx, std::list<CTransaction>
std::map<uint256, const CTransaction*>::iterator it = mapSaplingNullifiers.find(spendDescription.nullifier); std::map<uint256, const CTransaction*>::iterator it = mapSaplingNullifiers.find(spendDescription.nullifier);
if (it != mapSaplingNullifiers.end()) { if (it != mapSaplingNullifiers.end()) {
const CTransaction &txConflict = *it->second; const CTransaction &txConflict = *it->second;
if (txConflict != tx) if (txConflict != tx) {
remove(txConflict, removed, true); remove(txConflict, removed, true);
}
} }
} }
} }
@@ -461,7 +464,7 @@ void CTxMemPool::checkNullifiers(NullifierType type) const
const std::map<uint256, const CTransaction*>* mapToUse; const std::map<uint256, const CTransaction*>* mapToUse;
switch (type) { switch (type) {
case SPROUT_NULLIFIER: case SPROUT_NULLIFIER:
mapToUse = &mapNullifiers; mapToUse = &mapSproutNullifiers;
break; break;
case SAPLING_NULLIFIER: case SAPLING_NULLIFIER:
mapToUse = &mapSaplingNullifiers; mapToUse = &mapSaplingNullifiers;
@@ -583,7 +586,7 @@ bool CTxMemPool::nullifierExists(const uint256& nullifier, NullifierType type) c
{ {
switch (type) { switch (type) {
case SPROUT_NULLIFIER: case SPROUT_NULLIFIER:
return mapNullifiers.count(nullifier); return mapSproutNullifiers.count(nullifier);
case SAPLING_NULLIFIER: case SAPLING_NULLIFIER:
return mapSaplingNullifiers.count(nullifier); return mapSaplingNullifiers.count(nullifier);
default: default:

View File

@@ -131,7 +131,7 @@ private:
uint64_t totalTxSize = 0; //! sum of all mempool tx' byte sizes uint64_t totalTxSize = 0; //! sum of all mempool tx' byte sizes
uint64_t cachedInnerUsage; //! sum of dynamic memory usage of all the map elements (NOT the maps themselves) uint64_t cachedInnerUsage; //! sum of dynamic memory usage of all the map elements (NOT the maps themselves)
std::map<uint256, const CTransaction*> mapNullifiers; std::map<uint256, const CTransaction*> mapSproutNullifiers;
std::map<uint256, const CTransaction*> mapSaplingNullifiers; std::map<uint256, const CTransaction*> mapSaplingNullifiers;
void checkNullifiers(NullifierType type) const; void checkNullifiers(NullifierType type) const;

View File

@@ -382,7 +382,7 @@ public:
const uint256 &hashBlock, const uint256 &hashBlock,
const uint256 &hashAnchor, const uint256 &hashAnchor,
CAnchorsMap &mapAnchors, CAnchorsMap &mapAnchors,
CNullifiersMap &mapNullifiers, CNullifiersMap &mapSproutNullifiers,
CNullifiersMap& mapSaplingNullifiers) { CNullifiersMap& mapSaplingNullifiers) {
return false; return false;
} }