Browse Source

Generalize the PushAnchor implementation behavior.

pull/4/head
Sean Bowe 6 years ago
parent
commit
7703a673ea
  1. 28
      src/coins.cpp
  2. 9
      src/coins.h

28
src/coins.cpp

@ -160,10 +160,17 @@ bool CCoinsViewCache::GetNullifier(const uint256 &nullifier, ShieldedType type)
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();
auto currentRoot = GetBestAnchor(SPROUT);
auto currentRoot = GetBestAnchor(type);
// We don't want to overwrite an anchor we already have.
// 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)
// but this is simpler to reason about.
if (currentRoot != newrt) {
auto insertRet = cacheSproutAnchors.insert(std::make_pair(newrt, CAnchorsSproutCacheEntry()));
CAnchorsSproutMap::iterator ret = insertRet.first;
auto insertRet = cacheAnchors.insert(std::make_pair(newrt, CacheEntry()));
CacheIterator ret = insertRet.first;
ret->second.entered = true;
ret->second.tree = tree;
ret->second.flags = CAnchorsSproutCacheEntry::DIRTY;
ret->second.flags = CacheEntry::DIRTY;
if (insertRet.second) {
// An insert took place
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>
void CCoinsViewCache::AbstractPopAnchor(
const uint256 &newrt,

9
src/coins.h

@ -533,6 +533,15 @@ private:
Cache &cacheAnchors,
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

Loading…
Cancel
Save