From c386253be61e45a5d654401b558e3a189afc62f8 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Sun, 3 Jun 2012 19:42:38 -0700 Subject: [PATCH] New ledger constructors and ledger snapshot code. Remove dead code. --- src/Ledger.cpp | 102 +++++++++---------------------------------------- 1 file changed, 19 insertions(+), 83 deletions(-) diff --git a/src/Ledger.cpp b/src/Ledger.cpp index c428482ed4..431eea5db0 100644 --- a/src/Ledger.cpp +++ b/src/Ledger.cpp @@ -41,18 +41,29 @@ Ledger::Ledger(const uint256 &parentHash, const uint256 &transHash, const uint25 mTotCoins(totCoins), mCloseTime(timeStamp), mLedgerSeq(ledgerSeq), mLedgerInterval(LEDGER_INTERVAL), mClosed(false), mValidHash(false), mAccepted(false), mImmutable(false) { + assert(!!mParentHash); updateHash(); } -Ledger::Ledger(Ledger::pointer prevLedger) : mTotCoins(prevLedger->mTotCoins), - mLedgerSeq(prevLedger->mLedgerSeq + 1), mLedgerInterval(prevLedger->mLedgerInterval), +Ledger::Ledger(Ledger& ledger, bool isMutable) : mTotCoins(ledger.mTotCoins), + mLedgerSeq(ledger.mLedgerSeq), mLedgerInterval(ledger.mLedgerInterval), + mClosed(ledger.mClosed), mValidHash(false), mAccepted(ledger.mAccepted), mImmutable(!isMutable), + mTransactionMap(ledger.mTransactionMap->snapShot(isMutable)), + mAccountStateMap(ledger.mAccountStateMap->snapShot(isMutable)) +{ // Create a new ledger that's a snapshot of this one + updateHash(); +} + + +Ledger::Ledger(bool, Ledger& prevLedger) : mTotCoins(prevLedger.mTotCoins), + mLedgerSeq(prevLedger.mLedgerSeq + 1), mLedgerInterval(prevLedger.mLedgerInterval), mClosed(false), mValidHash(false), mAccepted(false), mImmutable(false), - mTransactionMap(new SHAMap()), mAccountStateMap(prevLedger->mAccountStateMap) -{ - prevLedger->updateHash(); - mParentHash = prevLedger->getHash(); - mAccountStateMap->setSeq(mLedgerSeq); - mCloseTime = prevLedger->getNextLedgerClose(); + mTransactionMap(new SHAMap()), mAccountStateMap(prevLedger.mAccountStateMap->snapShot(true)) +{ // Create a new ledger that follows this one + prevLedger.updateHash(); + mParentHash = prevLedger.getHash(); + assert(!!mParentHash); + mCloseTime = prevLedger.getNextLedgerClose(); } Ledger::Ledger(const std::vector& rawLedger) : mCloseTime(0), @@ -313,81 +324,6 @@ void Ledger::addJson(Json::Value& ret) ret[boost::lexical_cast(mLedgerSeq)] = ledger; } -Ledger::pointer Ledger::switchPreviousLedger(Ledger::pointer oldPrevious, Ledger::pointer newPrevious, int limit) -{ - // Build a new ledger that can replace this ledger as the active ledger, - // with a different previous ledger. We assume our ledger is trusted, as is its - // previous ledger. We make no assumptions about the new previous ledger. - - int count; - - // 1) Validate sequences and make sure the specified ledger is a valid prior ledger - if (newPrevious->getLedgerSeq() != oldPrevious->getLedgerSeq()) return Ledger::pointer(); - - // 2) Begin building a new ledger with the specified ledger as previous. - Ledger::pointer newLedger = boost::make_shared(newPrevious); - - // 3) For any transactions in our previous ledger but not in the new previous ledger, add them to the set - SHAMap::SHAMapDiff mapDifferences; - std::map > TxnDiff; - if (!newPrevious->mTransactionMap->compare(oldPrevious->mTransactionMap, mapDifferences, limit)) - return Ledger::pointer(); - if (!Transaction::convertToTransactions(oldPrevious->getLedgerSeq(), newPrevious->getLedgerSeq(), - false, true, mapDifferences, TxnDiff)) - return Ledger::pointer(); // new previous ledger contains invalid transactions - - // 4) Try to add those transactions to the new ledger. -#if 0 - do - { - count = 0; - std::map >::iterator it = TxnDiff.begin(); - while (it != TxnDiff.end()) - { - Transaction::pointer& tx = it->second.second; - if (!tx || newLedger->addTransaction(tx)) // FIXME: addTransaction doesn't do checks - { - ++count; - TxnDiff.erase(it++); - } - else ++it; - } - } while (count != 0); -#endif - // WRITEME: Handle rejected transactions left in TxnDiff - - // 5) Try to add transactions from this ledger to the new ledger. - std::map txnMap; - for (SHAMapItem::pointer mit = peekTransactionMap()->peekFirstItem(); - !!mit; mit = peekTransactionMap()->peekNextItem(mit->getTag())) - { - uint256 txnID = mit->getTag(); - Transaction::pointer tx = theApp->getMasterTransaction().fetch(txnID, false); - if(!tx) tx = Transaction::sharedTransaction(mit->peekData(), false); - txnMap.insert(std::make_pair(txnID, tx)); - } - - do - { - count = 0; - std::map::iterator it = txnMap.begin(); - while (it != txnMap.end()) - { - if (newLedger->addTransaction(it->second)) // FIXME: addTransaction doesn't do checks - { - ++count; - txnMap.erase(it++); - } - else ++it; - } - } while(count != 0); - - - // WRITEME: Handle rejected transactions left in txnMap - - return Ledger::pointer(newLedger); -} - void Ledger::setAcquiring(void) { if (!mTransactionMap || !mAccountStateMap) throw SHAMapException(InvalidMap);