From dda06d989d850fa89c6b884b4c2d752a6a5991b1 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Thu, 21 Jun 2012 17:37:27 -0700 Subject: [PATCH] Begin fixing consensus without having the full consensus LCL. --- src/LedgerConsensus.cpp | 15 +++++++++++++++ src/LedgerConsensus.h | 2 +- src/NetworkOPs.cpp | 29 +++++++++++++---------------- src/NetworkOPs.h | 4 ++-- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/src/LedgerConsensus.cpp b/src/LedgerConsensus.cpp index 0eaed74ce4..77b5379690 100644 --- a/src/LedgerConsensus.cpp +++ b/src/LedgerConsensus.cpp @@ -413,6 +413,20 @@ int LedgerConsensus::stateAccepted(int secondsSinceClose) int LedgerConsensus::timerEntry() { + if (!mHaveCorrectLCL) + { + Ledger::pointer consensus = theApp->getMasterLedger().getLedgerByHash(mPrevLedgerHash); + if (consensus) + { + Log(lsINFO) << "We have acquired the conensus ledger"; + if (theApp->getMasterLedger().getClosedLedger()->getHash() != mPrevLedgerHash) + theApp->getOPs().switchLastClosedLedger(consensus); + mPreviousLedger = consensus; + mHaveCorrectLCL = true; + // FIXME: We need some kind of idea what the consensus transaction set is + } + } + int sinceClose = theApp->getOPs().getNetworkTimeNC() - mCloseTime; switch (mState) @@ -836,6 +850,7 @@ void LedgerConsensus::accept(SHAMap::pointer set) } else Log(lsWARNING) << "Not validating"; statusChange(newcoin::neACCEPTED_LEDGER, newLCL); + // FIXME: If necessary, change state to TRACKING/FULL } void LedgerConsensus::endConsensus() diff --git a/src/LedgerConsensus.h b/src/LedgerConsensus.h index fa8be9ceb3..4c20659c20 100644 --- a/src/LedgerConsensus.h +++ b/src/LedgerConsensus.h @@ -137,7 +137,7 @@ public: int startup(); Ledger::pointer peekPreviousLedger() { return mPreviousLedger; } - uint256 getLCL() { return mPreviousLedger->getHash(); } + uint256 getLCL() { return mPrevLedgerHash; } SHAMap::pointer getTransactionTree(const uint256& hash, bool doAcquire); TransactionAcquire::pointer getAcquiring(const uint256& hash); diff --git a/src/NetworkOPs.cpp b/src/NetworkOPs.cpp index fb48de2ee5..ebf6284143 100644 --- a/src/NetworkOPs.cpp +++ b/src/NetworkOPs.cpp @@ -308,13 +308,8 @@ void NetworkOPs::checkState(const boost::system::error_code& result) // FIXME: Don't check unless last closed ledger is at least some seconds old // If full or tracking, check only at wobble time! - if (checkLastClosedLedger(peerList)) - { - // FIXME: if connected, go to ledger close process if it's time, marking that we have - // the wrong LCL - setStateTimer(3); - return; - } + uint256 networkClosed; + bool ledgerChange = checkLastClosedLedger(peerList, networkClosed); // WRITEME: Unless we are in omFULL and in the process of doing a consensus, // we must count how many nodes share our LCL, how many nodes disagree with our LCL, @@ -322,13 +317,13 @@ void NetworkOPs::checkState(const boost::system::error_code& result) // there shouldn't be a newer LCL. We need this information to do the next three // tests. - if (mMode == omCONNECTED) + if ((mMode == omCONNECTED) && !ledgerChange) { // count number of peers that agree with us and UNL nodes whose validations we have for LCL // if the ledger is good enough, go to omTRACKING - TODO setMode(omTRACKING); } - if (mMode == omTRACKING) + if ((mMode == omTRACKING) && !ledgerChange) { // check if the ledger is good enough to go to omFULL // Note: Do not go to omFULL if we don't have the previous ledger @@ -348,13 +343,13 @@ void NetworkOPs::checkState(const boost::system::error_code& result) int secondsToClose = theApp->getMasterLedger().getCurrentLedger()->getCloseTimeNC() - theApp->getOPs().getNetworkTimeNC(); if ((!mConsensus) && (secondsToClose < LEDGER_WOBBLE_TIME)) // pre close wobble - beginConsensus(theApp->getMasterLedger().getCurrentLedger()); + beginConsensus(networkClosed, theApp->getMasterLedger().getCurrentLedger()); if (mConsensus) setStateTimer(mConsensus->timerEntry()); else setStateTimer(4); } -bool NetworkOPs::checkLastClosedLedger(const std::vector& peerList) +bool NetworkOPs::checkLastClosedLedger(const std::vector& peerList, uint256& networkClosed) { // Returns true if there's an *abnormal* ledger issue, normal changing in TRACKING mode should return false // Do we have sufficient validations for our last closed ledger? Or do sufficient nodes // agree? And do we have no better ledger available? @@ -372,8 +367,8 @@ bool NetworkOPs::checkLastClosedLedger(const std::vector& peerLis ledgers[it->first].trustedValidations += it->second; } - Ledger::pointer currentClosed = mLedgerMaster->getClosedLedger(); - uint256 closedLedger = currentClosed->getHash(); + Ledger::pointer ourClosed = mLedgerMaster->getClosedLedger(); + uint256 closedLedger = ourClosed->getHash(); ValidationCount& ourVC = ledgers[closedLedger]; ++ourVC.nodesUsing; ourVC.highNode = theApp->getWallet().getNodePublic(); @@ -414,14 +409,16 @@ bool NetworkOPs::checkLastClosedLedger(const std::vector& peerLis switchLedgers = true; } } + networkClosed = closedLedger; if (!switchLedgers) return false; Log(lsWARNING) << "We are not running on the consensus ledger"; - Log(lsINFO) << "Our LCL " << currentClosed->getHash().GetHex(); + Log(lsINFO) << "Our LCL " << ourClosed->getHash().GetHex(); Log(lsINFO) << "Net LCL " << closedLedger.GetHex(); if ((mMode == omTRACKING) || (mMode == omFULL)) setMode(omCONNECTED); + Ledger::pointer consensus = mLedgerMaster->getLedgerByHash(closedLedger); if (!consensus) { @@ -490,7 +487,7 @@ void NetworkOPs::switchLastClosedLedger(Ledger::pointer newLedger) theApp->getConnectionPool().relayMessage(NULL, packet); } -int NetworkOPs::beginConsensus(Ledger::pointer closingLedger) +int NetworkOPs::beginConsensus(const uint256& networkClosed, Ledger::pointer closingLedger) { Log(lsINFO) << "Ledger close time for ledger " << closingLedger->getLedgerSeq() ; Log(lsINFO) << " LCL is " << closingLedger->getParentHash().GetHex(); @@ -509,7 +506,7 @@ int NetworkOPs::beginConsensus(Ledger::pointer closingLedger) if (!!mConsensus) mConsensus->abort(); prevLedger->setImmutable(); mConsensus = boost::make_shared( - prevLedger->getHash(), // FIXME: Only do this if the previous ledger is the consensus previous ledger + networkClosed, prevLedger, theApp->getMasterLedger().getCurrentLedger()->getCloseTimeNC()); Log(lsDEBUG) << "Pre-close time, initiating consensus engine"; diff --git a/src/NetworkOPs.h b/src/NetworkOPs.h index 58feca7a45..3925f150be 100644 --- a/src/NetworkOPs.h +++ b/src/NetworkOPs.h @@ -124,8 +124,8 @@ public: // network state machine void checkState(const boost::system::error_code& result); void switchLastClosedLedger(Ledger::pointer newLedger); // Used for the "jump" case - bool checkLastClosedLedger(const std::vector &); - int beginConsensus(Ledger::pointer closingLedger); + bool checkLastClosedLedger(const std::vector&, uint256& networkClosed); + int beginConsensus(const uint256& networkClosed, Ledger::pointer closingLedger); void endConsensus(); void setStateTimer(int seconds); Json::Value getServerInfo();