diff --git a/src/Amount.cpp b/src/Amount.cpp index bdfd1ec0e5..c9c0dcfd60 100644 --- a/src/Amount.cpp +++ b/src/Amount.cpp @@ -16,7 +16,7 @@ bool STAmount::currencyFromString(uint160& uDstCurrency, const std::string& sCur if (sCurrency.empty() || !sCurrency.compare(SYSTEM_CURRENCY_CODE)) { - uDstCurrency = 0; + uDstCurrency.zero(); } else if (3 == sCurrency.size()) { @@ -219,17 +219,22 @@ void STAmount::add(Serializer& s) const if (mIsNative) { assert(mOffset == 0); - if (!mIsNegative) s.add64(mValue | cPosNative); - else s.add64(mValue); - return; + if (!mIsNegative) + s.add64(mValue | cPosNative); + else + s.add64(mValue); + } + else + { + if (isZero()) + s.add64(cNotNative); + else if (mIsNegative) // 512 = not native + s.add64(mValue | (static_cast(mOffset + 512 + 97) << (64 - 10))); + else // 256 = positive + s.add64(mValue | (static_cast(mOffset + 512 + 256 + 97) << (64 - 10))); + + s.add160(mCurrency); } - if (isZero()) - s.add64(cNotNative); - else if (mIsNegative) // 512 = not native - s.add64(mValue | (static_cast(mOffset + 512 + 97) << (64 - 10))); - else // 256 = positive - s.add64(mValue | (static_cast(mOffset + 512 + 256 + 97) << (64 - 10))); - s.add160(mCurrency); } STAmount::STAmount(const char* name, int64 value) : SerializedType(name), mOffset(0), mIsNative(true) @@ -763,20 +768,25 @@ STAmount convertToInternalAmount(uint64 displayAmount, uint64 totalNow, uint64 t return STAmount(name, muldiv(displayAmount, totalNow, totalInit)); } -STAmount STAmount::deSerialize(SerializerIterator& it) +STAmount STAmount::deserialize(SerializerIterator& it) { STAmount *s = construct(it); STAmount ret(*s); + delete s; + return ret; } static STAmount serdes(const STAmount &s) { Serializer ser; + s.add(ser); + SerializerIterator sit(ser); - return STAmount::deSerialize(sit); + + return STAmount::deserialize(sit); } diff --git a/src/LedgerNode.cpp b/src/LedgerNode.cpp index 1e8fa6ff7b..1840e371ea 100644 --- a/src/LedgerNode.cpp +++ b/src/LedgerNode.cpp @@ -46,19 +46,16 @@ LedgerStateParms Ledger::writeBack(LedgerStateParms parms, SerializedLedgerEntry SerializedLedgerEntry::pointer Ledger::getASNode(LedgerStateParms& parms, const uint256& nodeID, LedgerEntryType let ) { -std::cerr << "getASNode>" << nodeID.ToString() << std::endl; SHAMapItem::pointer account = mAccountStateMap->peekItem(nodeID); -std::cerr << "getASNode: d: " << nodeID.ToString() << std::endl; + if (!account) { if ( (parms & lepCREATE) == 0 ) { parms = lepMISSING; -std::cerr << "getASNode: missing: " << nodeID.ToString() << std::endl; return SerializedLedgerEntry::pointer(); } -std::cerr << "getASNode: c: " << nodeID.ToString() << std::endl; parms = parms | lepCREATED | lepOKAY; SerializedLedgerEntry::pointer sle=boost::make_shared(let); sle->setIndex(nodeID); @@ -66,21 +63,16 @@ std::cerr << "getASNode: c: " << nodeID.ToString() << std::endl; return sle; } -std::cerr << "getASNode: a: " << nodeID.ToString() << std::endl; -std::cerr << "getASNode: e: " << strHex(account->peekSerializer().getData()) << std::endl; SerializedLedgerEntry::pointer sle = boost::make_shared(account->peekSerializer(), nodeID); -std::cerr << "getASNode: b: " << nodeID.ToString() << std::endl; if(sle->getType() != let) { // maybe it's a currency or something -std::cerr << "getASNode: wrong type: " << nodeID.ToString() << std::endl; parms = parms | lepWRONGTYPE; return SerializedLedgerEntry::pointer(); } parms = parms | lepOKAY; -std::cerr << "getASNode<" << nodeID.ToString() << std::endl; return sle; @@ -90,17 +82,7 @@ SerializedLedgerEntry::pointer Ledger::getAccountRoot(LedgerStateParms& parms, c { uint256 nodeID=getAccountRootIndex(accountID); - ScopedLock l(mAccountStateMap->Lock()); - - try - { - return getASNode(parms, nodeID, ltACCOUNT_ROOT); - } - catch (...) - { - parms = lepERROR; - return SerializedLedgerEntry::pointer(); - } + return getASNode(parms, nodeID, ltACCOUNT_ROOT); } SerializedLedgerEntry::pointer Ledger::getAccountRoot(LedgerStateParms& parms, const NewcoinAddress& naAccountID) @@ -117,15 +99,7 @@ SerializedLedgerEntry::pointer Ledger::getNickname(LedgerStateParms& parms, cons { ScopedLock l(mAccountStateMap->Lock()); - try - { - return getASNode(parms, nickHash, ltNICKNAME); - } - catch (...) - { - parms = lepERROR; - return SerializedLedgerEntry::pointer(); - } + return getASNode(parms, nickHash, ltNICKNAME); } // @@ -157,15 +131,7 @@ SerializedLedgerEntry::pointer Ledger::getRippleState(LedgerStateParms& parms, c { ScopedLock l(mAccountStateMap->Lock()); - try - { - return getASNode(parms, uNode, ltRIPPLE_STATE); - } - catch (...) - { - parms = lepERROR; - return SerializedLedgerEntry::pointer(); - } + return getASNode(parms, uNode, ltRIPPLE_STATE); } // @@ -200,32 +166,14 @@ SerializedLedgerEntry::pointer Ledger::getDirRoot(LedgerStateParms& parms, const { ScopedLock l(mAccountStateMap->Lock()); - try - { - return getASNode(parms, uRootIndex, ltDIR_ROOT); - } - catch (...) - { - parms = lepERROR; - return SerializedLedgerEntry::pointer(); - } + return getASNode(parms, uRootIndex, ltDIR_ROOT); } SerializedLedgerEntry::pointer Ledger::getDirNode(LedgerStateParms& parms, const uint256& uNodeIndex) { ScopedLock l(mAccountStateMap->Lock()); -std::cerr << "getDirNode: " << uNodeIndex.ToString() << std::endl; - try - { - return getASNode(parms, uNodeIndex, ltDIR_NODE); - } - catch (...) - { -std::cerr << "getDirNode: ERROR: " << uNodeIndex.ToString() << std::endl; - parms = lepERROR; - return SerializedLedgerEntry::pointer(); - } + return getASNode(parms, uNodeIndex, ltDIR_NODE); } // vim:ts=4 diff --git a/src/RPCServer.cpp b/src/RPCServer.cpp index e158c609b3..5ba83adc93 100644 --- a/src/RPCServer.cpp +++ b/src/RPCServer.cpp @@ -382,19 +382,17 @@ Json::Value RPCServer::doAccountLines(Json::Value ¶ms) AccountState::pointer as = mNetOps->getAccountState(uLedger, naAccount); if (as) { + Json::Value jsonLines = Json::Value(Json::objectValue); + ret["account"] = naAccount.humanAccountID(); // We access a committed ledger and need not worry about changes. uint256 uDirLineNodeFirst; uint256 uDirLineNodeLast; + // Ledger::getDirIndex(uBase, letKind, uNodeDir) - if (!mNetOps->getDirLineInfo(uLedger, naAccount, uDirLineNodeFirst, uDirLineNodeLast)) + if (mNetOps->getDirLineInfo(uLedger, naAccount, uDirLineNodeFirst, uDirLineNodeLast)) { - ret["lines"] = Json::Value(Json::objectValue); - } - else - { - Json::Value jsonLines = Json::Value(Json::objectValue); for (; uDirLineNodeFirst <= uDirLineNodeLast; uDirLineNodeFirst++) { @@ -409,27 +407,35 @@ Json::Value RPCServer::doAccountLines(Json::Value ¶ms) RippleState::pointer rsLine = mNetOps->getRippleState(uLedger, uNode); - rsLine->setViewAccount(naAccount); + if (rsLine) + { + rsLine->setViewAccount(naAccount); - naAccountPeer = rsLine->getAccountIDPeer(); - saBalance = rsLine->getBalance(); - saLimit = rsLine->getLimit(); - saLimitPeer = rsLine->getLimitPeer(); + naAccountPeer = rsLine->getAccountIDPeer(); + saBalance = rsLine->getBalance(); + saLimit = rsLine->getLimit(); + saLimitPeer = rsLine->getLimitPeer(); - Json::Value jPeer = Json::Value(Json::objectValue); + Json::Value jPeer = Json::Value(Json::objectValue); - jPeer["balance"] = saBalance.getText(); - jPeer["currency"] = saBalance.getCurrencyHuman(); - jPeer["limit"] = saLimit.getJson(0); - jPeer["limit_peer"] = saLimitPeer.getJson(0); + jPeer["node"] = uNode.ToString(); - ret[naAccountPeer.humanAccountID()] = jPeer; + jPeer["balance"] = saBalance.getText(); + jPeer["currency"] = saBalance.getCurrencyHuman(); + jPeer["limit"] = saLimit.getJson(0); + jPeer["limit_peer"] = saLimitPeer.getJson(0); + + jsonLines[naAccountPeer.humanAccountID()] = jPeer; + } + else + { + std::cerr << "doAccountLines: Bad index: " << uNode.ToString() << std::endl; + } } } - ret["lines"] = jsonLines; - } + ret["lines"] = jsonLines; } else { diff --git a/src/RippleState.cpp b/src/RippleState.cpp index 59bd7dbcab..736a1ecc83 100644 --- a/src/RippleState.cpp +++ b/src/RippleState.cpp @@ -13,6 +13,8 @@ RippleState::RippleState(SerializedLedgerEntry::pointer ledgerEntry) : mLowLimit = mLedgerEntry->getIValueFieldAmount(sfLowLimit); mHighLimit = mLedgerEntry->getIValueFieldAmount(sfHighLimit); + mBalance = mLedgerEntry->getIValueFieldAmount(sfBalance); + // YYY Should never fail. if (mLowID.isValid() && mHighID.isValid()) mValid = true; diff --git a/src/SHAMap.h b/src/SHAMap.h index 9db960b229..cf1afc777d 100644 --- a/src/SHAMap.h +++ b/src/SHAMap.h @@ -80,9 +80,14 @@ class hash_SMN { // These must be randomized for release public: std::size_t operator() (const SHAMapNode& mn) const +#if 0 { return mn.getDepth() ^ static_cast(mn.getNodeID().GetAt(0)); } +#else + { return mn.getDepth() ^ *reinterpret_cast(mn.getNodeID().begin()); } +#endif + std::size_t operator() (const uint256& u) const - { return static_cast(u.GetAt(0)); } + { return *reinterpret_cast(u.begin()); } }; class SHAMapItem @@ -109,18 +114,22 @@ public: void updateData(const std::vector& data) { mData=data; } - bool operator<(const SHAMapItem& i) const { return mTag < i.mTag; } - bool operator>(const SHAMapItem& i) const { return mTag > i.mTag; } bool operator==(const SHAMapItem& i) const { return mTag == i.mTag; } bool operator!=(const SHAMapItem& i) const { return mTag != i.mTag; } - bool operator<=(const SHAMapItem& i) const { return mTag <= i.mTag; } - bool operator>=(const SHAMapItem& i) const { return mTag >= i.mTag; } - bool operator<(const uint256& i) const { return mTag < i; } - bool operator>(const uint256& i) const { return mTag > i; } bool operator==(const uint256& i) const { return mTag == i; } bool operator!=(const uint256& i) const { return mTag != i; } +#if 0 + // This code is comment out because it is unused. It could work. + bool operator<(const SHAMapItem& i) const { return mTag < i.mTag; } + bool operator>(const SHAMapItem& i) const { return mTag > i.mTag; } + bool operator<=(const SHAMapItem& i) const { return mTag <= i.mTag; } + bool operator>=(const SHAMapItem& i) const { return mTag >= i.mTag; } + + bool operator<(const uint256& i) const { return mTag < i; } + bool operator>(const uint256& i) const { return mTag > i; } bool operator<=(const uint256& i) const { return mTag <= i; } bool operator>=(const uint256& i) const { return mTag >= i; } +#endif virtual void dump(); }; diff --git a/src/SHAMapNodes.cpp b/src/SHAMapNodes.cpp index 0547c0257f..0728695f32 100644 --- a/src/SHAMapNodes.cpp +++ b/src/SHAMapNodes.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -13,12 +14,9 @@ std::string SHAMapNode::getString() const { - std::string ret="NodeID("; - ret+=boost::lexical_cast(mDepth); - ret+=","; - ret+=mNodeID.GetHex(); - ret+=")"; - return ret; + return str(boost::format("NodeID(%s,%s)") + % boost::lexical_cast(mDepth) + % mNodeID.GetHex()); } uint256 SHAMapNode::smMasks[64]; @@ -77,7 +75,7 @@ void SHAMapNode::ClassInit() for(int i = 0; i < 64; i += 2) { smMasks[i] = selector; - *(selector.begin() + (i / 2)) = 0x0F; + *(selector.begin() + (i / 2)) = 0xF0; smMasks[i + 1]=selector; *(selector.begin() + (i / 2)) = 0xFF; } @@ -123,13 +121,14 @@ SHAMapNode SHAMapNode::getChildNodeID(int m) const assert((m >= 0) && (m < 16)); uint256 child(mNodeID); - child.PeekAt(mDepth / 8) |= m << (4 * (mDepth % 8)); + + child.begin()[mDepth/2] |= (mDepth & 1) ? m : m << 4; + return SHAMapNode(mDepth + 1, child); } int SHAMapNode::selectBranch(const uint256& hash) const { // Which branch would contain the specified hash - #ifdef DEBUG if (mDepth == 63) { @@ -147,8 +146,10 @@ int SHAMapNode::selectBranch(const uint256& hash) const #endif int branch = *(hash.begin() + (mDepth / 2)); - if (mDepth % 2) branch >>= 4; - else branch &= 0xf; + if (mDepth & 1) + branch &= 0xf; + else + branch >>= 4; assert((branch >= 0) && (branch < 16)); return branch; @@ -259,6 +260,7 @@ void SHAMapTreeNode::addRaw(Serializer &s) for (int i = 0; i < 16; ++i) s.add256(mHashes[i]); + s.add8(2); } diff --git a/src/SerializedTypes.cpp b/src/SerializedTypes.cpp index 6b4170868b..276b13464d 100644 --- a/src/SerializedTypes.cpp +++ b/src/SerializedTypes.cpp @@ -186,10 +186,20 @@ STVector256* STVector256::construct(SerializerIterator& u, const char *name) { std::vector data = u.getVL(); std::vector value; + int count = data.size() / (256 / 8); value.reserve(count); - for(int i = 0; i < count; i++) - value.push_back(uint256(std::vector(&data[i], &data[i + (256 / 8)]))); + + unsigned int uStart = 0; + for (unsigned int i = 0; i != count; i++) + { + unsigned int uEnd = uStart+(256/8); + + value.push_back(uint256(std::vector(&data[uStart], &data[uEnd]))); + + uStart = uEnd; + } + return new STVector256(name, value); } diff --git a/src/SerializedTypes.h b/src/SerializedTypes.h index d55e56bc8b..70e3660866 100644 --- a/src/SerializedTypes.h +++ b/src/SerializedTypes.h @@ -330,7 +330,7 @@ public: friend STAmount convertToInternalAmount(uint64 displayAmount, uint64 totalNow, uint64 totalInit, const char* name = NULL); - static STAmount deSerialize(SerializerIterator&); + static STAmount deserialize(SerializerIterator&); static bool currencyFromString(uint160& uDstCurrency, const std::string& sCurrency); }; diff --git a/src/TransactionEngine.cpp b/src/TransactionEngine.cpp index ba97bfa112..0d1ac5d171 100644 --- a/src/TransactionEngine.cpp +++ b/src/TransactionEngine.cpp @@ -8,7 +8,7 @@ typedef SerializedLedgerEntry SLE; -#define DIR_NODE_MAX 32 +#define DIR_NODE_MAX 1 // We return the uNodeDir so that on delete we can quickly know where the element is mentioned in the directory. TransactionEngineResult TransactionEngine::dirAdd( @@ -63,6 +63,10 @@ TransactionEngineResult TransactionEngine::dirAdd( { // Last node is not full, append. + std::cerr << "dirAdd: appending: PREV: " << svIndexes.peekValue()[0].ToString() << std::endl; + std::cerr << "dirAdd: appending: Node: " << strHex(uNodeDir) << std::endl; + std::cerr << "dirAdd: appending: Entry: " << uLedgerIndex.ToString() << std::endl; + svIndexes.peekValue().push_back(uLedgerIndex); sleNode->setIFieldV256(sfIndexes, svIndexes); diff --git a/src/uint256.h b/src/uint256.h index e18f080864..c87ffcb3ed 100644 --- a/src/uint256.h +++ b/src/uint256.h @@ -14,6 +14,7 @@ #include #include "types.h" +#include "utils.h" #include @@ -33,19 +34,12 @@ class base_uint { protected: enum { WIDTH=BITS/32 }; + + // This is really big-endian in byte order. + // We use unsigned int for speed. unsigned int pn[WIDTH]; + public: - - BIGNUM *toBN() const - { - // Convert to big-endian - unsigned char *be[WIDTH]; - - std::reverse_copy(begin(), end(), be); - - return BN_bin2bn(be, WIDTH, NULL); - } - bool isZero() const { for (int i = 0; i < WIDTH; i++) @@ -61,10 +55,7 @@ public: bool operator!() const { - for (int i = 0; i < WIDTH; i++) - if (pn[i] != 0) - return false; - return true; + return isZero(); } const base_uint operator~() const @@ -75,22 +66,13 @@ public: return ret; } - const base_uint operator-() const - { - base_uint ret; - for (int i = 0; i < WIDTH; i++) - ret.pn[i] = ~pn[i]; - ++ret; - return ret; - } - - base_uint& operator=(uint64 b) { - pn[0] = (unsigned int)b; - pn[1] = (unsigned int)(b >> 32); - for (int i = 2; i < WIDTH; i++) - pn[i] = 0; + zero(); + + // Put in least significant bits. + ((uint64_t *) end())[-1] = htobe64(b); + return *this; } @@ -115,100 +97,13 @@ public: return *this; } - base_uint& operator^=(uint64 b) - { - pn[0] ^= (unsigned int)(b & 0xffffffffu); - pn[1] ^= (unsigned int)(b >> 32); - return *this; - } - - base_uint& operator&=(uint64 b) - { - pn[0] &= (unsigned int)(b & 0xffffffffu); - pn[1] &= (unsigned int)(b >> 32); - return *this; - } - - base_uint& operator|=(uint64 b) - { - pn[0] |= (unsigned int)(b & 0xffffffffu); - pn[1] |= (unsigned int)(b >> 32); - return *this; - } - - base_uint& operator<<=(unsigned int shift) - { - base_uint a(*this); - for (int i = 0; i < WIDTH; i++) - pn[i] = 0; - int k = shift / 32; - shift = shift % 32; - for (int i = 0; i < WIDTH; i++) - { - if (i+k+1 < WIDTH && shift != 0) - pn[i+k+1] |= (a.pn[i] >> (32-shift)); - if (i+k < WIDTH) - pn[i+k] |= (a.pn[i] << shift); - } - return *this; - } - - base_uint& operator>>=(unsigned int shift) - { - base_uint a(*this); - for (int i = 0; i < WIDTH; i++) - pn[i] = 0; - int k = shift / 32; - shift = shift % 32; - for (int i = 0; i < WIDTH; i++) - { - if (i-k-1 >= 0 && shift != 0) - pn[i-k-1] |= (a.pn[i] << (32-shift)); - if (i-k >= 0) - pn[i-k] |= (a.pn[i] >> shift); - } - return *this; - } - - base_uint& operator+=(const base_uint& b) - { - uint64 carry = 0; - for (int i = 0; i < WIDTH; i++) - { - uint64 n = carry + pn[i] + b.pn[i]; - pn[i] = n & 0xffffffff; - carry = n >> 32; - } - return *this; - } - - base_uint& operator-=(const base_uint& b) - { - *this += -b; - return *this; - } - - base_uint& operator+=(uint64 b64) - { - base_uint b(b64); - *this += b; - return *this; - } - - base_uint& operator-=(uint64 b64) - { - base_uint b(b64); - *this += -b; - return *this; - } - - base_uint& operator++() { // prefix operator - int i = 0; - while (++pn[i] == 0 && i < WIDTH-1) - i++; + + for (int i = WIDTH-1; ++pn[i] == 0 && i; i--) + nothing(); + return *this; } @@ -223,9 +118,10 @@ public: base_uint& operator--() { // prefix operator - unsigned i = 0; - while (--pn[i] == -1 && i < WIDTH-1) - i++; + + for (int i = WIDTH-1; --pn[i] == (unsigned int) -1 && i; i--) + nothing(); + return *this; } @@ -237,53 +133,36 @@ public: return ret; } + friend inline int compare(const base_uint& a, const base_uint& b) + { + const unsigned char* pA = a.begin(); + const unsigned char* pAEnd = a.end(); + const unsigned char* pB = b.begin(); + + while (pA != pAEnd && *pA == *pB) + pA++, pB++; + + return pA == pAEnd ? 0 : *pA < *pB ? -1 : *pA > *pB ? 1 : 0; + } friend inline bool operator<(const base_uint& a, const base_uint& b) { - for (int i = base_uint::WIDTH-1; i >= 0; i--) - { - if (a.pn[i] < b.pn[i]) - return true; - else if (a.pn[i] > b.pn[i]) - return false; - } - return false; + return compare(a, b) < 0; } friend inline bool operator<=(const base_uint& a, const base_uint& b) { - for (int i = base_uint::WIDTH-1; i >= 0; i--) - { - if (a.pn[i] < b.pn[i]) - return true; - else if (a.pn[i] > b.pn[i]) - return false; - } - return true; + return compare(a, b) <= 0; } friend inline bool operator>(const base_uint& a, const base_uint& b) { - for (int i = base_uint::WIDTH-1; i >= 0; i--) - { - if (a.pn[i] > b.pn[i]) - return true; - else if (a.pn[i] < b.pn[i]) - return false; - } - return false; + return compare(a, b) > 0; } friend inline bool operator>=(const base_uint& a, const base_uint& b) { - for (int i = base_uint::WIDTH-1; i >= 0; i--) - { - if (a.pn[i] > b.pn[i]) - return true; - else if (a.pn[i] < b.pn[i]) - return false; - } - return true; + return compare(a, b) >= 0; } friend inline bool operator==(const base_uint& a, const base_uint& b) @@ -294,44 +173,14 @@ public: return true; } - friend inline bool operator==(const base_uint& a, uint64 b) - { - if (a.pn[0] != (unsigned int)(b & 0xffffffffu)) - return false; - if (a.pn[1] != (unsigned int)(b >> 32)) - return false; - for (int i = 2; i < base_uint::WIDTH; i++) - if (a.pn[i] != 0) - return false; - return true; - } - friend inline bool operator!=(const base_uint& a, const base_uint& b) { return (!(a == b)); } - friend inline bool operator!=(const base_uint& a, uint64 b) - { - return (!(a == b)); - } - - unsigned int GetAt(int j) const - { - return pn[j]; - } - - unsigned int& PeekAt(int j) - { - return pn[j]; - } - std::string GetHex() const { - char psz[sizeof(pn)*2 + 1]; - for (int i = 0; i < sizeof(pn); i++) - sprintf(psz + i*2, "%02X", ((unsigned char*)pn)[sizeof(pn) - i - 1]); - return std::string(psz, psz + sizeof(pn)*2); + return strHex(begin(), size()); } void SetHex(const char* psz) @@ -353,20 +202,23 @@ public: 0,1,2,3,4,5,6,7,8,9,0,0,0,0,0,0, 0,0xa,0xb,0xc,0xd,0xe,0xf,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0xa,0xb,0xc,0xd,0xe,0xf,0,0,0,0,0,0,0,0,0 }; - const char* pbegin = psz; + const char* pBegin = psz; while (phexdigit[(int) *psz] || *psz == '0') psz++; psz--; - unsigned char* p1 = (unsigned char*)pn; - unsigned char* pend = p1 + WIDTH * 4; - while (psz >= pbegin && p1 < pend) + + unsigned char* pOut = begin(); + + if (psz-pBegin > 2*size()) + psz = pBegin + 2*size(); + + while (pBegin != psz) { - *p1 = phexdigit[(unsigned char)*psz--]; - if (psz >= pbegin) - { - *p1 |= (phexdigit[(unsigned char)*psz--] << 4); - p1++; - } + unsigned char cHigh = phexdigit[(unsigned char) *pBegin++] << 4; + unsigned char cLow = pBegin == psz + ? 0 + : phexdigit[(unsigned char) *pBegin++]; + *pOut++ = cHigh | cLow; } } @@ -382,22 +234,22 @@ public: unsigned char* begin() { - return (unsigned char*)&pn[0]; + return (unsigned char*) &pn[0]; } unsigned char* end() { - return (unsigned char*)&pn[WIDTH]; + return (unsigned char*) &pn[WIDTH]; } const unsigned char* begin() const { - return (const unsigned char*)&pn[0]; + return (const unsigned char*) &pn[0]; } const unsigned char* end() const { - return (unsigned char*)&pn[WIDTH]; + return (unsigned char*) &pn[WIDTH]; } unsigned int size() const @@ -427,7 +279,6 @@ public: s.read((char*)pn, sizeof(pn)); } - friend class uint128; friend class uint160; friend class uint256; @@ -518,18 +369,16 @@ public: uint160(uint64 b) { - pn[0] = (unsigned int)(b & 0xffffffffu); - pn[1] = (unsigned int)(b >> 32); - for (int i = 2; i < WIDTH; i++) - pn[i] = 0; + *this = b; } uint160& operator=(uint64 b) { - pn[0] = (unsigned int)(b & 0xffffffffu); - pn[1] = (unsigned int)(b >> 32); - for (int i = 2; i < WIDTH; i++) - pn[i] = 0; + zero(); + + // Put in least significant bits. + ((uint64_t *) end())[-1] = htobe64(b); + return *this; } @@ -543,7 +392,7 @@ public: if (vch.size() == sizeof(pn)) memcpy(pn, &vch[0], sizeof(pn)); else - *this = 0; + zero(); } base_uint256 to256() const; @@ -551,52 +400,32 @@ public: 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; } -inline const uint160 operator<<(const base_uint160& a, unsigned int shift) { return uint160(a) <<= shift; } -inline const uint160 operator>>(const base_uint160& a, unsigned int shift) { return uint160(a) >>= shift; } -inline const uint160 operator<<(const uint160& a, unsigned int shift) { return uint160(a) <<= shift; } -inline const uint160 operator>>(const uint160& a, unsigned int shift) { return uint160(a) >>= shift; } inline const uint160 operator^(const base_uint160& a, const base_uint160& b) { return uint160(a) ^= b; } inline const uint160 operator&(const base_uint160& a, const base_uint160& b) { return uint160(a) &= b; } inline const uint160 operator|(const base_uint160& a, const base_uint160& b) { return uint160(a) |= b; } -inline const uint160 operator+(const base_uint160& a, const base_uint160& b) { return uint160(a) += b; } -inline const uint160 operator-(const base_uint160& a, const base_uint160& b) { return uint160(a) -= b; } -inline bool operator<(const base_uint160& a, const uint160& b) { return (base_uint160)a < (base_uint160)b; } -inline bool operator<=(const base_uint160& a, const uint160& b) { return (base_uint160)a <= (base_uint160)b; } -inline bool operator>(const base_uint160& a, const uint160& b) { return (base_uint160)a > (base_uint160)b; } -inline bool operator>=(const base_uint160& a, const uint160& b) { return (base_uint160)a >= (base_uint160)b; } inline bool operator==(const base_uint160& a, const uint160& b) { return (base_uint160)a == (base_uint160)b; } inline bool operator!=(const base_uint160& a, const uint160& b) { return (base_uint160)a != (base_uint160)b; } inline const uint160 operator^(const base_uint160& a, const uint160& b) { return (base_uint160)a ^ (base_uint160)b; } inline const uint160 operator&(const base_uint160& a, const uint160& b) { return (base_uint160)a & (base_uint160)b; } inline const uint160 operator|(const base_uint160& a, const uint160& b) { return (base_uint160)a | (base_uint160)b; } -inline const uint160 operator+(const base_uint160& a, const uint160& b) { return (base_uint160)a + (base_uint160)b; } -inline const uint160 operator-(const base_uint160& a, const uint160& b) { return (base_uint160)a - (base_uint160)b; } -inline bool operator<(const uint160& a, const base_uint160& b) { return (base_uint160)a < (base_uint160)b; } -inline bool operator<=(const uint160& a, const base_uint160& b) { return (base_uint160)a <= (base_uint160)b; } -inline bool operator>(const uint160& a, const base_uint160& b) { return (base_uint160)a > (base_uint160)b; } -inline bool operator>=(const uint160& a, const base_uint160& b) { return (base_uint160)a >= (base_uint160)b; } inline bool operator==(const uint160& a, const base_uint160& b) { return (base_uint160)a == (base_uint160)b; } inline bool operator!=(const uint160& a, const base_uint160& b) { return (base_uint160)a != (base_uint160)b; } inline const uint160 operator^(const uint160& a, const base_uint160& b) { return (base_uint160)a ^ (base_uint160)b; } inline const uint160 operator&(const uint160& a, const base_uint160& b) { return (base_uint160)a & (base_uint160)b; } inline const uint160 operator|(const uint160& a, const base_uint160& b) { return (base_uint160)a | (base_uint160)b; } -inline const uint160 operator+(const uint160& a, const base_uint160& b) { return (base_uint160)a + (base_uint160)b; } -inline const uint160 operator-(const uint160& a, const base_uint160& b) { return (base_uint160)a - (base_uint160)b; } - -inline bool operator<(const uint160& a, const uint160& b) { return (base_uint160)a < (base_uint160)b; } -inline bool operator<=(const uint160& a, const uint160& b) { return (base_uint160)a <= (base_uint160)b; } -inline bool operator>(const uint160& a, const uint160& b) { return (base_uint160)a > (base_uint160)b; } -inline bool operator>=(const uint160& a, const uint160& b) { return (base_uint160)a >= (base_uint160)b; } inline bool operator==(const uint160& a, const uint160& b) { return (base_uint160)a == (base_uint160)b; } inline bool operator!=(const uint160& a, const uint160& b) { return (base_uint160)a != (base_uint160)b; } inline const uint160 operator^(const uint160& a, const uint160& b) { return (base_uint160)a ^ (base_uint160)b; } inline const uint160 operator&(const uint160& a, const uint160& b) { return (base_uint160)a & (base_uint160)b; } inline const uint160 operator|(const uint160& a, const uint160& b) { return (base_uint160)a | (base_uint160)b; } -inline const uint160 operator+(const uint160& a, const uint160& b) { return (base_uint160)a + (base_uint160)b; } -inline const uint160 operator-(const uint160& a, const uint160& b) { return (base_uint160)a - (base_uint160)b; } + +inline const std::string strHex(const uint160& ui) +{ + return strHex(ui.begin(), ui.size()); +} ////////////////////////////////////////////////////////////////////////////// // @@ -629,18 +458,16 @@ public: uint256(uint64 b) { - pn[0] = (unsigned int)(b & 0xffffffff); - pn[1] = (unsigned int)(b >> 32); - for (int i = 2; i < WIDTH; i++) - pn[i] = 0; + *this = b; } uint256& operator=(uint64 b) { - pn[0] = (unsigned int)(b & 0xffffffff); - pn[1] = (unsigned int)(b >> 32); - for (int i = 2; i < WIDTH; i++) - pn[i] = 0; + zero(); + + // Put in least significant bits. + ((uint64_t *) end())[-1] = htobe64(b); + return *this; } @@ -656,7 +483,7 @@ public: else { assert(false); - *this = 0; + zero(); } } @@ -666,52 +493,24 @@ public: inline bool operator==(const uint256& a, uint64 b) { return (base_uint256)a == b; } inline bool operator!=(const uint256& a, uint64 b) { return (base_uint256)a != b; } -inline const uint256 operator<<(const base_uint256& a, unsigned int shift) { return uint256(a) <<= shift; } -inline const uint256 operator>>(const base_uint256& a, unsigned int shift) { return uint256(a) >>= shift; } -inline const uint256 operator<<(const uint256& a, unsigned int shift) { return uint256(a) <<= shift; } -inline const uint256 operator>>(const uint256& a, unsigned int shift) { return uint256(a) >>= shift; } - inline const uint256 operator^(const base_uint256& a, const base_uint256& b) { return uint256(a) ^= b; } inline const uint256 operator&(const base_uint256& a, const base_uint256& b) { return uint256(a) &= b; } inline const uint256 operator|(const base_uint256& a, const base_uint256& b) { return uint256(a) |= b; } -inline const uint256 operator+(const base_uint256& a, const base_uint256& b) { return uint256(a) += b; } -inline const uint256 operator-(const base_uint256& a, const base_uint256& b) { return uint256(a) -= b; } - -inline bool operator<(const base_uint256& a, const uint256& b) { return (base_uint256)a < (base_uint256)b; } -inline bool operator<=(const base_uint256& a, const uint256& b) { return (base_uint256)a <= (base_uint256)b; } -inline bool operator>(const base_uint256& a, const uint256& b) { return (base_uint256)a > (base_uint256)b; } -inline bool operator>=(const base_uint256& a, const uint256& b) { return (base_uint256)a >= (base_uint256)b; } inline bool operator==(const base_uint256& a, const uint256& b) { return (base_uint256)a == (base_uint256)b; } inline bool operator!=(const base_uint256& a, const uint256& b) { return (base_uint256)a != (base_uint256)b; } inline const uint256 operator^(const base_uint256& a, const uint256& b) { return (base_uint256)a ^ (base_uint256)b; } inline const uint256 operator&(const base_uint256& a, const uint256& b) { return (base_uint256)a & (base_uint256)b; } inline const uint256 operator|(const base_uint256& a, const uint256& b) { return (base_uint256)a | (base_uint256)b; } -inline const uint256 operator+(const base_uint256& a, const uint256& b) { return (base_uint256)a + (base_uint256)b; } -inline const uint256 operator-(const base_uint256& a, const uint256& b) { return (base_uint256)a - (base_uint256)b; } - -inline bool operator<(const uint256& a, const base_uint256& b) { return (base_uint256)a < (base_uint256)b; } -inline bool operator<=(const uint256& a, const base_uint256& b) { return (base_uint256)a <= (base_uint256)b; } -inline bool operator>(const uint256& a, const base_uint256& b) { return (base_uint256)a > (base_uint256)b; } -inline bool operator>=(const uint256& a, const base_uint256& b) { return (base_uint256)a >= (base_uint256)b; } inline bool operator==(const uint256& a, const base_uint256& b) { return (base_uint256)a == (base_uint256)b; } inline bool operator!=(const uint256& a, const base_uint256& b) { return (base_uint256)a != (base_uint256)b; } inline const uint256 operator^(const uint256& a, const base_uint256& b) { return (base_uint256)a ^ (base_uint256)b; } inline const uint256 operator&(const uint256& a, const base_uint256& b) { return (base_uint256)a & (base_uint256)b; } inline const uint256 operator|(const uint256& a, const base_uint256& b) { return (base_uint256)a | (base_uint256)b; } -inline const uint256 operator+(const uint256& a, const base_uint256& b) { return (base_uint256)a + (base_uint256)b; } -inline const uint256 operator-(const uint256& a, const base_uint256& b) { return (base_uint256)a - (base_uint256)b; } - -inline bool operator<(const uint256& a, const uint256& b) { return (base_uint256)a < (base_uint256)b; } -inline bool operator<=(const uint256& a, const uint256& b) { return (base_uint256)a <= (base_uint256)b; } -inline bool operator>(const uint256& a, const uint256& b) { return (base_uint256)a > (base_uint256)b; } -inline bool operator>=(const uint256& a, const uint256& b) { return (base_uint256)a >= (base_uint256)b; } inline bool operator==(const uint256& a, const uint256& b) { return (base_uint256)a == (base_uint256)b; } inline bool 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; } -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; } uint256 uint160extend256(const uint160& uSource, uint uNamespace); @@ -719,7 +518,6 @@ inline int Testuint256AdHoc(std::vector vArg) { uint256 g(0); - printf("%s\n", g.ToString().c_str()); --g; printf("--g\n"); printf("%s\n", g.ToString().c_str()); @@ -754,11 +552,11 @@ inline int Testuint256AdHoc(std::vector vArg) a.pn[3] = 15; printf("%s\n", a.ToString().c_str()); b = 1; - b <<= 52; + // b <<= 52; a |= b; - a ^= 0x500; + // a ^= 0x500; printf("a %s\n", a.ToString().c_str()); @@ -816,7 +614,7 @@ inline int Testuint256AdHoc(std::vector vArg) printf("%s\n", x1.ToString().c_str()); for (int i = 0; i < 270; i += 4) { - x2 = x1 << i; + // x2 = x1 << i; printf("%s\n", x2.ToString().c_str()); } @@ -825,11 +623,11 @@ inline int Testuint256AdHoc(std::vector vArg) for (int i = 0; i < 270; i += 4) { x2 = x1; - x2 >>= i; + // x2 >>= i; printf("%s\n", x2.ToString().c_str()); } - + #if 0 for (int i = 0; i < 100; i++) { uint256 k = (~uint256(0) >> i); @@ -841,6 +639,7 @@ inline int Testuint256AdHoc(std::vector vArg) uint256 k = (~uint256(0) << i); printf("%s\n", k.ToString().c_str()); } + #endif return (0); } diff --git a/src/utils.h b/src/utils.h index 592669e3ff..0e11312cbf 100644 --- a/src/utils.h +++ b/src/utils.h @@ -6,7 +6,8 @@ #include -#include "uint256.h" +#include "types.h" +// #include "uint256.h" #define nothing() do {} while (0) @@ -64,9 +65,11 @@ inline std::string strHex(const std::vector& vucData) return strHex(vucData.begin(), vucData.size()); } -inline const std::string strHex(const uint160& ui) +inline std::string strHex(const uint64 uiHost) { - return strHex(ui.begin(), ui.size()); + uint64_t uBig = htobe64(uiHost); + + return strHex((unsigned char*) &uBig, sizeof(uBig)); } template