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 Throw<std::runtime_error>(
37 "Attempt to create a SLE of unknown type " + std::to_string(safe_cast<std::uint16_t>(k.type)));
38
39 set(format->getSOTemplate());
40
41 setFieldU16(sfLedgerEntryType, static_cast<std::uint16_t>(type_));
42}
43
44STLedgerEntry::STLedgerEntry(SerialIter& sit, uint256 const& index) : STObject(sfLedgerEntry), key_(index)
45{
46 set(sit);
47 setSLEType();
48}
49
50STLedgerEntry::STLedgerEntry(STObject const& object, uint256 const& index) : STObject(object), key_(index)
51{
52 setSLEType();
53}
54
55void
57{
58 auto format = LedgerFormats::getInstance().findByType(safe_cast<LedgerEntryType>(getFieldU16(sfLedgerEntryType)));
59
60 if (format == nullptr)
61 Throw<std::runtime_error>("invalid ledger entry type");
62
63 type_ = format->getType();
64 applyTemplate(format->getSOTemplate()); // May throw
65}
66
69{
70 auto const format = LedgerFormats::getInstance().findByType(type_);
71
72 if (format == nullptr)
73 Throw<std::runtime_error>("invalid ledger entry type");
74
75 std::string ret = "\"";
76 ret += to_string(key_);
77 ret += "\" = { ";
78 ret += format->getName();
79 ret += ", ";
80 ret += STObject::getFullText();
81 ret += "}";
82 return ret;
83}
84
85STBase*
87{
88 return emplace(n, buf, *this);
89}
90
91STBase*
93{
94 return emplace(n, buf, std::move(*this));
95}
96
99{
100 return STI_LEDGERENTRY;
101}
102
105{
106 return str(boost::format("{ %s, %s }") % to_string(key_) % STObject::getText());
107}
108
111{
112 Json::Value ret(STObject::getJson(options));
113
114 ret[jss::index] = to_string(key_);
115
116 if (getType() == ltMPTOKEN_ISSUANCE)
117 ret[jss::mpt_issuance_id] = to_string(makeMptID(getFieldU32(sfSequence), getAccountID(sfIssuer)));
118
119 return ret;
120}
121
122bool
124{
125 static constexpr std::array<LedgerEntryType, 5> newPreviousTxnIDTypes = {
126 ltDIR_NODE, ltAMENDMENTS, ltFEE_SETTINGS, ltNEGATIVE_UNL, ltAMM};
127 // Exclude PrevTxnID/PrevTxnLgrSeq if the fixPreviousTxnID amendment is not
128 // enabled and the ledger object type is in the above set
129 bool const excludePrevTxnID = !rules.enabled(fixPreviousTxnID) &&
130 std::count(newPreviousTxnIDTypes.cbegin(), newPreviousTxnIDTypes.cend(), type_);
131 return !excludePrevTxnID && getFieldIndex(sfPreviousTxnID) != -1;
132}
133
134bool
135STLedgerEntry::thread(uint256 const& txID, std::uint32_t ledgerSeq, uint256& prevTxID, std::uint32_t& prevLedgerID)
136{
137 uint256 oldPrevTxID = getFieldH256(sfPreviousTxnID);
138
139 JLOG(debugLog().info()) << "Thread Tx:" << txID << " prev:" << oldPrevTxID;
140
141 if (oldPrevTxID == txID)
142 {
143 // this transaction is already threaded
144 XRPL_ASSERT(
145 getFieldU32(sfPreviousTxnLgrSeq) == ledgerSeq, "xrpl::STLedgerEntry::thread : ledger sequence match");
146 return false;
147 }
148
149 prevTxID = oldPrevTxID;
150 prevLedgerID = getFieldU32(sfPreviousTxnLgrSeq);
151 setFieldH256(sfPreviousTxnID, txID);
152 setFieldU32(sfPreviousTxnLgrSeq, ledgerSeq);
153 return true;
154}
155
156} // namespace xrpl
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:118
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
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:814
std::uint32_t getFieldU32(SField const &field) const
Definition STObject.cpp:576
void applyTemplate(SOTemplate const &type)
Definition STObject.cpp:148
int getFieldIndex(SField const &field) const
Definition STObject.cpp:368
std::string getFullText() const override
Definition STObject.cpp:277
void setFieldU32(SField const &field, std::uint32_t)
Definition STObject.cpp:718
std::string getText() const override
Definition STObject.cpp:308
void setFieldU16(SField const &field, std::uint16_t)
Definition STObject.cpp:712
uint256 getFieldH256(SField const &field) const
Definition STObject.cpp:606
void set(SOTemplate const &)
Definition STObject.cpp:132
AccountID getAccountID(SField const &field) const
Definition STObject.cpp:618
void setFieldH256(SField const &field, uint256 const &)
Definition STObject.cpp:736
std::uint16_t getFieldU16(SField const &field) const
Definition STObject.cpp:570
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
beast::Journal debugLog()
Returns a debug journal.
Definition Log.cpp:445
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:598
SerializedTypeID
Definition SField.h:91
MPTID makeMptID(std::uint32_t sequence, AccountID const &account)
Definition Indexes.cpp:146
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)