From 06816dd73eafdb8cff7e2e6fde014c3989da7253 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Thu, 10 Nov 2011 21:15:32 -0800 Subject: [PATCH] The low-level code that manages a tree of hash nodes. This manages the individual nodes but not the logic that connects them. It is designed around the assumption that not all nodes are necessarily resident in memory. --- SHAMap.h | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 SHAMap.h diff --git a/SHAMap.h b/SHAMap.h new file mode 100644 index 0000000000..62d2d1bea3 --- /dev/null +++ b/SHAMap.h @@ -0,0 +1,88 @@ +#ifndef __SHAMAP__ +#define __SHAMAP__ + +#include "uint256.h" + +class SHAMapNodeID +{ +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; +}; + +class SHAMapLeafNode +{ +private: + uint256 mNodeID; + std::list mHashes; + uint256 mNodeHash; + +public: + SHAMapLeafNode(const uint256& NodeID); + + const uint256& GetNodeID(void) const { return mNodeID; } + bool HasHash(const uint256& Hash) const; + + uint256& GetNodeHash(); + + bool AddHash(const uint256& Hash); + bool DelHash(const uint256& Hash); +}; + +typedef boost::shared_ptr pointer; + +class SHAMapInnerNode +{ +private: + int mDepth; + uint256 mNodeID; + uint256 mHash; + uint256[32] mHashes; + +public: + SHAMapInnerNode(int Depth, const uint256 &NodeID); + + int GetDepth() const { return mDepth; } + const uint256& GetNodeID() const { return mNodeID; } + const uint256& GetChildHash(int m) const; + const uint256& GetNodeHash() const; + + bool IsRootNode() const { return mDepth==0; } + bool IsLastInnerNode() const { return mDepth==9; } + + uint256 GetChildNodeID(int m) const; + void SetChildHash(int m, const uint256& Hash); + void SetChildHash(const SHAMapInnerNode &mChild); + 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 +{ +private: + boost::mutex mLock; + std::map mLeaves + std::multimap mInnerNodes; + +public: + SHAMap(); + + bool HasInnerNode(int m, const uint160 &nodeID); + bool GiveInnerNode(SHAMapInnerNode::pointer); + SHAMapInnerNode::pointer GetInnerNode(int m, const uint160 &nodeID); + + bool HasLeafNode(int m, const uint160 &nodeID); + bool GiveLeafNode(SHAMapLeafNode::pointer); + SHAMapLeafNode::pointer GetLeafNode(int m, const uint160 &nodeID); +};