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

Conflicts:
	src/SerializedObject.h
This commit is contained in:
jed
2012-10-01 20:13:28 -07:00
12 changed files with 334 additions and 264 deletions

View File

@@ -28,7 +28,7 @@ enum SerializedTypeID
enum SOE_Flags enum SOE_Flags
{ {
SOE_END = -1, // marks end of object SOE_INVALID = -1,
SOE_REQUIRED = 0, // required SOE_REQUIRED = 0, // required
SOE_OPTIONAL = 1, // optional SOE_OPTIONAL = 1, // optional
}; };

View File

@@ -1,106 +1,118 @@
#include "LedgerFormats.h" #include "LedgerFormats.h"
std::map<int, LedgerEntryFormat*> LedgerEntryFormat::byType;
std::map<std::string, LedgerEntryFormat*> LedgerEntryFormat::byName;
#define LEF_BASE \ #define LEF_BASE \
{ sfLedgerIndex, SOE_OPTIONAL }, \ << SOElement(sfLedgerIndex, SOE_OPTIONAL) \
{ sfLedgerEntryType, SOE_REQUIRED }, \ << SOElement(sfLedgerEntryType, SOE_REQUIRED) \
{ sfFlags, SOE_REQUIRED }, << SOElement(sfFlags, SOE_REQUIRED)
LedgerEntryFormat LedgerFormats[]= #define DECLARE_LEF(name, type) lef = new LedgerEntryFormat(#name, type); (*lef) LEF_BASE
{
{ "AccountRoot", ltACCOUNT_ROOT, { LEF_BASE
{ sfAccount, SOE_REQUIRED },
{ sfSequence, SOE_REQUIRED },
{ sfBalance, SOE_REQUIRED },
{ sfLastTxnID, SOE_REQUIRED },
{ sfLastTxnSeq, SOE_REQUIRED },
{ sfAuthorizedKey, SOE_OPTIONAL },
{ sfEmailHash, SOE_OPTIONAL },
{ sfWalletLocator, SOE_OPTIONAL },
{ sfMessageKey, SOE_OPTIONAL },
{ sfTransferRate, SOE_OPTIONAL },
{ sfDomain, SOE_OPTIONAL },
{ sfPublishHash, SOE_OPTIONAL },
{ sfPublishSize, SOE_OPTIONAL },
{ sfInvalid, SOE_END } }
},
{ "Contract", ltCONTRACT, { LEF_BASE
{ sfAccount, SOE_REQUIRED },
{ sfBalance, SOE_REQUIRED },
{ sfLastTxnID, SOE_REQUIRED },
{ sfLastTxnSeq, SOE_REQUIRED },
{ sfIssuer, SOE_REQUIRED },
{ sfOwner, SOE_REQUIRED },
{ sfExpiration, SOE_REQUIRED },
{ sfBondAmount, SOE_REQUIRED },
{ sfCreateCode, SOE_REQUIRED },
{ sfFundCode, SOE_REQUIRED },
{ sfRemoveCode, SOE_REQUIRED },
{ sfExpireCode, SOE_REQUIRED },
{ sfInvalid, SOE_END } }
},
{ "DirectoryNode", ltDIR_NODE, { LEF_BASE
{ sfIndexes, SOE_REQUIRED },
{ sfIndexNext, SOE_OPTIONAL },
{ sfIndexPrevious, SOE_OPTIONAL },
{ sfInvalid, SOE_END } }
},
{ "GeneratorMap", ltGENERATOR_MAP, { LEF_BASE
{ sfGenerator, SOE_REQUIRED },
{ sfInvalid, SOE_END } }
},
{ "Nickname", ltNICKNAME, { LEF_BASE
{ sfAccount, SOE_REQUIRED },
{ sfMinimumOffer, SOE_OPTIONAL },
{ sfInvalid, SOE_END } }
},
{ "Offer", ltOFFER, { LEF_BASE
{ sfAccount, SOE_REQUIRED },
{ sfSequence, SOE_REQUIRED },
{ sfTakerPays, SOE_REQUIRED },
{ sfTakerGets, SOE_REQUIRED },
{ sfBookDirectory, SOE_REQUIRED },
{ sfBookNode, SOE_REQUIRED },
{ sfOwnerNode, SOE_REQUIRED },
{ sfLastTxnID, SOE_REQUIRED },
{ sfLastTxnSeq, SOE_REQUIRED },
{ sfExpiration, SOE_OPTIONAL },
{ sfInvalid, SOE_END } }
},
{ "RippleState", ltRIPPLE_STATE, { LEF_BASE
{ sfBalance, SOE_REQUIRED },
{ sfLowLimit, SOE_REQUIRED },
{ sfHighLimit, SOE_REQUIRED },
{ sfLastTxnID, SOE_REQUIRED },
{ sfLastTxnSeq, SOE_REQUIRED },
{ sfLowQualityIn, SOE_OPTIONAL },
{ sfLowQualityOut, SOE_OPTIONAL },
{ sfHighQualityIn, SOE_OPTIONAL },
{ sfHighQualityOut, SOE_OPTIONAL },
{ sfInvalid, SOE_END } }
},
{ NULL, ltINVALID }
};
LedgerEntryFormat* getLgrFormat(LedgerEntryType t) static bool LEFInit()
{ {
return getLgrFormat(static_cast<int>(t)); LedgerEntryFormat* lef;
DECLARE_LEF(AccountRoot, ltACCOUNT_ROOT)
<< SOElement(sfAccount, SOE_REQUIRED)
<< SOElement(sfSequence, SOE_REQUIRED)
<< SOElement(sfBalance, SOE_REQUIRED)
<< SOElement(sfLastTxnID, SOE_REQUIRED)
<< SOElement(sfLastTxnSeq, SOE_REQUIRED)
<< SOElement(sfAuthorizedKey, SOE_OPTIONAL)
<< SOElement(sfEmailHash, SOE_OPTIONAL)
<< SOElement(sfWalletLocator, SOE_OPTIONAL)
<< SOElement(sfMessageKey, SOE_OPTIONAL)
<< SOElement(sfTransferRate, SOE_OPTIONAL)
<< SOElement(sfDomain, SOE_OPTIONAL)
<< SOElement(sfPublishHash, SOE_OPTIONAL)
<< SOElement(sfPublishSize, SOE_OPTIONAL)
;
DECLARE_LEF(Contract, ltCONTRACT)
<< SOElement(sfAccount, SOE_REQUIRED)
<< SOElement(sfBalance, SOE_REQUIRED)
<< SOElement(sfLastTxnID, SOE_REQUIRED)
<< SOElement(sfLastTxnSeq, SOE_REQUIRED)
<< SOElement(sfIssuer, SOE_REQUIRED)
<< SOElement(sfOwner, SOE_REQUIRED)
<< SOElement(sfExpiration, SOE_REQUIRED)
<< SOElement(sfBondAmount, SOE_REQUIRED)
<< SOElement(sfCreateCode, SOE_REQUIRED)
<< SOElement(sfFundCode, SOE_REQUIRED)
<< SOElement(sfRemoveCode, SOE_REQUIRED)
<< SOElement(sfExpireCode, SOE_REQUIRED)
;
DECLARE_LEF(DirectoryNode, ltDIR_NODE)
<< SOElement(sfIndexes, SOE_REQUIRED)
<< SOElement(sfIndexNext, SOE_OPTIONAL)
<< SOElement(sfIndexPrevious, SOE_OPTIONAL)
;
DECLARE_LEF(GeneratorMap, ltGENERATOR_MAP)
<< SOElement(sfGenerator, SOE_REQUIRED)
;
DECLARE_LEF(Nickname, ltNICKNAME)
<< SOElement(sfAccount, SOE_REQUIRED)
<< SOElement(sfMinimumOffer, SOE_OPTIONAL)
;
DECLARE_LEF(Offer, ltOFFER)
<< SOElement(sfAccount, SOE_REQUIRED)
<< SOElement(sfSequence, SOE_REQUIRED)
<< SOElement(sfTakerPays, SOE_REQUIRED)
<< SOElement(sfTakerGets, SOE_REQUIRED)
<< SOElement(sfBookDirectory, SOE_REQUIRED)
<< SOElement(sfBookNode, SOE_REQUIRED)
<< SOElement(sfOwnerNode, SOE_REQUIRED)
<< SOElement(sfLastTxnID, SOE_REQUIRED)
<< SOElement(sfLastTxnSeq, SOE_REQUIRED)
<< SOElement(sfExpiration, SOE_OPTIONAL)
;
DECLARE_LEF(RippleState, ltRIPPLE_STATE)
<< SOElement(sfBalance, SOE_REQUIRED)
<< SOElement(sfLowLimit, SOE_REQUIRED)
<< SOElement(sfHighLimit, SOE_REQUIRED)
<< SOElement(sfLastTxnID, SOE_REQUIRED)
<< SOElement(sfLastTxnSeq, SOE_REQUIRED)
<< SOElement(sfLowQualityIn, SOE_OPTIONAL)
<< SOElement(sfLowQualityOut, SOE_OPTIONAL)
<< SOElement(sfHighQualityIn, SOE_OPTIONAL)
<< SOElement(sfHighQualityOut, SOE_OPTIONAL)
;
return true;
} }
LedgerEntryFormat* getLgrFormat(int t) bool LEFInitComplete = LEFInit();
LedgerEntryFormat* LedgerEntryFormat::getLgrFormat(LedgerEntryType t)
{ {
for (LedgerEntryFormat* f = LedgerFormats; f->t_name != NULL; ++f) std::map<int, LedgerEntryFormat*>::iterator it = byType.find(static_cast<int>(t));
if (f->t_type == t) if (it == byType.end())
return f;
return NULL; return NULL;
return it->second;
} }
LedgerEntryFormat* getLgrFormat(const std::string& t) LedgerEntryFormat* LedgerEntryFormat::getLgrFormat(int t)
{ {
for (LedgerEntryFormat* f = LedgerFormats; f->t_name != NULL; ++f) std::map<int, LedgerEntryFormat*>::iterator it = byType.find((t));
if (t == f->t_name) if (it == byType.end())
return f;
return NULL; return NULL;
return it->second;
}
LedgerEntryFormat* LedgerEntryFormat::getLgrFormat(const std::string& t)
{
std::map<std::string, LedgerEntryFormat*>::iterator it = byName.find((t));
if (it == byName.end())
return NULL;
return it->second;
} }
// vim:ts=4 // vim:ts=4

View File

@@ -39,16 +39,31 @@ enum LedgerSpecificFlags
lsfPassive = 0x00010000, lsfPassive = 0x00010000,
}; };
struct LedgerEntryFormat class LedgerEntryFormat
{ {
const char * t_name; public:
std::string t_name;
LedgerEntryType t_type; LedgerEntryType t_type;
SOElement elements[24]; std::vector<SOElement::ptr> elements;
static std::map<int, LedgerEntryFormat*> byType;
static std::map<std::string, LedgerEntryFormat*> byName;
LedgerEntryFormat(const char *name, LedgerEntryType type) : t_name(name), t_type(type)
{
byName[name] = this;
byType[type] = this;
}
LedgerEntryFormat& operator<<(const SOElement& el)
{
elements.push_back(new SOElement(el));
return *this;
}
static LedgerEntryFormat* getLgrFormat(LedgerEntryType t);
static LedgerEntryFormat* getLgrFormat(const std::string& t);
static LedgerEntryFormat* getLgrFormat(int t);
}; };
extern LedgerEntryFormat LedgerFormats[];
extern LedgerEntryFormat* getLgrFormat(LedgerEntryType t);
extern LedgerEntryFormat* getLgrFormat(const std::string& t);
extern LedgerEntryFormat* getLgrFormat(int t);
#endif #endif
// vim:ts=4 // vim:ts=4

View File

@@ -10,7 +10,7 @@ SerializedLedgerEntry::SerializedLedgerEntry(SerializerIterator& sit, const uint
{ {
set(sit); set(sit);
uint16 type = getFieldU16(sfLedgerEntryType); uint16 type = getFieldU16(sfLedgerEntryType);
mFormat = getLgrFormat(static_cast<LedgerEntryType>(type)); mFormat = LedgerEntryFormat::getLgrFormat(static_cast<LedgerEntryType>(type));
if (mFormat == NULL) if (mFormat == NULL)
throw std::runtime_error("invalid ledger entry type"); throw std::runtime_error("invalid ledger entry type");
mType = mFormat->t_type; mType = mFormat->t_type;
@@ -25,7 +25,7 @@ SerializedLedgerEntry::SerializedLedgerEntry(const Serializer& s, const uint256&
set(sit); set(sit);
uint16 type = getFieldU16(sfLedgerEntryType); uint16 type = getFieldU16(sfLedgerEntryType);
mFormat = getLgrFormat(static_cast<LedgerEntryType>(type)); mFormat = LedgerEntryFormat::getLgrFormat(static_cast<LedgerEntryType>(type));
if (mFormat == NULL) if (mFormat == NULL)
throw std::runtime_error("invalid ledger entry type"); throw std::runtime_error("invalid ledger entry type");
mType = mFormat->t_type; mType = mFormat->t_type;
@@ -39,7 +39,7 @@ SerializedLedgerEntry::SerializedLedgerEntry(const Serializer& s, const uint256&
SerializedLedgerEntry::SerializedLedgerEntry(LedgerEntryType type) : STObject(sfLedgerEntry), mType(type) SerializedLedgerEntry::SerializedLedgerEntry(LedgerEntryType type) : STObject(sfLedgerEntry), mType(type)
{ {
mFormat = getLgrFormat(type); mFormat = LedgerEntryFormat::getLgrFormat(type);
if (mFormat == NULL) throw std::runtime_error("invalid ledger entry type"); if (mFormat == NULL) throw std::runtime_error("invalid ledger entry type");
set(mFormat->elements); set(mFormat->elements);
setFieldU16(sfLedgerEntryType, static_cast<uint16>(mFormat->t_type)); setFieldU16(sfLedgerEntryType, static_cast<uint16>(mFormat->t_type));

View File

@@ -116,33 +116,32 @@ std::auto_ptr<SerializedType> STObject::makeDeserializedObject(SerializedTypeID
} }
} }
void STObject::set(SOElement::ptr elem) void STObject::set(const std::vector<SOElement::ptr>& type)
{ {
mData.empty(); mData.empty();
mType.empty(); mType.empty();
while (elem->flags != SOE_END) BOOST_FOREACH(const SOElement::ptr& elem, type)
{ {
mType.push_back(elem); mType.push_back(elem);
if (elem->flags == SOE_OPTIONAL) if (elem->flags == SOE_OPTIONAL)
giveObject(makeNonPresentObject(elem->e_field)); giveObject(makeNonPresentObject(elem->e_field));
else else
giveObject(makeDefaultObject(elem->e_field)); giveObject(makeDefaultObject(elem->e_field));
++elem;
} }
} }
bool STObject::setType(SOElement::ptrList t) bool STObject::setType(const std::vector<SOElement::ptr> &type)
{ {
boost::ptr_vector<SerializedType> newData; boost::ptr_vector<SerializedType> newData;
bool valid = true; bool valid = true;
mType.empty(); mType.empty();
while (t->flags != SOE_END) BOOST_FOREACH(const SOElement::ptr& elem, type)
{ {
bool match = false; bool match = false;
for (boost::ptr_vector<SerializedType>::iterator it = mData.begin(); it != mData.end(); ++it) for (boost::ptr_vector<SerializedType>::iterator it = mData.begin(); it != mData.end(); ++it)
if (it->getFName() == t->e_field) if (it->getFName() == elem->e_field)
{ {
match = true; match = true;
newData.push_back(mData.release(it).release()); newData.push_back(mData.release(it).release());
@@ -151,15 +150,15 @@ bool STObject::setType(SOElement::ptrList t)
if (!match) if (!match)
{ {
if (t->flags != SOE_OPTIONAL) if (elem->flags != SOE_OPTIONAL)
{ {
Log(lsTRACE) << "setType !valid missing"; Log(lsTRACE) << "setType !valid missing";
valid = false; valid = false;
} }
newData.push_back(makeNonPresentObject(t->e_field)); newData.push_back(makeNonPresentObject(elem->e_field));
} }
mType.push_back(t++); mType.push_back(elem);
} }
if (mData.size() != 0) if (mData.size() != 0)
{ {
@@ -742,7 +741,7 @@ Json::Value STObject::getJson(int options) const
if (it.getSType() != STI_NOTPRESENT) if (it.getSType() != STI_NOTPRESENT)
{ {
if (!it.getFName().hasName()) if (!it.getFName().hasName())
ret[boost::lexical_cast<std::string>(index)] = it.getJson(options); ret[lexical_cast_i(index)] = it.getJson(options);
else else
ret[it.getName()] = it.getJson(options); ret[it.getName()] = it.getJson(options);
} }
@@ -837,11 +836,19 @@ std::auto_ptr<STObject> STObject::parseJson(const Json::Value& object, SField::r
{ {
case STI_UINT8: case STI_UINT8:
if (value.isString()) if (value.isString())
data.push_back(new STUInt8(field, boost::lexical_cast<unsigned char>(value.asString()))); data.push_back(new STUInt8(field, lexical_cast_s<unsigned char>(value.asString())));
else if (value.isInt()) else if (value.isInt())
data.push_back(new STUInt8(field, boost::lexical_cast<unsigned char>(value.asInt()))); {
if (value.asInt() < 0 || value.asInt() > 255)
throw std::runtime_error("value out of rand");
data.push_back(new STUInt8(field, static_cast<unsigned char>(value.asInt())));
}
else if (value.isUInt()) else if (value.isUInt())
data.push_back(new STUInt8(field, boost::lexical_cast<unsigned char>(value.asUInt()))); {
if (value.asUInt() > 255)
throw std::runtime_error("value out of rand");
data.push_back(new STUInt8(field, static_cast<unsigned char>(value.asUInt())));
}
else else
throw std::runtime_error("Incorrect type"); throw std::runtime_error("Incorrect type");
break; break;
@@ -854,7 +861,7 @@ std::auto_ptr<STObject> STObject::parseJson(const Json::Value& object, SField::r
{ {
if (field == sfTransactionType) if (field == sfTransactionType)
{ {
TransactionFormat* f = getTxnFormat(strValue); TransactionFormat* f = TransactionFormat::getTxnFormat(strValue);
if (!f) if (!f)
throw std::runtime_error("Unknown transaction type"); throw std::runtime_error("Unknown transaction type");
data.push_back(new STUInt16(field, static_cast<uint16>(f->t_type))); data.push_back(new STUInt16(field, static_cast<uint16>(f->t_type)));
@@ -863,7 +870,7 @@ std::auto_ptr<STObject> STObject::parseJson(const Json::Value& object, SField::r
} }
else if (field == sfLedgerEntryType) else if (field == sfLedgerEntryType)
{ {
LedgerEntryFormat* f = getLgrFormat(strValue); LedgerEntryFormat* f = LedgerEntryFormat::getLgrFormat(strValue);
if (!f) if (!f)
throw std::runtime_error("Unknown ledger entry type"); throw std::runtime_error("Unknown ledger entry type");
data.push_back(new STUInt16(field, static_cast<uint16>(f->t_type))); data.push_back(new STUInt16(field, static_cast<uint16>(f->t_type)));
@@ -874,34 +881,34 @@ std::auto_ptr<STObject> STObject::parseJson(const Json::Value& object, SField::r
throw std::runtime_error("Invalid field data"); throw std::runtime_error("Invalid field data");
} }
else else
data.push_back(new STUInt16(field, boost::lexical_cast<uint16>(strValue))); data.push_back(new STUInt16(field, lexical_cast_s<uint16>(strValue)));
} }
else if (value.isInt()) else if (value.isInt())
data.push_back(new STUInt16(field, boost::lexical_cast<uint16>(value.asInt()))); data.push_back(new STUInt16(field, static_cast<uint16>(value.asInt())));
else if (value.isUInt()) else if (value.isUInt())
data.push_back(new STUInt16(field, boost::lexical_cast<uint16>(value.asUInt()))); data.push_back(new STUInt16(field, static_cast<uint16>(value.asUInt())));
else else
throw std::runtime_error("Incorrect type"); throw std::runtime_error("Incorrect type");
break; break;
case STI_UINT32: case STI_UINT32:
if (value.isString()) if (value.isString())
data.push_back(new STUInt32(field, boost::lexical_cast<uint32>(value.asString()))); data.push_back(new STUInt32(field, lexical_cast_s<uint32>(value.asString())));
else if (value.isInt()) else if (value.isInt())
data.push_back(new STUInt32(field, boost::lexical_cast<uint32>(value.asInt()))); data.push_back(new STUInt32(field, static_cast<uint32>(value.asInt())));
else if (value.isUInt()) else if (value.isUInt())
data.push_back(new STUInt32(field, boost::lexical_cast<uint32>(value.asUInt()))); data.push_back(new STUInt32(field, static_cast<uint32>(value.asUInt())));
else else
throw std::runtime_error("Incorrect type"); throw std::runtime_error("Incorrect type");
break; break;
case STI_UINT64: case STI_UINT64:
if (value.isString()) if (value.isString())
data.push_back(new STUInt64(field, boost::lexical_cast<uint64>(value.asString()))); data.push_back(new STUInt64(field, lexical_cast_s<uint64>(value.asString())));
else if (value.isInt()) else if (value.isInt())
data.push_back(new STUInt64(field, boost::lexical_cast<uint64>(value.asInt()))); data.push_back(new STUInt64(field, static_cast<uint64>(value.asInt())));
else if (value.isUInt()) else if (value.isUInt())
data.push_back(new STUInt64(field, boost::lexical_cast<uint64>(value.asUInt()))); data.push_back(new STUInt64(field, static_cast<uint64>(value.asUInt())));
else else
throw std::runtime_error("Incorrect type"); throw std::runtime_error("Incorrect type");
break; break;
@@ -929,7 +936,10 @@ std::auto_ptr<STObject> STObject::parseJson(const Json::Value& object, SField::r
break; break;
case STI_VL: case STI_VL:
{
// WRITEME // WRITEME
}
break;
case STI_AMOUNT: case STI_AMOUNT:
// WRITEME // WRITEME

View File

@@ -15,11 +15,11 @@ class SOElement
{ // An element in the description of a serialized object { // An element in the description of a serialized object
public: public:
typedef SOElement const * ptr; // used to point to one element typedef SOElement const * ptr; // used to point to one element
typedef SOElement const * ptrList; // used to point to a terminated list of elements
SField::ref e_field; SField::ref e_field;
const SOE_Flags flags; const SOE_Flags flags;
SOElement(SField::ref fi, SOE_Flags fl) : e_field(fi), flags(fl) { ; }
}; };
class STObject : public SerializedType class STObject : public SerializedType
@@ -36,10 +36,10 @@ public:
STObject(SField::ref name) : SerializedType(name) { ; } STObject(SField::ref name) : SerializedType(name) { ; }
STObject(SOElement::ptrList type, SField::ref name) : SerializedType(name) STObject(const std::vector<SOElement::ptr>& type, SField::ref name) : SerializedType(name)
{ set(type); } { set(type); }
STObject(SOElement::ptrList type, SerializerIterator& sit, SField::ref name) : SerializedType(name) STObject(const std::vector<SOElement::ptr>& type, SerializerIterator& sit, SField::ref name) : SerializedType(name)
{ set(sit); setType(type); } { set(sit); setType(type); }
static std::auto_ptr<STObject> parseJson(const Json::Value& value, SField::ref name = sfGeneric, int depth = 0); static std::auto_ptr<STObject> parseJson(const Json::Value& value, SField::ref name = sfGeneric, int depth = 0);
@@ -48,11 +48,11 @@ public:
static std::auto_ptr<SerializedType> deserialize(SerializerIterator& sit, SField::ref name); static std::auto_ptr<SerializedType> deserialize(SerializerIterator& sit, SField::ref name);
bool setType(SOElement::ptrList); bool setType(const std::vector<SOElement::ptr>& type);
bool isValidForType(); bool isValidForType();
bool isFieldAllowed(SField::ref); bool isFieldAllowed(SField::ref);
void set(SOElement::ptrList); void set(const std::vector<SOElement::ptr>&);
bool set(SerializerIterator& u, int depth = 0); bool set(SerializerIterator& u, int depth = 0);
virtual SerializedTypeID getSType() const { return STI_OBJECT; } virtual SerializedTypeID getSType() const { return STI_OBJECT; }

View File

@@ -9,7 +9,7 @@
SerializedTransaction::SerializedTransaction(TransactionType type) : STObject(sfTransaction), mType(type) SerializedTransaction::SerializedTransaction(TransactionType type) : STObject(sfTransaction), mType(type)
{ {
mFormat = getTxnFormat(type); mFormat = TransactionFormat::getTxnFormat(type);
if (mFormat == NULL) if (mFormat == NULL)
throw std::runtime_error("invalid transaction type"); throw std::runtime_error("invalid transaction type");
set(mFormat->elements); set(mFormat->elements);
@@ -28,7 +28,7 @@ SerializedTransaction::SerializedTransaction(SerializerIterator& sit) : STObject
set(sit); set(sit);
mType = static_cast<TransactionType>(getFieldU16(sfTransactionType)); mType = static_cast<TransactionType>(getFieldU16(sfTransactionType));
mFormat = getTxnFormat(mType); mFormat = TransactionFormat::getTxnFormat(mType);
if (!mFormat) if (!mFormat)
throw std::runtime_error("invalid transaction type"); throw std::runtime_error("invalid transaction type");
if (!setType(mFormat->elements)) if (!setType(mFormat->elements))

View File

@@ -54,13 +54,13 @@ std::string STUInt16::getText() const
{ {
if (getFName() == sfLedgerEntryType) if (getFName() == sfLedgerEntryType)
{ {
LedgerEntryFormat *f = getLgrFormat(value); LedgerEntryFormat *f = LedgerEntryFormat::getLgrFormat(value);
if (f != NULL) if (f != NULL)
return f->t_name; return f->t_name;
} }
if (getFName() == sfTransactionType) if (getFName() == sfTransactionType)
{ {
TransactionFormat *f = getTxnFormat(value); TransactionFormat *f = TransactionFormat::getTxnFormat(value);
if (f != NULL) if (f != NULL)
return f->t_name; return f->t_name;
} }

View File

@@ -3,18 +3,23 @@
#include "HashPrefixes.h" #include "HashPrefixes.h"
SOElement SerializedValidation::sValidationFormat[] = { std::vector<SOElement::ptr> sValidationFormat;
{ sfFlags, SOE_REQUIRED },
{ sfLedgerHash, SOE_REQUIRED }, static bool SVFInit()
{ sfLedgerSequence, SOE_OPTIONAL }, {
{ sfCloseTime, SOE_OPTIONAL }, sValidationFormat.push_back(new SOElement(sfFlags, SOE_REQUIRED));
{ sfLoadFee, SOE_OPTIONAL }, sValidationFormat.push_back(new SOElement(sfLedgerHash, SOE_REQUIRED));
{ sfBaseFee, SOE_OPTIONAL }, sValidationFormat.push_back(new SOElement(sfLedgerSequence, SOE_OPTIONAL));
{ sfSigningTime, SOE_REQUIRED }, sValidationFormat.push_back(new SOElement(sfCloseTime, SOE_OPTIONAL));
{ sfSigningPubKey, SOE_REQUIRED }, sValidationFormat.push_back(new SOElement(sfLoadFee, SOE_OPTIONAL));
{ sfInvalid, SOE_END } sValidationFormat.push_back(new SOElement(sfBaseFee, SOE_OPTIONAL));
sValidationFormat.push_back(new SOElement(sfSigningTime, SOE_REQUIRED));
sValidationFormat.push_back(new SOElement(sfSigningPubKey, SOE_REQUIRED));
return true;
}; };
bool SVFinitComplete = SVFInit();
const uint32 SerializedValidation::sFullFlag = 0x00010000; const uint32 SerializedValidation::sFullFlag = 0x00010000;
SerializedValidation::SerializedValidation(SerializerIterator& sit, bool checkSignature) SerializedValidation::SerializedValidation(SerializerIterator& sit, bool checkSignature)

View File

@@ -17,7 +17,6 @@ public:
typedef boost::shared_ptr<SerializedValidation> pointer; typedef boost::shared_ptr<SerializedValidation> pointer;
typedef const boost::shared_ptr<SerializedValidation>& ref; typedef const boost::shared_ptr<SerializedValidation>& ref;
static SOElement sValidationFormat[16];
static const uint32 sFullFlag; static const uint32 sFullFlag;
// These throw if the object is not valid // These throw if the object is not valid

View File

@@ -1,124 +1,138 @@
#include "TransactionFormats.h" #include "TransactionFormats.h"
std::map<int, TransactionFormat*> TransactionFormat::byType;
std::map<std::string, TransactionFormat*> TransactionFormat::byName;
#define TF_BASE \ #define TF_BASE \
{ sfTransactionType, SOE_REQUIRED }, \ << SOElement(sfTransactionType, SOE_REQUIRED) \
{ sfFlags, SOE_REQUIRED }, \ << SOElement(sfFlags, SOE_REQUIRED) \
{ sfSourceTag, SOE_OPTIONAL }, \ << SOElement(sfSourceTag, SOE_OPTIONAL) \
{ sfAccount, SOE_REQUIRED }, \ << SOElement(sfAccount, SOE_REQUIRED) \
{ sfSequence, SOE_REQUIRED }, \ << SOElement(sfSequence, SOE_REQUIRED) \
{ sfFee, SOE_REQUIRED }, \ << SOElement(sfFee, SOE_REQUIRED) \
{ sfSigningPubKey, SOE_REQUIRED }, \ << SOElement(sfSigningPubKey, SOE_REQUIRED) \
{ sfTxnSignature, SOE_OPTIONAL }, << SOElement(sfTxnSignature, SOE_OPTIONAL)
TransactionFormat TxnFormats[]= #define DECLARE_TF(name, type) tf = new TransactionFormat(#name, type); (*tf) TF_BASE
static bool TFInit()
{ {
{ "AccountSet", ttACCOUNT_SET, { TF_BASE TransactionFormat* tf;
{ sfEmailHash, SOE_OPTIONAL },
{ sfWalletLocator, SOE_OPTIONAL }, DECLARE_TF(AccountSet, ttACCOUNT_SET)
{ sfMessageKey, SOE_OPTIONAL }, << SOElement(sfEmailHash, SOE_OPTIONAL)
{ sfDomain, SOE_OPTIONAL }, << SOElement(sfWalletLocator, SOE_OPTIONAL)
{ sfTransferRate, SOE_OPTIONAL }, << SOElement(sfMessageKey, SOE_OPTIONAL)
{ sfPublishHash, SOE_OPTIONAL }, << SOElement(sfDomain, SOE_OPTIONAL)
{ sfPublishSize, SOE_OPTIONAL }, << SOElement(sfTransferRate, SOE_OPTIONAL)
{ sfInvalid, SOE_END } } << SOElement(sfPublishHash, SOE_OPTIONAL)
}, << SOElement(sfPublishSize, SOE_OPTIONAL)
{ "Claim", ttCLAIM, { TF_BASE ;
{ sfGenerator, SOE_REQUIRED },
{ sfPublicKey, SOE_REQUIRED }, DECLARE_TF(Claim, ttCLAIM)
{ sfSignature, SOE_REQUIRED }, << SOElement(sfGenerator, SOE_REQUIRED)
{ sfInvalid, SOE_END } } << SOElement(sfPublicKey, SOE_REQUIRED)
}, << SOElement(sfSignature, SOE_REQUIRED)
{ "CreditSet", ttCREDIT_SET, { TF_BASE ;
{ sfLimitAmount, SOE_OPTIONAL },
{ sfQualityIn, SOE_OPTIONAL }, DECLARE_TF(CreditSet, ttCREDIT_SET)
{ sfQualityOut, SOE_OPTIONAL }, << SOElement(sfLimitAmount, SOE_OPTIONAL)
{ sfInvalid, SOE_END } } << SOElement(sfQualityIn, SOE_OPTIONAL)
}, << SOElement(sfQualityOut, SOE_OPTIONAL)
;
/* /*
{ "Invoice", ttINVOICE, { TF_BASE DECLARE_TF(Invoice, ttINVOICE)
{ sfTarget, SOE_REQUIRED }, << SOElement(sfTarget, SOE_REQUIRED)
{ sfAmount, SOE_REQUIRED }, << SOElement(sfAmount, SOE_REQUIRED)
{ sfDestination, SOE_OPTIONAL }, << SOElement(sfDestination, SOE_OPTIONAL)
{ sfIdentifier, SOE_OPTIONAL }, << SOElement(sfIdentifier, SOE_OPTIONAL)
{ sfInvalid, SOE_END } } ;
}, )
*/ */
{ "NicknameSet", ttNICKNAME_SET, { TF_BASE
{ sfNickname, SOE_REQUIRED },
{ sfMinimumOffer, SOE_OPTIONAL },
{ sfInvalid, SOE_END } }
},
{ "OfferCreate", ttOFFER_CREATE, { TF_BASE
{ sfTakerPays, SOE_REQUIRED },
{ sfTakerGets, SOE_REQUIRED },
{ sfExpiration, SOE_OPTIONAL },
{ sfInvalid, SOE_END } }
},
{ "OfferCancel", ttOFFER_CANCEL, { TF_BASE
{ sfOfferSequence, SOE_REQUIRED },
{ sfInvalid, SOE_END } }
},
{ "PasswordFund", ttPASSWORD_FUND, { TF_BASE
{ sfDestination, SOE_REQUIRED },
{ sfInvalid, SOE_END } }
},
{ "PasswordSet", ttPASSWORD_SET, { TF_BASE
{ sfAuthorizedKey, SOE_REQUIRED },
{ sfGenerator, SOE_REQUIRED },
{ sfPublicKey, SOE_REQUIRED },
{ sfInvalid, SOE_END } }
},
{ "Payment", ttPAYMENT, { TF_BASE
{ sfDestination, SOE_REQUIRED },
{ sfAmount, SOE_REQUIRED },
{ sfSendMax, SOE_OPTIONAL },
{ sfPaths, SOE_OPTIONAL },
{ sfInvoiceID, SOE_OPTIONAL },
{ sfInvalid, SOE_END } }
},
{ "WalletAdd", ttWALLET_ADD, { TF_BASE
{ sfAmount, SOE_REQUIRED },
{ sfAuthorizedKey, SOE_REQUIRED },
{ sfPublicKey, SOE_REQUIRED },
{ sfInvalid, SOE_END } }
},
{ "Contract", ttCONTRACT, { TF_BASE
{ sfExpiration, SOE_REQUIRED },
{ sfBondAmount, SOE_REQUIRED },
{ sfStampEscrow, SOE_REQUIRED },
{ sfRippleEscrow, SOE_REQUIRED },
{ sfCreateCode, SOE_OPTIONAL },
{ sfFundCode, SOE_OPTIONAL },
{ sfRemoveCode, SOE_OPTIONAL },
{ sfExpireCode, SOE_OPTIONAL },
{ sfInvalid, SOE_END } }
},
{ "RemoveContract", ttCONTRACT_REMOVE, { TF_BASE
{ sfTarget, SOE_REQUIRED },
{ sfInvalid, SOE_END } }
},
{ NULL, ttINVALID }
};
TransactionFormat* getTxnFormat(TransactionType t) DECLARE_TF(NicknameSet, ttNICKNAME_SET)
{ << SOElement(sfNickname, SOE_REQUIRED)
return getTxnFormat(static_cast<int>(t)); << SOElement(sfMinimumOffer, SOE_OPTIONAL)
;
DECLARE_TF(OfferCreate, ttOFFER_CREATE)
<< SOElement(sfTakerPays, SOE_REQUIRED)
<< SOElement(sfTakerGets, SOE_REQUIRED)
<< SOElement(sfExpiration, SOE_OPTIONAL)
;
DECLARE_TF(OfferCancel, ttOFFER_CANCEL)
<< SOElement(sfOfferSequence, SOE_REQUIRED)
;
DECLARE_TF(PasswordFund, ttPASSWORD_FUND)
<< SOElement(sfDestination, SOE_REQUIRED)
;
DECLARE_TF(PasswordSet, ttPASSWORD_SET)
<< SOElement(sfAuthorizedKey, SOE_REQUIRED)
<< SOElement(sfGenerator, SOE_REQUIRED)
<< SOElement(sfPublicKey, SOE_REQUIRED)
;
DECLARE_TF(Payment, ttPAYMENT)
<< SOElement(sfDestination, SOE_REQUIRED)
<< SOElement(sfAmount, SOE_REQUIRED)
<< SOElement(sfSendMax, SOE_OPTIONAL)
<< SOElement(sfPaths, SOE_OPTIONAL)
<< SOElement(sfInvoiceID, SOE_OPTIONAL)
;
DECLARE_TF(WalletAdd, ttWALLET_ADD)
<< SOElement(sfAmount, SOE_REQUIRED)
<< SOElement(sfAuthorizedKey, SOE_REQUIRED)
<< SOElement(sfPublicKey, SOE_REQUIRED)
;
DECLARE_TF(Contract, ttCONTRACT)
<< SOElement(sfExpiration, SOE_REQUIRED)
<< SOElement(sfBondAmount, SOE_REQUIRED)
<< SOElement(sfStampEscrow, SOE_REQUIRED)
<< SOElement(sfRippleEscrow, SOE_REQUIRED)
<< SOElement(sfCreateCode, SOE_OPTIONAL)
<< SOElement(sfFundCode, SOE_OPTIONAL)
<< SOElement(sfRemoveCode, SOE_OPTIONAL)
<< SOElement(sfExpireCode, SOE_OPTIONAL)
;
DECLARE_TF(RemoveContract, ttCONTRACT_REMOVE)
<< SOElement(sfTarget, SOE_REQUIRED)
;
return true;
} }
TransactionFormat* getTxnFormat(int t) bool TFInitComplete = TFInit();
TransactionFormat* TransactionFormat::getTxnFormat(TransactionType t)
{ {
for (TransactionFormat* f = TxnFormats; f->t_name != NULL; ++f) std::map<int, TransactionFormat*>::iterator it = byType.find(static_cast<int>(t));
if (t == f->t_type) if (it == byType.end())
return f;
return NULL; return NULL;
return it->second;
} }
TransactionFormat* getTxnFormat(const std::string& format) TransactionFormat* TransactionFormat::getTxnFormat(int t)
{ {
for (TransactionFormat* f = TxnFormats; f->t_name != NULL; ++f) std::map<int, TransactionFormat*>::iterator it = byType.find((t));
if (format == f->t_name) if (it == byType.end())
return f;
return NULL; return NULL;
return it->second;
}
TransactionFormat* TransactionFormat::getTxnFormat(const std::string& t)
{
std::map<std::string, TransactionFormat*>::iterator it = byName.find((t));
if (it == byName.end())
return NULL;
return it->second;
} }
// vim:ts=4 // vim:ts=4

View File

@@ -21,11 +21,30 @@ enum TransactionType
ttCREDIT_SET = 20, ttCREDIT_SET = 20,
}; };
struct TransactionFormat class TransactionFormat
{ {
const char * t_name; public:
std::string t_name;
TransactionType t_type; TransactionType t_type;
SOElement elements[24]; std::vector<SOElement::ptr> elements;
static std::map<int, TransactionFormat*> byType;
static std::map<std::string, TransactionFormat*> byName;
TransactionFormat(const char *name, TransactionType type) : t_name(name), t_type(type)
{
byName[name] = this;
byType[type] = this;
}
TransactionFormat& operator<<(const SOElement& el)
{
elements.push_back(new SOElement(el));
return *this;
}
static TransactionFormat* getTxnFormat(TransactionType t);
static TransactionFormat* getTxnFormat(const std::string& t);
static TransactionFormat* getTxnFormat(int t);
}; };
const int TransactionMinLen = 32; const int TransactionMinLen = 32;
@@ -44,9 +63,5 @@ const uint32 tfPartialPayment = 0x00020000;
const uint32 tfLimitQuality = 0x00040000; const uint32 tfLimitQuality = 0x00040000;
const uint32 tfNoRippleDirect = 0x00080000; const uint32 tfNoRippleDirect = 0x00080000;
extern TransactionFormat TxnFormats[];
extern TransactionFormat* getTxnFormat(TransactionType t);
extern TransactionFormat* getTxnFormat(const std::string& t);
extern TransactionFormat* getTxnFormat(int t);
#endif #endif
// vim:ts=4 // vim:ts=4