From 9ea4e387b29858ee7bf35d1a11354fc5f0583a88 Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Fri, 27 Apr 2018 16:31:12 -0600 Subject: [PATCH] Generalize the PopAnchor implementation behavior. --- src/coins.cpp | 40 +++++++++++++++++++++++++++++++++------- src/coins.h | 9 +++++++++ 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/coins.cpp b/src/coins.cpp index 29bf04dc8..a6c43ce6b 100644 --- a/src/coins.cpp +++ b/src/coins.cpp @@ -187,8 +187,15 @@ void CCoinsViewCache::PushSproutAnchor(const ZCIncrementalMerkleTree &tree) { } } -void CCoinsViewCache::PopAnchor(const uint256 &newrt) { - auto currentRoot = GetBestAnchor(SPROUT); +template +void CCoinsViewCache::AbstractPopAnchor( + const uint256 &newrt, + ShieldedType type, + Cache &cacheAnchors, + uint256 &hash +) +{ + auto currentRoot = GetBestAnchor(type); // Blocks might not change the commitment tree, in which // case restoring the "old" anchor during a reorg must @@ -197,21 +204,40 @@ void CCoinsViewCache::PopAnchor(const uint256 &newrt) { // Bring the current best anchor into our local cache // so that its tree exists in memory. { - ZCIncrementalMerkleTree tree; - assert(GetSproutAnchorAt(currentRoot, tree)); + Tree tree; + switch (type) { + case SPROUT: + assert(GetSproutAnchorAt(currentRoot, tree)); + break; + case SAPLING: + // TODO + assert(false); + break; + default: + throw std::runtime_error("Unknown shielded type " + type); + } } // Mark the anchor as unentered, removing it from view - cacheSproutAnchors[currentRoot].entered = false; + cacheAnchors[currentRoot].entered = false; // Mark the cache entry as dirty so it's propagated - cacheSproutAnchors[currentRoot].flags = CAnchorsSproutCacheEntry::DIRTY; + cacheAnchors[currentRoot].flags = CacheEntry::DIRTY; // Mark the new root as the best anchor - hashSproutAnchor = newrt; + hash = newrt; } } +void CCoinsViewCache::PopAnchor(const uint256 &newrt) { + AbstractPopAnchor( + newrt, + SPROUT, + cacheSproutAnchors, + hashSproutAnchor + ); +} + void CCoinsViewCache::SetNullifiers(const CTransaction& tx, bool spent) { for (const JSDescription &joinsplit : tx.vjoinsplit) { for (const uint256 &nullifier : joinsplit.nullifiers) { diff --git a/src/coins.h b/src/coins.h index 055320056..f121ed2e7 100644 --- a/src/coins.h +++ b/src/coins.h @@ -524,6 +524,15 @@ private: * By making the copy constructor private, we prevent accidentally using it when one intends to create a cache on top of a base cache. */ CCoinsViewCache(const CCoinsViewCache &); + + //! Generalized interface for popping anchors + template + void AbstractPopAnchor( + const uint256 &newrt, + ShieldedType type, + Cache &cacheAnchors, + uint256 &hash + ); }; #endif // BITCOIN_COINS_H