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>
64 getTrustLineItems(AccountID const& accountID, ReadView const& view)
65 {
66  std::vector<T> items;
68  view,
69  accountID,
70  [&items, &accountID](std::shared_ptr<SLE const> const& sleCur) {
71  auto ret = T::makeItem(accountID, sleCur);
72  if (ret)
73  items.push_back(std::move(*ret));
74  });
75 
76  return items;
77 }
78 } // namespace detail
79 
81 PathFindTrustLine::getItems(AccountID const& accountID, ReadView const& view)
82 {
83  return detail::getTrustLineItems<PathFindTrustLine>(accountID, view);
84 }
85 
87  std::shared_ptr<SLE const> const& sle,
88  AccountID const& viewAccount)
89  : TrustLineBase(sle, viewAccount)
90  , lowQualityIn_(sle->getFieldU32(sfLowQualityIn))
91  , lowQualityOut_(sle->getFieldU32(sfLowQualityOut))
92  , highQualityIn_(sle->getFieldU32(sfHighQualityIn))
93  , highQualityOut_(sle->getFieldU32(sfHighQualityOut))
94 {
95 }
96 
99  AccountID const& accountID,
100  std::shared_ptr<SLE const> const& sle)
101 {
102  if (!sle || sle->getType() != ltRIPPLE_STATE)
103  return {};
104  return std::optional{RPCTrustLine{sle, accountID}};
105 }
106 
108 RPCTrustLine::getItems(AccountID const& accountID, ReadView const& view)
109 {
110  return detail::getTrustLineItems<RPCTrustLine>(accountID, view);
111 }
112 
113 } // namespace ripple
ripple::sfHighQualityIn
const SF_UINT32 sfHighQualityIn
ripple::STAmount::negate
void negate()
Definition: STAmount.h:387
std::shared_ptr
STL class.
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:178
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:351
ripple::forEachItem
void forEachItem(ReadView const &view, AccountID const &id, std::function< void(std::shared_ptr< SLE const > const &)> f)
Iterate all items in an account's owner directory.
Definition: View.cpp:375
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::PathFindTrustLine::getItems
static std::vector< PathFindTrustLine > getItems(AccountID const &accountID, ReadView const &view)
Definition: TrustLine.cpp:81
ripple::TrustLineBase::mHighLimit
const STAmount mHighLimit
Definition: TrustLine.h:151
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:43
ripple::sfLowQualityIn
const SF_UINT32 sfLowQualityIn
ripple::TrustLineBase::mBalance
STAmount mBalance
Definition: TrustLine.h:153
cstdint
ripple::sfHighLimit
const SF_AMOUNT sfHighLimit
memory
ripple::ReadView
A view into a ledger.
Definition: ReadView.h:192
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:157
ripple::detail::getTrustLineItems
std::vector< T > getTrustLineItems(AccountID const &accountID, ReadView const &view)
Definition: TrustLine.cpp:64
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:98
ripple::sfBalance
const SF_AMOUNT sfBalance
ripple::sfHighQualityOut
const SF_UINT32 sfHighQualityOut
std::optional
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
Definition: TrustLine.h:161
ripple::TrustLineBase
Wraps a trust line SLE for convenience.
Definition: TrustLine.h:44
ripple::TrustLineBase::mLowLimit
const STAmount mLowLimit
Definition: TrustLine.h:150
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:108