From dcf4ad2c217e96db9de864a68bf1e20eb0973482 Mon Sep 17 00:00:00 2001 From: David Schwartz Date: Mon, 16 Jun 2014 10:24:39 -0700 Subject: [PATCH] Ledger load and ledger replay fixes: * Stash the loaded ledger where consensus can find it. * When loading a ledger for startup, try the backend too * Apply replay transactions to a mutable snapshot --- src/ripple/module/app/main/Application.cpp | 45 ++++++++++++++++++---- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/src/ripple/module/app/main/Application.cpp b/src/ripple/module/app/main/Application.cpp index 4f3e503dd..e91acdcb9 100644 --- a/src/ripple/module/app/main/Application.cpp +++ b/src/ripple/module/app/main/Application.cpp @@ -1232,7 +1232,8 @@ bool ApplicationImp::loadOldLedger ( m_journal.warning << "Invalid entry in ledger"; } } - // TODO(david): close ledger, update hash + + loadLedger->setAccepted(); } } } @@ -1245,6 +1246,16 @@ bool ApplicationImp::loadOldLedger ( uint256 hash; hash.SetHex (ledgerID); loadLedger = Ledger::loadByHash (hash); + + if (!loadLedger) + { + // Try to build the ledger from the back end + auto il = std::make_shared (hash, 0, InboundLedger::fcGENERIC, + get_seconds_clock ()); + if (il->checkLocal ()) + loadLedger = il->getLedger (); + } + } else // assume by sequence loadLedger = Ledger::loadByIndex ( @@ -1265,12 +1276,23 @@ bool ApplicationImp::loadOldLedger ( replayLedger = loadLedger; // this is the prior ledger - loadLedger = Ledger::loadByIndex (replayLedger->getLedgerSeq() - 1); - if (!loadLedger || (replayLedger->getParentHash() != loadLedger->getHash())) + loadLedger = Ledger::loadByHash (replayLedger->getParentHash ()); + if (!loadLedger) { - m_journal.fatal << "Replay ledger missing/damaged"; - assert (false); - return false; + + // Try to build the ledger from the back end + auto il = std::make_shared ( + replayLedger->getParentHash(), 0, InboundLedger::fcGENERIC, + get_seconds_clock ()); + if (il->checkLocal ()) + loadLedger = il->getLedger (); + + if (!loadLedger) + { + m_journal.fatal << "Replay ledger missing/damaged"; + assert (false); + return false; + } } } @@ -1308,9 +1330,13 @@ bool ApplicationImp::loadOldLedger ( if (replay) { - // inject transaction from replayLedger into consensus set + // inject transaction(s) from the replayLedger into our open ledger SHAMap::ref txns = replayLedger->peekTransactionMap(); - Ledger::ref cur = getLedgerMaster().getCurrentLedger(); + + // Get a mutable snapshot of the open ledger + Ledger::pointer cur = getLedgerMaster().getCurrentLedger(); + cur = std::make_shared (*cur, true); + assert (!cur->isImmutable()); for (auto it = txns->peekFirstItem(); it != nullptr; it = txns->peekNextItem(it->getTag())) @@ -1322,6 +1348,9 @@ bool ApplicationImp::loadOldLedger ( if (!cur->addTransaction(it->getTag(), s)) m_journal.warning << "Unable to add transaction " << it->getTag(); } + + // Switch to the mutable snapshot + m_ledgerMaster->switchLedgers (loadLedger, cur); } } catch (SHAMapMissingNode&)