rippled
NoRippleCheck.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012-2014 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/misc/LoadFeeTrack.h>
22 #include <ripple/app/paths/TrustLine.h>
23 #include <ripple/ledger/ReadView.h>
24 #include <ripple/net/RPCErr.h>
25 #include <ripple/protocol/ErrorCodes.h>
26 #include <ripple/protocol/TxFlags.h>
27 #include <ripple/protocol/jss.h>
28 #include <ripple/rpc/Context.h>
29 #include <ripple/rpc/impl/RPCHelpers.h>
30 #include <ripple/rpc/impl/Tuning.h>
31 
32 namespace ripple {
33 
34 static void
36  RPC::JsonContext& context,
37  Json::Value& txArray,
38  AccountID const& accountID,
39  std::uint32_t& sequence,
40  ReadView const& ledger)
41 {
42  txArray["Sequence"] = Json::UInt(sequence++);
43  txArray["Account"] = toBase58(accountID);
44  auto& fees = ledger.fees();
45  // Convert the reference transaction cost in fee units to drops
46  // scaled to represent the current fee load.
47  txArray["Fee"] =
48  scaleFeeLoad(fees.base, context.app.getFeeTrack(), fees, false)
49  .jsonClipped();
50 }
51 
52 // {
53 // account: <account>
54 // ledger_hash : <ledger>
55 // ledger_index : <ledger_index>
56 // limit: integer // optional, number of problems
57 // role: gateway|user // account role to assume
58 // transactions: true // optional, reccommend transactions
59 // }
62 {
63  auto const& params(context.params);
64  if (!params.isMember(jss::account))
65  return RPC::missing_field_error("account");
66 
67  if (!params.isMember("role"))
68  return RPC::missing_field_error("role");
69  bool roleGateway = false;
70  {
71  std::string const role = params["role"].asString();
72  if (role == "gateway")
73  roleGateway = true;
74  else if (role != "user")
75  return RPC::invalid_field_error("role");
76  }
77 
78  unsigned int limit;
79  if (auto err = readLimitField(limit, RPC::Tuning::noRippleCheck, context))
80  return *err;
81 
82  bool transactions = false;
83  if (params.isMember(jss::transactions))
84  transactions = params["transactions"].asBool();
85 
87  auto result = RPC::lookupLedger(ledger, context);
88  if (!ledger)
89  return result;
90 
91  Json::Value dummy;
92  Json::Value& jvTransactions =
93  transactions ? (result[jss::transactions] = Json::arrayValue) : dummy;
94 
95  auto id = parseBase58<AccountID>(params[jss::account].asString());
96  if (!id)
97  {
99  return result;
100  }
101  auto const accountID{std::move(id.value())};
102  auto const sle = ledger->read(keylet::account(accountID));
103  if (!sle)
104  return rpcError(rpcACT_NOT_FOUND);
105 
106  std::uint32_t seq = sle->getFieldU32(sfSequence);
107 
108  Json::Value& problems = (result["problems"] = Json::arrayValue);
109 
110  bool bDefaultRipple = sle->getFieldU32(sfFlags) & lsfDefaultRipple;
111 
112  if (bDefaultRipple & !roleGateway)
113  {
114  problems.append(
115  "You appear to have set your default ripple flag even though you "
116  "are not a gateway. This is not recommended unless you are "
117  "experimenting");
118  }
119  else if (roleGateway & !bDefaultRipple)
120  {
121  problems.append("You should immediately set your default ripple flag");
122  if (transactions)
123  {
124  Json::Value& tx = jvTransactions.append(Json::objectValue);
125  tx["TransactionType"] = jss::AccountSet;
126  tx["SetFlag"] = 8;
127  fillTransaction(context, tx, accountID, seq, *ledger);
128  }
129  }
130 
132  *ledger,
133  accountID,
134  uint256(),
135  0,
136  limit,
137  [&](std::shared_ptr<SLE const> const& ownedItem) {
138  if (ownedItem->getType() == ltRIPPLE_STATE)
139  {
140  bool const bLow = accountID ==
141  ownedItem->getFieldAmount(sfLowLimit).getIssuer();
142 
143  bool const bNoRipple = ownedItem->getFieldU32(sfFlags) &
144  (bLow ? lsfLowNoRipple : lsfHighNoRipple);
145 
146  std::string problem;
147  bool needFix = false;
148  if (bNoRipple & roleGateway)
149  {
150  problem = "You should clear the no ripple flag on your ";
151  needFix = true;
152  }
153  else if (!roleGateway & !bNoRipple)
154  {
155  problem =
156  "You should probably set the no ripple flag on your ";
157  needFix = true;
158  }
159  if (needFix)
160  {
161  AccountID peer =
162  ownedItem
164  .getIssuer();
165  STAmount peerLimit = ownedItem->getFieldAmount(
166  bLow ? sfHighLimit : sfLowLimit);
167  problem += to_string(peerLimit.getCurrency());
168  problem += " line to ";
169  problem += to_string(peerLimit.getIssuer());
170  problems.append(problem);
171 
172  STAmount limitAmount(ownedItem->getFieldAmount(
173  bLow ? sfLowLimit : sfHighLimit));
174  limitAmount.setIssuer(peer);
175 
176  Json::Value& tx = jvTransactions.append(Json::objectValue);
177  tx["TransactionType"] = jss::TrustSet;
178  tx["LimitAmount"] = limitAmount.getJson(JsonOptions::none);
179  tx["Flags"] = bNoRipple ? tfClearNoRipple : tfSetNoRipple;
180  fillTransaction(context, tx, accountID, seq, *ledger);
181 
182  return true;
183  }
184  }
185  return false;
186  });
187 
188  return result;
189 }
190 
191 } // namespace ripple
ripple::RPC::Tuning::noRippleCheck
static constexpr LimitRange noRippleCheck
Limits for the no_ripple_check command.
Definition: rpc/impl/Tuning.h:52
ripple::RPC::JsonContext
Definition: Context.h:53
std::string
STL class.
std::shared_ptr
STL class.
ripple::rpcError
Json::Value rpcError(int iError)
Definition: RPCErr.cpp:29
ripple::forEachItemAfter
bool forEachItemAfter(ReadView const &view, Keylet const &root, uint256 const &after, std::uint64_t const hint, unsigned int limit, std::function< bool(std::shared_ptr< SLE const > const &)> const &f)
Iterate all items after an item in the given directory.
Definition: View.cpp:394
Json::arrayValue
@ arrayValue
array value (ordered list)
Definition: json_value.h:42
ripple::sfSequence
const SF_UINT32 sfSequence
Json::UInt
unsigned int UInt
Definition: json_forwards.h:27
ripple::tfSetNoRipple
constexpr std::uint32_t tfSetNoRipple
Definition: TxFlags.h:109
ripple::ReadView::fees
virtual Fees const & fees() const =0
Returns the fees for the base ledger.
ripple::STAmount::getJson
Json::Value getJson(JsonOptions) const override
Definition: STAmount.cpp:655
ripple::toBase58
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition: AccountID.cpp:104
ripple::STAmount::setIssuer
void setIssuer(AccountID const &uIssuer)
Definition: STAmount.h:433
ripple::RPC::lookupLedger
Status lookupLedger(std::shared_ptr< ReadView const > &ledger, JsonContext &context, Json::Value &result)
Look up a ledger from a request and fill a Json::Result with the data representing a ledger.
Definition: RPCHelpers.cpp:675
ripple::Application::getFeeTrack
virtual LoadFeeTrack & getFeeTrack()=0
ripple::STAmount::getIssuer
AccountID const & getIssuer() const
Definition: STAmount.h:359
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:550
ripple::RPC::missing_field_error
Json::Value missing_field_error(std::string const &name)
Definition: ErrorCodes.h:262
ripple::fillTransaction
static void fillTransaction(RPC::JsonContext &context, Json::Value &txArray, AccountID const &accountID, std::uint32_t &sequence, ReadView const &ledger)
Definition: NoRippleCheck.cpp:35
ripple::base_uint
Integers of any length that is a multiple of 32-bits.
Definition: base_uint.h:82
ripple::sfLowLimit
const SF_AMOUNT sfLowLimit
ripple::lsfDefaultRipple
@ lsfDefaultRipple
Definition: LedgerFormats.h:232
Json::Value::append
Value & append(const Value &value)
Append value to array at the end.
Definition: json_value.cpp:882
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:43
ripple::keylet::account
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition: Indexes.cpp:133
ripple::JsonOptions::none
@ none
ripple::rpcACT_NOT_FOUND
@ rpcACT_NOT_FOUND
Definition: ErrorCodes.h:70
ripple::STAmount
Definition: STAmount.h:45
ripple::RPC::Context::app
Application & app
Definition: Context.h:42
ripple::tfClearNoRipple
constexpr std::uint32_t tfClearNoRipple
Definition: TxFlags.h:110
ripple::doNoRippleCheck
Json::Value doNoRippleCheck(RPC::JsonContext &)
Definition: NoRippleCheck.cpp:61
std::uint32_t
ripple::sfHighLimit
const SF_AMOUNT sfHighLimit
ripple::STLedgerEntry::getType
LedgerEntryType getType() const
Definition: STLedgerEntry.h:119
ripple::ReadView::read
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
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::rpcACT_MALFORMED
@ rpcACT_MALFORMED
Definition: ErrorCodes.h:90
ripple::sfFlags
const SF_UINT32 sfFlags
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::scaleFeeLoad
XRPAmount scaleFeeLoad(XRPAmount fee, LoadFeeTrack const &feeTrack, Fees const &fees, bool bUnlimited)
Definition: LoadFeeTrack.cpp:89
ripple::ltRIPPLE_STATE
@ ltRIPPLE_STATE
A ledger object which describes a bidirectional trust line.
Definition: LedgerFormats.h:74
ripple::RPC::JsonContext::params
Json::Value params
Definition: Context.h:64
ripple::STAmount::getCurrency
Currency const & getCurrency() const
Definition: STAmount.h:353
ripple::RPC::inject_error
void inject_error(error_code_i code, JsonValue &json)
Add or update the json update to reflect the error code.
Definition: ErrorCodes.h:212
ripple::RPC::invalid_field_error
Json::Value invalid_field_error(std::string const &name)
Definition: ErrorCodes.h:304
ripple::STObject::getFieldAmount
STAmount const & getFieldAmount(SField const &field) const
Definition: STObject.cpp:603
ripple::XRPAmount::jsonClipped
Json::Value jsonClipped() const
Definition: XRPAmount.h:209
Json::Value
Represents a JSON value.
Definition: json_value.h:145