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 ripple {
30
32 : STObject(sfLedgerEntry), key_(k.key), type_(k.type)
33{
34 auto const format = LedgerFormats::getInstance().findByType(type_);
35
36 if (format == nullptr)
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 set(format->getSOTemplate());
42
43 setFieldU16(sfLedgerEntryType, static_cast<std::uint16_t>(type_));
44}
45
47 : STObject(sfLedgerEntry), key_(index)
48{
49 set(sit);
50 setSLEType();
51}
52
53STLedgerEntry::STLedgerEntry(STObject const& object, uint256 const& index)
54 : STObject(object), key_(index)
55{
56 setSLEType();
57}
58
59void
61{
63 safe_cast<LedgerEntryType>(getFieldU16(sfLedgerEntryType)));
64
65 if (format == nullptr)
66 Throw<std::runtime_error>("invalid ledger entry type");
67
68 type_ = format->getType();
69 applyTemplate(format->getSOTemplate()); // May throw
70}
71
74{
75 auto const format = LedgerFormats::getInstance().findByType(type_);
76
77 if (format == nullptr)
78 Throw<std::runtime_error>("invalid ledger entry type");
79
80 std::string ret = "\"";
81 ret += to_string(key_);
82 ret += "\" = { ";
83 ret += format->getName();
84 ret += ", ";
85 ret += STObject::getFullText();
86 ret += "}";
87 return ret;
88}
89
90STBase*
92{
93 return emplace(n, buf, *this);
94}
95
96STBase*
98{
99 return emplace(n, buf, std::move(*this));
100}
101
104{
105 return STI_LEDGERENTRY;
106}
107
110{
111 return str(
112 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 ret[jss::mpt_issuance_id] = to_string(
124 makeMptID(getFieldU32(sfSequence), getAccountID(sfIssuer)));
125
126 return ret;
127}
128
129bool
131{
132 static constexpr std::array<LedgerEntryType, 5> newPreviousTxnIDTypes = {
133 ltDIR_NODE, ltAMENDMENTS, ltFEE_SETTINGS, ltNEGATIVE_UNL, ltAMM};
134 // Exclude PrevTxnID/PrevTxnLgrSeq if the fixPreviousTxnID amendment is not
135 // enabled and the ledger object type is in the above set
136 bool const excludePrevTxnID = !rules.enabled(fixPreviousTxnID) &&
138 newPreviousTxnIDTypes.cbegin(),
139 newPreviousTxnIDTypes.cend(),
140 type_);
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 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 "ripple::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 ripple
T cbegin(T... args)
Represents a JSON value.
Definition json_value.h:131
Item const * findByType(KeyType type) const
Retrieve a format based on its type.
static LedgerFormats const & getInstance()
Rules controlling protocol behavior.
Definition Rules.h:19
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:111
A type which can be exported to a well known binary format.
Definition STBase.h:116
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition STBase.h:214
STBase * move(std::size_t n, void *buf) override
STLedgerEntry(Keylet const &k)
Create an empty object with the given key and type.
LedgerEntryType getType() const
SerializedTypeID getSType() const override
std::string getFullText() const override
LedgerEntryType type_
bool thread(uint256 const &txID, std::uint32_t ledgerSeq, uint256 &prevTxID, std::uint32_t &prevLedgerID)
Json::Value getJson(JsonOptions options=JsonOptions::none) const override
STBase * copy(std::size_t n, void *buf) const override
bool isThreadedType(Rules const &rules) const
std::string getText() const override
void applyTemplate(SOTemplate const &type)
Definition STObject.cpp:153
AccountID getAccountID(SField const &field) const
Definition STObject.cpp:638
std::uint16_t getFieldU16(SField const &field) const
Definition STObject.cpp:590
void setFieldH256(SField const &field, uint256 const &)
Definition STObject.cpp:756
std::uint32_t getFieldU32(SField const &field) const
Definition STObject.cpp:596
void setFieldU16(SField const &field, std::uint16_t)
Definition STObject.cpp:732
void set(SOTemplate const &)
Definition STObject.cpp:137
int getFieldIndex(SField const &field) const
Definition STObject.cpp:394
std::string getFullText() const override
Definition STObject.cpp:291
std::string getText() const override
Definition STObject.cpp:322
Json::Value getJson(JsonOptions=JsonOptions::none) const override
Definition STObject.cpp:834
void setFieldU32(SField const &field, std::uint32_t)
Definition STObject.cpp:738
uint256 getFieldH256(SField const &field) const
Definition STObject.cpp:626
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:6
SerializedTypeID
Definition SField.h:91
beast::Journal debugLog()
Returns a debug journal.
Definition Log.cpp:457
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:611
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:18
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:20
LedgerEntryType type
Definition Keylet.h:22
T to_string(T... args)