Map sync testing framework.

This commit is contained in:
JoelKatz
2012-01-31 12:29:18 -08:00
parent a7bdb81d3d
commit ca26a22314
2 changed files with 78 additions and 0 deletions

View File

@@ -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<unsigned char>& rawNode);
bool operator==(const SHAMap& s) { return getHash()==s.getHash(); }
static bool TestSHAMap();
bool deepCompare(SHAMap& other);
virtual void dump();
};

View File

@@ -110,3 +110,77 @@ bool SHAMap::addKnownNode(const SHAMapNode& node, const std::vector<unsigned cha
// WRITEME
return true;
}
bool SHAMap::deepCompare(SHAMap& other)
{ // Intended for debug/test only
std::stack<SHAMapInnerNode::pointer> 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;
}