Sanitize validation handling.

This commit is contained in:
JoelKatz
2012-06-20 18:01:38 -07:00
parent b47f76a21f
commit 93a05d55ca
3 changed files with 41 additions and 22 deletions

View File

@@ -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<Peer::pointer>& peerLis
// node is using. THis is kind of fundamental.
boost::unordered_map<uint256, ValidationCount> ledgers;
{
boost::unordered_map<uint256, int> current = theApp->getValidations().getCurrentValidations();
for (boost::unordered_map<uint256, int>::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<Peer::pointer>::const_iterator it = peerList.begin(), end = peerList.end(); it != end; ++it)
{
if (!*it)
@@ -375,13 +388,6 @@ bool NetworkOPs::checkLastClosedLedger(const std::vector<Peer::pointer>& 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<Peer::pointer>& 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<Peer::pointer>& 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;

View File

@@ -74,3 +74,26 @@ void ValidationCollection::getValidationCount(const uint256& ledger, bool curren
}
}
}
boost::unordered_map<uint256, int> ValidationCollection::getCurrentValidations()
{
uint64 now = theApp->getOPs().getNetworkTimeNC();
boost::unordered_map<uint256, int> ret;
{
boost::mutex::scoped_lock sl(mValidationLock);
boost::unordered_map<uint160, SerializedValidation::pointer>::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;
}

View File

@@ -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<uint256, int> getCurrentValidations();
};
#endif