rippled
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 <ripple/basics/Log.h>
21 #include <ripple/basics/contract.h>
22 #include <ripple/basics/safe_cast.h>
23 #include <ripple/json/to_string.h>
24 #include <ripple/protocol/Indexes.h>
25 #include <ripple/protocol/STLedgerEntry.h>
26 #include <ripple/protocol/jss.h>
27 #include <boost/format.hpp>
28 #include <limits>
29 
30 namespace ripple {
31 
33  : STObject(sfLedgerEntry), key_(k.key), type_(k.type)
34 {
35  // The on-ledger representation of a key type is a 16-bit unsigned integer
36  // but the LedgerEntryType enum has a larger range (including negative
37  // values), so catch obviously wrong values:
38  constexpr auto const minValidValue =
40 
41  constexpr auto const maxValidValue =
43 
44  if (type_ < minValidValue || type_ > maxValidValue)
45  Throw<std::runtime_error>("invalid ledger entry type: out of range");
46 
47  auto const format = LedgerFormats::getInstance().findByType(type_);
48 
49  if (format == nullptr)
50  Throw<std::runtime_error>("unknown ledger entry type");
51 
52  set(format->getSOTemplate());
53 
55 }
56 
58  : STObject(sfLedgerEntry), key_(index)
59 {
60  set(sit);
61  setSLEType();
62 }
63 
64 STLedgerEntry::STLedgerEntry(STObject const& object, uint256 const& index)
65  : STObject(object), key_(index)
66 {
67  setSLEType();
68 }
69 
70 void
72 {
73  auto format = LedgerFormats::getInstance().findByType(
74  safe_cast<LedgerEntryType>(getFieldU16(sfLedgerEntryType)));
75 
76  if (format == nullptr)
77  Throw<std::runtime_error>("invalid ledger entry type");
78 
79  type_ = format->getType();
80  applyTemplate(format->getSOTemplate()); // May throw
81 }
82 
85 {
86  auto const format = LedgerFormats::getInstance().findByType(type_);
87 
88  if (format == nullptr)
89  Throw<std::runtime_error>("invalid ledger entry type");
90 
91  std::string ret = "\"";
92  ret += to_string(key_);
93  ret += "\" = { ";
94  ret += format->getName();
95  ret += ", ";
96  ret += STObject::getFullText();
97  ret += "}";
98  return ret;
99 }
100 
103 {
104  return str(
105  boost::format("{ %s, %s }") % to_string(key_) % STObject::getText());
106 }
107 
110 {
111  Json::Value ret(STObject::getJson(options));
112 
113  ret[jss::index] = to_string(key_);
114 
115  return ret;
116 }
117 
118 bool
120 {
121  return getFieldIndex(sfPreviousTxnID) != -1;
122 }
123 
124 bool
126  uint256 const& txID,
127  std::uint32_t ledgerSeq,
128  uint256& prevTxID,
129  std::uint32_t& prevLedgerID)
130 {
131  uint256 oldPrevTxID = getFieldH256(sfPreviousTxnID);
132 
133  JLOG(debugLog().info()) << "Thread Tx:" << txID << " prev:" << oldPrevTxID;
134 
135  if (oldPrevTxID == txID)
136  {
137  // this transaction is already threaded
138  assert(getFieldU32(sfPreviousTxnLgrSeq) == ledgerSeq);
139  return false;
140  }
141 
142  prevTxID = oldPrevTxID;
143  prevLedgerID = getFieldU32(sfPreviousTxnLgrSeq);
145  setFieldU32(sfPreviousTxnLgrSeq, ledgerSeq);
146  return true;
147 }
148 
149 } // namespace ripple
ripple::sfPreviousTxnLgrSeq
const SF_UINT32 sfPreviousTxnLgrSeq
ripple::STLedgerEntry::setSLEType
void setSLEType()
Definition: STLedgerEntry.cpp:71
ripple::sfPreviousTxnID
const SF_HASH256 sfPreviousTxnID
ripple::Keylet
A pair of SHAMap key and LedgerEntryType.
Definition: Keylet.h:38
ripple::STLedgerEntry::getJson
Json::Value getJson(JsonOptions options) const override
Definition: STLedgerEntry.cpp:109
std::string
STL class.
ripple::STObject::setFieldU16
void setFieldU16(SField const &field, std::uint16_t)
Definition: STObject.cpp:626
ripple::JsonOptions
JsonOptions
Definition: STBase.h:34
ripple::STLedgerEntry::key_
uint256 key_
Definition: STLedgerEntry.h:116
ripple::STLedgerEntry::getFullText
std::string getFullText() const override
Definition: STLedgerEntry.cpp:84
ripple::to_string
std::string to_string(ListDisposition disposition)
Definition: ValidatorList.cpp:42
ripple::debugLog
beast::Journal debugLog()
Returns a debug journal.
Definition: Log.cpp:452
ripple::sfLedgerEntry
const SField sfLedgerEntry
ripple::STObject::getFullText
virtual std::string getFullText() const override
Definition: STObject.cpp:227
ripple::base_uint< 256 >
ripple::STObject::applyTemplate
void applyTemplate(const SOTemplate &type) noexcept(false)
Definition: STObject.cpp:89
ripple::STObject::setFieldH256
void setFieldH256(SField const &field, uint256 const &)
Definition: STObject.cpp:650
ripple::KnownFormats::findByType
Item const * findByType(KeyType type) const
Retrieve a format based on its type.
Definition: KnownFormats.h:125
ripple::STLedgerEntry::getText
std::string getText() const override
Definition: STLedgerEntry.cpp:102
ripple::STLedgerEntry::STLedgerEntry
STLedgerEntry(Keylet const &k)
Create an empty object with the given key and type.
Definition: STLedgerEntry.cpp:32
ripple::SerialIter
Definition: Serializer.h:308
std::uint16_t
ripple::STObject::getFieldU16
std::uint16_t getFieldU16(SField const &field) const
Definition: STObject.cpp:526
ripple::LedgerFormats::getInstance
static LedgerFormats const & getInstance()
Definition: LedgerFormats.cpp:240
std::numeric_limits::min
T min(T... args)
ripple::STObject::getFieldIndex
int getFieldIndex(SField const &field) const
Definition: STObject.cpp:330
ripple::STObject
Definition: STObject.h:51
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::sfLedgerEntryType
const SF_UINT16 sfLedgerEntryType
ripple::STLedgerEntry::thread
bool thread(uint256 const &txID, std::uint32_t ledgerSeq, uint256 &prevTxID, std::uint32_t &prevLedgerID)
Definition: STLedgerEntry.cpp:125
limits
ripple::LedgerEntryType
LedgerEntryType
Ledger entry types.
Definition: LedgerFormats.h:36
ripple::STLedgerEntry::type_
LedgerEntryType type_
Definition: STLedgerEntry.h:117
std::numeric_limits::max
T max(T... args)
ripple::STObject::getFieldU32
std::uint32_t getFieldU32(SField const &field) const
Definition: STObject.cpp:532
ripple::STLedgerEntry::isThreadedType
bool isThreadedType() const
Definition: STLedgerEntry.cpp:119
ripple::STObject::getJson
virtual Json::Value getJson(JsonOptions options) const override
Definition: STObject.cpp:698
ripple::STObject::set
void set(const SOTemplate &)
Definition: STObject.cpp:73
ripple::STObject::setFieldU32
void setFieldU32(SField const &field, std::uint32_t)
Definition: STObject.cpp:632
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::STObject::getText
virtual std::string getText() const override
Definition: STObject.cpp:258
ripple::STObject::getFieldH256
uint256 getFieldH256(SField const &field) const
Definition: STObject.cpp:556