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 
91 STBase*
92 STLedgerEntry::copy(std::size_t n, void* buf) const
93 {
94  return emplace(n, buf, *this);
95 }
96 
97 STBase*
99 {
100  return emplace(n, buf, std::move(*this));
101 }
102 
105 {
106  return STI_LEDGERENTRY;
107 }
108 
111 {
112  return str(
113  boost::format("{ %s, %s }") % to_string(key_) % STObject::getText());
114 }
115 
118 {
119  Json::Value ret(STObject::getJson(options));
120 
121  ret[jss::index] = to_string(key_);
122 
123  return ret;
124 }
125 
126 bool
128 {
129  return getFieldIndex(sfPreviousTxnID) != -1;
130 }
131 
132 bool
134  uint256 const& txID,
135  std::uint32_t ledgerSeq,
136  uint256& prevTxID,
137  std::uint32_t& prevLedgerID)
138 {
139  uint256 oldPrevTxID = getFieldH256(sfPreviousTxnID);
140 
141  JLOG(debugLog().info()) << "Thread Tx:" << txID << " prev:" << oldPrevTxID;
142 
143  if (oldPrevTxID == txID)
144  {
145  // this transaction is already threaded
146  assert(getFieldU32(sfPreviousTxnLgrSeq) == ledgerSeq);
147  return false;
148  }
149 
150  prevTxID = oldPrevTxID;
151  prevLedgerID = getFieldU32(sfPreviousTxnLgrSeq);
153  setFieldU32(sfPreviousTxnLgrSeq, ledgerSeq);
154  return true;
155 }
156 
157 } // namespace ripple
ripple::STLedgerEntry::move
STBase * move(std::size_t n, void *buf) override
Definition: STLedgerEntry.cpp:98
ripple::sfPreviousTxnLgrSeq
const SF_UINT32 sfPreviousTxnLgrSeq
ripple::STLedgerEntry::setSLEType
void setSLEType()
Definition: STLedgerEntry.cpp:61
ripple::STObject::applyTemplate
void applyTemplate(const SOTemplate &type)
Definition: STObject.cpp:116
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:117
std::string
STL class.
ripple::STObject::setFieldU16
void setFieldU16(SField const &field, std::uint16_t)
Definition: STObject.cpp:653
ripple::JsonOptions
JsonOptions
Definition: STBase.h:34
ripple::STLedgerEntry::key_
uint256 key_
Definition: STLedgerEntry.h:32
ripple::SerializedTypeID
SerializedTypeID
Definition: SField.h:52
ripple::STLedgerEntry::getFullText
std::string getFullText() const override
Definition: STLedgerEntry.cpp:74
ripple::STI_LEDGERENTRY
@ STI_LEDGERENTRY
Definition: SField.h:83
ripple::debugLog
beast::Journal debugLog()
Returns a debug journal.
Definition: Log.cpp:452
ripple::sfLedgerEntry
const SField sfLedgerEntry
ripple::STObject::getFullText
std::string getFullText() const override
Definition: STObject.cpp:254
ripple::base_uint< 256 >
ripple::STBase::emplace
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition: STBase.h:165
ripple::STObject::setFieldH256
void setFieldH256(SField const &field, uint256 const &)
Definition: STObject.cpp:677
ripple::STLedgerEntry::getText
std::string getText() const override
Definition: STLedgerEntry.cpp:110
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
ripple::sfPreviousTxnID
const SF_UINT256 sfPreviousTxnID
std::uint16_t
ripple::STObject::getFieldU16
std::uint16_t getFieldU16(SField const &field) const
Definition: STObject.cpp:553
ripple::LedgerFormats::getInstance
static LedgerFormats const & getInstance()
Definition: LedgerFormats.cpp:274
ripple::STObject::getFieldIndex
int getFieldIndex(SField const &field) const
Definition: STObject.cpp:357
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:133
ripple::STBase
A type which can be exported to a well known binary format.
Definition: STBase.h:66
limits
std::size_t
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:41
ripple::STLedgerEntry::getSType
SerializedTypeID getSType() const override
Definition: STLedgerEntry.cpp:104
ripple::STLedgerEntry::type_
LedgerEntryType type_
Definition: STLedgerEntry.h:33
ripple::STObject::getFieldU32
std::uint32_t getFieldU32(SField const &field) const
Definition: STObject.cpp:559
ripple::STLedgerEntry::copy
STBase * copy(std::size_t n, void *buf) const override
Definition: STLedgerEntry.cpp:92
ripple::STLedgerEntry::isThreadedType
bool isThreadedType() const
Definition: STLedgerEntry.cpp:127
ripple::STObject::getJson
Json::Value getJson(JsonOptions options) const override
Definition: STObject.cpp:725
ripple::STObject::set
void set(const SOTemplate &)
Definition: STObject.cpp:100
ripple::STObject::setFieldU32
void setFieldU32(SField const &field, std::uint32_t)
Definition: STObject.cpp:659
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
std::string getText() const override
Definition: STObject.cpp:285
ripple::STObject::getFieldH256
uint256 getFieldH256(SField const &field) const
Definition: STObject.cpp:583