Reduce likelihood of malformed SOTemplate:

Formerly an SOTemplate was default constructed and its elements
added using push_back().  This left open the possibility of a
malformed SOTemplate if adding one of the elements caused a throw.

With this commit the SOTemplate requires an initializer_list of
its elements at construction.  Elements may not be added after
construction.  With this approach either the SOTemplate is fully
constructed with all of its elements or the constructor throws,
which prevents an invalid SOTemplate from even existing.

This change requires all SOTemplate construction to be adjusted
at the call site.  Those changes are also in this commit.

The SOE_Flags enum is also renamed to SOEStyle, which harmonizes
the name with other uses in the code base.  SOEStyle elements
are renamed (slightly) to have an "soe" prefix rather than "SOE_".
This heads toward reserving identifiers with all upper case for
macros.  The new style also aligns with other prominent enums in
the code base like the collection of TER identifiers.

SOElement is adjusted so it can be stored directly in an STL
container, rather than requiring storage in a unique_ptr.
Correspondingly, unique_ptr usage is removed from both
SOTemplate and KnownFormats.
This commit is contained in:
Scott Schurr
2019-02-27 12:56:22 -08:00
committed by Nik Bougalis
parent 57fe197d3e
commit afcc4ff296
17 changed files with 601 additions and 560 deletions

View File

@@ -237,10 +237,9 @@ public:
unexpected (sfGeneric.isUseful (), "sfGeneric must not be useful");
{
// Try to put sfGeneric in an SOTemplate.
SOTemplate elements;
except<std::runtime_error>( [&]()
{
elements.push_back (SOElement (sfGeneric, SOE_REQUIRED));
SOTemplate elements {{ sfGeneric, soeREQUIRED }};
});
}
@@ -260,19 +259,19 @@ public:
}
{
// Try to put sfInvalid in an SOTemplate.
SOTemplate elements;
except<std::runtime_error>( [&]()
{
elements.push_back (SOElement (sfInvalid, SOE_REQUIRED));
SOTemplate elements {{ sfInvalid, soeREQUIRED }};
});
}
{
// Try to put the same SField into an SOTemplate twice.
SOTemplate elements;
elements.push_back (SOElement (sfAccount, SOE_REQUIRED));
except<std::runtime_error>( [&]()
{
elements.push_back (SOElement (sfAccount, SOE_REQUIRED));
SOTemplate elements {
{ sfAccount, soeREQUIRED },
{ sfAccount, soeREQUIRED },
};
});
}
@@ -284,12 +283,14 @@ public:
SField const& sfTestObject = sfMajority;
SOTemplate elements;
elements.push_back (SOElement (sfFlags, SOE_REQUIRED));
elements.push_back (SOElement (sfTestVL, SOE_REQUIRED));
elements.push_back (SOElement (sfTestH256, SOE_OPTIONAL));
elements.push_back (SOElement (sfTestU32, SOE_REQUIRED));
elements.push_back (SOElement (sfTestV256, SOE_OPTIONAL));
SOTemplate const elements
{
{ sfFlags, soeREQUIRED },
{ sfTestVL, soeREQUIRED },
{ sfTestH256, soeOPTIONAL },
{ sfTestU32, soeREQUIRED },
{ sfTestV256, soeOPTIONAL },
};
STObject object1 (elements, sfTestObject);
STObject object2 (object1);
@@ -419,17 +420,14 @@ public:
}
// read templated object
auto const sot = [&]()
{
SOTemplate sot;
sot.push_back(SOElement(sf1, SOE_REQUIRED));
sot.push_back(SOElement(sf2, SOE_OPTIONAL));
sot.push_back(SOElement(sf3, SOE_DEFAULT));
sot.push_back(SOElement(sf4, SOE_OPTIONAL));
sot.push_back(SOElement(sf5, SOE_DEFAULT));
return sot;
}();
SOTemplate const sot
{
{ sf1, soeREQUIRED },
{ sf2, soeOPTIONAL },
{ sf3, soeDEFAULT },
{ sf4, soeOPTIONAL },
{ sf5, soeDEFAULT },
};
{
auto const st = [&]()
@@ -643,14 +641,13 @@ public:
auto const& sf1 = sfIndexes;
auto const& sf2 = sfHashes;
auto const& sf3 = sfAmendments;
auto const sot = [&]()
{
SOTemplate sot;
sot.push_back(SOElement(sf1, SOE_REQUIRED));
sot.push_back(SOElement(sf2, SOE_OPTIONAL));
sot.push_back(SOElement(sf3, SOE_DEFAULT));
return sot;
}();
SOTemplate const sot
{
{ sf1, soeREQUIRED },
{ sf2, soeOPTIONAL },
{ sf3, soeDEFAULT },
};
STObject st(sot, sfGeneric);
auto const& cst(st);
BEAST_EXPECT(cst[sf1].size() == 0);

View File

@@ -1250,9 +1250,11 @@ public:
{
// Construct an SOTemplate to get the ball rolling on building
// an STObject that can contain another STObject.
SOTemplate recurse;
recurse.push_back (SOElement {sfTransactionMetaData, SOE_OPTIONAL});
recurse.push_back (SOElement {sfTransactionHash, SOE_OPTIONAL});
SOTemplate const recurse
{
{ sfTransactionMetaData, soeOPTIONAL},
{ sfTransactionHash, soeOPTIONAL},
};
// Make an STObject that nests objects ten levels deep. There's
// a minimum transaction size we must meet, so include a hash256.
@@ -1312,10 +1314,12 @@ public:
// Construct an SOTemplate to get the ball rolling on building
// an STObject that can contain an STArray.
SOTemplate recurse;
recurse.push_back (SOElement {sfTransactionMetaData, SOE_OPTIONAL});
recurse.push_back (SOElement {sfTransactionHash, SOE_OPTIONAL});
recurse.push_back (SOElement {sfTemplate, SOE_OPTIONAL});
SOTemplate recurse
{
{ sfTransactionMetaData, soeOPTIONAL },
{ sfTransactionHash, soeOPTIONAL },
{ sfTemplate, soeOPTIONAL },
};
// Make an STObject that nests ten levels deep alternating objects
// and arrays. Include a hash256 to meet the minimum transaction