mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
Start to flesh out this code.
This commit is contained in:
41
SHAMap.cpp
Normal file
41
SHAMap.cpp
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
#include "BitcoinUtil.h"
|
||||||
|
#include "SHAMap.h"
|
||||||
|
|
||||||
|
bool SHAMapNodeID::operator<(const SHAMapNodeID &s) const
|
||||||
|
{
|
||||||
|
if(s.mDepth<mDepth) return true;
|
||||||
|
if(s.mDepth>mDepth) return false;
|
||||||
|
return mNodeID<s.mNodeID;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SHAMapNodeID::operator>(const SHAMapNodeID &s) const
|
||||||
|
{
|
||||||
|
if(s.mDepth<mDepth) return false;
|
||||||
|
if(s.mDepth>mDepth) return true;
|
||||||
|
return mNodeID>s.mNodeID;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SHAMapNodeID::operator<=(const SHAMapNodeID &s) const
|
||||||
|
{
|
||||||
|
if(s.mDepth<mDepth) return true;
|
||||||
|
if(s.mDepth>mDepth) return false;
|
||||||
|
return mNodeID<=s.mNodeID;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SHAMapNodeID::operator>=(const SHAMapNodeID &s) const
|
||||||
|
{
|
||||||
|
if(s.mDepth<mDepth) return false;
|
||||||
|
if(s.mDepth>mDepth) 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);
|
||||||
|
}
|
||||||
|
|
||||||
40
SHAMap.h
40
SHAMap.h
@@ -1,7 +1,12 @@
|
|||||||
#ifndef __SHAMAP__
|
#ifndef __SHAMAP__
|
||||||
#define __SHAMAP__
|
#define __SHAMAP__
|
||||||
|
|
||||||
|
#include <list>
|
||||||
|
|
||||||
|
#include <boost/shared_ptr.hpp>
|
||||||
|
|
||||||
#include "uint256.h"
|
#include "uint256.h"
|
||||||
|
#include "ScopedLock.h"
|
||||||
|
|
||||||
class SHAMapNodeID
|
class SHAMapNodeID
|
||||||
{
|
{
|
||||||
@@ -9,16 +14,19 @@ public:
|
|||||||
int mDepth;
|
int mDepth;
|
||||||
uint256 mNodeID;
|
uint256 mNodeID;
|
||||||
|
|
||||||
bool operator<(const Transaction &) const;
|
bool operator<(const SHAMapNodeID &) const;
|
||||||
bool operator>(const Transaction &) const;
|
bool operator>(const SHAMapNodeID &) const;
|
||||||
bool operator==(const Transaction &) const;
|
bool operator==(const SHAMapNodeID &) const;
|
||||||
bool operator!=(const Transaction &) const;
|
bool operator!=(const SHAMapNodeID &) const;
|
||||||
bool operator<=(const Transaction &) const;
|
bool operator<=(const SHAMapNodeID &) const;
|
||||||
bool operator>=(const Transaction &) const;
|
bool operator>=(const SHAMapNodeID &) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
class SHAMapLeafNode
|
class SHAMapLeafNode
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
typedef boost::shared_ptr<SHAMapLeafNode> pointer;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint256 mNodeID;
|
uint256 mNodeID;
|
||||||
std::list<uint256> mHashes;
|
std::list<uint256> mHashes;
|
||||||
@@ -36,15 +44,16 @@ public:
|
|||||||
bool DelHash(const uint256& Hash);
|
bool DelHash(const uint256& Hash);
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef boost::shared_ptr<SHAMapLeafNode> pointer;
|
|
||||||
|
|
||||||
class SHAMapInnerNode
|
class SHAMapInnerNode
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
typedef boost::shared_ptr<SHAMapInnerNode> pointer;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int mDepth;
|
int mDepth;
|
||||||
uint256 mNodeID;
|
uint256 mNodeID;
|
||||||
uint256 mHash;
|
uint256 mHash;
|
||||||
uint256[32] mHashes;
|
uint256 mHashes[32];
|
||||||
|
|
||||||
public:
|
public:
|
||||||
SHAMapInnerNode(int Depth, const uint256 &NodeID);
|
SHAMapInnerNode(int Depth, const uint256 &NodeID);
|
||||||
@@ -63,21 +72,24 @@ public:
|
|||||||
void SetChildHash(const SHAMapLeafNode &mLeaf);
|
void SetChildHash(const SHAMapLeafNode &mLeaf);
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef boost::shared_ptr<SHAMapInnerNode> pointer;
|
|
||||||
|
|
||||||
// Not all nodes need to be resident in memory, the class can hold subsets
|
// 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
|
// This class does not handle any inter-node logic because that requires reads/writes
|
||||||
|
|
||||||
class SHAMap
|
class SHAMap
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
typedef boost::shared_ptr<SHAMap> pointer;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
boost::mutex mLock;
|
mutable boost::mutex mLock;
|
||||||
std::map<SHAMapNodeID, SHAMapLeafNode> mLeaves
|
std::map<SHAMapNodeID, SHAMapLeafNode> mLeaves;
|
||||||
std::multimap<SHAMapNodeID, SHAMapInnerNode> mInnerNodes;
|
std::multimap<SHAMapNodeID, SHAMapInnerNode> mInnerNodes;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
SHAMap();
|
SHAMap();
|
||||||
|
|
||||||
|
ScopedLock Lock() const { return ScopedLock(mLock); }
|
||||||
|
|
||||||
bool HasInnerNode(int m, const uint160 &nodeID);
|
bool HasInnerNode(int m, const uint160 &nodeID);
|
||||||
bool GiveInnerNode(SHAMapInnerNode::pointer);
|
bool GiveInnerNode(SHAMapInnerNode::pointer);
|
||||||
SHAMapInnerNode::pointer GetInnerNode(int m, const uint160 &nodeID);
|
SHAMapInnerNode::pointer GetInnerNode(int m, const uint160 &nodeID);
|
||||||
@@ -86,3 +98,5 @@ public:
|
|||||||
bool GiveLeafNode(SHAMapLeafNode::pointer);
|
bool GiveLeafNode(SHAMapLeafNode::pointer);
|
||||||
SHAMapLeafNode::pointer GetLeafNode(int m, const uint160 &nodeID);
|
SHAMapLeafNode::pointer GetLeafNode(int m, const uint160 &nodeID);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user