mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
Continue fleshing out these classes.
This commit is contained in:
73
SHAMap.h
73
SHAMap.h
@@ -15,7 +15,7 @@ class SHAMap;
|
||||
// The trees are designed for rapid synchronization and compression of differences
|
||||
|
||||
|
||||
class SHAMapNodeID
|
||||
class SHAMapNode
|
||||
{ // Identified a node in a SHA256 hash
|
||||
private:
|
||||
static uint256 smMasks[11]; // AND with hash to get node id
|
||||
@@ -29,32 +29,33 @@ public:
|
||||
static const int rootDepth=0;
|
||||
static const int leafDepth=10;
|
||||
|
||||
SHAMapNodeID(int depth, const uint256 &hash);
|
||||
SHAMapNode(int depth, const uint256 &hash);
|
||||
int getDepth() { return mDepth; }
|
||||
const uint256 &getNodeID();
|
||||
|
||||
bool isRoot() const { return mDepth==0; }
|
||||
bool isLeaf() const { return mDepth==leafDepth; }
|
||||
bool isInner() const { return !isRoot() && !isLeaf(); }
|
||||
SHAMapNodeID getParentNodeID() { return SHAMapNodeID(mDepth-1, mNodeID); }
|
||||
SHAMapNodeID getChildNodeID(int m);
|
||||
virtual bool IsPopulated(void) const { return false; }
|
||||
SHAMapNode getParentNodeID() { return SHAMapNode(mDepth-1, mNodeID); }
|
||||
SHAMapNode getChildNodeID(int m);
|
||||
|
||||
int selectBranch(const uint256 &hash);
|
||||
|
||||
static uint256 getNodeID(int depth, const uint256 &hash);
|
||||
|
||||
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;
|
||||
bool operator<(const SHAMapNode &) const;
|
||||
bool operator>(const SHAMapNode &) const;
|
||||
bool operator==(const SHAMapNode &) const;
|
||||
bool operator!=(const SHAMapNode &) const;
|
||||
bool operator<=(const SHAMapNode &) const;
|
||||
bool operator>=(const SHAMapNode &) const;
|
||||
|
||||
static void ClassInit(void);
|
||||
static void ClassInit();
|
||||
};
|
||||
|
||||
|
||||
class SHAMapLeafNode
|
||||
class SHAMapLeafNode : public SHAMapNode
|
||||
{
|
||||
friend class SHAMap;
|
||||
|
||||
@@ -62,26 +63,29 @@ public:
|
||||
typedef boost::shared_ptr<SHAMapLeafNode> pointer;
|
||||
|
||||
private:
|
||||
SHAMapNodeID mNodeID;
|
||||
uint256 mHash;
|
||||
std::list<uint256> mHashes;
|
||||
|
||||
void updateHash(void);
|
||||
void updateHash();
|
||||
|
||||
protected:
|
||||
bool hasHash(const uint256 &hash) const;
|
||||
bool addHash(const uint256 &hash);
|
||||
bool delHash(const uint256 &hash);
|
||||
|
||||
public:
|
||||
SHAMapLeafNode(const uint256& NodeID);
|
||||
SHAMapLeafNode(const SHAMapNode& nodeID);
|
||||
|
||||
virtual bool IsPopulated(void) const { return true; }
|
||||
|
||||
const SHAMapNodeID& GetNodeID(void) const { return mNodeID; }
|
||||
const uint256& GetNodeHash() const { return mHash; }
|
||||
bool isEmpty() const { return mHashes.empty(); }
|
||||
int getHashCount() const { return mHashes.size(); }
|
||||
const uint256& GetHash(int m) const;
|
||||
bool hasHash(const uint256 &hash) const;
|
||||
|
||||
};
|
||||
|
||||
class SHAMapInnerNode
|
||||
class SHAMapInnerNode : public SHAMapNode
|
||||
{
|
||||
friend class SHAMap;
|
||||
|
||||
@@ -89,20 +93,22 @@ public:
|
||||
typedef boost::shared_ptr<SHAMapInnerNode> pointer;
|
||||
|
||||
private:
|
||||
SHAMapNodeID mNodeID;
|
||||
uint256 mHash;
|
||||
uint256 mHashes[32];
|
||||
|
||||
void updateHash(void);
|
||||
void updateHash();
|
||||
|
||||
protected:
|
||||
void SetChildHash(int m, const uint256 &hash);
|
||||
|
||||
public:
|
||||
SHAMapInnerNode(int Depth, const uint256 &NodeID);
|
||||
const SHAMapNodeID& GetNodeID() const { return mNodeID; }
|
||||
|
||||
|
||||
virtual bool IsPopulated(void) const { return true; }
|
||||
const uint256& GetNodeHash() const { return mHash; }
|
||||
const uint256& GetChildHash(int m) const;
|
||||
bool isEmpty() const;
|
||||
};
|
||||
|
||||
|
||||
@@ -114,8 +120,9 @@ public:
|
||||
|
||||
private:
|
||||
mutable boost::mutex mLock;
|
||||
std::map<SHAMapNodeID, SHAMapLeafNode> mLeaves;
|
||||
std::multimap<SHAMapNodeID, SHAMapInnerNode> mInnerNodes;
|
||||
std::map<SHAMapNode, SHAMapLeafNode> mLeafByID;
|
||||
std::map<SHAMapNode, SHAMapInnerNode> mInnerNodeByID;
|
||||
std::map<uint256, SHAMapNode> mNodeByHash; // includes nodes not present
|
||||
|
||||
public:
|
||||
SHAMap();
|
||||
@@ -123,18 +130,18 @@ public:
|
||||
ScopedLock Lock() const { return ScopedLock(mLock); }
|
||||
|
||||
// inner node access functions
|
||||
bool HasInnerNode(const SHAMapNodeID &id);
|
||||
bool HasInnerNode(const SHAMapNode &id);
|
||||
bool GiveInnerNode(SHAMapInnerNode::pointer);
|
||||
SHAMapInnerNode::pointer GetInnerNode(const SHAMapNodeID &);
|
||||
SHAMapInnerNode::pointer GetInnerNode(const SHAMapNode &);
|
||||
|
||||
// leaf node access functions
|
||||
bool HasLeafNode(const SHAMapNodeID &id);
|
||||
bool HasLeafNode(const SHAMapNode &id);
|
||||
bool GiveLeafNode(SHAMapLeafNode::pointer);
|
||||
SHAMapLeafNode::pointer GetLeafNode(const SHAMapNodeID &);
|
||||
SHAMapLeafNode::pointer GetLeafNode(const SHAMapNode &);
|
||||
|
||||
// generic node functions
|
||||
std::vector<unsigned char> getRawNode(const SHAMapNodeID &id);
|
||||
bool addRawNode(const SHAMapNodeID& nodeID, std::vector<unsigned char> rawNode);
|
||||
std::vector<unsigned char> getRawNode(const SHAMapNode &id);
|
||||
bool addRawNode(const SHAMapNode& nodeID, std::vector<unsigned char> rawNode);
|
||||
|
||||
// normal hash access functions
|
||||
bool hasHash(const uint256 &hash);
|
||||
@@ -145,9 +152,15 @@ public:
|
||||
bool nextHash(uint256 &hash);
|
||||
bool prevHash(uint256 &hash);
|
||||
|
||||
// special overloads for backed maps
|
||||
// comparison/sync functions
|
||||
void getMissingNodes(std::vector<SHAMapNode> &nodeHashes, int max);
|
||||
void getMissingObjects(std::vector<uint256> &objectHashes, int max);
|
||||
bool addKnownNode(const std::vector<unsigned char>& rawNode);
|
||||
|
||||
// overloads for backed maps
|
||||
virtual bool fetchNode(const uint256 &hash, std::vector<unsigned char>& rawNode);
|
||||
virtual bool writeNode(const uint256 &hash, const std::vector<unsigned char>& rawNode);
|
||||
virtual bool haveObject(const uint256 &hash);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user