diff --git a/src/SHAMap.cpp b/src/SHAMap.cpp index 93f355c99..8a2c7cc33 100644 --- a/src/SHAMap.cpp +++ b/src/SHAMap.cpp @@ -12,7 +12,7 @@ SHAMap::SHAMap(uint32 seq) : mSeq(seq), mState(Modifying) { - root = boost::make_shared(SHAMapNode(0, uint256()), mSeq); + root = boost::make_shared(mSeq, SHAMapNode(0, uint256())); root->makeInner(); mTNByID[*root] = root; } @@ -88,6 +88,7 @@ SHAMapTreeNode::pointer SHAMap::checkCacheNode(const SHAMapNode& iNode) SHAMapTreeNode::pointer SHAMap::walkTo(const uint256& id, bool modify) { // walk down to the terminal node for this ID + SHAMapTreeNode::pointer inNode = root; while (!inNode->isLeaf()) @@ -323,10 +324,10 @@ SHAMapItem::pointer SHAMap::peekPrevItem(const uint256& id) else for(int i=node->selectBranch(id)-1; i>=0; i--) if(!node->isEmptyBranch(i)) { - node=getNode(node->getChildNodeID(i), node->getChildHash(i), false); + node = getNode(node->getChildNodeID(i), node->getChildHash(i), false); if(!node) throw SHAMapException(MissingNode); - SHAMapItem::pointer item=firstBelow(node); - if(!item) throw SHAMapException(MissingNode); + SHAMapItem::pointer item = firstBelow(node); + if (!item) throw SHAMapException(MissingNode); return item; } } @@ -337,8 +338,8 @@ SHAMapItem::pointer SHAMap::peekPrevItem(const uint256& id) SHAMapItem::pointer SHAMap::peekItem(const uint256& id) { boost::recursive_mutex::scoped_lock sl(mLock); - SHAMapTreeNode::pointer leaf=walkTo(id, false); - if(!leaf) return SHAMapItem::pointer(); + SHAMapTreeNode::pointer leaf = walkTo(id, false); + if (!leaf) return SHAMapItem::pointer(); return leaf->peekItem(); } @@ -433,7 +434,7 @@ bool SHAMap::addGiveItem(SHAMapItem::pointer item, bool isTransaction) std::stack stack = getStack(tag, true); if (stack.empty()) throw SHAMapException(MissingNode); - SHAMapTreeNode::pointer node=stack.top(); + SHAMapTreeNode::pointer node = stack.top(); stack.pop(); if (node->isLeaf() && (node->peekItem()->getTag() == tag)) @@ -464,9 +465,10 @@ bool SHAMap::addGiveItem(SHAMapItem::pointer item, bool isTransaction) { // this is a leaf node that has to be made an inner node holding two items #ifdef ST_DEBUG std::cerr << "aGI leaf " << node->getString() << std::endl; + std::cerr << "Existing: " << node->peekItem()->getTag().GetHex() << std::endl; #endif SHAMapItem::pointer otherItem = node->peekItem(); - assert(otherItem && (tag != otherItem->getTag()) ); + assert(otherItem && (tag != otherItem->getTag())); node->makeInner(); @@ -475,10 +477,11 @@ bool SHAMap::addGiveItem(SHAMapItem::pointer item, bool isTransaction) while ((b1 = node->selectBranch(tag)) == (b2 = node->selectBranch(otherItem->getTag()))) { // we need a new inner node, since both go on same branch at this level #ifdef ST_DEBUG - std::cerr << "need new inner node at " << node->getDepth() << std::endl; + std::cerr << "need new inner node at " << node->getDepth() << ", " + << b1 << "==" << b2 << std::endl; #endif SHAMapTreeNode::pointer newNode = - boost::make_shared(node->getChildNodeID(b1), mSeq); + boost::make_shared(mSeq, node->getChildNodeID(b1)); newNode->makeInner(); if(!mTNByID.insert(std::make_pair(SHAMapNode(*newNode), newNode)).second) assert(false); @@ -607,8 +610,8 @@ void SHAMap::dump(bool hash) std::cerr << " MAP Contains" << std::endl; boost::recursive_mutex::scoped_lock sl(mLock); - for(boost::unordered_map::iterator it=mTNByID.begin(); - it!=mTNByID.end(); ++it) + for(boost::unordered_map::iterator it = mTNByID.begin(); + it != mTNByID.end(); ++it) { std::cerr << it->second->getString() << std::endl; if(hash) std::cerr << " " << it->second->getNodeHash().GetHex() << std::endl; diff --git a/src/SHAMap.h b/src/SHAMap.h index 35cc4a579..836d4f97a 100644 --- a/src/SHAMap.h +++ b/src/SHAMap.h @@ -153,7 +153,7 @@ private: SHAMapTreeNode& operator=(const SHAMapTreeNode&); // no implementation public: - SHAMapTreeNode(const SHAMapNode& nodeID, uint32 seq); // empty node + SHAMapTreeNode(uint32 seq, const SHAMapNode& nodeID); // empty node SHAMapTreeNode(const SHAMapTreeNode& node, uint32 seq); // copy node from older tree SHAMapTreeNode(const SHAMapNode& nodeID, SHAMapItem::pointer item, TNType type, uint32 seq); diff --git a/src/SHAMapNodes.cpp b/src/SHAMapNodes.cpp index badd100ad..e791e28e8 100644 --- a/src/SHAMapNodes.cpp +++ b/src/SHAMapNodes.cpp @@ -74,18 +74,18 @@ bool SHAMapNode::operator!=(const uint256 &s) const void SHAMapNode::ClassInit() { // set up the depth masks uint256 selector; - for(int i=0; i<64; i+=2) + for(int i = 0; i < 64; i += 2) { - smMasks[i]=selector; - *(selector.begin()+(i/2))=0x0F; - smMasks[i+1]=selector; - *(selector.begin()+(i/2))=0xFF; + smMasks[i] = selector; + *(selector.begin() + (i / 2)) = 0x0F; + smMasks[i + 1]=selector; + *(selector.begin() + (i / 2)) = 0xFF; } } uint256 SHAMapNode::getNodeID(int depth, const uint256& hash) { - assert(depth>=0 && depth<64); + assert(depth >= 0 && depth < 64); return hash & smMasks[depth]; } @@ -134,6 +134,7 @@ int SHAMapNode::selectBranch(const uint256& hash) const assert(false); return -1; } + if ((hash & smMasks[mDepth]) != mNodeID) { std::cerr << "selectBranch(" << getString() << std::endl; @@ -155,15 +156,15 @@ void SHAMapNode::dump() const std::cerr << getString() << std::endl; } -SHAMapTreeNode::SHAMapTreeNode(const SHAMapNode& nodeID, uint32 seq) : SHAMapNode(nodeID), mHash(0), mSeq(seq), +SHAMapTreeNode::SHAMapTreeNode(uint32 seq, const SHAMapNode& nodeID) : SHAMapNode(nodeID), mHash(0), mSeq(seq), mType(tnERROR), mFullBelow(false) { } SHAMapTreeNode::SHAMapTreeNode(const SHAMapTreeNode& node, uint32 seq) : SHAMapNode(node), - mHash(node.mHash), mItem(node.mItem), mSeq(seq), mType(node.mType), mFullBelow(false) + mHash(node.mHash), mSeq(seq), mType(node.mType), mFullBelow(false) { - if(node.mItem) + if (node.mItem) mItem = boost::make_shared(*node.mItem); else memcpy(mHashes, node.mHashes, sizeof(mHashes)); @@ -342,13 +343,19 @@ std::string SHAMapTreeNode::getString() const for(int i = 0; i < 16; ++i) if (!isEmptyBranch(i)) { - ret += ",b"; + ret += "\nb"; ret += boost::lexical_cast(i); + ret += " = "; + ret += mHashes[i].GetHex(); } } if (isLeaf()) { - ret += ",leaf"; + ret += ",leaf\n"; + ret += " Tag="; + ret += getTag().GetHex(); + ret += "\n Hash="; + ret += mHash.GetHex(); } return ret; }