Split up ripple_SerializedObject.h

This commit is contained in:
Vinnie Falco
2013-06-07 14:39:35 -07:00
parent ef232f4359
commit 6d8ee90a8d
12 changed files with 237 additions and 139 deletions

View File

@@ -23,14 +23,7 @@ enum SerializedTypeID
STI_VALIDATION = 10003,
};
enum SOE_Flags
{
SOE_INVALID = -1,
SOE_REQUIRED = 0, // required
SOE_OPTIONAL = 1, // optional, may be present with default value
SOE_DEFAULT = 2, // optional, if present, must not have default value
};
// VFALCO: TODO, rename this to NamedField
class SField
{
public:

View File

@@ -117,22 +117,6 @@ UPTR_T<SerializedType> STObject::makeDeserializedObject(SerializedTypeID id, SFi
}
}
void SOTemplate::push_back(const SOElement &r)
{
if (mIndex.empty())
mIndex.resize(SField::getNumFields() + 1, -1);
assert(r.e_field.getNum() < mIndex.size());
assert(getIndex(r.e_field) == -1);
mIndex[r.e_field.getNum()] = mTypes.size();
mTypes.push_back(new SOElement(r));
}
int SOTemplate::getIndex(SField::ref f) const
{
assert(f.getNum() < mIndex.size());
return mIndex[f.getNum()];
}
void STObject::set(const SOTemplate& type)
{
mData.clear();

View File

@@ -4,41 +4,17 @@
DEFINE_INSTANCE(SerializedObject);
DEFINE_INSTANCE(SerializedArray);
// Serializable object/array types
class SOElement
{ // An element in the description of a serialized object
public:
SField::ref e_field;
const SOE_Flags flags;
SOElement(SField::ref fi, SOE_Flags fl) : e_field(fi), flags(fl) { ; }
};
class SOTemplate
{
public:
SOTemplate() { ; }
const std::vector<const SOElement*>& peek() const { return mTypes; }
void push_back(const SOElement& r);
int getIndex(SField::ref) const;
private:
std::vector<const SOElement*> mTypes;
std::vector<int> mIndex; // field num -> index
};
class STObject : public SerializedType, private IS_INSTANCE(SerializedObject)
{
public:
STObject() : mType(NULL) { ; }
STObject(SField::ref name) : SerializedType(name), mType(NULL) { ; }
explicit STObject(SField::ref name) : SerializedType(name), mType(NULL) { ; }
STObject(const SOTemplate& type, SField::ref name) : SerializedType(name)
STObject (const SOTemplate& type, SField::ref name) : SerializedType(name)
{ set(type); }
STObject(const SOTemplate& type, SerializerIterator& sit, SField::ref name) : SerializedType(name)
STObject (const SOTemplate& type, SerializerIterator& sit, SField::ref name) : SerializedType(name)
{ set(sit); setType(type); }
UPTR_T<STObject> oClone() const { return UPTR_T<STObject>(new STObject(*this)); }
@@ -184,7 +160,7 @@ namespace boost
template<> struct range_const_iterator<STObject> { typedef STObject::const_iterator type; };
}
//------------------------------------------------------------------------------
class STArray : public SerializedType, private IS_INSTANCE(SerializedArray)
{
@@ -197,12 +173,12 @@ public:
typedef boost::ptr_vector<STObject>::size_type size_type;
public:
STArray() { ; }
STArray(int n) { value.reserve(n); }
STArray(SField::ref f) : SerializedType(f) { ; }
STArray () { ; }
explicit STArray(int n) { value.reserve(n); }
explicit STArray(SField::ref f) : SerializedType(f) { ; }
STArray(SField::ref f, int n) : SerializedType(f) { value.reserve(n); }
STArray(SField::ref f, const vector& v) : SerializedType(f), value(v) { ; }
STArray(vector& v) : value(v) { ; }
explicit STArray(vector& v) : value(v) { ; }
static UPTR_T<SerializedType> deserialize(SerializerIterator& sit, SField::ref name)
{ return UPTR_T<SerializedType>(construct(sit, name)); }
@@ -210,6 +186,8 @@ public:
const vector& getValue() const { return value; }
vector& getValue() { return value; }
// VFALCO: NOTE as long as we're married to boost why not use boost::iterator_facade?
//
// vector-like functions
void push_back(const STObject& object) { value.push_back(object.oClone().release()); }
STObject& operator[](int j) { return value[j]; }

View File

@@ -0,0 +1,42 @@
SOTemplate::SOTemplate ()
{
}
void SOTemplate::push_back (SOElement const& r)
{
// Ensure there is the enough space in the index mapping
// table for all possible fields.
//
if (mIndex.empty())
{
// Unmapped indices will be set to -1
//
mIndex.resize (SField::getNumFields() + 1, -1);
}
// Make sure the field's index is in range
//
assert (r.e_field.getNum() < mIndex.size());
// Make sure that this field hasn't already been assigned
//
assert(getIndex(r.e_field) == -1);
// Add the field to the index mapping table
//
mIndex [r.e_field.getNum ()] = mTypes.size();
// Append the new element.
//
mTypes.push_back (new SOElement (r));
}
int SOTemplate::getIndex (SField::ref f) const
{
// The mapping table should be large enough for any possible field
//
assert (f.getNum() < mIndex.size());
return mIndex[f.getNum()];
}

View File

@@ -0,0 +1,71 @@
#ifndef RIPPLE_SERIALIZEDOBJECTTEMPLATE_H
#define RIPPLE_SERIALIZEDOBJECTTEMPLATE_H
//------------------------------------------------------------------------------
/** Flags for elements in a SerializedObjectTemplate.
*/
// VFALCO: NOTE, these don't look like bit-flags...
enum SOE_Flags
{
SOE_INVALID = -1,
SOE_REQUIRED = 0, // required
SOE_OPTIONAL = 1, // optional, may be present with default value
SOE_DEFAULT = 2, // optional, if present, must not have default value
};
//------------------------------------------------------------------------------
/** An element in a SerializedObjectTemplate.
*/
class SOElement
{
public:
SField::ref const e_field;
SOE_Flags const flags;
SOElement (SField::ref fieldName, SOE_Flags flags)
: e_field (fieldName)
, flags (flags)
{
}
};
//------------------------------------------------------------------------------
/** Defines the fields and their attributes within a SerializedObject.
Each subclass of SerializedObject will provide its own template
describing the available fields and their metadata attributes.
*/
class SOTemplate
{
public:
/** Create an empty template.
After creating the template, call @ref push_back with the
desired fields.
@see push_back
*/
SOTemplate ();
std::vector <SOElement const*> const& peek() const
{
return mTypes;
}
/** Add an element to the template.
*/
void push_back (SOElement const& r);
/** Retrieve the position of a named field.
*/
int getIndex (SField::ref) const;
private:
std::vector<SOElement const*> mTypes;
std::vector <int> mIndex; // field num -> index
};
#endif

View File

@@ -82,6 +82,7 @@
#include "protocol/ripple_RippleAddress.cpp"
#include "protocol/ripple_SerializedTypes.cpp"
#include "protocol/ripple_Serializer.cpp"
#include "protocol/ripple_SerializedObjectTemplate.cpp"
#include "protocol/ripple_SerializedObject.cpp"
#include "protocol/ripple_TER.cpp"
#include "protocol/ripple_TransactionFormat.cpp"

View File

@@ -72,6 +72,7 @@
#include "protocol/ripple_Serializer.h" // needs CKey
#include "protocol/ripple_TER.h"
#include "protocol/ripple_SerializedTypes.h" // needs Serializer, TER
#include "protocol/ripple_SerializedObjectTemplate.h"
#include "protocol/ripple_SerializedObject.h"
#include "protocol/ripple_LedgerFormat.h" // needs SOTemplate from SerializedObject
#include "protocol/ripple_TransactionFormat.h"

View File

@@ -315,6 +315,12 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="modules\ripple_data\protocol\ripple_SerializedObjectTemplate.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="modules\ripple_data\protocol\ripple_SerializedTypes.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
@@ -1311,6 +1317,7 @@
<ClInclude Include="modules\ripple_data\protocol\ripple_RippleSystem.h" />
<ClInclude Include="modules\ripple_data\protocol\ripple_SerializeDeclarations.h" />
<ClInclude Include="modules\ripple_data\protocol\ripple_SerializedObject.h" />
<ClInclude Include="modules\ripple_data\protocol\ripple_SerializedObjectTemplate.h" />
<ClInclude Include="modules\ripple_data\protocol\ripple_SerializedTypes.h" />
<ClInclude Include="modules\ripple_data\protocol\ripple_Serializer.h" />
<ClInclude Include="modules\ripple_data\protocol\ripple_TER.h" />

View File

@@ -816,6 +816,9 @@
<ClCompile Include="src\cpp\ripple\ripple_HashedObject.cpp">
<Filter>1. Modules\ripple_main\refactored</Filter>
</ClCompile>
<ClCompile Include="modules\ripple_data\protocol\ripple_SerializedObjectTemplate.cpp">
<Filter>1. Modules\ripple_data\protocol</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="database\sqlite3ext.h">
@@ -1520,6 +1523,9 @@
<ClInclude Include="src\cpp\ripple\ripple_HashedObject.h">
<Filter>1. Modules\ripple_main\refactored</Filter>
</ClInclude>
<ClInclude Include="modules\ripple_data\protocol\ripple_SerializedObjectTemplate.h">
<Filter>1. Modules\ripple_data\protocol</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="SConstruct" />

View File

@@ -1,28 +1,9 @@
DECLARE_INSTANCE(SerializedValidation);
SOTemplate sValidationFormat;
void SVFInit()
{
sValidationFormat.push_back(SOElement(sfFlags, SOE_REQUIRED));
sValidationFormat.push_back(SOElement(sfLedgerHash, SOE_REQUIRED));
sValidationFormat.push_back(SOElement(sfLedgerSequence, SOE_OPTIONAL));
sValidationFormat.push_back(SOElement(sfCloseTime, SOE_OPTIONAL));
sValidationFormat.push_back(SOElement(sfLoadFee, SOE_OPTIONAL));
sValidationFormat.push_back(SOElement(sfFeatures, SOE_OPTIONAL));
sValidationFormat.push_back(SOElement(sfBaseFee, SOE_OPTIONAL));
sValidationFormat.push_back(SOElement(sfReserveBase, SOE_OPTIONAL));
sValidationFormat.push_back(SOElement(sfReserveIncrement, SOE_OPTIONAL));
sValidationFormat.push_back(SOElement(sfSigningTime, SOE_REQUIRED));
sValidationFormat.push_back(SOElement(sfSigningPubKey, SOE_REQUIRED));
sValidationFormat.push_back(SOElement(sfSignature, SOE_OPTIONAL));
};
const uint32 SerializedValidation::sFullFlag = 0x1;
SerializedValidation::SerializedValidation(SerializerIterator& sit, bool checkSignature)
: STObject(sValidationFormat, sit, sfValidation), mTrusted(false)
SerializedValidation::SerializedValidation (SerializerIterator& sit, bool checkSignature)
: STObject (getFormat (), sit, sfValidation)
, mTrusted (false)
{
mNodeID = RippleAddress::createNodePublic(getFieldVL(sfSigningPubKey)).getNodeID();
assert(mNodeID.isNonZero());
@@ -33,9 +14,11 @@ SerializedValidation::SerializedValidation(SerializerIterator& sit, bool checkSi
}
}
SerializedValidation::SerializedValidation(const uint256& ledgerHash, uint32 signTime,
SerializedValidation::SerializedValidation (
const uint256& ledgerHash, uint32 signTime,
const RippleAddress& raPub, bool isFull)
: STObject(sValidationFormat, sfValidation), mTrusted(false)
: STObject (getFormat (), sfValidation)
, mTrusted (false)
{ // Does not sign
setFieldH256(sfLedgerHash, ledgerHash);
setFieldU32(sfSigningTime, signTime);
@@ -125,4 +108,32 @@ std::vector<unsigned char> SerializedValidation::getSigned() const
return s.peekData();
}
SOTemplate const& SerializedValidation::getFormat ()
{
struct FormatHolder
{
SOTemplate format;
FormatHolder ()
{
format.push_back (SOElement (sfFlags, SOE_REQUIRED));
format.push_back (SOElement (sfLedgerHash, SOE_REQUIRED));
format.push_back (SOElement (sfLedgerSequence, SOE_OPTIONAL));
format.push_back (SOElement (sfCloseTime, SOE_OPTIONAL));
format.push_back (SOElement (sfLoadFee, SOE_OPTIONAL));
format.push_back (SOElement (sfFeatures, SOE_OPTIONAL));
format.push_back (SOElement (sfBaseFee, SOE_OPTIONAL));
format.push_back (SOElement (sfReserveBase, SOE_OPTIONAL));
format.push_back (SOElement (sfReserveIncrement,SOE_OPTIONAL));
format.push_back (SOElement (sfSigningTime, SOE_REQUIRED));
format.push_back (SOElement (sfSigningPubKey, SOE_REQUIRED));
format.push_back (SOElement (sfSignature, SOE_OPTIONAL));
}
};
static FormatHolder holder;
return holder.format;
}
// vim:ts=4

View File

@@ -1,21 +1,23 @@
#ifndef __VALIDATION__
#define __VALIDATION__
#ifndef RIPPLE_SERIALIZEDVALIDATION_H
#define RIPPLE_SERIALIZEDVALIDATION_H
DEFINE_INSTANCE(SerializedValidation);
DEFINE_INSTANCE (SerializedValidation);
class SerializedValidation : public STObject, private IS_INSTANCE(SerializedValidation)
class SerializedValidation
: public STObject
, private IS_INSTANCE (SerializedValidation)
{
public:
typedef boost::shared_ptr<SerializedValidation> pointer;
typedef const boost::shared_ptr<SerializedValidation>& ref;
static const uint32 sFullFlag;
static const uint32 sFullFlag = 0x1;
// These throw if the object is not valid
SerializedValidation(SerializerIterator& sit, bool checkSignature = true);
SerializedValidation (SerializerIterator& sit, bool checkSignature = true);
// Does not sign the validation
SerializedValidation(const uint256& ledgerHash, uint32 signTime, const RippleAddress& raPub, bool isFull);
SerializedValidation (const uint256& ledgerHash, uint32 signTime, const RippleAddress& raPub, bool isFull);
uint256 getLedgerHash() const;
uint32 getSignTime() const;
@@ -36,15 +38,17 @@ public:
// The validation this replaced
const uint256& getPreviousHash() { return mPreviousHash; }
bool isPreviousHash(const uint256& h) { return mPreviousHash == h; }
bool isPreviousHash(const uint256& h) const { return mPreviousHash == h; }
void setPreviousHash(const uint256& h) { mPreviousHash = h; }
private:
static SOTemplate const& getFormat ();
void setNode ();
uint256 mPreviousHash;
uint160 mNodeID;
bool mTrusted;
void setNode();
};
#endif

View File

@@ -12,9 +12,9 @@
namespace po = boost::program_options;
// VFALCO: TODO make these singletons that initialize statically
extern void TFInit();
extern void LEFInit();
extern void SVFInit();
using namespace std;
using namespace boost::unit_test;
@@ -213,9 +213,9 @@ int main(int argc, char* argv[])
InstanceType::multiThread();
// VFALCO: TODO make these singletons that initialize statically
TFInit();
LEFInit();
SVFInit();
if (vm.count("unittest"))
{