From 8bd212e6bc2a69eace13ab141149f037830f994a Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Fri, 16 Nov 2012 15:11:16 -0800 Subject: [PATCH] Sanely handle optional fields set to their default values. Specify when this is allowed and when it's prohibited. --- src/cpp/ripple/FieldNames.h | 3 ++- src/cpp/ripple/LedgerFormats.cpp | 8 ++++---- src/cpp/ripple/SerializedObject.cpp | 10 ++++++++-- src/cpp/ripple/SerializedTypes.h | 2 +- src/cpp/ripple/TransactionFormats.cpp | 2 +- 5 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/cpp/ripple/FieldNames.h b/src/cpp/ripple/FieldNames.h index 8b1d5636a5..db118ad8b4 100644 --- a/src/cpp/ripple/FieldNames.h +++ b/src/cpp/ripple/FieldNames.h @@ -30,7 +30,8 @@ enum SOE_Flags { SOE_INVALID = -1, SOE_REQUIRED = 0, // required - SOE_OPTIONAL = 1, // optional + SOE_OPTIONAL = 1, // optional, may be present with default value + SOE_DEFAULT = 2, // optional, if present, must not have default value }; class SField diff --git a/src/cpp/ripple/LedgerFormats.cpp b/src/cpp/ripple/LedgerFormats.cpp index b775ff14a9..ea1240bc5c 100644 --- a/src/cpp/ripple/LedgerFormats.cpp +++ b/src/cpp/ripple/LedgerFormats.cpp @@ -39,10 +39,10 @@ static bool LEFInit() << 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) + << SOElement(sfCreateCode, SOE_OPTIONAL) + << SOElement(sfFundCode, SOE_OPTIONAL) + << SOElement(sfRemoveCode, SOE_OPTIONAL) + << SOElement(sfExpireCode, SOE_OPTIONAL) ; DECLARE_LEF(DirectoryNode, ltDIR_NODE) diff --git a/src/cpp/ripple/SerializedObject.cpp b/src/cpp/ripple/SerializedObject.cpp index 2ef6a84f8e..1991110449 100644 --- a/src/cpp/ripple/SerializedObject.cpp +++ b/src/cpp/ripple/SerializedObject.cpp @@ -138,7 +138,7 @@ void STObject::set(const std::vector& type) BOOST_FOREACH(const SOElement::ptr& elem, type) { mType.push_back(elem); - if (elem->flags == SOE_OPTIONAL) + if (elem->flags != SOE_REQUIRED) giveObject(makeNonPresentObject(elem->e_field)); else giveObject(makeDefaultObject(elem->e_field)); @@ -159,12 +159,18 @@ bool STObject::setType(const std::vector &type) { match = true; newData.push_back(mData.release(it).release()); + if ((elem->flags == SOE_DEFAULT) && it->isDefault()) + { + cLog(lsWARNING) << "setType( " << getFName().getName() << ") invalid default " + << elem->e_field.fieldName; + valid = false; + } break; } if (!match) { - if (elem->flags != SOE_OPTIONAL) + if (elem->flags == SOE_REQUIRED) { cLog(lsWARNING) << "setType( " << getFName().getName() << ") invalid missing " << elem->e_field.fieldName; diff --git a/src/cpp/ripple/SerializedTypes.h b/src/cpp/ripple/SerializedTypes.h index 7b1e8c0cf5..3745a92190 100644 --- a/src/cpp/ripple/SerializedTypes.h +++ b/src/cpp/ripple/SerializedTypes.h @@ -688,7 +688,7 @@ public: void addPath(const STPath& e) { value.push_back(e); } virtual bool isEquivalent(const SerializedType& t) const; - virtual bool isDefault() const { return value.empty(); } + virtual bool isDefault() const { return value.empty(); } void printDebug(); diff --git a/src/cpp/ripple/TransactionFormats.cpp b/src/cpp/ripple/TransactionFormats.cpp index f70a09116a..f54cb1868f 100644 --- a/src/cpp/ripple/TransactionFormats.cpp +++ b/src/cpp/ripple/TransactionFormats.cpp @@ -52,7 +52,7 @@ static bool TFInit() << SOElement(sfDestination, SOE_REQUIRED) << SOElement(sfAmount, SOE_REQUIRED) << SOElement(sfSendMax, SOE_OPTIONAL) - << SOElement(sfPaths, SOE_OPTIONAL) + << SOElement(sfPaths, SOE_DEFAULT) << SOElement(sfInvoiceID, SOE_OPTIONAL) ;