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/protocol/STLedgerEntry.h>
21 #include <ripple/basics/contract.h>
22 #include <ripple/basics/Log.h>
23 #include <ripple/basics/safe_cast.h>
24 #include <ripple/json/to_string.h>
25 #include <ripple/protocol/Indexes.h>
26 #include <ripple/protocol/jss.h>
27 #include <boost/format.hpp>
28 
29 namespace ripple {
30 
33  , key_ (k.key)
34  , type_ (k.type)
35 {
36  if (!(0u <= type_ &&
37  type_ <= std::min<unsigned>(std::numeric_limits<std::uint16_t>::max(),
39  Throw<std::runtime_error> ("invalid ledger entry type: out of range");
40  auto const format =
42 
43  if (format == nullptr)
44  Throw<std::runtime_error> ("invalid ledger entry type");
45 
46  set (format->getSOTemplate());
47 
49  static_cast <std::uint16_t> (type_));
50 }
51 
53  SerialIter& sit,
54  uint256 const& index)
56  , key_ (index)
57 {
58  set (sit);
59  setSLEType ();
60 }
61 
63  STObject const& object,
64  uint256 const& index)
65  : STObject (object)
66  , key_ (index)
67 {
68  setSLEType ();
69 }
70 
72 {
73  auto format = LedgerFormats::getInstance().findByType (
74  safe_cast <LedgerEntryType> (
76 
77  if (format == nullptr)
78  Throw<std::runtime_error> ("invalid ledger entry type");
79 
80  type_ = format->getType ();
81  applyTemplate (format->getSOTemplate()); // May throw
82 }
83 
85 {
86  auto const format =
88 
89  if (format == nullptr)
90  Throw<std::runtime_error> ("invalid ledger entry type");
91 
92  std::string ret = "\"";
93  ret += to_string (key_);
94  ret += "\" = { ";
95  ret += format->getName ();
96  ret += ", ";
97  ret += STObject::getFullText ();
98  ret += "}";
99  return ret;
100 }
101 
103 {
104  return str (boost::format ("{ %s, %s }")
105  % to_string (key_)
106  % STObject::getText ());
107 }
108 
110 {
111  Json::Value ret (STObject::getJson (options));
112 
113  ret[jss::index] = to_string (key_);
114 
115  return ret;
116 }
117 
119 {
120  return getFieldIndex (sfPreviousTxnID) != -1;
121 }
122 
124  uint256 const& txID,
125  std::uint32_t ledgerSeq,
126  uint256& prevTxID,
127  std::uint32_t& prevLedgerID)
128 {
129  uint256 oldPrevTxID = getFieldH256 (sfPreviousTxnID);
130 
131  JLOG (debugLog().info())
132  << "Thread Tx:" << txID << " prev:" << oldPrevTxID;
133 
134  if (oldPrevTxID == txID)
135  {
136  // this transaction is already threaded
137  assert (getFieldU32 (sfPreviousTxnLgrSeq) == ledgerSeq);
138  return false;
139  }
140 
141  prevTxID = oldPrevTxID;
142  prevLedgerID = getFieldU32 (sfPreviousTxnLgrSeq);
144  setFieldU32 (sfPreviousTxnLgrSeq, ledgerSeq);
145  return true;
146 }
147 
148 } // ripple
ripple::STLedgerEntry::setSLEType
void setSLEType()
Definition: STLedgerEntry.cpp:71
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:596
ripple::JsonOptions
JsonOptions
Definition: STBase.h:34
ripple::sfPreviousTxnLgrSeq
const SF_U32 sfPreviousTxnLgrSeq(access, STI_UINT32, 5, "PreviousTxnLgrSeq", SField::sMD_DeleteFinal)
Definition: SField.h:341
ripple::sfLedgerEntryType
const SF_U16 sfLedgerEntryType(access, STI_UINT16, 1, "LedgerEntryType", SField::sMD_Never)
Definition: SField.h:330
ripple::STLedgerEntry::key_
uint256 key_
Definition: STLedgerEntry.h:112
ripple::STLedgerEntry::getFullText
std::string getFullText() const override
Definition: STLedgerEntry.cpp:84
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:417
ripple::sfLedgerEntry
const SField sfLedgerEntry(access, STI_LEDGERENTRY, 257, "LedgerEntry")
Definition: SField.h:318
ripple::STObject::getFullText
virtual std::string getFullText() const override
Definition: STObject.cpp:237
std::underlying_type_t
ripple::base_uint< 256 >
ripple::STObject::applyTemplate
void applyTemplate(const SOTemplate &type) noexcept(false)
Definition: STObject.cpp:103
ripple::STObject::setFieldH256
void setFieldH256(SField const &field, uint256 const &)
Definition: STObject.cpp:616
ripple::KnownFormats::findByType
Item const * findByType(KeyType type) const
Retrieve a format based on its type.
Definition: KnownFormats.h:120
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:31
ripple::SerialIter
Definition: Serializer.h:311
std::uint16_t
ripple::STObject::getFieldU16
std::uint16_t getFieldU16(SField const &field) const
Definition: STObject.cpp:507
ripple::LedgerFormats::getInstance
static LedgerFormats const & getInstance()
Definition: LedgerFormats.cpp:217
ripple::STObject::getFieldIndex
int getFieldIndex(SField const &field) const
Definition: STObject.cpp:326
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:123
ripple::STLedgerEntry::type_
LedgerEntryType type_
Definition: STLedgerEntry.h:113
ripple::STObject::getFieldU32
std::uint32_t getFieldU32(SField const &field) const
Definition: STObject.cpp:512
ripple::STLedgerEntry::isThreadedType
bool isThreadedType() const
Definition: STLedgerEntry.cpp:118
ripple::STObject::getJson
virtual Json::Value getJson(JsonOptions options) const override
Definition: STObject.cpp:657
std::numeric_limits
ripple::STObject::set
void set(const SOTemplate &)
Definition: STObject.cpp:88
ripple::STObject::setFieldU32
void setFieldU32(SField const &field, std::uint32_t)
Definition: STObject.cpp:601
ripple::sfPreviousTxnID
const SF_U256 sfPreviousTxnID(access, STI_HASH256, 5, "PreviousTxnID", SField::sMD_DeleteFinal)
Definition: SField.h:405
Json::Value
Represents a JSON value.
Definition: json_value.h:141
ripple::STObject::getText
virtual std::string getText() const override
Definition: STObject.cpp:266
ripple::STObject::getFieldH256
uint256 getFieldH256(SField const &field) const
Definition: STObject.cpp:532