From 166e2b69cba07cb5529a281cc46c13f948a374fd Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Sun, 14 Jul 2013 17:41:02 -0700 Subject: [PATCH] Rename to SharedObjectArray Conflicts: Builds/VisualStudio2012/RippleD.vcxproj.filters --- Builds/VisualStudio2012/RippleD.vcxproj | 14 +- .../VisualStudio2012/RippleD.vcxproj.filters | 12 +- .../Builds/VisualStudio2012/beast.vcxproj | 2 +- .../VisualStudio2012/beast.vcxproj.filters | 6 +- .../beast/modules/beast_core/beast_core.h | 2 +- .../beast_core/containers/beast_Array.h | 4 +- .../containers/beast_ArrayAllocationBase.h | 2 +- .../beast_core/containers/beast_OwnedArray.h | 2 +- ...untedArray.h => beast_SharedObjectArray.h} | 30 +- .../beast_core/containers/beast_SortedSet.h | 2 +- .../beast_core/memory/beast_SharedObject.h | 4 +- .../threads/beast_CriticalSection.h | 2 +- TODO.txt | 40 +-- modules/ripple_core/ripple_core.cpp | 3 +- modules/ripple_core/ripple_core.h | 1 - .../validator/ripple_ChosenValidators.cpp | 27 -- .../validator/ripple_ChosenValidators.h | 41 --- .../ripple_StringsValidatorSource.cpp | 5 +- .../ripple_TrustedUriValidatorSource.cpp | 5 +- .../ripple_core/validator/ripple_Validator.h | 26 +- .../validator/ripple_ValidatorList.cpp | 35 -- .../validator/ripple_ValidatorList.h | 18 +- .../validator/ripple_ValidatorListImp.h | 44 +++ .../validator/ripple_Validators.cpp | 313 ++++++++++++------ .../ripple_core/validator/ripple_Validators.h | 4 +- src/cpp/ripple/ripple_Application.cpp | 5 + 26 files changed, 344 insertions(+), 305 deletions(-) rename Subtrees/beast/modules/beast_core/containers/{beast_ReferenceCountedArray.h => beast_SharedObjectArray.h} (96%) delete mode 100644 modules/ripple_core/validator/ripple_ChosenValidators.cpp delete mode 100644 modules/ripple_core/validator/ripple_ChosenValidators.h delete mode 100644 modules/ripple_core/validator/ripple_ValidatorList.cpp create mode 100644 modules/ripple_core/validator/ripple_ValidatorListImp.h diff --git a/Builds/VisualStudio2012/RippleD.vcxproj b/Builds/VisualStudio2012/RippleD.vcxproj index 89dc69643..12c867c44 100644 --- a/Builds/VisualStudio2012/RippleD.vcxproj +++ b/Builds/VisualStudio2012/RippleD.vcxproj @@ -355,12 +355,6 @@ true - - true - true - true - true - true true @@ -379,12 +373,6 @@ true true - - true - true - true - true - true true @@ -1453,11 +1441,11 @@ - + diff --git a/Builds/VisualStudio2012/RippleD.vcxproj.filters b/Builds/VisualStudio2012/RippleD.vcxproj.filters index b1d09786c..8045b9c9e 100644 --- a/Builds/VisualStudio2012/RippleD.vcxproj.filters +++ b/Builds/VisualStudio2012/RippleD.vcxproj.filters @@ -885,12 +885,6 @@ [1] Ripple\ripple_app\ledger - - [1] Ripple\ripple_core\validator - - - [1] Ripple\ripple_core\validator - [1] Ripple\ripple_core\validator @@ -1665,15 +1659,15 @@ [1] Ripple\ripple_core\validator - - [1] Ripple\ripple_core\validator - [1] Ripple\ripple_core\validator [1] Ripple\ripple_core\validator + + [1] Ripple\ripple_core\validator + diff --git a/Subtrees/beast/Builds/VisualStudio2012/beast.vcxproj b/Subtrees/beast/Builds/VisualStudio2012/beast.vcxproj index 137ce781a..8309bb27d 100644 --- a/Subtrees/beast/Builds/VisualStudio2012/beast.vcxproj +++ b/Subtrees/beast/Builds/VisualStudio2012/beast.vcxproj @@ -120,7 +120,7 @@ - + diff --git a/Subtrees/beast/Builds/VisualStudio2012/beast.vcxproj.filters b/Subtrees/beast/Builds/VisualStudio2012/beast.vcxproj.filters index cc8c9fba4..7b777f508 100644 --- a/Subtrees/beast/Builds/VisualStudio2012/beast.vcxproj.filters +++ b/Subtrees/beast/Builds/VisualStudio2012/beast.vcxproj.filters @@ -160,9 +160,6 @@ beast_core\containers - - beast_core\containers - beast_core\containers @@ -623,6 +620,9 @@ beast_crypto + + beast_core\containers + diff --git a/Subtrees/beast/modules/beast_core/beast_core.h b/Subtrees/beast/modules/beast_core/beast_core.h index cb17e5b69..5235d8a5f 100644 --- a/Subtrees/beast/modules/beast_core/beast_core.h +++ b/Subtrees/beast/modules/beast_core/beast_core.h @@ -239,7 +239,7 @@ namespace beast #include "containers/beast_NamedValueSet.h" #include "containers/beast_OwnedArray.h" #include "containers/beast_PropertySet.h" -#include "containers/beast_ReferenceCountedArray.h" +#include "containers/beast_SharedObjectArray.h" #include "containers/beast_ScopedValueSetter.h" #include "containers/beast_SharedTable.h" #include "containers/beast_SortedLookupTable.h" diff --git a/Subtrees/beast/modules/beast_core/containers/beast_Array.h b/Subtrees/beast/modules/beast_core/containers/beast_Array.h index c3f5a0c7e..92c66f391 100644 --- a/Subtrees/beast/modules/beast_core/containers/beast_Array.h +++ b/Subtrees/beast/modules/beast_core/containers/beast_Array.h @@ -43,7 +43,7 @@ You can of course have an array of pointers to any kind of object, e.g. Array , but if you do this, the array doesn't take any ownership of the objects - see the OwnedArray class or the - ReferenceCountedArray class for more powerful ways of holding lists of objects. + SharedObjectArray class for more powerful ways of holding lists of objects. For holding lists of strings, you can use Array\, but it's usually better to use the specialised class StringArray, which provides more useful functions. @@ -51,7 +51,7 @@ To make all the array's methods thread-safe, pass in "CriticalSection" as the templated TypeOfCriticalSectionToUse parameter, instead of the default DummyCriticalSection. - @see OwnedArray, ReferenceCountedArray, StringArray, CriticalSection + @see OwnedArray, SharedObjectArray, StringArray, CriticalSection */ template class ArrayAllocationBase diff --git a/Subtrees/beast/modules/beast_core/containers/beast_OwnedArray.h b/Subtrees/beast/modules/beast_core/containers/beast_OwnedArray.h index 04a62fc03..697a6e3dc 100644 --- a/Subtrees/beast/modules/beast_core/containers/beast_OwnedArray.h +++ b/Subtrees/beast/modules/beast_core/containers/beast_OwnedArray.h @@ -41,7 +41,7 @@ To make all the array's methods thread-safe, pass in "CriticalSection" as the templated TypeOfCriticalSectionToUse parameter, instead of the default DummyCriticalSection. - @see Array, ReferenceCountedArray, StringArray, CriticalSection + @see Array, SharedObjectArray, StringArray, CriticalSection */ template diff --git a/Subtrees/beast/modules/beast_core/containers/beast_ReferenceCountedArray.h b/Subtrees/beast/modules/beast_core/containers/beast_SharedObjectArray.h similarity index 96% rename from Subtrees/beast/modules/beast_core/containers/beast_ReferenceCountedArray.h rename to Subtrees/beast/modules/beast_core/containers/beast_SharedObjectArray.h index fa57ac029..e140b4767 100644 --- a/Subtrees/beast/modules/beast_core/containers/beast_ReferenceCountedArray.h +++ b/Subtrees/beast/modules/beast_core/containers/beast_SharedObjectArray.h @@ -34,7 +34,7 @@ /** Holds a list of objects derived from SharedObject. - A ReferenceCountedArray holds objects derived from SharedObject, + A SharedObjectArray holds objects derived from SharedObject, and takes care of incrementing and decrementing their ref counts when they are added and removed from the array. @@ -44,7 +44,7 @@ @see Array, OwnedArray, StringArray */ template -class ReferenceCountedArray +class SharedObjectArray { public: typedef SharedObjectPtr ObjectClassPtr; @@ -53,13 +53,13 @@ public: /** Creates an empty array. @see SharedObject, Array, OwnedArray */ - ReferenceCountedArray() noexcept + SharedObjectArray() noexcept : numUsed (0) { } /** Creates a copy of another array */ - ReferenceCountedArray (const ReferenceCountedArray& other) noexcept + SharedObjectArray (const SharedObjectArray& other) noexcept { const ScopedLockType lock (other.getLock()); numUsed = other.size(); @@ -73,9 +73,9 @@ public: /** Creates a copy of another array */ template - ReferenceCountedArray (const ReferenceCountedArray& other) noexcept + SharedObjectArray (const SharedObjectArray& other) noexcept { - const typename ReferenceCountedArray::ScopedLockType lock (other.getLock()); + const typename SharedObjectArray::ScopedLockType lock (other.getLock()); numUsed = other.size(); data.setAllocatedSize (numUsed); memcpy (data.elements, other.getRawDataPointer(), numUsed * sizeof (ObjectClass*)); @@ -88,9 +88,9 @@ public: /** Copies another array into this one. Any existing objects in this array will first be released. */ - ReferenceCountedArray& operator= (const ReferenceCountedArray& other) noexcept + SharedObjectArray& operator= (const SharedObjectArray& other) noexcept { - ReferenceCountedArray otherCopy (other); + SharedObjectArray otherCopy (other); swapWithArray (otherCopy); return *this; } @@ -99,9 +99,9 @@ public: Any existing objects in this array will first be released. */ template - ReferenceCountedArray& operator= (const ReferenceCountedArray& other) noexcept + SharedObjectArray& operator= (const SharedObjectArray& other) noexcept { - ReferenceCountedArray otherCopy (other); + SharedObjectArray otherCopy (other); swapWithArray (otherCopy); return *this; } @@ -109,7 +109,7 @@ public: /** Destructor. Any objects in the array will be released, and may be deleted if not referenced from elsewhere. */ - ~ReferenceCountedArray() + ~SharedObjectArray() { clear(); } @@ -409,7 +409,7 @@ public: all available elements will be copied. @see add */ - void addArray (const ReferenceCountedArray& arrayToAddFrom, + void addArray (const SharedObjectArray& arrayToAddFrom, int startIndex = 0, int numElementsToAdd = -1) noexcept { @@ -740,7 +740,7 @@ public: If you need to exchange two arrays, this is vastly quicker than using copy-by-value because it just swaps their internal pointers. */ - void swapWithArray (ReferenceCountedArray& otherArray) noexcept + void swapWithArray (SharedObjectArray& otherArray) noexcept { const ScopedLockType lock1 (getLock()); const ScopedLockType lock2 (otherArray.getLock()); @@ -754,7 +754,7 @@ public: @returns true only if the other array contains the same objects in the same order */ - bool operator== (const ReferenceCountedArray& other) const noexcept + bool operator== (const SharedObjectArray& other) const noexcept { const ScopedLockType lock2 (other.getLock()); const ScopedLockType lock1 (getLock()); @@ -773,7 +773,7 @@ public: @see operator== */ - bool operator!= (const ReferenceCountedArray& other) const noexcept + bool operator!= (const SharedObjectArray& other) const noexcept { return ! operator== (other); } diff --git a/Subtrees/beast/modules/beast_core/containers/beast_SortedSet.h b/Subtrees/beast/modules/beast_core/containers/beast_SortedSet.h index 93b2725a1..0af215fff 100644 --- a/Subtrees/beast/modules/beast_core/containers/beast_SortedSet.h +++ b/Subtrees/beast/modules/beast_core/containers/beast_SortedSet.h @@ -53,7 +53,7 @@ To make all the set's methods thread-safe, pass in "CriticalSection" as the templated TypeOfCriticalSectionToUse parameter, instead of the default DummyCriticalSection. - @see Array, OwnedArray, ReferenceCountedArray, StringArray, CriticalSection + @see Array, OwnedArray, SharedObjectArray, StringArray, CriticalSection */ template class SortedSet diff --git a/Subtrees/beast/modules/beast_core/memory/beast_SharedObject.h b/Subtrees/beast/modules/beast_core/memory/beast_SharedObject.h index 53a4f2a8f..e73a59be7 100644 --- a/Subtrees/beast/modules/beast_core/memory/beast_SharedObject.h +++ b/Subtrees/beast/modules/beast_core/memory/beast_SharedObject.h @@ -57,7 +57,7 @@ the pointers can be passed between threads safely. For a faster but non-thread-safe version, use SingleThreadedSharedObject instead. - @see SharedObjectPtr, ReferenceCountedArray, SingleThreadedSharedObject + @see SharedObjectPtr, SharedObjectArray, SingleThreadedSharedObject */ class BEAST_API SharedObject : Uncopyable { @@ -126,7 +126,7 @@ private: efficient). For more details on how to use it, see the SharedObject class notes. - @see SharedObject, SharedObjectPtr, ReferenceCountedArray + @see SharedObject, SharedObjectPtr, SharedObjectArray */ class BEAST_API SingleThreadedSharedObject : public Uncopyable { diff --git a/Subtrees/beast/modules/beast_core/threads/beast_CriticalSection.h b/Subtrees/beast/modules/beast_core/threads/beast_CriticalSection.h index 7c8015e8a..28e053c79 100644 --- a/Subtrees/beast/modules/beast_core/threads/beast_CriticalSection.h +++ b/Subtrees/beast/modules/beast_core/threads/beast_CriticalSection.h @@ -122,7 +122,7 @@ private: This is currently used by some templated classes, and most compilers should manage to optimise it out of existence. - @see CriticalSection, Array, OwnedArray, ReferenceCountedArray + @see CriticalSection, Array, OwnedArray, SharedObjectArray */ class BEAST_API DummyCriticalSection : Uncopyable { diff --git a/TODO.txt b/TODO.txt index 9677d785f..908475b5f 100644 --- a/TODO.txt +++ b/TODO.txt @@ -12,6 +12,10 @@ RIPPLE TODO -------------------------------------------------------------------------------- +- Replace endian conversion calls with beast calls: + htobe32, be32toh, ntohl, etc... + Start by removing the system headers which provide these routines, if possible + - Rename RPCHandler to CallHandler - Move everything in src/cpp/ripple into ripple_app and sort them into @@ -389,30 +393,22 @@ Nouns LocalFileValidatorSource - ValidatorSource which reads information from a local file. -TrustedUriList // A copy of this ships with the app -* has a KnownValidators + TrustedUriList // A copy of this ships with the app + * has a KnownValidators -KnownValidators -* A series of KnownValidator that comes from a TrustedUri -* Persistent storage has a timestamp + KnownValidators + * A series of KnownValidator that comes from a TrustedUri + * Persistent storage has a timestamp -RankedValidators -* Created as the union of all KnownValidators with "weight" being the -number of appearances. + RankedValidators + * Created as the union of all KnownValidators with "weight" being the + number of appearances. -ChosenValidators -* Result of the algorithm that chooses a random subset of RankedKnownValidators -* "local health" percentage is the percent of validations from this list that -you've seen recently. And have they been behaving. + ChosenValidators + * Result of the algorithm that chooses a random subset of RankedKnownValidators + * "local health" percentage is the percent of validations from this list that + you've seen recently. And have they been behaving. -Process: +Algorithm -For each TrustedUri: -Perform HTTPS request to retrieve a KnownValidators list - -Create the union of all unique KnownValidator, keyed by public key -- Calculate the number of appearances to determine the "weight" - -Algorithm: - -ChosenValidators chooseRandomSubset (RankedKnownValidators) + When updating a source \ No newline at end of file diff --git a/modules/ripple_core/ripple_core.cpp b/modules/ripple_core/ripple_core.cpp index 00345c1f6..3c9482d65 100644 --- a/modules/ripple_core/ripple_core.cpp +++ b/modules/ripple_core/ripple_core.cpp @@ -31,9 +31,8 @@ namespace ripple #include "functional/ripple_LoadEvent.cpp" #include "functional/ripple_LoadMonitor.cpp" -#include "validator/ripple_ChosenValidators.cpp" #include "validator/ripple_Validator.cpp" -#include "validator/ripple_ValidatorList.cpp" +#include "validator/ripple_ValidatorListImp.h" // private #include "validator/ripple_Validators.cpp" #include "validator/ripple_StringsValidatorSource.cpp" #include "validator/ripple_TrustedUriValidatorSource.cpp" diff --git a/modules/ripple_core/ripple_core.h b/modules/ripple_core/ripple_core.h index cc8a8d56c..4b8b13a48 100644 --- a/modules/ripple_core/ripple_core.h +++ b/modules/ripple_core/ripple_core.h @@ -39,7 +39,6 @@ namespace ripple #include "validator/ripple_Validator.h" #include "validator/ripple_ValidatorList.h" -#include "validator/ripple_ChosenValidators.h" #include "validator/ripple_Validators.h" #include "validator/ripple_StringsValidatorSource.h" #include "validator/ripple_TrustedUriValidatorSource.h" diff --git a/modules/ripple_core/validator/ripple_ChosenValidators.cpp b/modules/ripple_core/validator/ripple_ChosenValidators.cpp deleted file mode 100644 index 9b4b2d9bc..000000000 --- a/modules/ripple_core/validator/ripple_ChosenValidators.cpp +++ /dev/null @@ -1,27 +0,0 @@ -//------------------------------------------------------------------------------ -/* - Copyright (c) 2011-2013, OpenCoin, Inc. -*/ -//============================================================================== - -ChosenValidators::ChosenValidators (ValidatorList::Ptr chosenValidators, - ValidatorList::Ptr wellKnownValidators) - : m_list (chosenValidators) - , m_wellKnownList (wellKnownValidators) -{ -} - -ChosenValidators::~ChosenValidators () -{ -} - -ValidatorList::Ptr ChosenValidators::getList () -{ - return m_list; -} - -ValidatorList::Ptr ChosenValidators::getWellKnownList () -{ - return m_wellKnownList; -} - diff --git a/modules/ripple_core/validator/ripple_ChosenValidators.h b/modules/ripple_core/validator/ripple_ChosenValidators.h deleted file mode 100644 index 96833bdfd..000000000 --- a/modules/ripple_core/validator/ripple_ChosenValidators.h +++ /dev/null @@ -1,41 +0,0 @@ -//------------------------------------------------------------------------------ -/* - Copyright (c) 2011-2013, OpenCoin, Inc. -*/ -//============================================================================== - -#ifndef RIPPLE_CHOSENVALIDATORS_H_INCLUDED -#define RIPPLE_CHOSENVALIDATORS_H_INCLUDED - -/** A subset of validators chosen from a list of well known validators. - - @see Validators -*/ -class ChosenValidators : public SharedObject -{ -public: - typedef SharedObjectPtr Ptr; - - ChosenValidators (ValidatorList::Ptr chosenValidators, - ValidatorList::Ptr wellKnownValidators); - - ~ChosenValidators (); - - /** Retrieve the list of chosen validators. - - This is the subset of validators that we care about. - */ - ValidatorList::Ptr getList (); - - /** Retrieve the list of well known validators. - - This is the set from which the chosen validators were chosen. - */ - ValidatorList::Ptr getWellKnownList (); - -private: - ValidatorList::Ptr m_list; - ValidatorList::Ptr m_wellKnownList; -}; - -#endif diff --git a/modules/ripple_core/validator/ripple_StringsValidatorSource.cpp b/modules/ripple_core/validator/ripple_StringsValidatorSource.cpp index 9bf67bfb3..89375f865 100644 --- a/modules/ripple_core/validator/ripple_StringsValidatorSource.cpp +++ b/modules/ripple_core/validator/ripple_StringsValidatorSource.cpp @@ -15,11 +15,8 @@ public: { } - ValidatorList::Ptr fetch () + void fetch (Array & results) { - ValidatorList::Ptr list = new ValidatorList; - - return list; } private: diff --git a/modules/ripple_core/validator/ripple_TrustedUriValidatorSource.cpp b/modules/ripple_core/validator/ripple_TrustedUriValidatorSource.cpp index 4d54117ca..3b9725f97 100644 --- a/modules/ripple_core/validator/ripple_TrustedUriValidatorSource.cpp +++ b/modules/ripple_core/validator/ripple_TrustedUriValidatorSource.cpp @@ -16,11 +16,8 @@ public: { } - ValidatorList::Ptr fetch () + void fetch (Array & results) { - ValidatorList::Ptr list = new ValidatorList; - - return list; } private: diff --git a/modules/ripple_core/validator/ripple_Validator.h b/modules/ripple_core/validator/ripple_Validator.h index 8844761e6..88d1e9f4d 100644 --- a/modules/ripple_core/validator/ripple_Validator.h +++ b/modules/ripple_core/validator/ripple_Validator.h @@ -7,18 +7,33 @@ #ifndef RIPPLE_VALIDATOR_H_INCLUDED #define RIPPLE_VALIDATOR_H_INCLUDED -/** Identifies a validator. - - A validator signs ledgers and participates in the consensus process. +/** Fixed information on a validator. */ -class Validator +struct ValidatorInfo { -public: // VFALCO TODO magic number argh!!! // This type should be located elsewhere. // typedef UnsignedInteger <33> PublicKey; + PublicKey publicKey; + String friendlyName; + String organizationType; + String jurisdicton; +}; + +/** Identifies a validator. + + A validator signs ledgers and participates in the consensus process. +*/ +class Validator : public SharedObject +{ +public: + typedef SharedObjectPtr Ptr; + + typedef ValidatorInfo::PublicKey PublicKey; + +public: explicit Validator (PublicKey const& publicKey); //Validator (Validator const&); @@ -29,6 +44,7 @@ public: void incrementWeight () { ++m_weight; } private: + ValidatorInfo m_info; PublicKey m_publicKey; int m_weight; }; diff --git a/modules/ripple_core/validator/ripple_ValidatorList.cpp b/modules/ripple_core/validator/ripple_ValidatorList.cpp deleted file mode 100644 index 2b927d1f5..000000000 --- a/modules/ripple_core/validator/ripple_ValidatorList.cpp +++ /dev/null @@ -1,35 +0,0 @@ -//------------------------------------------------------------------------------ -/* - Copyright (c) 2011-2013, OpenCoin, Inc. -*/ -//============================================================================== - -ValidatorList::ValidatorList () - : m_isSigned (false) -{ -} - -ValidatorList::~ValidatorList () -{ -} - -int ValidatorList::size () const -{ - return m_list.size (); -} - -Validator& ValidatorList::operator [] (int index) -{ - return m_list.getReference (index); -} - -bool ValidatorList::isSigned () const -{ - return m_isSigned; -} - -void ValidatorList::add (Validator const& validator) -{ - m_list.add (validator); -} - diff --git a/modules/ripple_core/validator/ripple_ValidatorList.h b/modules/ripple_core/validator/ripple_ValidatorList.h index ad0eed1c8..f7cc2849e 100644 --- a/modules/ripple_core/validator/ripple_ValidatorList.h +++ b/modules/ripple_core/validator/ripple_ValidatorList.h @@ -18,27 +18,17 @@ class ValidatorList : public SharedObject public: typedef SharedObjectPtr Ptr; - /** Create an empty list. - */ - ValidatorList (); - - ~ValidatorList (); + virtual ~ValidatorList () { } /** Retrieve the number of items. */ - int size () const; + virtual int size () const noexcept = 0; - Validator& operator[] (int index); - - bool isSigned () const; + virtual Validator::Ptr operator[] (int index) = 0; /** Add a validator to the list. */ - void add (Validator const& validator); - -private: - bool m_isSigned; - Array m_list; + virtual void add (Validator::Ptr validator) = 0; }; #endif diff --git a/modules/ripple_core/validator/ripple_ValidatorListImp.h b/modules/ripple_core/validator/ripple_ValidatorListImp.h new file mode 100644 index 000000000..f14dfefaa --- /dev/null +++ b/modules/ripple_core/validator/ripple_ValidatorListImp.h @@ -0,0 +1,44 @@ +//------------------------------------------------------------------------------ +/* + Copyright (c) 2011-2013, OpenCoin, Inc. +*/ +//============================================================================== + +#ifndef RIPPLE_VALIDATORLISTIMP_H_INCLUDED +#define RIPPLE_VALIDATORLISTIMP_H_INCLUDED + +// This is a private header + +/** Private implementation of ValidatorList. +*/ +class ValidatorListImp : public ValidatorList +{ +public: + ValidatorListImp () + { + } + + ~ValidatorListImp () + { + } + + int size () const + { + return m_list.size (); + } + + Validator::Ptr operator[] (int index) + { + return m_list.getObjectPointer (index); + } + + void add (Validator::Ptr validator) + { + m_list.add (validator); + } + +private: + SharedObjectArray m_list; +}; + +#endif diff --git a/modules/ripple_core/validator/ripple_Validators.cpp b/modules/ripple_core/validator/ripple_Validators.cpp index 1f1f50f56..04ac4c29c 100644 --- a/modules/ripple_core/validator/ripple_Validators.cpp +++ b/modules/ripple_core/validator/ripple_Validators.cpp @@ -15,7 +15,6 @@ Two important questions: */ - class ValidatorsImp : public Validators , private ThreadWithCallQueue::EntryPoints @@ -25,15 +24,37 @@ public: // Tunable constants enum { - hoursBetweenFetches = 24, + // We will fetch a source at this interval + hoursBetweenFetches = 24 - secondsBetweenFetches = hoursBetweenFetches * 60 * 60, + ,secondsBetweenFetches = hoursBetweenFetches * 60 * 60 - timerGranularity = 60 * 60 // Wake up every hour + // Wake up every hour to check source times + ,timerGranularity = 60 * 60 + + // This tunes the preallocated arrays + ,expectedNumberOfResults = 1000 }; //-------------------------------------------------------------------------- + struct ValidatorInfoCompare + { + static int compareElements (ValidatorInfo const& lhs, ValidatorInfo const& rhs) + { + int result; + + if (lhs.publicKey < rhs.publicKey) + result = -1; + else if (lhs.publicKey > rhs.publicKey) + result = 1; + else + result = 0; + + return result; + } + }; + struct SourceInfo { enum Status @@ -46,22 +67,122 @@ public: explicit SourceInfo (Source* source_) : source (source_) , status (statusNone) + , numberOfFailures (0) { } Source* source; Status status; - Time lastFetch; - ValidatorList::Ptr list; + Time whenToFetch; + + /** The number of times a fetch has failed. + */ + int numberOfFailures; + }; + + // This is what comes back from a source + typedef Array SourceInfoArray; + + typedef Array ValidatorInfoArray; + + // The result of performing a fetch + struct FetchResult + { + // This is what comes back from the fetch + ValidatorInfoArray updatedList; + + // The original list before the fetch + SharedObjectArray oldList; + + // The new list after the fetch + SharedObjectArray newList; + + // The list of validators that were added + SharedObjectArray addedList; + + // The list of validators that were removed + SharedObjectArray removedList; + + FetchResult () + { + updatedList.ensureStorageAllocated (expectedNumberOfResults); + oldList.ensureStorageAllocated (expectedNumberOfResults); + newList.ensureStorageAllocated (expectedNumberOfResults); + addedList.ensureStorageAllocated (expectedNumberOfResults); + removedList.ensureStorageAllocated (expectedNumberOfResults); + } + + void clear () + { + updatedList.clearQuick (); + oldList.clear (); + newList.clear (); + addedList.clear (); + removedList.clear (); + } + }; + + //-------------------------------------------------------------------------- + + // Encapsulates the logic for creating the chosen validators. + // This is a separate class to facilitate the unit tests. + // + class Logic + { + public: + Logic () + : m_knownValidators (new ValidatorListImp) + , m_chosenValidators (new ValidatorListImp) + { + } + + void addSource (Source* source) + { + m_sources.add (source); + + m_sourceInfo.add (SourceInfo (source)); + } + + SourceInfoArray& getSources () + { + return m_sourceInfo; + } + + void processValidatorInfo (ValidatorInfoArray& fetchResults) + { + } + + void sortValidatorInfo (ValidatorInfoArray& arrayToSort) + { + ValidatorInfoArray sorted; + + sorted.ensureStorageAllocated (arrayToSort.size ()); + + for (int i = 0; i < arrayToSort.size (); ++i) + sorted.addSorted (ValidatorInfoCompare(), arrayToSort [i]); + + arrayToSort.swapWithArray (sorted); + } + + void fetchSource (SourceInfo& sourceInfo) + { + + } + + private: + OwnedArray m_sources; + SourceInfoArray m_sourceInfo; + ValidatorList::Ptr m_knownValidators; + ValidatorList::Ptr m_chosenValidators; }; //-------------------------------------------------------------------------- public: explicit ValidatorsImp (Validators::Listener* listener) - : m_thread ("Validators") + : m_listener (listener) + , m_thread ("Validators") , m_timer (this) - , m_listener (listener) { m_thread.start (this); } @@ -77,9 +198,7 @@ public: void doAddSource (Source* source) { - m_sources.add (source); - - m_info.add (SourceInfo (source)); + m_logic.addSource (source); } void onDeadlineTimer (DeadlineTimer&) @@ -89,63 +208,6 @@ public: m_thread.interrupt (); } - void mergeValidators (ValidatorList::Ptr dest, ValidatorList::Ptr source) - { - } - - // Construct the list of well known validators - ValidatorList::Ptr buildWellKnownValidators () - { - ValidatorList::Ptr list = new ValidatorList; - - // Go through each source and merge its list - for (int i = 0; i < m_info.size (); ++i) - { - SourceInfo const& info (m_info.getReference (i)); - - if (info.status == SourceInfo::statusFetched) - { - mergeValidators (list, info.list); - } - } - - return list; - } - - // Choose a subset of validators from the well known list - // - ValidatorList::Ptr chooseSubset (ValidatorList::Ptr list) - { - ValidatorList::Ptr result = new ValidatorList; - - return result; - } - - // Create a composite object representing the chosen validators. - // - ChosenValidators::Ptr createChosenValidators () - { - ValidatorList::Ptr wellKnownValidators = buildWellKnownValidators (); - - ValidatorList::Ptr validatorSubset = chooseSubset (wellKnownValidators); - - ChosenValidators::Ptr chosenValidators = new ChosenValidators ( - validatorSubset, - wellKnownValidators); - - return chosenValidators; - } - - // Create a fresh chosen validators from our source information - // and broadcast it. - // - void updateChosenValidators () - { - ChosenValidators::Ptr chosenValidators = createChosenValidators (); - - m_listener->onValidatorsChosen (chosenValidators); - } - // Goes through all the sources and refreshes them as needed // bool scanSources () @@ -154,28 +216,43 @@ public: Time currentTime = Time::getCurrentTime (); + ValidatorInfoArray fetchResults; + fetchResults.ensureStorageAllocated (1000); + + SourceInfoArray sourceInfoArray (m_logic.getSources ()); + // Find a source that needs to be processed // - for (int i = 0; i < m_info.size (); ++i) + for (int i = 0; i < sourceInfoArray.size (); ++i) { - SourceInfo& info (m_info.getReference (i)); + SourceInfo& sourceInfo (sourceInfoArray.getReference (i)); // See if we need to refresh its list // - if ((currentTime - info.lastFetch).inSeconds () > secondsBetweenFetches) + if (currentTime <= sourceInfo.whenToFetch) { - ValidatorList::Ptr list = info.source->fetch (); + sourceInfo.source->fetch (fetchResults); currentTime = Time::getCurrentTime (); - if (list != nullptr) + if (fetchResults.size () != 0) { - info.status = SourceInfo::statusFetched; - info.lastFetch = currentTime; - info.list = list; + sourceInfo.status = SourceInfo::statusFetched; + sourceInfo.whenToFetch = currentTime + RelativeTime (hoursBetweenFetches * 60.0 * 60.0); - updateChosenValidators (); + m_logic.processValidatorInfo (fetchResults); + + //m_listener->onValidatorsChosen (chosenValidators); } + else + { + // Failed to fetch + // Don't update fetch time + sourceInfo.status = SourceInfo::statusFailed; + sourceInfo.numberOfFailures++; + } + + fetchResults.clearQuick (); } interrupted = m_thread.interruptionPoint (); @@ -206,12 +283,10 @@ public: } private: + Logic m_logic; + Validators::Listener* const m_listener; ThreadWithCallQueue m_thread; DeadlineTimer m_timer; - Validators::Listener* const m_listener; - OwnedArray m_sources; - Array m_info; - ValidatorList::Ptr m_chosenValidators; }; Validators* Validators::New (Listener* listener) @@ -221,36 +296,28 @@ Validators* Validators::New (Listener* listener) //------------------------------------------------------------------------------ -/** Produces validators for unit tests. -*/ +// Produces validators for unit tests. class TestValidatorSource : public Validators::Source { public: - static Validator createValidator (unsigned int publicKeyIndex) - { - Validator validator (Validator::PublicKey::createFromInteger (publicKeyIndex)); - - return validator; - } - - TestValidatorSource (unsigned startIndex, int numEntries) + TestValidatorSource (unsigned int startIndex, int numEntries) : m_startIndex (startIndex) , m_numEntries (numEntries) { } - ValidatorList::Ptr fetch () + void fetch (Array & results) { - ValidatorList::Ptr list = new ValidatorList; - for (unsigned int publicKeyIndex = m_startIndex; publicKeyIndex < m_startIndex + m_numEntries; ++publicKeyIndex) { - list->add (createValidator (publicKeyIndex)); - } + ValidatorInfo info; - return list; + info.publicKey = Validator::PublicKey::createFromInteger (publicKeyIndex); + + results.add (info); + } } private: @@ -267,10 +334,60 @@ public: { } + void publicKeyTest () + { + beginTest ("compare"); + + ValidatorInfo::PublicKey one (ValidatorInfo::PublicKey::createFromInteger (1U)); + ValidatorInfo::PublicKey two (ValidatorInfo::PublicKey::createFromInteger (2U)); + + expect (one < two, "should be less"); + expect (two > one, "should be greater"); + } + + void fetchTest () + { + beginTest ("fetch"); + + TestValidatorSource source (0, 32); + + ValidatorsImp::ValidatorInfoArray results; + + source.fetch (results); + + expect (results.size () == 32, "size should be 32"); + } + void runTest () { - beginTest ("ValidatorList"); + publicKeyTest (); + + fetchTest (); } }; template class UnitTestType ; + +/* + +Goal: + + Provide the listener with a ValidatorList. + - This forms the UNL + +Task: + + fetch ValidatorInfo array from a source + + - We have the old one and the new one, compute the following: + + * unchanged validators list + * new validators list + * removed validators list + + - From the unchanged / new / removed, figure out what to do. + +*/ + + + diff --git a/modules/ripple_core/validator/ripple_Validators.h b/modules/ripple_core/validator/ripple_Validators.h index 922c0ee60..a89963131 100644 --- a/modules/ripple_core/validator/ripple_Validators.h +++ b/modules/ripple_core/validator/ripple_Validators.h @@ -33,7 +33,7 @@ public: This call blocks. */ - virtual ValidatorList::Ptr fetch () = 0; + virtual void fetch (Array & results) = 0; }; public: @@ -42,7 +42,7 @@ public: class Listener { public: - virtual void onValidatorsChosen (ChosenValidators::Ptr list) { } + virtual void onValidatorsChosen (ValidatorList::Ptr list) { } }; public: diff --git a/src/cpp/ripple/ripple_Application.cpp b/src/cpp/ripple/ripple_Application.cpp index b8fa56f20..5fbaadf84 100644 --- a/src/cpp/ripple/ripple_Application.cpp +++ b/src/cpp/ripple/ripple_Application.cpp @@ -343,12 +343,17 @@ private: bool volatile mShutdown; }; +// VFALCO TODO Why do we even have this function? +// It could just be handled in the destructor. +// void ApplicationImp::stop () { WriteLog (lsINFO, Application) << "Received shutdown request"; StopSustain (); mShutdown = true; mIOService.stop (); + // VFALCO TODO We shouldn't have to explicitly call this function. + // The NodeStore destructor should take care of it. m_nodeStore.waitWrite (); mValidations->flush (); mAuxService.stop ();