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  auto const format = LedgerFormats::getInstance().findByType(type_);
36 
37  if (format == nullptr)
38  Throw<std::runtime_error>(
39  "Attempt to create a SLE of unknown type " +
40  std::to_string(safe_cast<std::uint16_t>(k.type)));
41 
42  set(format->getSOTemplate());
43 
45 }
46 
48  : STObject(sfLedgerEntry), key_(index)
49 {
50  set(sit);
51  setSLEType();
52 }
53 
54 STLedgerEntry::STLedgerEntry(STObject const& object, uint256 const& index)
55  : STObject(object), key_(index)
56 {
57  setSLEType();
58 }
59 
60 void
62 {
63  auto format = LedgerFormats::getInstance().findByType(
64  safe_cast<LedgerEntryType>(getFieldU16(sfLedgerEntryType)));
65 
66  if (format == nullptr)
67  Throw<std::runtime_error>("invalid ledger entry type");
68 
69  type_ = format->getType();
70  applyTemplate(format->getSOTemplate()); // May throw
71 }
72 
75 {
76  auto const format = LedgerFormats::getInstance().findByType(type_);
77 
78  if (format == nullptr)
79  Throw<std::runtime_error>("invalid ledger entry type");
80 
81  std::string ret = "\"";
82  ret += to_string(key_);
83  ret += "\" = { ";
84  ret += format->getName();
85  ret += ", ";
86  ret += STObject::getFullText();
87  ret += "}";
88  return ret;
89 }
90 
93 {
94  return str(
95  boost::format("{ %s, %s }") % to_string(key_) % STObject::getText());
96 }
97 
100 {
101  Json::Value ret(STObject::getJson(options));
102 
103  ret[jss::index] = to_string(key_);
104 
105  return ret;
106 }
107 
108 bool
110 {
111  return getFieldIndex(sfPreviousTxnID) != -1;
112 }
113 
114 bool
116  uint256 const& txID,
117  std::uint32_t ledgerSeq,
118  uint256& prevTxID,
119  std::uint32_t& prevLedgerID)
120 {
121  uint256 oldPrevTxID = getFieldH256(sfPreviousTxnID);
122 
123  JLOG(debugLog().info()) << "Thread Tx:" << txID << " prev:" << oldPrevTxID;
124 
125  if (oldPrevTxID == txID)
126  {
127  // this transaction is already threaded
128  assert(getFieldU32(sfPreviousTxnLgrSeq) == ledgerSeq);
129  return false;
130  }
131 
132  prevTxID = oldPrevTxID;
133  prevLedgerID = getFieldU32(sfPreviousTxnLgrSeq);
135  setFieldU32(sfPreviousTxnLgrSeq, ledgerSeq);
136  return true;
137 }
138 
139 } // namespace ripple
ripple::sfPreviousTxnLgrSeq
const SF_UINT32 sfPreviousTxnLgrSeq
ripple::STLedgerEntry::setSLEType
void setSLEType()
Definition: STLedgerEntry.cpp:61
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:99
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:74
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::STLedgerEntry::getText
std::string getText() const override
Definition: STLedgerEntry.cpp:92
std::to_string
T to_string(T... args)
ripple::Keylet::type
LedgerEntryType type
Definition: Keylet.h:41
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:310
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
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:115
limits
ripple::to_string
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
Definition: app/misc/impl/Manifest.cpp:38
ripple::STLedgerEntry::type_
LedgerEntryType type_
Definition: STLedgerEntry.h:117
ripple::STObject::getFieldU32
std::uint32_t getFieldU32(SField const &field) const
Definition: STObject.cpp:532
ripple::STLedgerEntry::isThreadedType
bool isThreadedType() const
Definition: STLedgerEntry.cpp:109
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
ripple::KnownFormats::findByType
Item const * findByType(KeyType type) const
Retrieve a format based on its type.
Definition: KnownFormats.h:127
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