diff --git a/SHAMap.cpp b/SHAMap.cpp index 650c71c18..90a249f23 100644 --- a/SHAMap.cpp +++ b/SHAMap.cpp @@ -118,6 +118,29 @@ SHAMapLeafNode::pointer SHAMap::walkToLeaf(const uint256& id, bool create, bool return returnLeaf(ln, modify); } +SHAMapInnerNode::pointer SHAMap::walkTo(const SHAMapNode& id) +{ // walk down to this ID, as far as possible + SHAMapInnerNode::pointer inNode=root; + int i=0; + do + { + int branch=inNode->selectBranch(id.getNodeID()); + if(branch<0) // somehow we got on the wrong branch + throw SHAMapException(InvalidNode); + if (inNode->isEmptyBranch(branch)) // we know no branches below this one + return inNode; + if(!inNode->isChildLeaf()) // this is the last inner node + return inNode; + + SHAMapInnerNode::pointer next=getInner(inNode->getChildNodeID(branch), inNode->getChildHash(branch), false); + if(!next) // we don't have the next node + return inNode; + assert(next->getDepth() == (inNode->getDepth()+1)); + inNode=next; + assert(i++ < SHAMapNode::leafDepth); + } while(1); +} + SHAMapLeafNode::pointer SHAMap::getLeaf(const SHAMapNode& id, const uint256& hash, bool modify) { // retrieve a leaf whose node hash is known assert(!!hash); diff --git a/SHAMap.h b/SHAMap.h index 4be2fd870..b249eb268 100644 --- a/SHAMap.h +++ b/SHAMap.h @@ -234,6 +234,7 @@ protected: SHAMapLeafNode::pointer returnLeaf(SHAMapLeafNode::pointer leaf, bool modify); SHAMapInnerNode::pointer getInner(const SHAMapNode& id, const uint256& hash, bool modify); SHAMapInnerNode::pointer returnNode(SHAMapInnerNode::pointer node, bool modify); + SHAMapInnerNode::pointer walkTo(const SHAMapNode& id); SHAMapItem::pointer firstBelow(SHAMapInnerNode::pointer); SHAMapItem::pointer lastBelow(SHAMapInnerNode::pointer); @@ -289,6 +290,7 @@ public: void getMissingNodes(std::vector& nodeIDs, std::vector& hashes, int max); bool getNodeFat(const SHAMapNode& node, std::vector& nodeIDs, std::list >& rawNode); + bool addRootNode(const uint256& hash, const std::vector& rootNode); bool addKnownNode(const SHAMapNode& nodeID, const std::vector& rawNode); // caution: otherMap must be accessed only by this function