rippled
Loading...
Searching...
No Matches
STLedgerEntry.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpl/protocol/STLedgerEntry.h>
21
22#include <xrpl/basics/Log.h>
23#include <xrpl/basics/contract.h>
24#include <xrpl/basics/safe_cast.h>
25#include <xrpl/json/to_string.h>
26#include <xrpl/protocol/Feature.h>
27#include <xrpl/protocol/Indexes.h>
28#include <xrpl/protocol/Rules.h>
29#include <xrpl/protocol/jss.h>
30#include <boost/format.hpp>
31#include <algorithm>
32#include <array>
33#include <limits>
34
35namespace ripple {
36
38 : STObject(sfLedgerEntry), key_(k.key), type_(k.type)
39{
40 auto const format = LedgerFormats::getInstance().findByType(type_);
41
42 if (format == nullptr)
43 Throw<std::runtime_error>(
44 "Attempt to create a SLE of unknown type " +
45 std::to_string(safe_cast<std::uint16_t>(k.type)));
46
47 set(format->getSOTemplate());
48
49 setFieldU16(sfLedgerEntryType, static_cast<std::uint16_t>(type_));
50}
51
53 : STObject(sfLedgerEntry), key_(index)
54{
55 set(sit);
56 setSLEType();
57}
58
59STLedgerEntry::STLedgerEntry(STObject const& object, uint256 const& index)
60 : STObject(object), key_(index)
61{
62 setSLEType();
63}
64
65void
67{
69 safe_cast<LedgerEntryType>(getFieldU16(sfLedgerEntryType)));
70
71 if (format == nullptr)
72 Throw<std::runtime_error>("invalid ledger entry type");
73
74 type_ = format->getType();
75 applyTemplate(format->getSOTemplate()); // May throw
76}
77
80{
81 auto const format = LedgerFormats::getInstance().findByType(type_);
82
83 if (format == nullptr)
84 Throw<std::runtime_error>("invalid ledger entry type");
85
86 std::string ret = "\"";
87 ret += to_string(key_);
88 ret += "\" = { ";
89 ret += format->getName();
90 ret += ", ";
91 ret += STObject::getFullText();
92 ret += "}";
93 return ret;
94}
95
96STBase*
98{
99 return emplace(n, buf, *this);
100}
101
102STBase*
104{
105 return emplace(n, buf, std::move(*this));
106}
107
110{
111 return STI_LEDGERENTRY;
112}
113
116{
117 return str(
118 boost::format("{ %s, %s }") % to_string(key_) % STObject::getText());
119}
120
123{
124 Json::Value ret(STObject::getJson(options));
125
126 ret[jss::index] = to_string(key_);
127
128 if (getType() == ltMPTOKEN_ISSUANCE)
129 ret[jss::mpt_issuance_id] = to_string(
130 makeMptID(getFieldU32(sfSequence), getAccountID(sfIssuer)));
131
132 return ret;
133}
134
135bool
137{
138 static constexpr std::array<LedgerEntryType, 5> newPreviousTxnIDTypes = {
139 ltDIR_NODE, ltAMENDMENTS, ltFEE_SETTINGS, ltNEGATIVE_UNL, ltAMM};
140 // Exclude PrevTxnID/PrevTxnLgrSeq if the fixPreviousTxnID amendment is not
141 // enabled and the ledger object type is in the above set
142 bool const excludePrevTxnID = !rules.enabled(fixPreviousTxnID) &&
144 newPreviousTxnIDTypes.cbegin(),
145 newPreviousTxnIDTypes.cend(),
146 type_);
147 return !excludePrevTxnID && getFieldIndex(sfPreviousTxnID) != -1;
148}
149
150bool
152 uint256 const& txID,
153 std::uint32_t ledgerSeq,
154 uint256& prevTxID,
155 std::uint32_t& prevLedgerID)
156{
157 uint256 oldPrevTxID = getFieldH256(sfPreviousTxnID);
158
159 JLOG(debugLog().info()) << "Thread Tx:" << txID << " prev:" << oldPrevTxID;
160
161 if (oldPrevTxID == txID)
162 {
163 // this transaction is already threaded
164 XRPL_ASSERT(
165 getFieldU32(sfPreviousTxnLgrSeq) == ledgerSeq,
166 "ripple::STLedgerEntry::thread : ledger sequence match");
167 return false;
168 }
169
170 prevTxID = oldPrevTxID;
171 prevLedgerID = getFieldU32(sfPreviousTxnLgrSeq);
172 setFieldH256(sfPreviousTxnID, txID);
173 setFieldU32(sfPreviousTxnLgrSeq, ledgerSeq);
174 return true;
175}
176
177} // namespace ripple
T cbegin(T... args)
Represents a JSON value.
Definition: json_value.h:147
Item const * findByType(KeyType type) const
Retrieve a format based on its type.
Definition: KnownFormats.h:127
static LedgerFormats const & getInstance()
Rules controlling protocol behavior.
Definition: Rules.h:35
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition: Rules.cpp:122
A type which can be exported to a well known binary format.
Definition: STBase.h:124
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition: STBase.h:222
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_
Definition: STLedgerEntry.h:34
bool thread(uint256 const &txID, std::uint32_t ledgerSeq, uint256 &prevTxID, std::uint32_t &prevLedgerID)
Json::Value getJson(JsonOptions options) const override
STBase * copy(std::size_t n, void *buf) const override
bool isThreadedType(Rules const &rules) const
std::string getText() const override
AccountID getAccountID(SField const &field) const
Definition: STObject.cpp:621
std::uint16_t getFieldU16(SField const &field) const
Definition: STObject.cpp:579
void setFieldH256(SField const &field, uint256 const &)
Definition: STObject.cpp:729
std::uint32_t getFieldU32(SField const &field) const
Definition: STObject.cpp:585
void setFieldU16(SField const &field, std::uint16_t)
Definition: STObject.cpp:705
void set(const SOTemplate &)
Definition: STObject.cpp:126
int getFieldIndex(SField const &field) const
Definition: STObject.cpp:383
std::string getFullText() const override
Definition: STObject.cpp:280
std::string getText() const override
Definition: STObject.cpp:311
Json::Value getJson(JsonOptions options) const override
Definition: STObject.cpp:795
void setFieldU32(SField const &field, std::uint32_t)
Definition: STObject.cpp:711
void applyTemplate(const SOTemplate &type)
Definition: STObject.cpp:142
uint256 getFieldH256(SField const &field) const
Definition: STObject.cpp:615
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:26
SerializedTypeID
Definition: SField.h:108
beast::Journal debugLog()
Returns a debug journal.
Definition: Log.cpp:452
std::string to_string(base_uint< Bits, Tag > const &a)
Definition: base_uint.h:629
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:36
A pair of SHAMap key and LedgerEntryType.
Definition: Keylet.h:39
LedgerEntryType type
Definition: Keylet.h:41
T to_string(T... args)