xrpld
Loading...
Searching...
No Matches
STLedgerEntry.cpp
1#include <xrpl/protocol/STLedgerEntry.h>
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/base_uint.h>
5#include <xrpl/basics/contract.h>
6#include <xrpl/basics/safe_cast.h>
7#include <xrpl/beast/utility/instrumentation.h>
8#include <xrpl/json/json_value.h>
9#include <xrpl/json/to_string.h> // IWYU pragma: keep
10#include <xrpl/protocol/Feature.h>
11#include <xrpl/protocol/Indexes.h>
12#include <xrpl/protocol/Keylet.h>
13#include <xrpl/protocol/LedgerFormats.h>
14#include <xrpl/protocol/Rules.h>
15#include <xrpl/protocol/SField.h>
16#include <xrpl/protocol/STBase.h>
17#include <xrpl/protocol/STObject.h>
18#include <xrpl/protocol/Serializer.h>
19#include <xrpl/protocol/jss.h>
20
21#include <algorithm>
22#include <array>
23#include <cstddef>
24#include <cstdint>
25#include <format>
26#include <stdexcept>
27#include <string>
28#include <utility>
29
30namespace xrpl {
31
32STLedgerEntry::STLedgerEntry(Keylet const& k) : STObject(sfLedgerEntry), key_(k.key), type_(k.type)
33{
34 auto const format = LedgerFormats::getInstance().findByType(type_);
35
36 if (format == nullptr)
37 {
39 "Attempt to create a SLE of unknown type " +
41 }
42
43 set(format->getSOTemplate());
44
45 setFieldU16(sfLedgerEntryType, static_cast<std::uint16_t>(type_));
46}
47
49 : STObject(sfLedgerEntry), key_(index), type_(ltANY)
50{
51 set(sit);
52 setSLEType();
53}
54
55STLedgerEntry::STLedgerEntry(STObject const& object, uint256 const& index)
56 : STObject(object), key_(index), type_(ltANY)
57{
58 setSLEType();
59}
60
61void
63{
65 safeCast<LedgerEntryType>(getFieldU16(sfLedgerEntryType)));
66
67 if (format == nullptr)
68 Throw<std::runtime_error>("invalid ledger entry type");
69
70 type_ = format->getType();
71 applyTemplate(format->getSOTemplate()); // May throw
72}
73
76{
77 auto const format = LedgerFormats::getInstance().findByType(type_);
78
79 if (format == nullptr)
80 Throw<std::runtime_error>("invalid ledger entry type");
81
82 std::string ret = "\"";
83 ret += to_string(key_);
84 ret += "\" = { ";
85 ret += format->getName();
86 ret += ", ";
87 ret += STObject::getFullText();
88 ret += "}";
89 return ret;
90}
91
92STBase*
94{
95 return emplace(n, buf, *this);
96}
97
98STBase*
100{
101 return emplace(n, buf, std::move(*this));
102}
103
106{
107 return STI_LEDGERENTRY;
108}
109
112{
113 return std::format("{{ {}, {} }}", to_string(key_), STObject::getText());
114}
115
118{
119 json::Value ret(STObject::getJson(options));
120
121 ret[jss::index] = to_string(key_);
122
123 if (getType() == ltMPTOKEN_ISSUANCE)
124 {
125 ret[jss::mpt_issuance_id] =
126 to_string(makeMptID(getFieldU32(sfSequence), getAccountID(sfIssuer)));
127 }
128
129 return ret;
130}
131
132bool
134{
135 static constexpr std::array<LedgerEntryType, 5> kNewPreviousTxnIdTypes = {
136 ltDIR_NODE, ltAMENDMENTS, ltFEE_SETTINGS, ltNEGATIVE_UNL, ltAMM};
137 // Exclude PrevTxnID/PrevTxnLgrSeq if the fixPreviousTxnID amendment is not
138 // enabled and the ledger object type is in the above set
139 bool const excludePrevTxnID = !rules.enabled(fixPreviousTxnID) &&
140 (std::count(kNewPreviousTxnIdTypes.cbegin(), kNewPreviousTxnIdTypes.cend(), type_) != 0);
141 return !excludePrevTxnID && getFieldIndex(sfPreviousTxnID) != -1;
142}
143
144bool
146 uint256 const& txID,
147 std::uint32_t ledgerSeq,
148 uint256& prevTxID,
149 std::uint32_t& prevLedgerID)
150{
151 uint256 const oldPrevTxID = getFieldH256(sfPreviousTxnID);
152
153 JLOG(debugLog().info()) << "Thread Tx:" << txID << " prev:" << oldPrevTxID;
154
155 if (oldPrevTxID == txID)
156 {
157 // this transaction is already threaded
158 XRPL_ASSERT(
159 getFieldU32(sfPreviousTxnLgrSeq) == ledgerSeq,
160 "xrpl::STLedgerEntry::thread : ledger sequence match");
161 return false;
162 }
163
164 prevTxID = oldPrevTxID;
165 prevLedgerID = getFieldU32(sfPreviousTxnLgrSeq);
166 setFieldH256(sfPreviousTxnID, txID);
167 setFieldU32(sfPreviousTxnLgrSeq, ledgerSeq);
168 return true;
169}
170
171} // namespace xrpl
T cbegin(T... args)
Represents a JSON value.
Definition json_value.h:117
Item const * findByType(KeyType type) const
Retrieve a format based on its type.
static LedgerFormats const & getInstance()
Rules controlling protocol behavior.
Definition Rules.h:40
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:180
A type which can be exported to a well known binary format.
Definition STBase.h:129
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition STBase.h:226
LedgerEntryType type_
uint256 const & key() const
Returns the 'key' (or 'index') of this item.
std::string getFullText() const override
STBase * copy(std::size_t n, void *buf) const override
bool thread(uint256 const &txID, std::uint32_t ledgerSeq, uint256 &prevTxID, std::uint32_t &prevLedgerID)
LedgerEntryType getType() const
SerializedTypeID getSType() const override
STLedgerEntry(Keylet const &k)
Create an empty object with the given key and type.
std::string getText() const override
bool isThreadedType(Rules const &rules) const
STBase * move(std::size_t n, void *buf) override
json::Value getJson(JsonOptions options=JsonOptions::Values::None) const override
std::uint32_t getFieldU32(SField const &field) const
Definition STObject.cpp:601
void applyTemplate(SOTemplate const &type)
Definition STObject.cpp:158
int getFieldIndex(SField const &field) const
Definition STObject.cpp:393
std::string getFullText() const override
Definition STObject.cpp:295
void setFieldU32(SField const &field, std::uint32_t)
Definition STObject.cpp:743
std::string getText() const override
Definition STObject.cpp:332
json::Value getJson(JsonOptions=JsonOptions::Values::None) const override
Definition STObject.cpp:845
STObject(STObject const &)=default
void setFieldU16(SField const &field, std::uint16_t)
Definition STObject.cpp:737
uint256 getFieldH256(SField const &field) const
Definition STObject.cpp:631
void set(SOTemplate const &)
Definition STObject.cpp:138
AccountID getAccountID(SField const &field) const
Definition STObject.cpp:643
void setFieldH256(SField const &field, uint256 const &)
Definition STObject.cpp:767
std::uint16_t getFieldU16(SField const &field) const
Definition STObject.cpp:595
T count(T... args)
T cend(T... args)
T format(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
beast::Journal debugLog()
Returns a debug journal.
Definition Log.cpp:399
constexpr Dest safeCast(Src s) noexcept
Definition safe_cast.h:21
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:651
SerializedTypeID
Definition SField.h:94
MPTID makeMptID(std::uint32_t const sequence, AccountID const &account)
Definition Indexes.cpp:184
@ ltANY
A special type, matching any ledger entry type.
BaseUInt< 256 > uint256
Definition base_uint.h:580
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:52
Note, should be treated as flags that can be | and &.
Definition STBase.h:22
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:20
LedgerEntryType type
Definition Keylet.h:22
T to_string(T... args)