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/basics/Log.h>
21#include <xrpl/basics/base_uint.h>
22#include <xrpl/basics/contract.h>
23#include <xrpl/basics/safe_cast.h>
24#include <xrpl/beast/utility/instrumentation.h>
25#include <xrpl/json/to_string.h>
26#include <xrpl/protocol/Feature.h>
27#include <xrpl/protocol/Indexes.h>
28#include <xrpl/protocol/Keylet.h>
29#include <xrpl/protocol/LedgerFormats.h>
30#include <xrpl/protocol/Rules.h>
31#include <xrpl/protocol/SField.h>
32#include <xrpl/protocol/STBase.h>
33#include <xrpl/protocol/STLedgerEntry.h>
34#include <xrpl/protocol/STObject.h>
35#include <xrpl/protocol/Serializer.h>
36#include <xrpl/protocol/jss.h>
37
38#include <boost/format/free_funcs.hpp>
39
40#include <algorithm>
41#include <array>
42#include <cstddef>
43#include <cstdint>
44#include <stdexcept>
45#include <string>
46#include <utility>
47
48namespace ripple {
49
51 : STObject(sfLedgerEntry), key_(k.key), type_(k.type)
52{
53 auto const format = LedgerFormats::getInstance().findByType(type_);
54
55 if (format == nullptr)
56 Throw<std::runtime_error>(
57 "Attempt to create a SLE of unknown type " +
58 std::to_string(safe_cast<std::uint16_t>(k.type)));
59
60 set(format->getSOTemplate());
61
62 setFieldU16(sfLedgerEntryType, static_cast<std::uint16_t>(type_));
63}
64
66 : STObject(sfLedgerEntry), key_(index)
67{
68 set(sit);
69 setSLEType();
70}
71
72STLedgerEntry::STLedgerEntry(STObject const& object, uint256 const& index)
73 : STObject(object), key_(index)
74{
75 setSLEType();
76}
77
78void
80{
82 safe_cast<LedgerEntryType>(getFieldU16(sfLedgerEntryType)));
83
84 if (format == nullptr)
85 Throw<std::runtime_error>("invalid ledger entry type");
86
87 type_ = format->getType();
88 applyTemplate(format->getSOTemplate()); // May throw
89}
90
93{
94 auto const format = LedgerFormats::getInstance().findByType(type_);
95
96 if (format == nullptr)
97 Throw<std::runtime_error>("invalid ledger entry type");
98
99 std::string ret = "\"";
100 ret += to_string(key_);
101 ret += "\" = { ";
102 ret += format->getName();
103 ret += ", ";
104 ret += STObject::getFullText();
105 ret += "}";
106 return ret;
107}
108
109STBase*
111{
112 return emplace(n, buf, *this);
113}
114
115STBase*
117{
118 return emplace(n, buf, std::move(*this));
119}
120
123{
124 return STI_LEDGERENTRY;
125}
126
129{
130 return str(
131 boost::format("{ %s, %s }") % to_string(key_) % STObject::getText());
132}
133
136{
137 Json::Value ret(STObject::getJson(options));
138
139 ret[jss::index] = to_string(key_);
140
141 if (getType() == ltMPTOKEN_ISSUANCE)
142 ret[jss::mpt_issuance_id] = to_string(
143 makeMptID(getFieldU32(sfSequence), getAccountID(sfIssuer)));
144
145 return ret;
146}
147
148bool
150{
151 static constexpr std::array<LedgerEntryType, 5> newPreviousTxnIDTypes = {
152 ltDIR_NODE, ltAMENDMENTS, ltFEE_SETTINGS, ltNEGATIVE_UNL, ltAMM};
153 // Exclude PrevTxnID/PrevTxnLgrSeq if the fixPreviousTxnID amendment is not
154 // enabled and the ledger object type is in the above set
155 bool const excludePrevTxnID = !rules.enabled(fixPreviousTxnID) &&
157 newPreviousTxnIDTypes.cbegin(),
158 newPreviousTxnIDTypes.cend(),
159 type_);
160 return !excludePrevTxnID && getFieldIndex(sfPreviousTxnID) != -1;
161}
162
163bool
165 uint256 const& txID,
166 std::uint32_t ledgerSeq,
167 uint256& prevTxID,
168 std::uint32_t& prevLedgerID)
169{
170 uint256 oldPrevTxID = getFieldH256(sfPreviousTxnID);
171
172 JLOG(debugLog().info()) << "Thread Tx:" << txID << " prev:" << oldPrevTxID;
173
174 if (oldPrevTxID == txID)
175 {
176 // this transaction is already threaded
177 XRPL_ASSERT(
178 getFieldU32(sfPreviousTxnLgrSeq) == ledgerSeq,
179 "ripple::STLedgerEntry::thread : ledger sequence match");
180 return false;
181 }
182
183 prevTxID = oldPrevTxID;
184 prevLedgerID = getFieldU32(sfPreviousTxnLgrSeq);
185 setFieldH256(sfPreviousTxnID, txID);
186 setFieldU32(sfPreviousTxnLgrSeq, ledgerSeq);
187 return true;
188}
189
190} // namespace ripple
T cbegin(T... args)
Represents a JSON value.
Definition json_value.h:149
Item const * findByType(KeyType type) const
Retrieve a format based on its type.
static LedgerFormats const & getInstance()
Rules controlling protocol behavior.
Definition Rules.h:38
bool enabled(uint256 const &feature) const
Returns true if a feature is enabled.
Definition Rules.cpp:130
A type which can be exported to a well known binary format.
Definition STBase.h:135
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition STBase.h:233
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:172
AccountID getAccountID(SField const &field) const
Definition STObject.cpp:657
std::uint16_t getFieldU16(SField const &field) const
Definition STObject.cpp:609
void setFieldH256(SField const &field, uint256 const &)
Definition STObject.cpp:775
std::uint32_t getFieldU32(SField const &field) const
Definition STObject.cpp:615
void setFieldU16(SField const &field, std::uint16_t)
Definition STObject.cpp:751
void set(SOTemplate const &)
Definition STObject.cpp:156
int getFieldIndex(SField const &field) const
Definition STObject.cpp:413
std::string getFullText() const override
Definition STObject.cpp:310
std::string getText() const override
Definition STObject.cpp:341
Json::Value getJson(JsonOptions=JsonOptions::none) const override
Definition STObject.cpp:853
void setFieldU32(SField const &field, std::uint32_t)
Definition STObject.cpp:757
uint256 getFieldH256(SField const &field) const
Definition STObject.cpp:645
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:25
SerializedTypeID
Definition SField.h:110
beast::Journal debugLog()
Returns a debug journal.
Definition Log.cpp:476
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:630
MPTID makeMptID(std::uint32_t sequence, AccountID const &account)
Definition Indexes.cpp:170
Note, should be treated as flags that can be | and &.
Definition STBase.h:37
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:39
LedgerEntryType type
Definition Keylet.h:41
T to_string(T... args)