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 
29 namespace ripple {
30 
32  : STObject(sfLedgerEntry), key_(k.key), type_(k.type)
33 {
34  if (!(0u <= type_ &&
35  type_ <= std::min<unsigned>(
39  Throw<std::runtime_error>("invalid ledger entry type: out of range");
40  auto const format = LedgerFormats::getInstance().findByType(type_);
41 
42  if (format == nullptr)
43  Throw<std::runtime_error>("invalid ledger entry type");
44 
45  set(format->getSOTemplate());
46 
48 }
49 
51  : STObject(sfLedgerEntry), key_(index)
52 {
53  set(sit);
54  setSLEType();
55 }
56 
57 STLedgerEntry::STLedgerEntry(STObject const& object, uint256 const& index)
58  : STObject(object), key_(index)
59 {
60  setSLEType();
61 }
62 
63 void
65 {
66  auto format = LedgerFormats::getInstance().findByType(
67  safe_cast<LedgerEntryType>(getFieldU16(sfLedgerEntryType)));
68 
69  if (format == nullptr)
70  Throw<std::runtime_error>("invalid ledger entry type");
71 
72  type_ = format->getType();
73  applyTemplate(format->getSOTemplate()); // May throw
74 }
75 
78 {
79  auto const format = LedgerFormats::getInstance().findByType(type_);
80 
81  if (format == nullptr)
82  Throw<std::runtime_error>("invalid ledger entry type");
83 
84  std::string ret = "\"";
85  ret += to_string(key_);
86  ret += "\" = { ";
87  ret += format->getName();
88  ret += ", ";
89  ret += STObject::getFullText();
90  ret += "}";
91  return ret;
92 }
93 
96 {
97  return str(
98  boost::format("{ %s, %s }") % to_string(key_) % STObject::getText());
99 }
100 
103 {
104  Json::Value ret(STObject::getJson(options));
105 
106  ret[jss::index] = to_string(key_);
107 
108  return ret;
109 }
110 
111 bool
113 {
114  return getFieldIndex(sfPreviousTxnID) != -1;
115 }
116 
117 bool
119  uint256 const& txID,
120  std::uint32_t ledgerSeq,
121  uint256& prevTxID,
122  std::uint32_t& prevLedgerID)
123 {
124  uint256 oldPrevTxID = getFieldH256(sfPreviousTxnID);
125 
126  JLOG(debugLog().info()) << "Thread Tx:" << txID << " prev:" << oldPrevTxID;
127 
128  if (oldPrevTxID == txID)
129  {
130  // this transaction is already threaded
131  assert(getFieldU32(sfPreviousTxnLgrSeq) == ledgerSeq);
132  return false;
133  }
134 
135  prevTxID = oldPrevTxID;
136  prevLedgerID = getFieldU32(sfPreviousTxnLgrSeq);
138  setFieldU32(sfPreviousTxnLgrSeq, ledgerSeq);
139  return true;
140 }
141 
142 } // namespace ripple
ripple::STLedgerEntry::setSLEType
void setSLEType()
Definition: STLedgerEntry.cpp:64
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:102
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::sfPreviousTxnLgrSeq
const SF_U32 sfPreviousTxnLgrSeq(access, STI_UINT32, 5, "PreviousTxnLgrSeq", SField::sMD_DeleteFinal)
Definition: SField.h:356
ripple::sfLedgerEntryType
const SF_U16 sfLedgerEntryType(access, STI_UINT16, 1, "LedgerEntryType", SField::sMD_Never)
Definition: SField.h:345
ripple::STLedgerEntry::key_
uint256 key_
Definition: STLedgerEntry.h:122
ripple::STLedgerEntry::getFullText
std::string getFullText() const override
Definition: STLedgerEntry.cpp:77
ripple::to_string
std::string to_string(ListDisposition disposition)
Definition: ValidatorList.cpp:41
ripple::debugLog
beast::Journal debugLog()
Returns a debug journal.
Definition: Log.cpp:452
ripple::sfLedgerEntry
const SField sfLedgerEntry(access, STI_LEDGERENTRY, 257, "LedgerEntry")
Definition: SField.h:333
ripple::STObject::getFullText
virtual std::string getFullText() const override
Definition: STObject.cpp:227
std::underlying_type_t
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:95
ripple::STLedgerEntry::STLedgerEntry
STLedgerEntry(Keylet const &k)
Create an empty object with the given key and type.
Definition: STLedgerEntry.cpp:31
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:230
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::STLedgerEntry::thread
bool thread(uint256 const &txID, std::uint32_t ledgerSeq, uint256 &prevTxID, std::uint32_t &prevLedgerID)
Definition: STLedgerEntry.cpp:118
ripple::STLedgerEntry::type_
LedgerEntryType type_
Definition: STLedgerEntry.h:123
ripple::STObject::getFieldU32
std::uint32_t getFieldU32(SField const &field) const
Definition: STObject.cpp:532
ripple::STLedgerEntry::isThreadedType
bool isThreadedType() const
Definition: STLedgerEntry.cpp:112
ripple::STObject::getJson
virtual Json::Value getJson(JsonOptions options) const override
Definition: STObject.cpp:698
std::numeric_limits
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
ripple::sfPreviousTxnID
const SF_U256 sfPreviousTxnID(access, STI_HASH256, 5, "PreviousTxnID", SField::sMD_DeleteFinal)
Definition: SField.h:420
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