From 53b61d4d657a616942475d2bb0d2937f0b962cc2 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Sat, 12 Nov 2011 14:12:38 -0800 Subject: [PATCH] Start to flesh out this code. --- SHAMap.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ SHAMap.h | 40 +++++++++++++++++++++++++++------------- 2 files changed, 68 insertions(+), 13 deletions(-) create mode 100644 SHAMap.cpp diff --git a/SHAMap.cpp b/SHAMap.cpp new file mode 100644 index 0000000000..edb634c928 --- /dev/null +++ b/SHAMap.cpp @@ -0,0 +1,41 @@ +#include "BitcoinUtil.h" +#include "SHAMap.h" + +bool SHAMapNodeID::operator<(const SHAMapNodeID &s) const +{ + if(s.mDepthmDepth) return false; + return mNodeID(const SHAMapNodeID &s) const +{ + if(s.mDepthmDepth) return true; + return mNodeID>s.mNodeID; +} + +bool SHAMapNodeID::operator<=(const SHAMapNodeID &s) const +{ + if(s.mDepthmDepth) return false; + return mNodeID<=s.mNodeID; +} + +bool SHAMapNodeID::operator>=(const SHAMapNodeID &s) const +{ + if(s.mDepthmDepth) return true; + return mNodeID>=s.mNodeID; +} + +bool SHAMapNodeID::operator==(const SHAMapNodeID &s) const +{ + return (s.mDepth==mDepth) && (s.mNodeID==mNodeID); +} + +bool SHAMapNodeID::operator!=(const SHAMapNodeID &s) const +{ + return (s.mDepth!=mDepth) || (s.mNodeID!=mNodeID); +} + diff --git a/SHAMap.h b/SHAMap.h index 62d2d1bea3..79ac96035c 100644 --- a/SHAMap.h +++ b/SHAMap.h @@ -1,7 +1,12 @@ #ifndef __SHAMAP__ #define __SHAMAP__ +#include + +#include + #include "uint256.h" +#include "ScopedLock.h" class SHAMapNodeID { @@ -9,16 +14,19 @@ public: int mDepth; uint256 mNodeID; - bool operator<(const Transaction &) const; - bool operator>(const Transaction &) const; - bool operator==(const Transaction &) const; - bool operator!=(const Transaction &) const; - bool operator<=(const Transaction &) const; - bool operator>=(const Transaction &) const; + bool operator<(const SHAMapNodeID &) const; + bool operator>(const SHAMapNodeID &) const; + bool operator==(const SHAMapNodeID &) const; + bool operator!=(const SHAMapNodeID &) const; + bool operator<=(const SHAMapNodeID &) const; + bool operator>=(const SHAMapNodeID &) const; }; class SHAMapLeafNode { +public: + typedef boost::shared_ptr pointer; + private: uint256 mNodeID; std::list mHashes; @@ -36,15 +44,16 @@ public: bool DelHash(const uint256& Hash); }; -typedef boost::shared_ptr pointer; - class SHAMapInnerNode { +public: + typedef boost::shared_ptr pointer; + private: int mDepth; uint256 mNodeID; uint256 mHash; - uint256[32] mHashes; + uint256 mHashes[32]; public: SHAMapInnerNode(int Depth, const uint256 &NodeID); @@ -63,21 +72,24 @@ public: void SetChildHash(const SHAMapLeafNode &mLeaf); }; -typedef boost::shared_ptr pointer; - // Not all nodes need to be resident in memory, the class can hold subsets // This class does not handle any inter-node logic because that requires reads/writes class SHAMap { +public: + typedef boost::shared_ptr pointer; + private: - boost::mutex mLock; - std::map mLeaves + mutable boost::mutex mLock; + std::map mLeaves; std::multimap mInnerNodes; public: SHAMap(); + ScopedLock Lock() const { return ScopedLock(mLock); } + bool HasInnerNode(int m, const uint160 &nodeID); bool GiveInnerNode(SHAMapInnerNode::pointer); SHAMapInnerNode::pointer GetInnerNode(int m, const uint160 &nodeID); @@ -86,3 +98,5 @@ public: bool GiveLeafNode(SHAMapLeafNode::pointer); SHAMapLeafNode::pointer GetLeafNode(int m, const uint160 &nodeID); }; + +#endif