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