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/RPCLedgerHelpers.h>
7#include <xrpld/rpc/detail/Tuning.h>
8
9#include <xrpl/ledger/ReadView.h>
10#include <xrpl/protocol/ErrorCodes.h>
11#include <xrpl/protocol/RPCErr.h>
12#include <xrpl/protocol/TxFlags.h>
13#include <xrpl/protocol/jss.h>
14
15namespace ripple {
16
17static void
19 RPC::JsonContext& context,
20 Json::Value& txArray,
21 AccountID const& accountID,
22 std::uint32_t& sequence,
23 ReadView const& ledger)
24{
25 txArray["Sequence"] = Json::UInt(sequence++);
26 txArray["Account"] = toBase58(accountID);
27 auto& fees = ledger.fees();
28 // Convert the reference transaction cost in fee units to drops
29 // scaled to represent the current fee load.
30 txArray["Fee"] =
31 scaleFeeLoad(fees.base, context.app.getFeeTrack(), fees, false)
32 .jsonClipped();
33}
34
35// {
36// account: <account>
37// ledger_hash : <ledger>
38// ledger_index : <ledger_index>
39// limit: integer // optional, number of problems
40// role: gateway|user // account role to assume
41// transactions: true // optional, reccommend transactions
42// }
45{
46 auto const& params(context.params);
47 if (!params.isMember(jss::account))
48 return RPC::missing_field_error("account");
49
50 if (!params.isMember("role"))
51 return RPC::missing_field_error("role");
52
53 if (!params[jss::account].isString())
54 return RPC::invalid_field_error(jss::account);
55
56 bool roleGateway = false;
57 {
58 std::string const role = params["role"].asString();
59 if (role == "gateway")
60 roleGateway = true;
61 else if (role != "user")
62 return RPC::invalid_field_error("role");
63 }
64
65 unsigned int limit;
66 if (auto err = readLimitField(limit, RPC::Tuning::noRippleCheck, context))
67 return *err;
68
69 bool transactions = false;
70 if (params.isMember(jss::transactions))
71 transactions = params["transactions"].asBool();
72
73 // The document[https://xrpl.org/noripple_check.html#noripple_check] states
74 // that transactions params is a boolean value, however, assigning any
75 // string value works. Do not allow this. This check is for api Version 2
76 // onwards only
77 if (context.apiVersion > 1u && params.isMember(jss::transactions) &&
78 !params[jss::transactions].isBool())
79 {
80 return RPC::invalid_field_error(jss::transactions);
81 }
82
84 auto result = RPC::lookupLedger(ledger, context);
85 if (!ledger)
86 return result;
87
88 Json::Value dummy;
89 Json::Value& jvTransactions =
90 transactions ? (result[jss::transactions] = Json::arrayValue) : dummy;
91
92 auto id = parseBase58<AccountID>(params[jss::account].asString());
93 if (!id)
94 {
96 return result;
97 }
98 auto const accountID{std::move(id.value())};
99 auto const sle = ledger->read(keylet::account(accountID));
100 if (!sle)
102
103 std::uint32_t seq = sle->getFieldU32(sfSequence);
104
105 Json::Value& problems = (result["problems"] = Json::arrayValue);
106
107 bool bDefaultRipple = sle->getFieldU32(sfFlags) & lsfDefaultRipple;
108
109 if (bDefaultRipple & !roleGateway)
110 {
111 problems.append(
112 "You appear to have set your default ripple flag even though you "
113 "are not a gateway. This is not recommended unless you are "
114 "experimenting");
115 }
116 else if (roleGateway & !bDefaultRipple)
117 {
118 problems.append("You should immediately set your default ripple flag");
119 if (transactions)
120 {
121 Json::Value& tx = jvTransactions.append(Json::objectValue);
122 tx["TransactionType"] = jss::AccountSet;
123 tx["SetFlag"] = 8;
124 fillTransaction(context, tx, accountID, seq, *ledger);
125 }
126 }
127
129 *ledger,
130 accountID,
131 uint256(),
132 0,
133 limit,
134 [&](std::shared_ptr<SLE const> const& ownedItem) {
135 if (ownedItem->getType() == ltRIPPLE_STATE)
136 {
137 bool const bLow = accountID ==
138 ownedItem->getFieldAmount(sfLowLimit).getIssuer();
139
140 bool const bNoRipple = ownedItem->getFieldU32(sfFlags) &
141 (bLow ? lsfLowNoRipple : lsfHighNoRipple);
142
143 std::string problem;
144 bool needFix = false;
145 if (bNoRipple & roleGateway)
146 {
147 problem = "You should clear the no ripple flag on your ";
148 needFix = true;
149 }
150 else if (!roleGateway & !bNoRipple)
151 {
152 problem =
153 "You should probably set the no ripple flag on your ";
154 needFix = true;
155 }
156 if (needFix)
157 {
158 AccountID peer =
159 ownedItem
160 ->getFieldAmount(bLow ? sfHighLimit : sfLowLimit)
161 .getIssuer();
162 STAmount peerLimit = ownedItem->getFieldAmount(
163 bLow ? sfHighLimit : sfLowLimit);
164 problem += to_string(peerLimit.getCurrency());
165 problem += " line to ";
166 problem += to_string(peerLimit.getIssuer());
167 problems.append(problem);
168
169 STAmount limitAmount(ownedItem->getFieldAmount(
170 bLow ? sfLowLimit : sfHighLimit));
171 limitAmount.setIssuer(peer);
172
173 Json::Value& tx = jvTransactions.append(Json::objectValue);
174 tx["TransactionType"] = jss::TrustSet;
175 tx["LimitAmount"] = limitAmount.getJson(JsonOptions::none);
176 tx["Flags"] = bNoRipple ? tfClearNoRipple : tfSetNoRipple;
177 fillTransaction(context, tx, accountID, seq, *ledger);
178
179 return true;
180 }
181 }
182 return false;
183 });
184
185 return result;
186}
187
188} // 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