diff --git a/src/NetworkOPs.cpp b/src/NetworkOPs.cpp index bad18d8862..09620bd1f5 100644 --- a/src/NetworkOPs.cpp +++ b/src/NetworkOPs.cpp @@ -261,10 +261,10 @@ void NetworkOPs::setStateTimer(int sec) class ValidationCount { public: - int trustedValidations, untrustedValidations, nodesUsing; + int trustedValidations, nodesUsing; NewcoinAddress highNode; - ValidationCount() : trustedValidations(0), untrustedValidations(0), nodesUsing(0) { ; } + ValidationCount() : trustedValidations(0), nodesUsing(0) { ; } bool operator>(const ValidationCount& v) { if (trustedValidations > v.trustedValidations) return true; @@ -362,6 +362,19 @@ bool NetworkOPs::checkLastClosedLedger(const std::vector& peerLis // node is using. THis is kind of fundamental. boost::unordered_map ledgers; + + { + boost::unordered_map current = theApp->getValidations().getCurrentValidations(); + for (boost::unordered_map::iterator it = current.begin(), end = current.end(); it != end; ++it) + ledgers[it->first].trustedValidations += it->second; + } + + Ledger::pointer currentClosed = mLedgerMaster->getClosedLedger(); + uint256 closedLedger = currentClosed->getHash(); + ValidationCount& ourVC = ledgers[closedLedger]; + ++ourVC.nodesUsing; + ourVC.highNode = theApp->getWallet().getNodePublic(); + for (std::vector::const_iterator it = peerList.begin(), end = peerList.end(); it != end; ++it) { if (!*it) @@ -375,13 +388,6 @@ bool NetworkOPs::checkLastClosedLedger(const std::vector& peerLis { // FIXME: If we have this ledger, don't count it if it's too far past its close time ValidationCount& vc = ledgers[peerLedger]; - if (vc.nodesUsing == 0) - { - theApp->getValidations().getValidationCount(peerLedger, true, - vc.trustedValidations, vc.untrustedValidations); - Log(lsTRACE) << peerLedger.GetHex() << " has " << vc.trustedValidations << - " trusted validations and " << vc.untrustedValidations << " untrusted"; - } if ((vc.nodesUsing == 0) || ((*it)->getNodePublic() > vc.highNode)) vc.highNode = (*it)->getNodePublic(); ++vc.nodesUsing; @@ -389,17 +395,7 @@ bool NetworkOPs::checkLastClosedLedger(const std::vector& peerLis } } - Ledger::pointer currentClosed = mLedgerMaster->getClosedLedger(); - uint256 closedLedger = currentClosed->getHash(); - ValidationCount& ourVC = ledgers[closedLedger]; - if (ourVC.nodesUsing == 0) - { - ourVC.highNode = theApp->getWallet().getNodePublic(); - theApp->getValidations().getValidationCount(closedLedger, true, - ourVC.trustedValidations, ourVC.untrustedValidations); - } - ++ourVC.nodesUsing; - ValidationCount bestVC = ourVC; + ValidationCount bestVC = ledgers[closedLedger]; // 3) Is there a network ledger we'd like to switch to? If so, do we have it? bool switchLedgers = false; @@ -407,8 +403,7 @@ bool NetworkOPs::checkLastClosedLedger(const std::vector& peerLis it != end; ++it) { Log(lsTRACE) << "L: " << it->first.GetHex() << - " t=" << it->second.trustedValidations << ", u=" << it->second.untrustedValidations << - ", n=" << it->second.nodesUsing; + " t=" << it->second.trustedValidations << ", n=" << it->second.nodesUsing; if (it->second > bestVC) { bestVC = it->second; diff --git a/src/ValidationCollection.cpp b/src/ValidationCollection.cpp index 5061d0d131..e68aecd5ee 100644 --- a/src/ValidationCollection.cpp +++ b/src/ValidationCollection.cpp @@ -74,3 +74,26 @@ void ValidationCollection::getValidationCount(const uint256& ledger, bool curren } } } + +boost::unordered_map ValidationCollection::getCurrentValidations() +{ + uint64 now = theApp->getOPs().getNetworkTimeNC(); + boost::unordered_map ret; + + { + boost::mutex::scoped_lock sl(mValidationLock); + boost::unordered_map::iterator it = mCurrentValidations.begin(); + while (it != mCurrentValidations.end()) + { + if (now > (it->second->getCloseTime() + 2 * LEDGER_INTERVAL)) + it = mCurrentValidations.erase(it); + else + { + ++ret[it->second->getLedgerHash()]; + ++it; + } + } + } + + return ret; +} diff --git a/src/ValidationCollection.h b/src/ValidationCollection.h index 62540f5853..7df9c91bb2 100644 --- a/src/ValidationCollection.h +++ b/src/ValidationCollection.h @@ -24,6 +24,7 @@ public: bool addValidation(SerializedValidation::pointer); ValidationSet getValidations(const uint256& ledger); void getValidationCount(const uint256& ledger, bool currentOnly, int& trusted, int& untrusted); + boost::unordered_map getCurrentValidations(); }; #endif