From 9f97307f999ff4aa4527ac3f7266ad70f9812664 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Sun, 14 Oct 2012 21:32:38 -0700 Subject: [PATCH] Add 'walkMap' function. --- src/SHAMap.h | 2 ++ src/SHAMapDiff.cpp | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/SHAMap.h b/src/SHAMap.h index f144b5ca9..079ddf0c0 100644 --- a/src/SHAMap.h +++ b/src/SHAMap.h @@ -400,6 +400,8 @@ public: static std::vector checkTrustedPath(const uint256& ledgerHash, const uint256& leafIndex, const std::list >& path); + void walkMap(std::vector& missingNodes, int maxMissing); + bool deepCompare(SHAMap& other); virtual void dump(bool withHashes = false); }; diff --git a/src/SHAMapDiff.cpp b/src/SHAMapDiff.cpp index d0e78e8f9..05619df21 100644 --- a/src/SHAMapDiff.cpp +++ b/src/SHAMapDiff.cpp @@ -186,3 +186,38 @@ bool SHAMap::compare(SHAMap::ref otherMap, SHAMapDiff& differences, int maxCount return true; } + +void SHAMap::walkMap(std::vector& missingNodes, int maxMissing) +{ + std::stack nodeStack; + + boost::recursive_mutex::scoped_lock sl(mLock); + + if (!root->isInner()) // root is only node, and we have it + return; + + nodeStack.push(root); + + while (!nodeStack.empty()) + { + SHAMapTreeNode::pointer node = nodeStack.top(); + nodeStack.pop(); + + for (int i = 0; i < 16; ++i) + if (!node->isEmptyBranch(i)) + { + try + { + SHAMapTreeNode::pointer d = getNode(node->getChildNodeID(i), node->getChildHash(i), false); + if (d->isInner()) + nodeStack.push(d); + } + catch (SHAMapMissingNode& n) + { + missingNodes.push_back(n); + if (--maxMissing <= 0) + return; + } + } + } +}