From 632bf2e5860b143bc9e0b5b081f812065d1d67ec Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Mon, 9 Jul 2012 15:56:36 -0700 Subject: [PATCH] Updates to CLC. --- src/LedgerConsensus.cpp | 32 +++++++++++++++++++++++++++----- src/LedgerConsensus.h | 7 ++++--- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/LedgerConsensus.cpp b/src/LedgerConsensus.cpp index d9e525dd19..847df5ea2c 100644 --- a/src/LedgerConsensus.cpp +++ b/src/LedgerConsensus.cpp @@ -196,7 +196,8 @@ int LCTransaction::getAgreeLevel() } LedgerConsensus::LedgerConsensus(const uint256& prevLCLHash, Ledger::pointer previousLedger, uint64 closeTime) - : mState(lcsPRE_CLOSE), mCloseTime(closeTime), mPrevLedgerHash(prevLCLHash), mPreviousLedger(previousLedger) + : mState(lcsPRE_CLOSE), mCloseTime(closeTime), mPrevLedgerHash(prevLCLHash), mPreviousLedger(previousLedger), + mPreviousProposers(0), mPreviousSeconds(0) { mValSeed = theConfig.VALIDATION_SEED; Log(lsDEBUG) << "Creating consensus object"; @@ -357,10 +358,15 @@ int LedgerConsensus::startup() int LedgerConsensus::statePreClose(int currentSeconds) { // it is shortly before ledger close time - if (ConinuousLedgerTiming::shouldClose()) + bool anyTransactions = mNetOps->getCurrentLedger()->peekTransactionMap()->getHash().isNonZero(); + int proposersClosed = mPeerPositions.size(); + + if (ContinuousLedgerTiming::shouldClose(anyTransactions, mPreviousProposers, proposersClosed, + mPreviousSeconds, currentSeconds)) { // it is time to close the ledger (swap default and wobble ledgers) Log(lsINFO) << "Closing ledger"; mState = lcsESTABLISH; + mConsensusStartTime = boost::posix_time::second_clock::universal_time(); theApp->getMasterLedger().closeTime(); statusChange(newcoin::neCLOSING_LEDGER, mPreviousLedger); Ledger::pointer initial = theApp->getMasterLedger().endWobble(); @@ -437,10 +443,9 @@ void LedgerConsensus::timerEntry() assert(false); } -bool LedgerConsensus::updateOurPositions(int percentPrevConverge) -{ // returns true if the network has consensus +void LedgerConsensus::updateOurPositions(int percentPrevConverge) +{ bool changes = false; - bool stable = true; SHAMap::pointer ourPosition; std::vector addedTx, removedTx; @@ -482,6 +487,23 @@ bool LedgerConsensus::updateOurPositions(int percentPrevConverge) return stable; } +bool LedgerConsensus::haveConsensus(int currentSeconds) +{ + int agree = 0, disagree = 0; + uint256 ourPosition = mOurPosition->getCurrentHash(); + for (boost::unordered_map::iterator it = mPeerPosition.begin(), + end = mPeerPositions.end(); it != end; ++it) + { + if (it->second->getCurrentHash() == ourPosition) + ++agree; + else + ++disagree; + } + int currentValidations = + return ContinuousLedgerTiming::haveConsensus(mPreviousProposers, agree + disagree, agree, currentValidations, + prevagreetime, currentSeconds); +} + SHAMap::pointer LedgerConsensus::getTransactionTree(const uint256& hash, bool doAcquire) { boost::unordered_map::iterator it = mComplete.find(hash); diff --git a/src/LedgerConsensus.h b/src/LedgerConsensus.h index ebfdec6ccf..5bd51e24d5 100644 --- a/src/LedgerConsensus.h +++ b/src/LedgerConsensus.h @@ -87,8 +87,9 @@ protected: NewcoinAddress mValSeed; bool mProposing, mValidating, mHaveCorrectLCL; - int mPreviousClose; // The number of seconds the previous ledger took to close - int mPreviousValidators; // The number of validations for the previous ledger + boost::posix_time::ptime mConsensusStartTime; + int mPreviousProposers; + int mPreviousSeconds; // Convergence tracking, trusted peers indexed by hash of public key boost::unordered_map mPeerPositions; @@ -126,7 +127,7 @@ protected: // manipulating our own position void takeInitialPosition(Ledger::pointer initialLedger); - bool updateOurPositions(int percentPrevConverge); + void updateOurPositions(int percentPrevConverge); void statusChange(newcoin::NodeEvent, Ledger::pointer ledger); int getThreshold(); void beginAccept();