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>
45 :
STBase(other.getFName()), v_(
std::move(other.v_)), mType(other.mType)
68 :
STBase(name), mType(nullptr)
71 Throw<std::runtime_error>(
"Maximum nesting depth of STObject exceeded");
86 bool const isAMMObj = name == sfAuctionSlot || name == sfVoteEntry;
87 if (!rules || (rules->enabled(fixInnerObjTemplate) && isAMMObj) ||
88 (rules->enabled(fixInnerObjTemplate2) && !isAMMObj))
106 return emplace(n, buf, std::move(*
this));
132 v_ = std::move(other.v_);
143 for (
auto const& elem : type)
155 auto throwFieldErr = [](
std::string const& field,
char const* description) {
157 ss <<
"Field '" << field <<
"' " << description;
159 JLOG(
debugLog().error()) <<
"STObject::applyTemplate failed: " << text;
160 Throw<FieldErr>(text);
165 v.reserve(type.size());
166 for (
auto const& e : type)
170 return b.get().getFName() == e.sField();
172 if (iter !=
v_.
end())
174 if ((e.style() ==
soeDEFAULT) && iter->get().isDefault())
177 e.sField().fieldName,
178 "may not be explicitly set to default.");
180 v.emplace_back(std::move(*iter));
187 throwFieldErr(e.sField().fieldName,
"is required but missing.");
192 for (
auto const& e :
v_)
195 if (!e->getFName().isDiscardable())
198 e->getFName().getName(),
"found in disallowed location.");
219 bool reachedEndOfObject =
false;
234 if (type == STI_OBJECT && field == 1)
236 reachedEndOfObject =
true;
240 if (type == STI_ARRAY && field == 1)
243 <<
"Encountered object with embedded end-of-array marker";
244 Throw<std::runtime_error>(
"Illegal end-of-array marker in object");
251 JLOG(
debugLog().error()) <<
"Unknown field: field_type=" << type
252 <<
", field_name=" << field;
253 Throw<std::runtime_error>(
"Unknown field");
261 obj->applyTemplateFromSField(fn);
269 sf.cbegin(), sf.cend(), [](
STBase const* lhs,
STBase const* rhs) {
270 return lhs->getFName() == rhs->getFName();
273 if (dup != sf.cend())
274 Throw<std::runtime_error>(
"Duplicate field detected");
276 return reachedEndOfObject;
304 for (
auto const& elem :
v_)
306 if (elem->getSType() != STI_NOTPRESENT)
313 ret += elem->getFullText();
326 for (
auto const& elem :
v_)
334 ret += elem->getText();
356 return (st1.getSType() == st2.getSType()) &&
357 st1.isEquivalent(st2);
370 return (st1->getSType() == st2->getSType()) &&
371 st1->isEquivalent(*st2);
396 if (
mType !=
nullptr)
400 for (
auto const& elem :
v_)
402 if (elem->getFName() == field)
434 return v_[index]->getFName();
455 if (createOkay &&
isFree())
478 return peekField<STObject>(field);
484 return peekField<STArray>(field);
543 if (f->
getSType() != STI_NOTPRESENT)
586 return getFieldByValue<STUInt8>(field);
592 return getFieldByValue<STUInt16>(field);
598 return getFieldByValue<STUInt32>(field);
604 return getFieldByValue<STUInt64>(field);
610 return getFieldByValue<STUInt128>(field);
616 return getFieldByValue<STUInt160>(field);
622 return getFieldByValue<STUInt192>(field);
628 return getFieldByValue<STUInt256>(field);
634 return getFieldByValue<STInt32>(field);
640 return getFieldByValue<STAccount>(field);
647 STBlob const& b = getFieldByConstRef<STBlob>(field,
empty);
655 return getFieldByConstRef<STAmount>(field,
empty);
662 return getFieldByConstRef<STPathSet>(field,
empty);
669 return getFieldByConstRef<STVector256>(field,
empty);
676 auto ret = getFieldByConstRef<STObject>(field,
empty);
678 ret.applyTemplateFromSField(field);
686 return getFieldByConstRef<STArray>(field,
empty);
693 return getFieldByConstRef<STCurrency>(field,
empty);
700 return getFieldByConstRef<STNumber>(field,
empty);
715 v_[i] = std::move(v);
720 Throw<std::runtime_error>(
"missing field in templated STObject");
728 setFieldUsingSetValue<STUInt8>(field, v);
734 setFieldUsingSetValue<STUInt16>(field, v);
740 setFieldUsingSetValue<STUInt32>(field, v);
746 setFieldUsingSetValue<STUInt64>(field, v);
752 setFieldUsingSetValue<STUInt128>(field, v);
758 setFieldUsingSetValue<STUInt256>(field, v);
764 setFieldUsingSetValue<STInt32>(field, v);
770 setFieldUsingSetValue<STVector256>(field, v);
776 setFieldUsingSetValue<STAccount>(field, v);
838 for (
auto const& elem :
v_)
840 if (elem->getSType() != STI_NOTPRESENT)
841 ret[elem->getFName().getJsonName()] = elem->getJson(options);
852 for (
auto const& t1 :
v_)
854 if ((t1->getSType() != STI_NOTPRESENT) && t1->getFName().isBinary())
858 for (
auto const& t2 : obj.
v_)
860 if (t1->getFName() == t2->getFName())
877 for (
auto const& t2 : obj.
v_)
879 if ((t2->getSType() != STI_NOTPRESENT) && t2->getFName().isBinary())
898 for (
STBase const*
const field : fields)
905 (sType != STI_OBJECT) ||
906 (field->getFName().fieldType == STI_OBJECT),
907 "ripple::STObject::add : valid field type");
908 field->addFieldID(s);
910 if (sType == STI_ARRAY || sType == STI_OBJECT)
924 STBase const& base = elem.get();
925 if ((base.
getSType() != STI_NOTPRESENT) &&
934 return lhs->getFName().fieldCode < rhs->getFName().fieldCode;
T adjacent_find(T... args)
Like std::vector<char> but better.
std::string const & getName() const
bool shouldInclude(bool withSigningField) const
static SField const & getField(int fieldCode)
Defines the fields and their attributes within a STObject.
int getIndex(SField const &) const
Retrieve the position of a named field.
A type which can be exported to a well known binary format.
void setFName(SField const &n)
A STBase is a field.
virtual SerializedTypeID getSType() const
SField const & getFName() const
static STBase * emplace(std::size_t n, void *buf, T &&val)
std::uint8_t const * data() const
value_type value() const noexcept
STPathSet const & getFieldPathSet(SField const &field) const
unsigned char getFieldU8(SField const &field) const
uint192 getFieldH192(SField const &field) const
STBase * getPField(SField const &field, bool createOkay=false)
void setFieldIssue(SField const &field, STIssue const &)
void applyTemplate(SOTemplate const &type)
Blob getFieldVL(SField const &field) const
STObject getFieldObject(SField const &field) const
bool hasMatchingEntry(STBase const &)
bool clearFlag(std::uint32_t)
AccountID getAccountID(SField const &field) const
static std::vector< STBase const * > getSortedFields(STObject const &objToSort, WhichFields whichFields)
STArray const & getFieldArray(SField const &field) const
uint160 getFieldH160(SField const &field) const
void setFieldCurrency(SField const &field, STCurrency const &)
STBase const * peekAtPField(SField const &field) const
STBase const * peekAtPIndex(int offset) const
void setFieldArray(SField const &field, STArray const &v)
void setFieldPathSet(SField const &field, STPathSet const &)
SField const & getFieldSType(int index) const
void setFieldObject(SField const &field, STObject const &v)
STObject(STObject const &)=default
bool isFlag(std::uint32_t) const
std::uint16_t getFieldU16(SField const &field) const
void setFieldH256(SField const &field, uint256 const &)
void setFieldNumber(SField const &field, STNumber const &)
bool setFlag(std::uint32_t)
std::uint32_t getFieldU32(SField const &field) const
void setFieldI32(SField const &field, std::int32_t)
STBase * copy(std::size_t n, void *buf) const override
void makeFieldAbsent(SField const &field)
STObject & peekFieldObject(SField const &field)
void setFieldU16(SField const &field, std::uint16_t)
std::uint64_t getFieldU64(SField const &field) const
STBase * makeFieldPresent(SField const &field)
void setFieldU8(SField const &field, unsigned char)
void set(SOTemplate const &)
void add(Serializer &s) const override
void setFieldAmount(SField const &field, STAmount const &)
bool delField(SField const &field)
STBase const & peekAtIndex(int offset) const
STBase & getField(SField const &field)
STAmount const & getFieldAmount(SField const &field) const
std::int32_t getFieldI32(SField const &field) const
uint256 getSigningHash(HashPrefix prefix) const
uint256 getHash(HashPrefix prefix) const
int getFieldIndex(SField const &field) const
STBase & getIndex(int offset)
void setFieldUsingAssignment(SField const &field, T const &value)
SerializedTypeID getSType() const override
std::string getFullText() const override
std::string getText() const override
bool isFieldPresent(SField const &field) const
static STObject makeInnerObject(SField const &name)
STCurrency const & getFieldCurrency(SField const &field) const
bool operator==(STObject const &o) const
void setAccountID(SField const &field, AccountID const &)
uint128 getFieldH128(SField const &field) const
void applyTemplateFromSField(SField const &)
void setFieldV256(SField const &field, STVector256 const &v)
Json::Value getJson(JsonOptions=JsonOptions::none) const override
void setFieldH128(SField const &field, uint128 const &)
void setFieldU64(SField const &field, std::uint64_t)
bool isEquivalent(STBase const &t) const override
void setFieldU32(SField const &field, std::uint32_t)
STBase const & peekAtField(SField const &field) const
STBase * move(std::size_t n, void *buf) override
std::size_t emplace_back(Args &&... args)
STBase * getPIndex(int offset)
STArray & peekFieldArray(SField const &field)
STNumber const & getFieldNumber(SField const &field) const
STObject & operator=(STObject const &)=default
void setFieldVL(SField const &field, Blob const &)
std::uint32_t getFlags() const
uint256 getFieldH256(SField const &field) const
STVector256 const & getFieldV256(SField const &field) const
bool isDefault() const override
void getFieldID(int &type, int &name)
std::size_t empty() const noexcept
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,...
beast::Journal debugLog()
Returns a debug journal.
std::vector< unsigned char > Blob
Storage for linear binary data.
std::optional< Rules > const & getCurrentTransactionRules()
HashPrefix
Prefix for hashing functions.
bool matches(char const *string, char const *regex)
Return true if the string loosely matches the regex.
void throwFieldNotFound(SField const &field)
Note, should be treated as flags that can be | and &.