Merge branch 'master' of github.com:jedmccaleb/NewCoin

This commit is contained in:
Arthur Britto
2012-11-01 17:21:17 -07:00
5 changed files with 42 additions and 15 deletions

View File

@@ -191,6 +191,7 @@ void Application::sweep()
mHashedObjectStore.sweep(); mHashedObjectStore.sweep();
mMasterLedger.sweep(); mMasterLedger.sweep();
mTempNodeCache.sweep(); mTempNodeCache.sweep();
mValidations.sweep();
mSweepTimer.expires_from_now(boost::posix_time::seconds(60)); mSweepTimer.expires_from_now(boost::posix_time::seconds(60));
mSweepTimer.async_wait(boost::bind(&Application::sweep, this)); mSweepTimer.async_wait(boost::bind(&Application::sweep, this));
} }

View File

@@ -4,6 +4,8 @@
#include "HashPrefixes.h" #include "HashPrefixes.h"
#include "Log.h" #include "Log.h"
DECLARE_INSTANCE(SerializedValidation);
std::vector<SOElement::ptr> sValidationFormat; std::vector<SOElement::ptr> sValidationFormat;
static bool SVFInit() static bool SVFInit()

View File

@@ -3,8 +3,11 @@
#include "SerializedObject.h" #include "SerializedObject.h"
#include "RippleAddress.h" #include "RippleAddress.h"
#include "InstanceCounter.h"
class SerializedValidation : public STObject DEFINE_INSTANCE(SerializedValidation);
class SerializedValidation : public STObject, private IS_INSTANCE(SerializedValidation)
{ {
protected: protected:
uint256 mPreviousHash; uint256 mPreviousHash;

View File

@@ -10,6 +10,23 @@
SETUP_LOG(); SETUP_LOG();
typedef std::pair<const uint160, SerializedValidation::pointer> u160_val_pair; typedef std::pair<const uint160, SerializedValidation::pointer> u160_val_pair;
typedef boost::shared_ptr<ValidationSet> VSpointer;
VSpointer ValidationCollection::findCreateSet(const uint256& ledgerHash)
{
VSpointer j = mValidations.fetch(ledgerHash);
if (!j)
{
j = boost::make_shared<ValidationSet>();
mValidations.canonicalize(ledgerHash, j);
}
return j;
}
VSpointer ValidationCollection::findSet(const uint256& ledgerHash)
{
return mValidations.fetch(ledgerHash);
}
bool ValidationCollection::addValidation(const SerializedValidation::pointer& val) bool ValidationCollection::addValidation(const SerializedValidation::pointer& val)
{ {
@@ -37,7 +54,7 @@ bool ValidationCollection::addValidation(const SerializedValidation::pointer& va
{ {
boost::mutex::scoped_lock sl(mValidationLock); boost::mutex::scoped_lock sl(mValidationLock);
if (!mValidations[hash].insert(std::make_pair(node, val)).second) if (!findCreateSet(hash)->insert(std::make_pair(node, val)).second)
return false; return false;
if (isCurrent) if (isCurrent)
{ {
@@ -63,25 +80,24 @@ bool ValidationCollection::addValidation(const SerializedValidation::pointer& va
ValidationSet ValidationCollection::getValidations(const uint256& ledger) ValidationSet ValidationCollection::getValidations(const uint256& ledger)
{ {
ValidationSet ret;
{ {
boost::mutex::scoped_lock sl(mValidationLock); boost::mutex::scoped_lock sl(mValidationLock);
boost::unordered_map<uint256, ValidationSet>::iterator it = mValidations.find(ledger); VSpointer set = findSet(ledger);
if (it != mValidations.end()) if (set != VSpointer())
ret = it->second; return *set;
} }
return ret; return ValidationSet();
} }
void ValidationCollection::getValidationCount(const uint256& ledger, bool currentOnly, int& trusted, int &untrusted) void ValidationCollection::getValidationCount(const uint256& ledger, bool currentOnly, int& trusted, int &untrusted)
{ {
trusted = untrusted = 0; trusted = untrusted = 0;
boost::mutex::scoped_lock sl(mValidationLock); boost::mutex::scoped_lock sl(mValidationLock);
boost::unordered_map<uint256, ValidationSet>::iterator it = mValidations.find(ledger); VSpointer set = findSet(ledger);
uint32 now = theApp->getOPs().getNetworkTimeNC(); uint32 now = theApp->getOPs().getNetworkTimeNC();
if (it != mValidations.end()) if (set)
{ {
for (ValidationSet::iterator vit = it->second.begin(), end = it->second.end(); vit != end; ++vit) for (ValidationSet::iterator vit = set->begin(), end = set->end(); vit != end; ++vit)
{ {
bool isTrusted = vit->second->isTrusted(); bool isTrusted = vit->second->isTrusted();
if (isTrusted && currentOnly) if (isTrusted && currentOnly)
@@ -107,10 +123,10 @@ int ValidationCollection::getTrustedValidationCount(const uint256& ledger)
{ {
int trusted = 0; int trusted = 0;
boost::mutex::scoped_lock sl(mValidationLock); boost::mutex::scoped_lock sl(mValidationLock);
for (boost::unordered_map<uint256, ValidationSet>::iterator it = mValidations.find(ledger), VSpointer set = findSet(ledger);
end = mValidations.end(); it != end; ++it) if (set)
{ {
for (ValidationSet::iterator vit = it->second.begin(), end = it->second.end(); vit != end; ++vit) for (ValidationSet::iterator vit = set->begin(), end = set->end(); vit != end; ++vit)
{ {
if (vit->second->isTrusted()) if (vit->second->isTrusted())
++trusted; ++trusted;

View File

@@ -9,6 +9,7 @@
#include "uint256.h" #include "uint256.h"
#include "types.h" #include "types.h"
#include "SerializedValidation.h" #include "SerializedValidation.h"
#include "TaggedCache.h"
typedef boost::unordered_map<uint160, SerializedValidation::pointer> ValidationSet; typedef boost::unordered_map<uint160, SerializedValidation::pointer> ValidationSet;
@@ -19,7 +20,7 @@ class ValidationCollection
protected: protected:
boost::mutex mValidationLock; boost::mutex mValidationLock;
boost::unordered_map<uint256, ValidationSet> mValidations; TaggedCache<uint256, ValidationSet> mValidations;
boost::unordered_map<uint160, SerializedValidation::pointer> mCurrentValidations; boost::unordered_map<uint160, SerializedValidation::pointer> mCurrentValidations;
std::vector<SerializedValidation::pointer> mStaleValidations; std::vector<SerializedValidation::pointer> mStaleValidations;
@@ -28,8 +29,11 @@ protected:
void doWrite(); void doWrite();
void condWrite(); void condWrite();
boost::shared_ptr<ValidationSet> findCreateSet(const uint256& ledgerHash);
boost::shared_ptr<ValidationSet> findSet(const uint256& ledgerHash);
public: public:
ValidationCollection() : mWriting(false) { ; } ValidationCollection() : mValidations("Validations", 128, 500), mWriting(false) { ; }
bool addValidation(const SerializedValidation::pointer&); bool addValidation(const SerializedValidation::pointer&);
ValidationSet getValidations(const uint256& ledger); ValidationSet getValidations(const uint256& ledger);
@@ -43,6 +47,7 @@ public:
boost::unordered_map<uint256, currentValidationCount> getCurrentValidations(uint256 currentLedger); boost::unordered_map<uint256, currentValidationCount> getCurrentValidations(uint256 currentLedger);
void flush(); void flush();
void sweep() { mValidations.sweep(); }
}; };
#endif #endif