A better way to convert between uint160 and uint256 types.

This commit is contained in:
JoelKatz
2011-12-15 16:52:50 -08:00
parent 56484476a9
commit 8083b677e2
5 changed files with 24 additions and 27 deletions

View File

@@ -36,4 +36,18 @@ bool u160ToHuman(uint160& buf, std::string& retStr)
{ {
retStr=EncodeBase58(buf.begin(),buf.end()); retStr=EncodeBase58(buf.begin(),buf.end());
return(true); return(true);
} }
uint160 uint256::to160() const
{
uint160 m;
memcpy(&m, this, sizeof(uint160));
return m;
}
uint256 uint160::to256() const
{
uint256 m;
memcpy(&m, this, sizeof(this));
return m;
}

View File

@@ -67,7 +67,7 @@ AccountState::pointer Ledger::getAccountState(const uint160& accountID)
std::cerr << "Ledger:getAccountState(" << accountID.GetHex() << ")" << std::endl; std::cerr << "Ledger:getAccountState(" << accountID.GetHex() << ")" << std::endl;
#endif #endif
ScopedLock l(mTransactionMap->Lock()); ScopedLock l(mTransactionMap->Lock());
SHAMapItem::pointer item=mAccountStateMap->peekItem(uint160to256(accountID)); SHAMapItem::pointer item=mAccountStateMap->peekItem(accountID.to256());
if(!item) if(!item)
{ {
#ifdef DEBUG #ifdef DEBUG
@@ -81,7 +81,7 @@ AccountState::pointer Ledger::getAccountState(const uint160& accountID)
uint64 Ledger::getBalance(const uint160& accountID) uint64 Ledger::getBalance(const uint160& accountID)
{ {
ScopedLock l(mTransactionMap->Lock()); ScopedLock l(mTransactionMap->Lock());
SHAMapItem::pointer item=mAccountStateMap->peekItem(uint160to256(accountID)); SHAMapItem::pointer item=mAccountStateMap->peekItem(accountID.to256());
if(!item) return 0; if(!item) return 0;
return AccountState(item->getData()).getBalance(); return AccountState(item->getData()).getBalance();
} }

View File

@@ -178,7 +178,7 @@ SHAMapItem::SHAMapItem(const uint256& tag, const std::vector<unsigned char>& dat
{ ; } { ; }
SHAMapItem::SHAMapItem(const uint160& tag, const std::vector<unsigned char>& data) SHAMapItem::SHAMapItem(const uint160& tag, const std::vector<unsigned char>& data)
: mTag(uint160to256(tag)), mData(data) : mTag(tag.to256()), mData(data)
{ ; } { ; }
SHAMapItem::pointer SHAMap::peekFirstItem() SHAMapItem::pointer SHAMap::peekFirstItem()

View File

@@ -278,8 +278,8 @@ public:
SHAMapItem::pointer peekNextItem(const uint256&); SHAMapItem::pointer peekNextItem(const uint256&);
SHAMapItem::pointer peekPrevItem(const uint256&); SHAMapItem::pointer peekPrevItem(const uint256&);
SHAMapItem::pointer peekPrevItem(const uint160& u) { return peekPrevItem(uint160to256(u)); } SHAMapItem::pointer peekPrevItem(const uint160& u) { return peekPrevItem(u.to256()); }
SHAMapItem::pointer peekNextItem(const uint160& u) { return peekNextItem(uint160to256(u)); } SHAMapItem::pointer peekNextItem(const uint160& u) { return peekNextItem(u.to256()); }
// comparison/sync functions // comparison/sync functions
void getMissingNodes(std::vector<SHAMapNode>& nodeHashes, int max); void getMissingNodes(std::vector<SHAMapNode>& nodeHashes, int max);

View File

@@ -468,6 +468,8 @@ public:
else else
*this = 0; *this = 0;
} }
uint256 to256() const;
}; };
inline bool operator==(const uint160& a, uint64 b) { return (base_uint160)a == b; } inline bool operator==(const uint160& a, uint64 b) { return (base_uint160)a == b; }
@@ -582,6 +584,8 @@ public:
else else
*this = 0; *this = 0;
} }
uint160 to160() const;
}; };
@@ -634,27 +638,6 @@ inline const uint256 operator|(const uint256& a, const uint256& b) { return (b
inline const uint256 operator+(const uint256& a, const uint256& b) { return (base_uint256)a + (base_uint256)b; } inline const uint256 operator+(const uint256& a, const uint256& b) { return (base_uint256)a + (base_uint256)b; }
inline const uint256 operator-(const uint256& a, const uint256& b) { return (base_uint256)a - (base_uint256)b; } inline const uint256 operator-(const uint256& a, const uint256& b) { return (base_uint256)a - (base_uint256)b; }
static uint256 uint160to256(const uint160& u)
{
uint256 m;
memcpy(&m, &u, sizeof(u));
return m;
}
static uint160 uint256to160(const uint256& u)
{
uint160 m;
memcpy(&m, &u, sizeof(m));
return m;
}
inline int Testuint256AdHoc(std::vector<std::string> vArg) inline int Testuint256AdHoc(std::vector<std::string> vArg)
{ {