rippled
Loading...
Searching...
No Matches
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 <xrpld/app/main/Application.h>
21#include <xrpld/app/misc/LoadFeeTrack.h>
22#include <xrpld/app/paths/TrustLine.h>
23#include <xrpld/rpc/Context.h>
24#include <xrpld/rpc/detail/RPCHelpers.h>
25#include <xrpld/rpc/detail/Tuning.h>
26
27#include <xrpl/ledger/ReadView.h>
28#include <xrpl/protocol/ErrorCodes.h>
29#include <xrpl/protocol/RPCErr.h>
30#include <xrpl/protocol/TxFlags.h>
31#include <xrpl/protocol/jss.h>
32
33namespace ripple {
34
35static void
37 RPC::JsonContext& context,
38 Json::Value& txArray,
39 AccountID const& accountID,
40 std::uint32_t& sequence,
41 ReadView const& ledger)
42{
43 txArray["Sequence"] = Json::UInt(sequence++);
44 txArray["Account"] = toBase58(accountID);
45 auto& fees = ledger.fees();
46 // Convert the reference transaction cost in fee units to drops
47 // scaled to represent the current fee load.
48 txArray["Fee"] =
49 scaleFeeLoad(fees.base, context.app.getFeeTrack(), fees, false)
50 .jsonClipped();
51}
52
53// {
54// account: <account>
55// ledger_hash : <ledger>
56// ledger_index : <ledger_index>
57// limit: integer // optional, number of problems
58// role: gateway|user // account role to assume
59// transactions: true // optional, reccommend transactions
60// }
63{
64 auto const& params(context.params);
65 if (!params.isMember(jss::account))
66 return RPC::missing_field_error("account");
67
68 if (!params.isMember("role"))
69 return RPC::missing_field_error("role");
70
71 if (!params[jss::account].isString())
72 return RPC::invalid_field_error(jss::account);
73
74 bool roleGateway = false;
75 {
76 std::string const role = params["role"].asString();
77 if (role == "gateway")
78 roleGateway = true;
79 else if (role != "user")
80 return RPC::invalid_field_error("role");
81 }
82
83 unsigned int limit;
84 if (auto err = readLimitField(limit, RPC::Tuning::noRippleCheck, context))
85 return *err;
86
87 bool transactions = false;
88 if (params.isMember(jss::transactions))
89 transactions = params["transactions"].asBool();
90
91 // The document[https://xrpl.org/noripple_check.html#noripple_check] states
92 // that transactions params is a boolean value, however, assigning any
93 // string value works. Do not allow this. This check is for api Version 2
94 // onwards only
95 if (context.apiVersion > 1u && params.isMember(jss::transactions) &&
96 !params[jss::transactions].isBool())
97 {
98 return RPC::invalid_field_error(jss::transactions);
99 }
100
102 auto result = RPC::lookupLedger(ledger, context);
103 if (!ledger)
104 return result;
105
106 Json::Value dummy;
107 Json::Value& jvTransactions =
108 transactions ? (result[jss::transactions] = Json::arrayValue) : dummy;
109
110 auto id = parseBase58<AccountID>(params[jss::account].asString());
111 if (!id)
112 {
114 return result;
115 }
116 auto const accountID{std::move(id.value())};
117 auto const sle = ledger->read(keylet::account(accountID));
118 if (!sle)
120
121 std::uint32_t seq = sle->getFieldU32(sfSequence);
122
123 Json::Value& problems = (result["problems"] = Json::arrayValue);
124
125 bool bDefaultRipple = sle->getFieldU32(sfFlags) & lsfDefaultRipple;
126
127 if (bDefaultRipple & !roleGateway)
128 {
129 problems.append(
130 "You appear to have set your default ripple flag even though you "
131 "are not a gateway. This is not recommended unless you are "
132 "experimenting");
133 }
134 else if (roleGateway & !bDefaultRipple)
135 {
136 problems.append("You should immediately set your default ripple flag");
137 if (transactions)
138 {
139 Json::Value& tx = jvTransactions.append(Json::objectValue);
140 tx["TransactionType"] = jss::AccountSet;
141 tx["SetFlag"] = 8;
142 fillTransaction(context, tx, accountID, seq, *ledger);
143 }
144 }
145
147 *ledger,
148 accountID,
149 uint256(),
150 0,
151 limit,
152 [&](std::shared_ptr<SLE const> const& ownedItem) {
153 if (ownedItem->getType() == ltRIPPLE_STATE)
154 {
155 bool const bLow = accountID ==
156 ownedItem->getFieldAmount(sfLowLimit).getIssuer();
157
158 bool const bNoRipple = ownedItem->getFieldU32(sfFlags) &
159 (bLow ? lsfLowNoRipple : lsfHighNoRipple);
160
161 std::string problem;
162 bool needFix = false;
163 if (bNoRipple & roleGateway)
164 {
165 problem = "You should clear the no ripple flag on your ";
166 needFix = true;
167 }
168 else if (!roleGateway & !bNoRipple)
169 {
170 problem =
171 "You should probably set the no ripple flag on your ";
172 needFix = true;
173 }
174 if (needFix)
175 {
176 AccountID peer =
177 ownedItem
178 ->getFieldAmount(bLow ? sfHighLimit : sfLowLimit)
179 .getIssuer();
180 STAmount peerLimit = ownedItem->getFieldAmount(
181 bLow ? sfHighLimit : sfLowLimit);
182 problem += to_string(peerLimit.getCurrency());
183 problem += " line to ";
184 problem += to_string(peerLimit.getIssuer());
185 problems.append(problem);
186
187 STAmount limitAmount(ownedItem->getFieldAmount(
188 bLow ? sfLowLimit : sfHighLimit));
189 limitAmount.setIssuer(peer);
190
191 Json::Value& tx = jvTransactions.append(Json::objectValue);
192 tx["TransactionType"] = jss::TrustSet;
193 tx["LimitAmount"] = limitAmount.getJson(JsonOptions::none);
194 tx["Flags"] = bNoRipple ? tfClearNoRipple : tfSetNoRipple;
195 fillTransaction(context, tx, accountID, seq, *ledger);
196
197 return true;
198 }
199 }
200 return false;
201 });
202
203 return result;
204}
205
206} // namespace ripple
Represents a JSON value.
Definition json_value.h:149
Value & append(Value const &value)
Append value to array at the end.
virtual LoadFeeTrack & getFeeTrack()=0
A view into a ledger.
Definition ReadView.h:51
virtual Fees const & fees() const =0
Returns the fees for the base ledger.
Json::Value getJson(JsonOptions=JsonOptions::none) const override
Definition STAmount.cpp:772
void setIssuer(AccountID const &uIssuer)
Definition STAmount.h:588
Currency const & getCurrency() const
Definition STAmount.h:502
AccountID const & getIssuer() const
Definition STAmount.h:508
Json::Value jsonClipped() const
Definition XRPAmount.h:218
@ arrayValue
array value (ordered list)
Definition json_value.h:44
@ objectValue
object value (collection of name/value pairs).
Definition json_value.h:45
unsigned int UInt
static LimitRange constexpr noRippleCheck
Limits for the no_ripple_check command.
Json::Value invalid_field_error(std::string const &name)
Definition ErrorCodes.h:325
void inject_error(error_code_i code, JsonValue &json)
Add or update the json update to reflect the error code.
Definition ErrorCodes.h:233
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.
Json::Value missing_field_error(std::string const &name)
Definition ErrorCodes.h:283
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:184
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Json::Value doNoRippleCheck(RPC::JsonContext &)
@ rpcACT_NOT_FOUND
Definition ErrorCodes.h:70
@ rpcACT_MALFORMED
Definition ErrorCodes.h:90
base_uint< 256 > uint256
Definition base_uint.h:558
@ lsfDefaultRipple
Json::Value rpcError(int iError)
Definition RPCErr.cpp:31
constexpr std::uint32_t tfClearNoRipple
Definition TxFlags.h:117
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:684
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:630
XRPAmount scaleFeeLoad(XRPAmount fee, LoadFeeTrack const &feeTrack, Fees const &fees, bool bUnlimited)
constexpr std::uint32_t tfSetNoRipple
Definition TxFlags.h:116
static void fillTransaction(RPC::JsonContext &context, Json::Value &txArray, AccountID const &accountID, std::uint32_t &sequence, ReadView const &ledger)
unsigned int apiVersion
Definition Context.h:49
Application & app
Definition Context.h:41