Generalize the PushAnchor implementation behavior.

This commit is contained in:
Sean Bowe
2018-04-27 16:52:33 -06:00
parent 9ea4e387b2
commit 7703a673ea
2 changed files with 31 additions and 6 deletions

View File

@@ -160,10 +160,17 @@ bool CCoinsViewCache::GetNullifier(const uint256 &nullifier, ShieldedType type)
return tmp; return tmp;
} }
void CCoinsViewCache::PushSproutAnchor(const ZCIncrementalMerkleTree &tree) { template<typename Tree, typename Cache, typename CacheIterator, typename CacheEntry>
void CCoinsViewCache::AbstractPushAnchor(
const Tree &tree,
ShieldedType type,
Cache &cacheAnchors,
uint256 &hash
)
{
uint256 newrt = tree.root(); uint256 newrt = tree.root();
auto currentRoot = GetBestAnchor(SPROUT); auto currentRoot = GetBestAnchor(type);
// We don't want to overwrite an anchor we already have. // We don't want to overwrite an anchor we already have.
// This occurs when a block doesn't modify mapSproutAnchors at all, // This occurs when a block doesn't modify mapSproutAnchors at all,
@@ -171,22 +178,31 @@ void CCoinsViewCache::PushSproutAnchor(const ZCIncrementalMerkleTree &tree) {
// different way (make all blocks modify mapSproutAnchors somehow) // different way (make all blocks modify mapSproutAnchors somehow)
// but this is simpler to reason about. // but this is simpler to reason about.
if (currentRoot != newrt) { if (currentRoot != newrt) {
auto insertRet = cacheSproutAnchors.insert(std::make_pair(newrt, CAnchorsSproutCacheEntry())); auto insertRet = cacheAnchors.insert(std::make_pair(newrt, CacheEntry()));
CAnchorsSproutMap::iterator ret = insertRet.first; CacheIterator ret = insertRet.first;
ret->second.entered = true; ret->second.entered = true;
ret->second.tree = tree; ret->second.tree = tree;
ret->second.flags = CAnchorsSproutCacheEntry::DIRTY; ret->second.flags = CacheEntry::DIRTY;
if (insertRet.second) { if (insertRet.second) {
// An insert took place // An insert took place
cachedCoinsUsage += ret->second.tree.DynamicMemoryUsage(); cachedCoinsUsage += ret->second.tree.DynamicMemoryUsage();
} }
hashSproutAnchor = newrt; hash = newrt;
} }
} }
void CCoinsViewCache::PushSproutAnchor(const ZCIncrementalMerkleTree &tree) {
AbstractPushAnchor<ZCIncrementalMerkleTree, CAnchorsSproutMap, CAnchorsSproutMap::iterator, CAnchorsSproutCacheEntry>(
tree,
SPROUT,
cacheSproutAnchors,
hashSproutAnchor
);
}
template<typename Tree, typename Cache, typename CacheEntry> template<typename Tree, typename Cache, typename CacheEntry>
void CCoinsViewCache::AbstractPopAnchor( void CCoinsViewCache::AbstractPopAnchor(
const uint256 &newrt, const uint256 &newrt,

View File

@@ -533,6 +533,15 @@ private:
Cache &cacheAnchors, Cache &cacheAnchors,
uint256 &hash uint256 &hash
); );
//! Generalized interface for pushing anchors
template<typename Tree, typename Cache, typename CacheIterator, typename CacheEntry>
void AbstractPushAnchor(
const Tree &tree,
ShieldedType type,
Cache &cacheAnchors,
uint256 &hash
);
}; };
#endif // BITCOIN_COINS_H #endif // BITCOIN_COINS_H