From 379875ae475df8227042f25cbc6862d3d4942f99 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Thu, 16 May 2013 00:57:56 -0700 Subject: [PATCH 1/4] Hash is not valid if transaction is added to ledger. --- src/cpp/ripple/Ledger.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/cpp/ripple/Ledger.cpp b/src/cpp/ripple/Ledger.cpp index 8bb7709e2..33344bcfd 100644 --- a/src/cpp/ripple/Ledger.cpp +++ b/src/cpp/ripple/Ledger.cpp @@ -256,6 +256,7 @@ bool Ledger::addTransaction(const uint256& txID, const Serializer& txn) cLog(lsWARNING) << "Attempt to add transaction to ledger that already had it"; return false; } + mValidHash = false; return true; } @@ -270,6 +271,7 @@ bool Ledger::addTransaction(const uint256& txID, const Serializer& txn, const Se cLog(lsFATAL) << "Attempt to add transaction+MD to ledger that already had it"; return false; } + mValidHash = false; return true; } From 6556719547a1f66523bb7a5547a45623079429b0 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Thu, 16 May 2013 00:58:20 -0700 Subject: [PATCH 2/4] LedgerMaster keeps cached snapshot of the current ledger. --- src/cpp/ripple/LedgerMaster.cpp | 8 ++++++++ src/cpp/ripple/LedgerMaster.h | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/src/cpp/ripple/LedgerMaster.cpp b/src/cpp/ripple/LedgerMaster.cpp index dd93c393e..56a28a802 100644 --- a/src/cpp/ripple/LedgerMaster.cpp +++ b/src/cpp/ripple/LedgerMaster.cpp @@ -18,6 +18,14 @@ uint32 LedgerMaster::getCurrentLedgerIndex() return mCurrentLedger->getLedgerSeq(); } +Ledger::ref LedgerMaster::getCurrentSnapshot() +{ + if (!mCurrentSnapshot || (mCurrentSnapshot->getHash() != mCurrentLedger->getHash())) + mCurrentSnapshot = boost::make_shared(boost::ref(*mCurrentLedger), false); + assert(mCurrentSnapshot->isImmutable()); + return mCurrentSnapshot; +} + void LedgerMaster::addHeldTransaction(Transaction::ref transaction) { // returns true if transaction was added boost::recursive_mutex::scoped_lock ml(mLock); diff --git a/src/cpp/ripple/LedgerMaster.h b/src/cpp/ripple/LedgerMaster.h index 2d872bbdc..dddffaa42 100644 --- a/src/cpp/ripple/LedgerMaster.h +++ b/src/cpp/ripple/LedgerMaster.h @@ -26,6 +26,7 @@ protected: TransactionEngine mEngine; Ledger::pointer mCurrentLedger; // The ledger we are currently processiong + Ledger::pointer mCurrentSnapshot; // Snapshot of the current ledger Ledger::pointer mFinalizedLedger; // The ledger that most recently closed Ledger::pointer mValidLedger; // The highest-sequence ledger we have fully accepted Ledger::pointer mPubLedger; // The last ledger we have published @@ -74,6 +75,9 @@ public: // The current ledger is the ledger we believe new transactions should go in Ledger::ref getCurrentLedger() { return mCurrentLedger; } + // An immutable snapshot of the current ledger + Ledger::ref getCurrentSnapshot(); + // The finalized ledger is the last closed/accepted ledger Ledger::ref getClosedLedger() { return mFinalizedLedger; } From eecc9017b1f8de5cb5e693a5ca2647159f66bfcb Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Thu, 16 May 2013 00:58:34 -0700 Subject: [PATCH 3/4] Wrapper to get current snapshot. --- src/cpp/ripple/NetworkOPs.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cpp/ripple/NetworkOPs.h b/src/cpp/ripple/NetworkOPs.h index c18f8b55c..94c522d50 100644 --- a/src/cpp/ripple/NetworkOPs.h +++ b/src/cpp/ripple/NetworkOPs.h @@ -183,6 +183,7 @@ public: Ledger::ref getClosedLedger() { return mLedgerMaster->getClosedLedger(); } Ledger::ref getValidatedLedger() { return mLedgerMaster->getValidatedLedger(); } Ledger::ref getCurrentLedger() { return mLedgerMaster->getCurrentLedger(); } + Ledger::ref getCurrentSnapshot() { return mLedgerMaster->getCurrentSnapshot(); } Ledger::pointer getLedgerByHash(const uint256& hash) { return mLedgerMaster->getLedgerByHash(hash); } Ledger::pointer getLedgerBySeq(const uint32 seq); void missingNodeInLedger(const uint32 seq); From 28f54176f3a2e31d23e4a89db492866cbfcea330 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Thu, 16 May 2013 00:58:44 -0700 Subject: [PATCH 4/4] Use the current snapshot to get immutable ledger optimizations. --- src/cpp/ripple/RPCHandler.cpp | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/src/cpp/ripple/RPCHandler.cpp b/src/cpp/ripple/RPCHandler.cpp index de444d4da..37ca82103 100644 --- a/src/cpp/ripple/RPCHandler.cpp +++ b/src/cpp/ripple/RPCHandler.cpp @@ -177,8 +177,7 @@ Json::Value RPCHandler::transactionSign(Json::Value jvRequest, bool bSubmit) return rpcError(rpcINVALID_PARAMS); } - Ledger::pointer lSnapshot = boost::make_shared( - boost::ref(*mNetOps->getCurrentLedger()), false); + Ledger::pointer lSnapshot = mNetOps->getCurrentSnapshot(); { ScopedUnlock su(theApp->getMasterLock()); bool bValid; @@ -1031,6 +1030,9 @@ Json::Value RPCHandler::doAccountLines(Json::Value jvRequest, int& cost, ScopedL if (!lpLedger) return jvResult; + if (lpLedger->isImmutable()) + MasterLockHolder.unlock(); + if (!jvRequest.isMember("account")) return rpcError(rpcINVALID_PARAMS); @@ -1065,9 +1067,6 @@ Json::Value RPCHandler::doAccountLines(Json::Value jvRequest, int& cost, ScopedL AccountState::pointer as = mNetOps->getAccountState(lpLedger, raAccount); - if (lpLedger->isImmutable()) - MasterLockHolder.unlock(); - if (as) { jvResult["account"] = raAccount.humanAccountID(); @@ -1124,6 +1123,9 @@ Json::Value RPCHandler::doAccountOffers(Json::Value jvRequest, int& cost, Scoped if (!lpLedger) return jvResult; + if (lpLedger->isImmutable()) + MasterLockHolder.unlock(); + if (!jvRequest.isMember("account")) return rpcError(rpcINVALID_PARAMS); @@ -1860,6 +1862,9 @@ Json::Value RPCHandler::doLedger(Json::Value jvRequest, int& cost, ScopedLock& M if (!lpLedger) return jvResult; + if (lpLedger->isImmutable()) + MasterLockHolder.unlock(); + bool bFull = jvRequest.isMember("full") && jvRequest["full"].asBool(); bool bTransactions = jvRequest.isMember("transactions") && jvRequest["transactions"].asBool(); bool bAccounts = jvRequest.isMember("accounts") && jvRequest["accounts"].asBool(); @@ -1869,15 +1874,6 @@ Json::Value RPCHandler::doLedger(Json::Value jvRequest, int& cost, ScopedLock& M | (bTransactions ? LEDGER_JSON_DUMP_TXRP : 0) | (bAccounts ? LEDGER_JSON_DUMP_STATE : 0); - if ((bFull || bAccounts) && !lpLedger->isImmutable()) - { // For full or accounts, it's cheaper to snapshot - lpLedger = boost::make_shared(*lpLedger, false); - assert(lpLedger->isImmutable()); - } - - if (lpLedger->isImmutable()) - MasterLockHolder.unlock(); - Json::Value ret(Json::objectValue); lpLedger->addJson(ret, iOptions); @@ -2620,7 +2616,7 @@ Json::Value RPCHandler::lookupLedger(Json::Value jvRequest, Ledger::pointer& lpL switch (iLedgerIndex) { case LEDGER_CURRENT: - lpLedger = mNetOps->getCurrentLedger(); + lpLedger = mNetOps->getCurrentSnapshot(); iLedgerIndex = lpLedger->getLedgerSeq(); break;