diff --git a/src/Ledger.h b/src/Ledger.h index d059f01581..7e2ebb9782 100644 --- a/src/Ledger.h +++ b/src/Ledger.h @@ -140,9 +140,8 @@ public: boost::posix_time::ptime getCloseTime() const; // low level functions - SHAMap::pointer peekTransactionMap() { return mTransactionMap; } - SHAMap::pointer peekAccountStateMap() { return mAccountStateMap; } - Ledger::pointer snapShot(bool isMutable); + SHAMap::ref peekTransactionMap() { return mTransactionMap; } + SHAMap::ref peekAccountStateMap() { return mAccountStateMap; } // ledger sync functions void setAcquiring(void); diff --git a/src/SHAMap.h b/src/SHAMap.h index e7f5046d5c..c239bad307 100644 --- a/src/SHAMap.h +++ b/src/SHAMap.h @@ -23,10 +23,6 @@ class SHAMap; class SHAMapNode { // Identifies a node in a SHA256 hash -public: - typedef boost::shared_ptr pointer; - typedef const boost::shared_ptr& ref; - private: static uint256 smMasks[65]; // AND with hash to get node id @@ -84,7 +80,8 @@ inline std::ostream& operator<<(std::ostream& out, const SHAMapNode& node) { ret class SHAMapItem { // an item stored in a SHAMap public: - typedef boost::shared_ptr pointer; + typedef boost::shared_ptr pointer; + typedef const boost::shared_ptr& ref; private: uint256 mTag; @@ -136,7 +133,8 @@ class SHAMapTreeNode : public SHAMapNode friend class SHAMap; public: - typedef boost::shared_ptr pointer; + typedef boost::shared_ptr pointer; + typedef const boost::shared_ptr& ref; enum TNType { @@ -163,7 +161,7 @@ private: public: SHAMapTreeNode(uint32 seq, const SHAMapNode& nodeID); // empty node SHAMapTreeNode(const SHAMapTreeNode& node, uint32 seq); // copy node from older tree - SHAMapTreeNode(const SHAMapNode& nodeID, const SHAMapItem::pointer& item, TNType type, uint32 seq); + SHAMapTreeNode(const SHAMapNode& nodeID, SHAMapItem::ref item, TNType type, uint32 seq); // raw node functions SHAMapTreeNode(const SHAMapNode& id, const std::vector& data, uint32 seq, SHANodeFormat format); @@ -200,7 +198,7 @@ public: // item node function bool hasItem() const { return !!mItem; } - SHAMapItem::pointer peekItem() { return mItem; } + SHAMapItem::ref peekItem() { return mItem; } SHAMapItem::pointer getItem() const; bool setItem(const SHAMapItem::pointer& i, TNType type); const uint256& getTag() const { return mItem->getTag(); } @@ -292,7 +290,7 @@ protected: SHAMapItem::pointer onlyBelow(SHAMapTreeNode*); void eraseChildren(SHAMapTreeNode::pointer); - bool walkBranch(SHAMapTreeNode* node, SHAMapItem::pointer otherMapItem, bool isFirstMap, + bool walkBranch(SHAMapTreeNode* node, SHAMapItem::ref otherMapItem, bool isFirstMap, SHAMapDiff& differences, int& maxCount); public: @@ -321,8 +319,8 @@ public: uint256 getHash() { return root->getNodeHash(); } // save a copy if you have a temporary anyway - bool updateGiveItem(const SHAMapItem::pointer&, bool isTransaction, bool hasMeta); - bool addGiveItem(const SHAMapItem::pointer&, bool isTransaction, bool hasMeta); + bool updateGiveItem(SHAMapItem::ref, bool isTransaction, bool hasMeta); + bool addGiveItem(SHAMapItem::ref, bool isTransaction, bool hasMeta); // save a copy if you only need a temporary SHAMapItem::pointer peekItem(const uint256& id); diff --git a/src/SHAMapDiff.cpp b/src/SHAMapDiff.cpp index 3f5adb3e0d..fa1ae6a642 100644 --- a/src/SHAMapDiff.cpp +++ b/src/SHAMapDiff.cpp @@ -20,13 +20,15 @@ class SHAMapDiffNode mNodeID(id), mOurHash(ourHash), mOtherHash(otherHash) { ; } }; -bool SHAMap::walkBranch(SHAMapTreeNode* node, SHAMapItem::pointer otherMapItem, bool isFirstMap, +bool SHAMap::walkBranch(SHAMapTreeNode* node, SHAMapItem::ref otherMapItem, bool isFirstMap, SHAMapDiff& differences, int& maxCount) { // Walk a branch of a SHAMap that's matched by an empty branch or single item in the other map std::stack nodeStack; nodeStack.push(node); - + + bool emptyBranch = !otherMapItem; + while (!nodeStack.empty()) { SHAMapTreeNode* node = nodeStack.top(); @@ -41,7 +43,7 @@ bool SHAMap::walkBranch(SHAMapTreeNode* node, SHAMapItem::pointer otherMapItem, { // This is a leaf node, process its item SHAMapItem::pointer item = node->getItem(); - if (otherMapItem && (otherMapItem->getTag() < item->getTag())) + if (!emptyBranch && (otherMapItem->getTag() < item->getTag())) { // this item comes after the item from the other map, so add the other item if (isFirstMap) // this is first map, so other item is from second differences.insert(std::make_pair(otherMapItem->getTag(), @@ -49,11 +51,12 @@ bool SHAMap::walkBranch(SHAMapTreeNode* node, SHAMapItem::pointer otherMapItem, else differences.insert(std::make_pair(otherMapItem->getTag(), std::make_pair(otherMapItem, SHAMapItem::pointer()))); - if (--maxCount <= 0) return false; - otherMapItem = SHAMapItem::pointer(); + if (--maxCount <= 0) + return false; + emptyBranch = true; } - if ((!otherMapItem) || (item->getTag() < otherMapItem->getTag())) + if (emptyBranch || (item->getTag() < otherMapItem->getTag())) { // unmatched if (isFirstMap) differences.insert(std::make_pair(item->getTag(), std::make_pair(item, SHAMapItem::pointer()))); @@ -77,7 +80,7 @@ bool SHAMap::walkBranch(SHAMapTreeNode* node, SHAMapItem::pointer otherMapItem, } } - if (otherMapItem) + if (!emptyBranch) { // otherMapItem was unmatched, must add if (isFirstMap) // this is first map, so other item is from second differences.insert(std::make_pair(otherMapItem->getTag(),