1#include <xrpl/basics/Blob.h>
2#include <xrpl/basics/Log.h>
3#include <xrpl/basics/Slice.h>
4#include <xrpl/basics/base_uint.h>
5#include <xrpl/basics/contract.h>
6#include <xrpl/beast/utility/instrumentation.h>
7#include <xrpl/json/json_value.h>
8#include <xrpl/protocol/AccountID.h>
9#include <xrpl/protocol/Feature.h>
10#include <xrpl/protocol/HashPrefix.h>
11#include <xrpl/protocol/InnerObjectFormats.h>
12#include <xrpl/protocol/Rules.h>
13#include <xrpl/protocol/SField.h>
14#include <xrpl/protocol/SOTemplate.h>
15#include <xrpl/protocol/STAccount.h>
16#include <xrpl/protocol/STAmount.h>
17#include <xrpl/protocol/STArray.h>
18#include <xrpl/protocol/STBase.h>
19#include <xrpl/protocol/STBitString.h>
20#include <xrpl/protocol/STBlob.h>
21#include <xrpl/protocol/STCurrency.h>
22#include <xrpl/protocol/STInteger.h>
23#include <xrpl/protocol/STIssue.h>
24#include <xrpl/protocol/STNumber.h>
25#include <xrpl/protocol/STObject.h>
26#include <xrpl/protocol/STPathSet.h>
27#include <xrpl/protocol/STVector256.h>
28#include <xrpl/protocol/Serializer.h>
29#include <xrpl/protocol/detail/STVar.h>
67 Throw<std::runtime_error>(
"Maximum nesting depth of STObject exceeded");
82 bool const isAMMObj = name == sfAuctionSlot || name == sfVoteEntry;
83 if (!rules || (rules->enabled(fixInnerObjTemplate) && isAMMObj) ||
84 (rules->enabled(fixInnerObjTemplate2) && !isAMMObj))
101 return emplace(n, buf, std::move(*
this));
127 v_ = std::move(other.v_);
138 for (
auto const& elem : type)
150 auto throwFieldErr = [](
std::string const& field,
char const* description) {
152 ss <<
"Field '" << field <<
"' " << description;
154 JLOG(
debugLog().error()) <<
"STObject::applyTemplate failed: " << text;
155 Throw<FieldErr>(text);
160 v.reserve(type.
size());
161 for (
auto const& e : type)
165 if (iter !=
v_.
end())
167 if ((e.style() ==
soeDEFAULT) && iter->get().isDefault())
169 throwFieldErr(e.sField().fieldName,
"may not be explicitly set to default.");
171 v.emplace_back(std::move(*iter));
178 throwFieldErr(e.sField().fieldName,
"is required but missing.");
183 for (
auto const& e :
v_)
186 if (!e->getFName().isDiscardable())
188 throwFieldErr(e->getFName().getName(),
"found in disallowed location.");
208 bool reachedEndOfObject =
false;
223 if (type == STI_OBJECT && field == 1)
225 reachedEndOfObject =
true;
229 if (type == STI_ARRAY && field == 1)
231 JLOG(
debugLog().error()) <<
"Encountered object with embedded end-of-array marker";
232 Throw<std::runtime_error>(
"Illegal end-of-array marker in object");
239 JLOG(
debugLog().error()) <<
"Unknown field: field_type=" << type <<
", field_name=" << field;
240 Throw<std::runtime_error>(
"Unknown field");
248 obj->applyTemplateFromSField(fn);
256 return lhs->getFName() == rhs->getFName();
259 if (dup != sf.cend())
260 Throw<std::runtime_error>(
"Duplicate field detected");
262 return reachedEndOfObject;
290 for (
auto const& elem :
v_)
292 if (elem->getSType() != STI_NOTPRESENT)
299 ret += elem->getFullText();
312 for (
auto const& elem :
v_)
320 ret += elem->getText();
337 return (st1.getSType() == st2.getSType()) && st1.isEquivalent(st2);
344 return std::equal(sf1.begin(), sf1.end(), sf2.begin(), sf2.end(), [](
STBase const* st1,
STBase const* st2) {
345 return (st1->getSType() == st2->getSType()) && st1->isEquivalent(*st2);
370 if (
mType !=
nullptr)
374 for (
auto const& elem :
v_)
376 if (elem->getFName() == field)
408 return v_[index]->getFName();
429 if (createOkay &&
isFree())
452 return peekField<STObject>(field);
458 return peekField<STArray>(field);
517 if (f->
getSType() != STI_NOTPRESENT)
566 return getFieldByValue<STUInt8>(field);
572 return getFieldByValue<STUInt16>(field);
578 return getFieldByValue<STUInt32>(field);
584 return getFieldByValue<STUInt64>(field);
590 return getFieldByValue<STUInt128>(field);
596 return getFieldByValue<STUInt160>(field);
602 return getFieldByValue<STUInt192>(field);
608 return getFieldByValue<STUInt256>(field);
614 return getFieldByValue<STInt32>(field);
620 return getFieldByValue<STAccount>(field);
627 STBlob const& b = getFieldByConstRef<STBlob>(field,
empty);
635 return getFieldByConstRef<STAmount>(field,
empty);
642 return getFieldByConstRef<STPathSet>(field,
empty);
649 return getFieldByConstRef<STVector256>(field,
empty);
656 auto ret = getFieldByConstRef<STObject>(field,
empty);
658 ret.applyTemplateFromSField(field);
666 return getFieldByConstRef<STArray>(field,
empty);
673 return getFieldByConstRef<STCurrency>(field,
empty);
680 return getFieldByConstRef<STNumber>(field,
empty);
695 v_[i] = std::move(v);
700 Throw<std::runtime_error>(
"missing field in templated STObject");
708 setFieldUsingSetValue<STUInt8>(field, v);
714 setFieldUsingSetValue<STUInt16>(field, v);
720 setFieldUsingSetValue<STUInt32>(field, v);
726 setFieldUsingSetValue<STUInt64>(field, v);
732 setFieldUsingSetValue<STUInt128>(field, v);
738 setFieldUsingSetValue<STUInt256>(field, v);
744 setFieldUsingSetValue<STInt32>(field, v);
750 setFieldUsingSetValue<STVector256>(field, v);
756 setFieldUsingSetValue<STAccount>(field, v);
818 for (
auto const& elem :
v_)
820 if (elem->getSType() != STI_NOTPRESENT)
821 ret[elem->getFName().getJsonName()] = elem->getJson(options);
832 for (
auto const& t1 :
v_)
834 if ((t1->getSType() != STI_NOTPRESENT) && t1->getFName().isBinary())
838 for (
auto const& t2 : obj.
v_)
840 if (t1->getFName() == t2->getFName())
857 for (
auto const& t2 : obj.
v_)
859 if ((t2->getSType() != STI_NOTPRESENT) && t2->getFName().isBinary())
877 for (
STBase const*
const field : fields)
884 (sType != STI_OBJECT) || (field->getFName().fieldType == STI_OBJECT),
885 "xrpl::STObject::add : valid field type");
886 field->addFieldID(s);
888 if (sType == STI_ARRAY || sType == STI_OBJECT)
902 STBase const& base = elem.get();
911 return lhs->getFName().fieldCode < rhs->getFName().fieldCode;
T adjacent_find(T... args)
Like std::vector<char> but better.
static SField const & getField(int fieldCode)
bool shouldInclude(bool withSigningField) const
std::string const & getName() const
Defines the fields and their attributes within a STObject.
int getIndex(SField const &) const
Retrieve the position of a named field.
SOEStyle style(SField const &sf) const
std::size_t size() const
The number of entries in this template.
A type which can be exported to a well known binary format.
SField const & getFName() const
static STBase * emplace(std::size_t n, void *buf, T &&val)
virtual SerializedTypeID getSType() const
void setFName(SField const &n)
A STBase is a field.
std::uint8_t const * data() const
value_type value() const noexcept
void setFieldU8(SField const &field, unsigned char)
SField const & getFieldSType(int index) const
uint192 getFieldH192(SField const &field) const
STBase const * peekAtPIndex(int offset) const
Json::Value getJson(JsonOptions=JsonOptions::none) const override
STCurrency const & getFieldCurrency(SField const &field) const
Blob getFieldVL(SField const &field) const
void setFieldIssue(SField const &field, STIssue const &)
uint128 getFieldH128(SField const &field) const
bool operator==(STObject const &o) const
bool isEquivalent(STBase const &t) const override
void setFieldNumber(SField const &field, STNumber const &)
void setFieldV256(SField const &field, STVector256 const &v)
void setFieldU64(SField const &field, std::uint64_t)
unsigned char getFieldU8(SField const &field) const
std::uint32_t getFieldU32(SField const &field) const
STBase const & peekAtIndex(int offset) const
bool hasMatchingEntry(STBase const &)
STNumber const & getFieldNumber(SField const &field) const
void setFieldVL(SField const &field, Blob const &)
std::size_t emplace_back(Args &&... args)
void applyTemplate(SOTemplate const &type)
int getFieldIndex(SField const &field) const
uint256 getHash(HashPrefix prefix) const
void setFieldI32(SField const &field, std::int32_t)
std::string getFullText() const override
void setFieldU32(SField const &field, std::uint32_t)
STArray & peekFieldArray(SField const &field)
std::string getText() const override
STArray const & getFieldArray(SField const &field) const
void setFieldArray(SField const &field, STArray const &v)
STObject & peekFieldObject(SField const &field)
SOEStyle getStyle(SField const &field) const
STBase & getField(SField const &field)
void setFieldAmount(SField const &field, STAmount const &)
void add(Serializer &s) const override
bool isFlag(std::uint32_t) const
bool isFieldPresent(SField const &field) const
STObject & operator=(STObject const &)=default
STObject(STObject const &)=default
SerializedTypeID getSType() const override
void setFieldU16(SField const &field, std::uint16_t)
uint256 getFieldH256(SField const &field) const
static STObject makeInnerObject(SField const &name)
std::int32_t getFieldI32(SField const &field) const
STBase const & peekAtField(SField const &field) const
void set(SOTemplate const &)
uint256 getSigningHash(HashPrefix prefix) const
bool clearFlag(std::uint32_t)
void setFieldCurrency(SField const &field, STCurrency const &)
std::uint64_t getFieldU64(SField const &field) const
void setFieldPathSet(SField const &field, STPathSet const &)
STBase * move(std::size_t n, void *buf) override
static std::vector< STBase const * > getSortedFields(STObject const &objToSort, WhichFields whichFields)
STBase * getPField(SField const &field, bool createOkay=false)
STBase * makeFieldPresent(SField const &field)
STBase const * peekAtPField(SField const &field) const
void applyTemplateFromSField(SField const &)
bool setFlag(std::uint32_t)
void setFieldObject(SField const &field, STObject const &v)
void setFieldH128(SField const &field, uint128 const &)
STObject getFieldObject(SField const &field) const
void setAccountID(SField const &field, AccountID const &)
void setFieldUsingAssignment(SField const &field, T const &value)
bool delField(SField const &field)
STBase & getIndex(int offset)
uint160 getFieldH160(SField const &field) const
AccountID getAccountID(SField const &field) const
STBase * getPIndex(int offset)
STVector256 const & getFieldV256(SField const &field) const
STPathSet const & getFieldPathSet(SField const &field) const
bool isDefault() const override
void makeFieldAbsent(SField const &field)
void setFieldH256(SField const &field, uint256 const &)
std::uint16_t getFieldU16(SField const &field) const
STAmount const & getFieldAmount(SField const &field) const
std::uint32_t getFlags() const
STBase * copy(std::size_t n, void *buf) const override
std::size_t empty() const noexcept
void getFieldID(int &type, int &name)
int addFieldID(int type, int name)
uint256 getSHA512Half() const
An immutable linear range of bytes.
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
std::size_t size() const noexcept
Returns the number of bytes in the storage.
T emplace_back(T... args)
@ objectValue
object value (collection of name/value pairs).
nonPresentObject_t nonPresentObject
defaultObject_t defaultObject
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
bool set(T &target, std::string const &name, Section const §ion)
Set a value from a configuration Section If the named value is not found or doesn't parse as a T,...
void throwFieldNotFound(SField const &field)
beast::Journal debugLog()
Returns a debug journal.
SOEStyle
Kind of element in each entry of an SOTemplate.
bool matches(char const *string, char const *regex)
Return true if the string loosely matches the regex.
std::optional< Rules > const & getCurrentTransactionRules()
std::vector< unsigned char > Blob
Storage for linear binary data.
HashPrefix
Prefix for hashing functions.
Note, should be treated as flags that can be | and &.