From ca26a2231486b238cf4ff6f63dae7bf681777b92 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Tue, 31 Jan 2012 12:29:18 -0800 Subject: [PATCH] Map sync testing framework. --- SHAMap.h | 4 +++ SHAMapSync.cpp | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/SHAMap.h b/SHAMap.h index d6b929bb75..4be2fd870a 100644 --- a/SHAMap.h +++ b/SHAMap.h @@ -266,6 +266,7 @@ public: bool addItem(const SHAMapItem& i); bool updateItem(const SHAMapItem& i); SHAMapItem getItem(const uint256& id); + uint256 getHash() const { return root->getNodeHash(); } uint256 getHash() { return root->getNodeHash(); } // save a copy if you have a temporary anyway @@ -302,7 +303,10 @@ public: // overloads for backed maps bool fetchNode(const uint256& hash, std::vector& rawNode); + bool operator==(const SHAMap& s) { return getHash()==s.getHash(); } + static bool TestSHAMap(); + bool deepCompare(SHAMap& other); virtual void dump(); }; diff --git a/SHAMapSync.cpp b/SHAMapSync.cpp index b3025cfa72..750b4121cb 100644 --- a/SHAMapSync.cpp +++ b/SHAMapSync.cpp @@ -110,3 +110,77 @@ bool SHAMap::addKnownNode(const SHAMapNode& node, const std::vector stack; + SHAMapInnerNode::pointer node=root; + + while(node) + { + SHAMapInnerNode::pointer node=stack.top(); + stack.pop(); + + SHAMapInnerNode::pointer otherNode; + if(node->isRoot()) otherNode=other.root; + else otherNode=other.getInner(*node, node->getNodeHash(), false); + + if(!otherNode) + { + std::cerr << "unable to fetch node" << std::endl; + return false; + } + else if(otherNode->getNodeHash()!=node->getNodeHash()) + { + std::cerr << "node hash mismatch" << std::endl; + return false; + } + +#ifdef DEBUG + std::cerr << "Comparing inner nodes " << node->getString() << std::endl; +#endif + + for(int i=0; i<32; i++) + { + if(node->isEmptyBranch(i)) + { + if(!otherNode->isEmptyBranch(i)) + return false; + } + else + { + if(node->isChildLeaf()) + { // + SHAMapLeafNode::pointer leaf=getLeaf(node->getChildNodeID(i), node->getChildHash(i), false); + if(!leaf) + { + std::cerr << "unable to fetch leaf" << std::endl; + return false; + } + SHAMapLeafNode::pointer otherLeaf=other.getLeaf(*leaf, leaf->getNodeHash(), false); + if(!otherLeaf) + { + std::cerr << "unable to fetch other leaf" << std::endl; + return false; + } + if(leaf->getNodeHash()!=otherLeaf->getNodeHash()) + { + std::cerr << "leaf hash mismatch" << std::endl; + return false; + } + } + else + { // do we have this inner node? + SHAMapInnerNode::pointer next=getInner(node->getChildNodeID(i), node->getChildHash(i), false); + if(!next) + { + std::cerr << "unable to fetch inner node" << std::endl; + return false; + } + stack.push(next); + } + } + } + } + return true; +}