From e10505d2a70fbf127c0670c84a0754af3593916a Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Thu, 31 May 2012 20:24:49 -0700 Subject: [PATCH 1/7] Cosmetic changes. --- src/RPCServer.cpp | 1 - src/SHAMap.h | 6 +----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/src/RPCServer.cpp b/src/RPCServer.cpp index 5ba83adc9..ccd7b66bd 100644 --- a/src/RPCServer.cpp +++ b/src/RPCServer.cpp @@ -389,7 +389,6 @@ Json::Value RPCServer::doAccountLines(Json::Value ¶ms) // 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)) { diff --git a/src/SHAMap.h b/src/SHAMap.h index 18677c7be..f0e41bc51 100644 --- a/src/SHAMap.h +++ b/src/SHAMap.h @@ -80,11 +80,7 @@ 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 *reinterpret_cast(u.begin()); } @@ -176,7 +172,7 @@ public: uint32 getSeq() const { return mSeq; } void setSeq(uint32 s) { mSeq = s; } const uint256& getNodeHash() const { return mHash; } - TNType getType() const { return mType; } + TNType getType() const { return mType; } // type functions bool isLeaf() const { return (mType == tnTRANSACTION) || (mType == tnACCOUNT_STATE); } From a0009f7c6f9b38211ec56bf181bb0a6313f3d001 Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Thu, 31 May 2012 20:26:14 -0700 Subject: [PATCH 2/7] Rework Ledger index calculation. --- src/Ledger.h | 9 ++++----- src/LedgerFormats.h | 19 ++++++++++++------- src/LedgerIndex.cpp | 31 +++++++++++++++++++++++++++++++ src/LedgerNode.cpp | 28 +++++++++------------------- src/NetworkOPs.cpp | 7 +++---- src/NetworkOPs.h | 3 +-- src/TransactionEngine.cpp | 14 ++++++-------- src/TransactionEngine.h | 2 -- 8 files changed, 66 insertions(+), 47 deletions(-) diff --git a/src/Ledger.h b/src/Ledger.h index 334132eaa..477f4fb34 100644 --- a/src/Ledger.h +++ b/src/Ledger.h @@ -144,8 +144,7 @@ public: static Ledger::pointer loadByHash(const uint256& ledgerHash); // index calculation functions - static uint256 getAccountRootIndex(const uint160& account) - { return uint160extend256(account, lnsAccounts); } // Index is accountID extended to 256 bits + static uint256 getAccountRootIndex(const uint160& uAccountID); static uint256 getAccountRootIndex(const NewcoinAddress& account) { return getAccountRootIndex(account.getAccountID()); } @@ -156,8 +155,7 @@ public: SLE::pointer getGenerator(LedgerStateParms& parms, const uint160& uGeneratorID); - static uint256 getGeneratorIndex(const uint160& uGeneratorID) - { return uint160extend256(uGeneratorID, lnsGenerator); } // Index is the generator ID extended to 256 bits in namespace 1 + static uint256 getGeneratorIndex(const uint160& uGeneratorID); // // Ripple functions @@ -169,6 +167,7 @@ public: static uint256 getRippleStateIndex(const NewcoinAddress& naA, const NewcoinAddress& naB) { return getRippleStateIndex(naA, naB, uint160()); } + static uint256 getRippleDirIndex(const uint160& uAccountID); RippleState::pointer getRippleState(const uint256& uNode); SLE::pointer getRippleState(LedgerStateParms& parms, const uint256& uNode); @@ -194,7 +193,7 @@ public: // Directory functions // - static uint256 getDirIndex(const uint256& uBase, const LedgerEntryType letKind, const uint64 uNodeDir=0); + static uint256 getDirIndex(const uint256& uBase, const uint64 uNodeDir=0); SLE::pointer getDirRoot(LedgerStateParms& parms, const uint256& uRootIndex); SLE::pointer getDirNode(LedgerStateParms& parms, const uint256& uNodeIndex); diff --git a/src/LedgerFormats.h b/src/LedgerFormats.h index 6adc19bc6..d1a18ea4b 100644 --- a/src/LedgerFormats.h +++ b/src/LedgerFormats.h @@ -3,6 +3,7 @@ #include "SerializedObject.h" +// Used as the type of a transaction or the type of a ledger entry. enum LedgerEntryType { ltINVALID = -1, @@ -14,15 +15,19 @@ enum LedgerEntryType ltNICKNAME }; -// In the format 160 + namespace + other. +// Used as a prefix for computing ledger indexes (keys). enum LedgerNameSpace { - lnsGenerator = -1, - lnsAccounts, - lnsRipple, - lnsBonds, - lnsInvoices, - lnsMultiSig + spaceAccount, + spaceGenerator, + spaceNickname, + spaceRipple, + spaceRippleDir, + spaceOffer, + spaceOfferDir, + spaceBond, + spaceInvoice, + spaceMultiSig, }; enum LedgerSpecificFlags diff --git a/src/LedgerIndex.cpp b/src/LedgerIndex.cpp index a00f70834..52dde491d 100644 --- a/src/LedgerIndex.cpp +++ b/src/LedgerIndex.cpp @@ -1,6 +1,16 @@ #include "Ledger.h" +uint256 Ledger::getAccountRootIndex(const uint160& uAccountID) +{ + Serializer s; + + s.add8(spaceAccount); + s.add160(uAccountID); + + return s.getSHA512Half(); +} + uint256 Ledger::getRippleStateIndex(const NewcoinAddress& naA, const NewcoinAddress& naB, const uint160& uCurrency) { uint160 uAID = naA.getAccountID(); @@ -8,6 +18,7 @@ uint256 Ledger::getRippleStateIndex(const NewcoinAddress& naA, const NewcoinAddr bool bAltB = uAID < uBID; Serializer s; + s.add8(spaceRipple); s.add160(bAltB ? uAID : uBID); s.add160(bAltB ? uBID : uAID); s.add160(uCurrency); @@ -15,6 +26,26 @@ uint256 Ledger::getRippleStateIndex(const NewcoinAddress& naA, const NewcoinAddr return s.getSHA512Half(); } +uint256 Ledger::getRippleDirIndex(const uint160& uAccountID) +{ + Serializer s; + + s.add8(spaceRippleDir); + s.add160(uAccountID); + + return s.getSHA512Half(); +} + +uint256 Ledger::getGeneratorIndex(const uint160& uGeneratorID) +{ + Serializer s; + + s.add8(spaceGenerator); + s.add160(uGeneratorID); + + return s.getSHA512Half(); +} + uint160 Ledger::getOfferBase(const uint160& currencyIn, const uint160& accountIn, const uint160& currencyOut, const uint160& accountOut) { diff --git a/src/LedgerNode.cpp b/src/LedgerNode.cpp index 1840e371e..d3ebd0ba5 100644 --- a/src/LedgerNode.cpp +++ b/src/LedgerNode.cpp @@ -138,28 +138,18 @@ SerializedLedgerEntry::pointer Ledger::getRippleState(LedgerStateParms& parms, c // Directory // -uint256 Ledger::getDirIndex(const uint256& uBase, const LedgerEntryType letKind, const uint64 uNodeDir) +// For a directory entry put in the 64 bit index or quality. +uint256 Ledger::getDirIndex(const uint256& uBase, const uint64 uNodeDir) { - // Indexes are stored in little endian format. - // The low bytes are indexed first, so when printed as a hex stream the hex is in byte order. - // Therefore, we place uNodeDir in the 8 right most bytes. - Serializer sKey; + // Indexes are stored in big endian format: they print as hex as stored. + // Most significant bytes are first. Least significant bytes repesent adjcent entries. + // We place uNodeDir in the 8 right most bytes to be adjcent. + // Want uNodeDir in big endian format so ++ goes to the next entry for indexes. + uint256 uNode(uBase); - sKey.add256(uBase); - sKey.add8(letKind); + ((uint64*) uNode.end())[-1] = htobe64(uNodeDir); - uint256 uResult = sKey.getSHA512Half(); - - Serializer sNode; // Put in a fixed byte order: BIG. YYY - - sNode.add64(uNodeDir); - - // YYY SLOPPY - std::vector vucData = sNode.getData(); - - std::copy(vucData.begin(), vucData.end(), uResult.end()-(64/8)); - - return uResult; + return uNode; } SerializedLedgerEntry::pointer Ledger::getDirRoot(LedgerStateParms& parms, const uint256& uRootIndex) diff --git a/src/NetworkOPs.cpp b/src/NetworkOPs.cpp index 01e8266f5..3e0745036 100644 --- a/src/NetworkOPs.cpp +++ b/src/NetworkOPs.cpp @@ -172,11 +172,10 @@ SLE::pointer NetworkOPs::getGenerator(const uint256& uLedger, const uint160& uGe bool NetworkOPs::getDirInfo( const uint256& uLedger, const uint256& uBase, - const LedgerEntryType letKind, uint256& uDirLineNodeFirst, uint256& uDirLineNodeLast) { - uint256 uRootIndex = Ledger::getDirIndex(uBase, letKind); + uint256 uRootIndex = Ledger::getDirIndex(uBase, 0); LedgerStateParms lspRoot = lepNONE; SLE::pointer sleRoot = mLedgerMaster->getLedgerByHash(uLedger)->getDirRoot(lspRoot, uRootIndex); @@ -186,8 +185,8 @@ bool NetworkOPs::getDirInfo( std::cerr << "getDirInfo: first: " << strHex(sleRoot->getIFieldU64(sfFirstNode)) << std::endl; std::cerr << "getDirInfo: last: " << strHex(sleRoot->getIFieldU64(sfLastNode)) << std::endl; - uDirLineNodeFirst = Ledger::getDirIndex(uBase, letKind, sleRoot->getIFieldU64(sfFirstNode)); - uDirLineNodeLast = Ledger::getDirIndex(uBase, letKind, sleRoot->getIFieldU64(sfLastNode)); + uDirLineNodeFirst = Ledger::getDirIndex(uBase, sleRoot->getIFieldU64(sfFirstNode)); + uDirLineNodeLast = Ledger::getDirIndex(uBase, sleRoot->getIFieldU64(sfLastNode)); std::cerr << "getDirInfo: first: " << uDirLineNodeFirst.ToString() << std::endl; std::cerr << "getDirInfo: last: " << uDirLineNodeLast.ToString() << std::endl; diff --git a/src/NetworkOPs.h b/src/NetworkOPs.h index c8553f80a..130e71576 100644 --- a/src/NetworkOPs.h +++ b/src/NetworkOPs.h @@ -73,7 +73,6 @@ public: // bool getDirInfo(const uint256& uLedger, const uint256& uBase, - const LedgerEntryType letKind, uint256& uDirNodeFirst, uint256& uDirNodeLast); STVector256 getDirNode(const uint256& uLedger, const uint256& uDirLineNode); @@ -82,7 +81,7 @@ public: // bool getDirLineInfo(const uint256& uLedger, const NewcoinAddress& naAccount, uint256& uDirLineNodeFirst, uint256& uDirLineNodeLast) - { return getDirInfo(uLedger, uint160extend256(naAccount.getAccountID(), 0), ltRIPPLE_STATE, uDirLineNodeFirst, uDirLineNodeLast); } + { return getDirInfo(uLedger, Ledger::getRippleDirIndex(naAccount.getAccountID()), uDirLineNodeFirst, uDirLineNodeLast); } RippleState::pointer getRippleState(const uint256& uLedger, const uint256& uIndex); diff --git a/src/TransactionEngine.cpp b/src/TransactionEngine.cpp index 6ae66d58e..b33498561 100644 --- a/src/TransactionEngine.cpp +++ b/src/TransactionEngine.cpp @@ -14,12 +14,11 @@ typedef SerializedLedgerEntry SLE; TransactionEngineResult TransactionEngine::dirAdd( std::vector& accounts, uint64& uNodeDir, - const LedgerEntryType letKind, const uint256& uBase, const uint256& uLedgerIndex) { // Get the root. - uint256 uRootIndex = Ledger::getDirIndex(uBase, letKind); + uint256 uRootIndex = Ledger::getDirIndex(uBase, 0); LedgerStateParms lspRoot = lepNONE; SLE::pointer sleRoot = mLedger->getDirRoot(lspRoot, uRootIndex); bool bRootNew; @@ -50,7 +49,7 @@ TransactionEngineResult TransactionEngine::dirAdd( } // Get the last node. - uint256 uNodeIndex = Ledger::getDirIndex(uBase, letKind, uNodeDir); + uint256 uNodeIndex = Ledger::getDirIndex(uBase, uNodeDir); LedgerStateParms lspNode = lepNONE; SLE::pointer sleNode = bRootNew ? SLE::pointer() : mLedger->getDirNode(lspNode, uNodeIndex); @@ -111,12 +110,11 @@ TransactionEngineResult TransactionEngine::dirAdd( TransactionEngineResult TransactionEngine::dirDelete( std::vector& accounts, const uint64& uNodeDir, - const LedgerEntryType letKind, const uint256& uBase, const uint256& uLedgerIndex) { uint64 uNodeCur = uNodeDir; - uint256 uNodeIndex = Ledger::getDirIndex(uBase, letKind, uNodeCur); + uint256 uNodeIndex = Ledger::getDirIndex(uBase, uNodeCur); LedgerStateParms lspNode = lepNONE; SLE::pointer sleNode = mLedger->getDirNode(lspNode, uNodeIndex); @@ -141,7 +139,7 @@ TransactionEngineResult TransactionEngine::dirDelete( { // Get root information LedgerStateParms lspRoot = lepNONE; - SLE::pointer sleRoot = mLedger->getDirRoot(lspRoot, Ledger::getDirIndex(uBase, letKind)); + SLE::pointer sleRoot = mLedger->getDirRoot(lspRoot, Ledger::getDirIndex(uBase, 0)); if (!sleRoot) { @@ -197,7 +195,7 @@ TransactionEngineResult TransactionEngine::dirDelete( // Get replacement node. lspNode = lepNONE; - sleNode = mLedger->getDirNode(lspNode, Ledger::getDirIndex(uBase, letKind, uNodeCur)); + sleNode = mLedger->getDirNode(lspNode, Ledger::getDirIndex(uBase, uNodeCur)); svIndexes = sleNode->getIFieldV256(sfIndexes); } } @@ -640,7 +638,7 @@ TransactionEngineResult TransactionEngine::doCreditSet(const SerializedTransacti // XXX Verify extend is passing the right bits, not the zero bits. // XXX Make dirAdd more flexiable to take vector. - terResult = dirAdd(accounts, uSrcRef, ltRIPPLE_STATE, uint160extend256(uSrcAccountID, 0), sleRippleState->getIndex()); + terResult = dirAdd(accounts, uSrcRef, Ledger::getRippleDirIndex(uSrcAccountID), sleRippleState->getIndex()); } std::cerr << "doCreditSet<" << std::endl; diff --git a/src/TransactionEngine.h b/src/TransactionEngine.h index 770956122..0a4bcb3a7 100644 --- a/src/TransactionEngine.h +++ b/src/TransactionEngine.h @@ -75,14 +75,12 @@ private: TransactionEngineResult dirAdd( std::vector& accounts, uint64& uNodeDir, // Node of entry. - const LedgerEntryType letKind, const uint256& uBase, const uint256& uLedgerIndex); TransactionEngineResult dirDelete( std::vector& accounts, const uint64& uNodeDir, // Node item is mentioned in. - const LedgerEntryType letKind, const uint256& uBase, // Key of item. const uint256& uLedgerIndex); // Item being deleted From 79bf1706313163787e2479a2b5a66abf5256ec22 Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Thu, 31 May 2012 20:26:28 -0700 Subject: [PATCH 3/7] Remove uint160extend256. --- src/uint256.h | 2 -- src/utils.cpp | 12 ------------ 2 files changed, 14 deletions(-) diff --git a/src/uint256.h b/src/uint256.h index ae7a9dae5..c059023de 100644 --- a/src/uint256.h +++ b/src/uint256.h @@ -526,8 +526,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; } -uint256 uint160extend256(const uint160& uSource, uint uNamespace); - inline int Testuint256AdHoc(std::vector vArg) { uint256 g(0); diff --git a/src/utils.cpp b/src/utils.cpp index ca6240cc2..6a75ead20 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -1,18 +1,6 @@ #include "utils.h" #include "uint256.h" -// XXX Assume little-endian. -uint256 uint160extend256(const uint160& uSource, uint uNamespace) -{ - uint256 uResult; - - // Place right justified: in most significant bits. - memcpy(uResult.end() - uSource.size(), uSource.begin(), uSource.size()); - uResult.begin()[uResult.size() - uSource.size() - 1] = uNamespace; - - return uResult; -} - // // Time support // We have our own epoch. From e6745d0b3bb0c223aa497a0f5c620729289ba352 Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Fri, 1 Jun 2012 01:16:31 -0700 Subject: [PATCH 4/7] Add mNonce256 to Application. --- src/Application.cpp | 2 +- src/Application.h | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Application.cpp b/src/Application.cpp index 5f7710f56..026f862a6 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -36,7 +36,7 @@ Application::Application() : mTxnDB(NULL), mLedgerDB(NULL), mWalletDB(NULL), mHashNodeDB(NULL), mNetNodeDB(NULL), mConnectionPool(mIOService), mPeerDoor(NULL), mRPCDoor(NULL) { - nothing(); + RAND_bytes(mNonce256.begin(), mNonce256.size()); } extern const char *TxnDBInit[], *LedgerDBInit[], *WalletDBInit[], *HashNodeDBInit[], *NetNodeDBInit[]; diff --git a/src/Application.h b/src/Application.h index 30bfc2624..96e58bdee 100644 --- a/src/Application.h +++ b/src/Application.h @@ -47,6 +47,8 @@ class Application PeerDoor* mPeerDoor; RPCDoor* mRPCDoor; + uint256 mNonce256; + std::map mPeerMap; boost::recursive_mutex mPeerMapLock; @@ -73,8 +75,7 @@ public: DatabaseCon* getHashNodeDB() { return mHashNodeDB; } DatabaseCon* getNetNodeDB() { return mNetNodeDB; } - //Serializer* getSerializer(){ return(mSerializer); } - //void setSerializer(Serializer* ser){ mSerializer=ser; } + uint256 getNonce256() { return mNonce256; } void run(); void stop(); From c13bf4d6064f8519a5b27ee1ef70e4e5d97f92a1 Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Fri, 1 Jun 2012 01:17:16 -0700 Subject: [PATCH 5/7] Use mNonce256 in SHAMap hashing. --- src/SHAMap.cpp | 14 ++++++++++++++ src/SHAMap.h | 8 +++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/SHAMap.cpp b/src/SHAMap.cpp index 372bd3f0f..13e336fb1 100644 --- a/src/SHAMap.cpp +++ b/src/SHAMap.cpp @@ -10,6 +10,20 @@ #include "Serializer.h" #include "BitcoinUtil.h" #include "SHAMap.h" +#include "Application.h" + +std::size_t hash_SMN::operator() (const SHAMapNode& mn) const +{ + return mn.getDepth() + ^ *reinterpret_cast(mn.getNodeID().begin()) + ^ *reinterpret_cast(theApp->getNonce256().begin()); +} + +std::size_t hash_SMN::operator() (const uint256& u) const +{ + return *reinterpret_cast(u.begin()) + ^ *reinterpret_cast(theApp->getNonce256().begin()); +} SHAMap::SHAMap(uint32 seq) : mSeq(seq), mState(Modifying) { diff --git a/src/SHAMap.h b/src/SHAMap.h index f0e41bc51..631be2a0a 100644 --- a/src/SHAMap.h +++ b/src/SHAMap.h @@ -77,13 +77,11 @@ public: }; class hash_SMN -{ // These must be randomized for release +{ public: - std::size_t operator() (const SHAMapNode& mn) const - { return mn.getDepth() ^ *reinterpret_cast(mn.getNodeID().begin()); } + std::size_t operator() (const SHAMapNode& mn) const; - std::size_t operator() (const uint256& u) const - { return *reinterpret_cast(u.begin()); } + std::size_t operator() (const uint256& u) const; }; class SHAMapItem From 75ca063c50490cb879853a894d4702ce31e3a7f2 Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Fri, 1 Jun 2012 01:18:03 -0700 Subject: [PATCH 6/7] Fix alignment issues for uint160. --- src/uint256.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/uint256.h b/src/uint256.h index c059023de..90fc6e87b 100644 --- a/src/uint256.h +++ b/src/uint256.h @@ -387,12 +387,14 @@ public: *this = b; } - uint160& operator=(uint64 b) + uint160& operator=(uint64 uHost) { + uint64 uBig = htobe64(uHost); + zero(); // Put in least significant bits. - ((uint64_t *) end())[-1] = htobe64(b); + memcpy(((uint64_t*)end())-1, &uBig, sizeof(uBig)); return *this; } From 745806cb290b5bdb0f6e5b60fd839bee0500d4d0 Mon Sep 17 00:00:00 2001 From: Arthur Britto Date: Fri, 1 Jun 2012 01:18:27 -0700 Subject: [PATCH 7/7] Fix doCreditSet used wrong field. --- src/TransactionEngine.cpp | 2 +- src/TransactionEngine.h | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/TransactionEngine.cpp b/src/TransactionEngine.cpp index b33498561..27fbf6169 100644 --- a/src/TransactionEngine.cpp +++ b/src/TransactionEngine.cpp @@ -593,7 +593,7 @@ TransactionEngineResult TransactionEngine::doCreditSet(const SerializedTransacti bAddIndex = !(sleRippleState->getFlags() & uFlags); - sleRippleState->setIFieldAmount(bSltD ? sfLowLimit : sfHighID, saLimitAmount); + sleRippleState->setIFieldAmount(bSltD ? sfLowLimit : sfHighLimit, saLimitAmount); accounts.push_back(std::make_pair(taaMODIFY, sleRippleState)); diff --git a/src/TransactionEngine.h b/src/TransactionEngine.h index 0a4bcb3a7..d4ce91682 100644 --- a/src/TransactionEngine.h +++ b/src/TransactionEngine.h @@ -1,7 +1,6 @@ #ifndef __TRANSACTIONENGINE__ #define __TRANSACTIONENGINE__ -#include "Ledger.h" #include "Ledger.h" #include "SerializedTransaction.h" #include "SerializedLedger.h"