rippled
Loading...
Searching...
No Matches
STLedgerEntry.cpp
1#include <xrpl/basics/Log.h>
2#include <xrpl/basics/base_uint.h>
3#include <xrpl/basics/contract.h>
4#include <xrpl/basics/safe_cast.h>
5#include <xrpl/beast/utility/instrumentation.h>
6#include <xrpl/json/to_string.h>
7#include <xrpl/protocol/Feature.h>
8#include <xrpl/protocol/Indexes.h>
9#include <xrpl/protocol/Keylet.h>
10#include <xrpl/protocol/LedgerFormats.h>
11#include <xrpl/protocol/Rules.h>
12#include <xrpl/protocol/SField.h>
13#include <xrpl/protocol/STBase.h>
14#include <xrpl/protocol/STLedgerEntry.h>
15#include <xrpl/protocol/STObject.h>
16#include <xrpl/protocol/Serializer.h>
17#include <xrpl/protocol/jss.h>
18
19#include <boost/format/free_funcs.hpp>
20
21#include <algorithm>
22#include <array>
23#include <cstddef>
24#include <cstdint>
25#include <stdexcept>
26#include <string>
27#include <utility>
28
29namespace xrpl {
30
31STLedgerEntry::STLedgerEntry(Keylet const& k) : STObject(sfLedgerEntry), key_(k.key), type_(k.type)
32{
33 auto const format = LedgerFormats::getInstance().findByType(type_);
34
35 if (format == nullptr)
36 {
37 Throw<std::runtime_error>(
38 "Attempt to create a SLE of unknown type " +
39 std::to_string(safe_cast<std::uint16_t>(k.type)));
40 }
41
42 set(format->getSOTemplate());
43
44 setFieldU16(sfLedgerEntryType, static_cast<std::uint16_t>(type_));
45}
46
48 : STObject(sfLedgerEntry), key_(index), type_(ltANY)
49{
50 set(sit);
51 setSLEType();
52}
53
54STLedgerEntry::STLedgerEntry(STObject const& object, uint256 const& index)
55 : STObject(object), key_(index), type_(ltANY)
56{
57 setSLEType();
58}
59
60void
62{
64 safe_cast<LedgerEntryType>(getFieldU16(sfLedgerEntryType)));
65
66 if (format == nullptr)
67 Throw<std::runtime_error>("invalid ledger entry type");
68
69 type_ = format->getType();
70 applyTemplate(format->getSOTemplate()); // May throw
71}
72
75{
76 auto const format = LedgerFormats::getInstance().findByType(type_);
77
78 if (format == nullptr)
79 Throw<std::runtime_error>("invalid ledger entry type");
80
81 std::string ret = "\"";
82 ret += to_string(key_);
83 ret += "\" = { ";
84 ret += format->getName();
85 ret += ", ";
86 ret += STObject::getFullText();
87 ret += "}";
88 return ret;
89}
90
91STBase*
93{
94 return emplace(n, buf, *this);
95}
96
97STBase*
99{
100 return emplace(n, buf, std::move(*this));
101}
102
105{
106 return STI_LEDGERENTRY;
107}
108
111{
112 return str(boost::format("{ %s, %s }") % to_string(key_) % STObject::getText());
113}
114
117{
118 Json::Value ret(STObject::getJson(options));
119
120 ret[jss::index] = to_string(key_);
121
122 if (getType() == ltMPTOKEN_ISSUANCE)
123 {
124 ret[jss::mpt_issuance_id] =
125 to_string(makeMptID(getFieldU32(sfSequence), getAccountID(sfIssuer)));
126 }
127
128 return ret;
129}
130
131bool
133{
134 static constexpr std::array<LedgerEntryType, 5> newPreviousTxnIDTypes = {
135 ltDIR_NODE, ltAMENDMENTS, ltFEE_SETTINGS, ltNEGATIVE_UNL, ltAMM};
136 // Exclude PrevTxnID/PrevTxnLgrSeq if the fixPreviousTxnID amendment is not
137 // enabled and the ledger object type is in the above set
138 bool const excludePrevTxnID = !rules.enabled(fixPreviousTxnID) &&
139 (std::count(newPreviousTxnIDTypes.cbegin(), newPreviousTxnIDTypes.cend(), type_) != 0);
140 return !excludePrevTxnID && getFieldIndex(sfPreviousTxnID) != -1;
141}
142
143bool
145 uint256 const& txID,
146 std::uint32_t ledgerSeq,
147 uint256& prevTxID,
148 std::uint32_t& prevLedgerID)
149{
150 uint256 const oldPrevTxID = getFieldH256(sfPreviousTxnID);
151
152 JLOG(debugLog().info()) << "Thread Tx:" << txID << " prev:" << oldPrevTxID;
153
154 if (oldPrevTxID == txID)
155 {
156 // this transaction is already threaded
157 XRPL_ASSERT(
158 getFieldU32(sfPreviousTxnLgrSeq) == ledgerSeq,
159 "xrpl::STLedgerEntry::thread : ledger sequence match");
160 return false;
161 }
162
163 prevTxID = oldPrevTxID;
164 prevLedgerID = getFieldU32(sfPreviousTxnLgrSeq);
165 setFieldH256(sfPreviousTxnID, txID);
166 setFieldU32(sfPreviousTxnLgrSeq, ledgerSeq);
167 return true;
168}
169
170} // 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:18
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:120
A type which can be exported to a well known binary format.
Definition STBase.h:115
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition STBase.h:213
LedgerEntryType type_
Json::Value getJson(JsonOptions options=JsonOptions::none) const override
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=JsonOptions::none) const override
Definition STObject.cpp:831
std::uint32_t getFieldU32(SField const &field) const
Definition STObject.cpp:593
void applyTemplate(SOTemplate const &type)
Definition STObject.cpp:154
int getFieldIndex(SField const &field) const
Definition STObject.cpp:385
std::string getFullText() const override
Definition STObject.cpp:286
void setFieldU32(SField const &field, std::uint32_t)
Definition STObject.cpp:735
std::string getText() const override
Definition STObject.cpp:323
void setFieldU16(SField const &field, std::uint16_t)
Definition STObject.cpp:729
uint256 getFieldH256(SField const &field) const
Definition STObject.cpp:623
void set(SOTemplate const &)
Definition STObject.cpp:134
AccountID getAccountID(SField const &field) const
Definition STObject.cpp:635
void setFieldH256(SField const &field, uint256 const &)
Definition STObject.cpp:753
std::uint16_t getFieldU16(SField const &field) const
Definition STObject.cpp:587
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:453
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:602
SerializedTypeID
Definition SField.h:90
@ ltANY
A special type, matching any ledger entry type.
MPTID makeMptID(std::uint32_t sequence, AccountID const &account)
Definition Indexes.cpp:151
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)