From 0baccf10b0760fc9d0925c2137dc4a178d55645a Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Thu, 14 Mar 2013 18:46:52 -0700 Subject: [PATCH] Cleanups needed to support new code. --- src/cpp/ripple/OrderBookDB.cpp | 99 +++++++++------------------------- src/cpp/ripple/OrderBookDB.h | 24 +++------ src/cpp/ripple/Pathfinder.cpp | 21 ++++---- 3 files changed, 44 insertions(+), 100 deletions(-) diff --git a/src/cpp/ripple/OrderBookDB.cpp b/src/cpp/ripple/OrderBookDB.cpp index 8e76b75791..2e00bae4db 100644 --- a/src/cpp/ripple/OrderBookDB.cpp +++ b/src/cpp/ripple/OrderBookDB.cpp @@ -17,7 +17,6 @@ void OrderBookDB::invalidate() mSeq = 0; } -// TODO: this would be way faster if we could just look under the order dirs void OrderBookDB::setup(Ledger::ref ledger) { boost::unordered_set mSeen; @@ -30,14 +29,13 @@ void OrderBookDB::setup(Ledger::ref ledger) LoadEvent::autoptr ev = theApp->getJobQueue().getLoadEventAP(jtOB_SETUP, "OrderBookDB::setup"); - mXRPOrders.clear(); - mIssuerMap.clear(); - - // walk through the entire ledger looking for orderbook entries - uint256 currentIndex = ledger->getFirstLedgerIndex(); + mDestMap.clear(); + mSourceMap.clear(); cLog(lsDEBUG) << "OrderBookDB>"; + // walk through the entire ledger looking for orderbook entries + uint256 currentIndex = ledger->getFirstLedgerIndex(); while (currentIndex.isNonZero()) { SLE::pointer entry = ledger->getSLEi(currentIndex); @@ -55,10 +53,8 @@ void OrderBookDB::setup(Ledger::ref ledger) OrderBook::pointer book = boost::make_shared(boost::cref(index), boost::cref(ci), boost::cref(co), boost::cref(ii), boost::cref(io)); - if (!book->getCurrencyIn()) // XRP - mXRPOrders.push_back(book); - else - mIssuerMap[book->getIssuerIn()].push_back(book); + mSourceMap[currencyIssuer_t(ci, ii)].push_back(book); + mDestMap[currencyIssuer_t(co, io)].push_back(book); } } @@ -68,29 +64,31 @@ void OrderBookDB::setup(Ledger::ref ledger) cLog(lsDEBUG) << "OrderBookDB<"; } -// return list of all orderbooks that want IssuerID -std::vector& OrderBookDB::getBooks(const uint160& issuerID) +// return list of all orderbooks that want this issuerID and currencyID +void OrderBookDB::getBooksByTakerPays(const uint160& issuerID, const uint160& currencyID, + std::vector& bookRet) { boost::recursive_mutex::scoped_lock sl(mLock); - boost::unordered_map< uint160, std::vector >::iterator it = mIssuerMap.find(issuerID); - return (it == mIssuerMap.end()) - ? mEmptyVector - : it->second; + boost::unordered_map< currencyIssuer_t, std::vector >::const_iterator + it = mSourceMap.find(currencyIssuer_t(currencyID, issuerID)); + if (it != mSourceMap.end()) + bookRet = it->second; + else + bookRet.clear(); + + cLog(lsINFO) << "gbTP: " << currencyID << " : " << issuerID << " : " << bookRet.size(); } -// return list of all orderbooks that want this issuerID and currencyID -void OrderBookDB::getBooks(const uint160& issuerID, const uint160& currencyID, std::vector& bookRet) +// return list of all orderbooks that give this issuerID and currencyID +void OrderBookDB::getBooksByTakerGets(const uint160& issuerID, const uint160& currencyID, + std::vector& bookRet) { boost::recursive_mutex::scoped_lock sl(mLock); - boost::unordered_map< uint160, std::vector >::iterator it = mIssuerMap.find(issuerID); - if (it != mIssuerMap.end()) - { - BOOST_FOREACH(OrderBook::ref book, it->second) - { - if (book->getCurrencyIn() == currencyID) - bookRet.push_back(book); - } - } + boost::unordered_map< currencyIssuer_t, std::vector >::const_iterator + it = mDestMap.find(currencyIssuer_t(currencyID, issuerID)); + if (it != mDestMap.end()) + bookRet = it->second; + else bookRet.clear(); } BookListeners::pointer OrderBookDB::makeBookListeners(const uint160& currencyPays, const uint160& currencyGets, @@ -133,53 +131,6 @@ BookListeners::pointer OrderBookDB::getBookListeners(const uint160& currencyPays return (*it3).second; } -/* -"CreatedNode" : { -"LedgerEntryType" : "Offer", -"LedgerIndex" : "F353BF8A7DCE35EA2985596F4C8421E30EF3B9A21618566BFE0ED00B62A8A5AB", -"NewFields" : { -"Account" : "rB5TihdPbKgMrkFqrqUC3yLdE8hhv4BdeY", -"BookDirectory" : "FF26BE244767D0EA9EFD523941439009E4185E4CBB918F714C08E1BC9BF04000", -"Sequence" : 112, -"TakerGets" : "400000000", -"TakerPays" : { -"currency" : "BTC", -"issuer" : "r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV", -"value" : "1" -} -} -} - -"ModifiedNode" : { -"FinalFields" : { -"Account" : "rHTxKLzRbniScyQFGMb3NodmxA848W8dKM", -"BookDirectory" : "407AF8FFDE71114B1981574FDDA9B0334572D56FC579735B4B0BD7A625405555", -"BookNode" : "0000000000000000", -"Flags" : 0, -"OwnerNode" : "0000000000000000", -"Sequence" : 32, -"TakerGets" : "149900000000", -"TakerPays" : { -"currency" : "USD", -"issuer" : "r9vbV3EHvXWjSkeQ6CAcYVPGeq7TuiXY2X", -"value" : "49.96666666666667" -} -}, -"LedgerEntryType" : "Offer", -"LedgerIndex" : "C60F8CC514208FA5F7BD03CF1B64B38B7183CD52318FCBBD3726350D4FE693B0", -"PreviousFields" : { -"TakerGets" : "150000000000", -"TakerPays" : { -"currency" : "USD", -"issuer" : "r9vbV3EHvXWjSkeQ6CAcYVPGeq7TuiXY2X", -"value" : "50" -} -}, -"PreviousTxnID" : "1A6AAE3F1AC5A8A7554A5ABC395D17FED5BF62CD90181AA8E4315EDFED4EDEB3", -"PreviousTxnLgrSeq" : 140734 -} - -*/ // Based on the meta, send the meta to the streams that are listening // We need to determine which streams a given meta effects void OrderBookDB::processTxn(Ledger::ref ledger, const ALTransaction& alTx, Json::Value& jvObj) diff --git a/src/cpp/ripple/OrderBookDB.h b/src/cpp/ripple/OrderBookDB.h index 15ebd24d04..5ad90ab1bc 100644 --- a/src/cpp/ripple/OrderBookDB.h +++ b/src/cpp/ripple/OrderBookDB.h @@ -15,6 +15,8 @@ // But, for now it is probably faster to just generate it each time. // +typedef std::pair currencyIssuer_t; + class BookListeners { boost::unordered_map mListeners; @@ -30,10 +32,8 @@ public: class OrderBookDB { - std::vector mEmptyVector; - std::vector mXRPOrders; - boost::unordered_map > mIssuerMap; - //std::vector mAllOrderBooks; + boost::unordered_map< currencyIssuer_t, std::vector > mSourceMap; // by ci/ii + boost::unordered_map< currencyIssuer_t, std::vector > mDestMap; // by co/io // issuerPays, issuerGets, currencyPays, currencyGets std::map > > > mListeners; @@ -46,18 +46,11 @@ public: void setup(Ledger::ref ledger); void invalidate(); - // return list of all orderbooks that want XRP - std::vector& getXRPInBooks(){ return mXRPOrders; } - - // return list of all orderbooks that want IssuerID - std::vector& getBooks(const uint160& issuerID); - // return list of all orderbooks that want this issuerID and currencyID - void getBooks(const uint160& issuerID, const uint160& currencyID, std::vector& bookRet); - - // returns the best rate we can find - float getPrice(uint160& currencyPays,uint160& currencyGets); - + void getBooksByTakerPays(const uint160& issuerID, const uint160& currencyID, + std::vector& bookRet); + void getBooksByTakerGets(const uint160& issuerID, const uint160& currencyID, + std::vector& bookRet); BookListeners::pointer getBookListeners(const uint160& currencyPays, const uint160& currencyGets, const uint160& issuerPays, const uint160& issuerGets); @@ -66,7 +59,6 @@ public: // see if this txn effects any orderbook void processTxn(Ledger::ref ledger, const ALTransaction& alTx, Json::Value& jvObj); - }; #endif diff --git a/src/cpp/ripple/Pathfinder.cpp b/src/cpp/ripple/Pathfinder.cpp index be60e664ed..51d5831bfb 100644 --- a/src/cpp/ripple/Pathfinder.cpp +++ b/src/cpp/ripple/Pathfinder.cpp @@ -363,7 +363,9 @@ bool Pathfinder::findPaths(const unsigned int iMaxSteps, const unsigned int iMax else if (!speEnd.mCurrencyID) { // Cursor is for XRP, continue with qualifying books: XRP -> non-XRP - BOOST_FOREACH(OrderBook::ref book, theApp->getOrderBookDB().getXRPInBooks()) + std::vector xrpBooks; + theApp->getOrderBookDB().getBooksByTakerPays(ACCOUNT_XRP, CURRENCY_XRP, xrpBooks); + BOOST_FOREACH(OrderBook::ref book, xrpBooks) { // New end is an order book with the currency and issuer. @@ -470,12 +472,10 @@ bool Pathfinder::findPaths(const unsigned int iMaxSteps, const unsigned int iMax } } - // Every book that wants the source currency. - std::vector books; // XXX Flip argument order to norm. - theApp->getOrderBookDB().getBooks(speEnd.mIssuerID, speEnd.mCurrencyID, books); - + std::vector books; + theApp->getOrderBookDB().getBooksByTakerPays(speEnd.mIssuerID, speEnd.mCurrencyID, books); BOOST_FOREACH(OrderBook::ref book, books) { if (!spPath.hasSeen(ACCOUNT_XRP, book->getCurrencyOut(), book->getIssuerOut())) @@ -631,7 +631,9 @@ void Pathfinder::addOptions(PathOption::pointer tail) { if (!tail->mCurrencyID) { // source XRP - BOOST_FOREACH(OrderBook::ref book, theApp->getOrderBookDB().getXRPInBooks()) + std::vector xrpBooks; + theApp->getOrderBookDB().getBooksByTakerPays(ISSUER_XRP, CURRENCY_XRP, xrpBooks); + BOOST_FOREACH(OrderBook::ref book, xrpBooks) { PathOption::pointer pathOption(new PathOption(tail)); @@ -663,11 +665,10 @@ void Pathfinder::addOptions(PathOption::pointer tail) } } - // every offer that wants the source currency + // Every offer that can take this currency in std::vector books; - theApp->getOrderBookDB().getBooks(tail->mCurrentAccount, tail->mCurrencyID, books); - - BOOST_FOREACH(OrderBook::ref book,books) + theApp->getOrderBookDB().getBooksByTakerPays(tail->mCurrentAccount, tail->mCurrencyID, books); + BOOST_FOREACH(OrderBook::ref book, books) { PathOption::pointer pathOption(new PathOption(tail));