From 7703a673ea73301027988d77d8a70cb613501cb5 Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Fri, 27 Apr 2018 16:52:33 -0600 Subject: [PATCH] Generalize the PushAnchor implementation behavior. --- src/coins.cpp | 28 ++++++++++++++++++++++------ src/coins.h | 9 +++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/coins.cpp b/src/coins.cpp index a6c43ce6b..7f400ed44 100644 --- a/src/coins.cpp +++ b/src/coins.cpp @@ -160,10 +160,17 @@ bool CCoinsViewCache::GetNullifier(const uint256 &nullifier, ShieldedType type) return tmp; } -void CCoinsViewCache::PushSproutAnchor(const ZCIncrementalMerkleTree &tree) { +template +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( + tree, + SPROUT, + cacheSproutAnchors, + hashSproutAnchor + ); +} + template void CCoinsViewCache::AbstractPopAnchor( const uint256 &newrt, diff --git a/src/coins.h b/src/coins.h index f121ed2e7..b8c41e5d7 100644 --- a/src/coins.h +++ b/src/coins.h @@ -533,6 +533,15 @@ private: Cache &cacheAnchors, uint256 &hash ); + + //! Generalized interface for pushing anchors + template + void AbstractPushAnchor( + const Tree &tree, + ShieldedType type, + Cache &cacheAnchors, + uint256 &hash + ); }; #endif // BITCOIN_COINS_H