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 <boost/format/free_funcs.hpp>
22
23#include <algorithm>
24#include <array>
25#include <cstddef>
26#include <cstdint>
27#include <stdexcept>
28#include <string>
29#include <utility>
30
31namespace xrpl {
32
33STLedgerEntry::STLedgerEntry(Keylet const& k) : STObject(sfLedgerEntry), key_(k.key), type_(k.type)
34{
35 auto const format = LedgerFormats::getInstance().findByType(type_);
36
37 if (format == nullptr)
38 {
40 "Attempt to create a SLE of unknown type " +
42 }
43
44 set(format->getSOTemplate());
45
46 setFieldU16(sfLedgerEntryType, static_cast<std::uint16_t>(type_));
47}
48
50 : STObject(sfLedgerEntry), key_(index), type_(ltANY)
51{
52 set(sit);
53 setSLEType();
54}
55
56STLedgerEntry::STLedgerEntry(STObject const& object, uint256 const& index)
57 : STObject(object), key_(index), type_(ltANY)
58{
59 setSLEType();
60}
61
62void
64{
66 safeCast<LedgerEntryType>(getFieldU16(sfLedgerEntryType)));
67
68 if (format == nullptr)
69 Throw<std::runtime_error>("invalid ledger entry type");
70
71 type_ = format->getType();
72 applyTemplate(format->getSOTemplate()); // May throw
73}
74
77{
78 auto const format = LedgerFormats::getInstance().findByType(type_);
79
80 if (format == nullptr)
81 Throw<std::runtime_error>("invalid ledger entry type");
82
83 std::string ret = "\"";
84 ret += to_string(key_);
85 ret += "\" = { ";
86 ret += format->getName();
87 ret += ", ";
88 ret += STObject::getFullText();
89 ret += "}";
90 return ret;
91}
92
93STBase*
95{
96 return emplace(n, buf, *this);
97}
98
99STBase*
101{
102 return emplace(n, buf, std::move(*this));
103}
104
107{
108 return STI_LEDGERENTRY;
109}
110
113{
114 return str(boost::format("{ %s, %s }") % to_string(key_) % STObject::getText());
115}
116
119{
120 json::Value ret(STObject::getJson(options));
121
122 ret[jss::index] = to_string(key_);
123
124 if (getType() == ltMPTOKEN_ISSUANCE)
125 {
126 ret[jss::mpt_issuance_id] =
127 to_string(makeMptID(getFieldU32(sfSequence), getAccountID(sfIssuer)));
128 }
129
130 return ret;
131}
132
133bool
135{
136 static constexpr std::array<LedgerEntryType, 5> kNewPreviousTxnIdTypes = {
137 ltDIR_NODE, ltAMENDMENTS, ltFEE_SETTINGS, ltNEGATIVE_UNL, ltAMM};
138 // Exclude PrevTxnID/PrevTxnLgrSeq if the fixPreviousTxnID amendment is not
139 // enabled and the ledger object type is in the above set
140 bool const excludePrevTxnID = !rules.enabled(fixPreviousTxnID) &&
141 (std::count(kNewPreviousTxnIdTypes.cbegin(), kNewPreviousTxnIdTypes.cend(), type_) != 0);
142 return !excludePrevTxnID && getFieldIndex(sfPreviousTxnID) != -1;
143}
144
145bool
147 uint256 const& txID,
148 std::uint32_t ledgerSeq,
149 uint256& prevTxID,
150 std::uint32_t& prevLedgerID)
151{
152 uint256 const oldPrevTxID = getFieldH256(sfPreviousTxnID);
153
154 JLOG(debugLog().info()) << "Thread Tx:" << txID << " prev:" << oldPrevTxID;
155
156 if (oldPrevTxID == txID)
157 {
158 // this transaction is already threaded
159 XRPL_ASSERT(
160 getFieldU32(sfPreviousTxnLgrSeq) == ledgerSeq,
161 "xrpl::STLedgerEntry::thread : ledger sequence match");
162 return false;
163 }
164
165 prevTxID = oldPrevTxID;
166 prevLedgerID = getFieldU32(sfPreviousTxnLgrSeq);
167 setFieldH256(sfPreviousTxnID, txID);
168 setFieldU32(sfPreviousTxnLgrSeq, ledgerSeq);
169 return true;
170}
171
172} // namespace xrpl
T cbegin(T... args)
Represents a JSON value.
Definition json_value.h:130
Item const * findByType(KeyType type) const
Retrieve a format based on its type.
static LedgerFormats const & getInstance()
Rules controlling protocol behavior.
Definition Rules.h:33
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:171
A type which can be exported to a well known binary format.
Definition STBase.h:117
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition STBase.h:215
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:591
void applyTemplate(SOTemplate const &type)
Definition STObject.cpp:155
int getFieldIndex(SField const &field) const
Definition STObject.cpp:383
std::string getFullText() const override
Definition STObject.cpp:285
void setFieldU32(SField const &field, std::uint32_t)
Definition STObject.cpp:733
std::string getText() const override
Definition STObject.cpp:322
json::Value getJson(JsonOptions=JsonOptions::Values::None) const override
Definition STObject.cpp:835
STObject(STObject const &)=default
void setFieldU16(SField const &field, std::uint16_t)
Definition STObject.cpp:727
uint256 getFieldH256(SField const &field) const
Definition STObject.cpp:621
void set(SOTemplate const &)
Definition STObject.cpp:135
AccountID getAccountID(SField const &field) const
Definition STObject.cpp:633
void setFieldH256(SField const &field, uint256 const &)
Definition STObject.cpp:757
std::uint16_t getFieldU16(SField const &field) const
Definition STObject.cpp:585
T count(T... args)
T cend(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 std::enable_if_t< std::is_integral_v< Dest > &&std::is_integral_v< Src >, Dest > safeCast(Src s) noexcept
Definition safe_cast.h:21
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:633
SerializedTypeID
Definition SField.h:93
@ ltANY
A special type, matching any ledger entry type.
BaseUInt< 256 > uint256
Definition base_uint.h:562
MPTID makeMptID(std::uint32_t sequence, AccountID const &account)
Definition Indexes.cpp:172
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:49
Note, should be treated as flags that can be | and &.
Definition STBase.h:17
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:19
LedgerEntryType type
Definition Keylet.h:21
T to_string(T... args)