rippled
TrustLine.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/app/main/Application.h>
21 #include <ripple/app/paths/TrustLine.h>
22 #include <ripple/protocol/STAmount.h>
23 #include <cstdint>
24 #include <memory>
25 
26 namespace ripple {
27 
29  std::shared_ptr<SLE const> const& sle,
30  AccountID const& viewAccount)
31  : key_(sle->key())
32  , mLowLimit(sle->getFieldAmount(sfLowLimit))
33  , mHighLimit(sle->getFieldAmount(sfHighLimit))
34  , mBalance(sle->getFieldAmount(sfBalance))
35  , mFlags(sle->getFieldU32(sfFlags))
36  , mViewLowest(mLowLimit.getIssuer() == viewAccount)
37 {
38  if (!mViewLowest)
39  mBalance.negate();
40 }
41 
44 {
46  ret["low_id"] = to_string(mLowLimit.getIssuer());
47  ret["high_id"] = to_string(mHighLimit.getIssuer());
48  return ret;
49 }
50 
53  AccountID const& accountID,
54  std::shared_ptr<SLE const> const& sle)
55 {
56  if (!sle || sle->getType() != ltRIPPLE_STATE)
57  return {};
58  return std::optional{PathFindTrustLine{sle, accountID}};
59 }
60 
61 namespace detail {
62 template <class T>
65  AccountID const& accountID,
66  ReadView const& view,
68 {
69  std::vector<T> items;
71  view,
72  accountID,
73  [&items, &accountID, &direction](
74  std::shared_ptr<SLE const> const& sleCur) {
75  auto ret = T::makeItem(accountID, sleCur);
76  if (ret &&
77  (direction == LineDirection::outgoing || !ret->getNoRipple()))
78  items.push_back(std::move(*ret));
79  });
80  // This list may be around for a while, so free up any unneeded
81  // capacity
82  items.shrink_to_fit();
83 
84  return items;
85 }
86 } // namespace detail
87 
90  AccountID const& accountID,
91  ReadView const& view,
92  LineDirection direction)
93 {
94  return detail::getTrustLineItems<PathFindTrustLine>(
95  accountID, view, direction);
96 }
97 
99  std::shared_ptr<SLE const> const& sle,
100  AccountID const& viewAccount)
101  : TrustLineBase(sle, viewAccount)
102  , lowQualityIn_(sle->getFieldU32(sfLowQualityIn))
103  , lowQualityOut_(sle->getFieldU32(sfLowQualityOut))
104  , highQualityIn_(sle->getFieldU32(sfHighQualityIn))
105  , highQualityOut_(sle->getFieldU32(sfHighQualityOut))
106 {
107 }
108 
111  AccountID const& accountID,
112  std::shared_ptr<SLE const> const& sle)
113 {
114  if (!sle || sle->getType() != ltRIPPLE_STATE)
115  return {};
116  return std::optional{RPCTrustLine{sle, accountID}};
117 }
118 
120 RPCTrustLine::getItems(AccountID const& accountID, ReadView const& view)
121 {
122  return detail::getTrustLineItems<RPCTrustLine>(accountID, view);
123 }
124 
125 } // namespace ripple
ripple::sfHighQualityIn
const SF_UINT32 sfHighQualityIn
ripple::STAmount::negate
void negate()
Definition: STAmount.h:402
ripple::detail::getTrustLineItems
std::vector< T > getTrustLineItems(AccountID const &accountID, ReadView const &view, LineDirection direction=LineDirection::outgoing)
Definition: TrustLine.cpp:64
std::shared_ptr
STL class.
ripple::LineDirection
LineDirection
Describes how an account was found in a path, and how to find the next set of paths.
Definition: TrustLine.h:41
ripple::TrustLineBase::TrustLineBase
TrustLineBase(std::shared_ptr< SLE const > const &sle, AccountID const &viewAccount)
Definition: TrustLine.cpp:28
std::vector
STL class.
ripple::TrustLineBase::getJson
Json::Value getJson(int)
Definition: TrustLine.cpp:43
ripple::RPCTrustLine
Definition: TrustLine.h:204
ripple::PathFindTrustLine::makeItem
static std::optional< PathFindTrustLine > makeItem(AccountID const &accountID, std::shared_ptr< SLE const > const &sle)
Definition: TrustLine.cpp:52
ripple::STAmount::getIssuer
AccountID const & getIssuer() const
Definition: STAmount.h:359
std::vector::push_back
T push_back(T... args)
ripple::base_uint< 160, detail::AccountIDTag >
ripple::sfLowQualityOut
const SF_UINT32 sfLowQualityOut
ripple::sfLowLimit
const SF_AMOUNT sfLowLimit
ripple::TrustLineBase::mHighLimit
const STAmount mHighLimit
Definition: TrustLine.h:174
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:43
ripple::sfLowQualityIn
const SF_UINT32 sfLowQualityIn
ripple::LineDirection::outgoing
@ outgoing
ripple::TrustLineBase::mBalance
STAmount mBalance
Definition: TrustLine.h:176
cstdint
ripple::sfHighLimit
const SF_AMOUNT sfHighLimit
memory
ripple::ReadView
A view into a ledger.
Definition: ReadView.h:125
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::TrustLineBase::mViewLowest
bool mViewLowest
Definition: TrustLine.h:180
ripple::sfFlags
const SF_UINT32 sfFlags
ripple::RPCTrustLine::makeItem
static std::optional< RPCTrustLine > makeItem(AccountID const &accountID, std::shared_ptr< SLE const > const &sle)
Definition: TrustLine.cpp:110
ripple::sfBalance
const SF_AMOUNT sfBalance
ripple::sfHighQualityOut
const SF_UINT32 sfHighQualityOut
std::vector::shrink_to_fit
T shrink_to_fit(T... args)
std::optional
ripple::forEachItem
void forEachItem(ReadView const &view, Keylet const &root, std::function< void(std::shared_ptr< SLE const > const &)> const &f)
Iterate all items in the given directory.
Definition: View.cpp:367
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::RPCTrustLine::RPCTrustLine
RPCTrustLine()=delete
ripple::ltRIPPLE_STATE
@ ltRIPPLE_STATE
A ledger object which describes a bidirectional trust line.
Definition: LedgerFormats.h:74
ripple::PathFindTrustLine::getItems
static std::vector< PathFindTrustLine > getItems(AccountID const &accountID, ReadView const &view, LineDirection direction)
Definition: TrustLine.cpp:89
ripple::PathFindTrustLine
Definition: TrustLine.h:184
ripple::TrustLineBase
Wraps a trust line SLE for convenience.
Definition: TrustLine.h:53
ripple::TrustLineBase::mLowLimit
const STAmount mLowLimit
Definition: TrustLine.h:173
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::RPCTrustLine::getItems
static std::vector< RPCTrustLine > getItems(AccountID const &accountID, ReadView const &view)
Definition: TrustLine.cpp:120