rippled
Loading...
Searching...
No Matches
NoRippleCheck.cpp
1#include <xrpld/app/main/Application.h>
2#include <xrpld/rpc/Context.h>
3#include <xrpld/rpc/detail/RPCHelpers.h>
4#include <xrpld/rpc/detail/RPCLedgerHelpers.h>
5#include <xrpld/rpc/detail/TrustLine.h>
6#include <xrpld/rpc/detail/Tuning.h>
7
8#include <xrpl/ledger/ReadView.h>
9#include <xrpl/ledger/helpers/DirectoryHelpers.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#include <xrpl/server/LoadFeeTrack.h>
15
16namespace xrpl {
17
18static void
20 RPC::JsonContext& context,
21 Json::Value& txArray,
22 AccountID const& accountID,
23 std::uint32_t& sequence,
24 ReadView const& ledger)
25{
26 txArray["Sequence"] = Json::UInt(sequence++);
27 txArray["Account"] = toBase58(accountID);
28 auto& fees = ledger.fees();
29 // Convert the reference transaction cost in fee units to drops
30 // scaled to represent the current fee load.
31 txArray["Fee"] = scaleFeeLoad(fees.base, context.app.getFeeTrack(), fees, false).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, recommend 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 {
60 roleGateway = true;
61 }
62 else if (role != "user")
63 {
64 return RPC::invalid_field_error("role");
65 }
66 }
67
68 unsigned int limit = 0;
69 if (auto err = readLimitField(limit, RPC::Tuning::noRippleCheck, context))
70 return *err;
71
72 bool transactions = false;
73 if (params.isMember(jss::transactions))
74 transactions = params["transactions"].asBool();
75
76 // The document[https://xrpl.org/noripple_check.html#noripple_check] states
77 // that transactions params is a boolean value, however, assigning any
78 // string value works. Do not allow this. This check is for api Version 2
79 // onwards only
80 if (context.apiVersion > 1u && params.isMember(jss::transactions) &&
81 !params[jss::transactions].isBool())
82 {
83 return RPC::invalid_field_error(jss::transactions);
84 }
85
87 auto result = RPC::lookupLedger(ledger, context);
88 if (!ledger)
89 return result;
90
91 Json::Value dummy; // NOLINT(misc-const-correctness)
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{id.value()};
102 auto const sle = ledger->read(keylet::account(accountID));
103 if (!sle)
105
106 std::uint32_t seq = sle->getFieldU32(sfSequence);
107
108 Json::Value& problems = (result["problems"] = Json::arrayValue);
109
110 bool const bDefaultRipple = (sle->getFieldU32(sfFlags) & lsfDefaultRipple) != 0u;
111
112 if ((static_cast<int>(bDefaultRipple) & static_cast<int>(!roleGateway)) != 0)
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 ((static_cast<int>(roleGateway) & static_cast<int>(!bDefaultRipple)) != 0)
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, accountID, uint256(), 0, limit, [&](std::shared_ptr<SLE const> const& ownedItem) {
133 if (ownedItem->getType() == ltRIPPLE_STATE)
134 {
135 bool const bLow = accountID == ownedItem->getFieldAmount(sfLowLimit).getIssuer();
136
137 bool const bNoRipple =
138 ownedItem->getFieldU32(sfFlags) & (bLow ? lsfLowNoRipple : lsfHighNoRipple);
139
140 std::string problem;
141 bool needFix = false;
142 if (bNoRipple & roleGateway)
143 {
144 problem = "You should clear the no ripple flag on your ";
145 needFix = true;
146 }
147 else if (!roleGateway & !bNoRipple)
148 {
149 problem = "You should probably set the no ripple flag on your ";
150 needFix = true;
151 }
152 if (needFix)
153 {
154 AccountID const peer =
155 ownedItem->getFieldAmount(bLow ? sfHighLimit : sfLowLimit).getIssuer();
156 STAmount const peerLimit =
157 ownedItem->getFieldAmount(bLow ? sfHighLimit : sfLowLimit);
158 problem += to_string(peerLimit.getCurrency());
159 problem += " line to ";
160 problem += to_string(peerLimit.getIssuer());
161 problems.append(problem);
162
163 STAmount limitAmount(
164 ownedItem->getFieldAmount(bLow ? sfLowLimit : sfHighLimit));
165 limitAmount.setIssuer(peer);
166
167 Json::Value& tx = jvTransactions.append(Json::objectValue);
168 tx["TransactionType"] = jss::TrustSet;
169 tx["LimitAmount"] = limitAmount.getJson(JsonOptions::none);
170 tx["Flags"] = bNoRipple ? tfClearNoRipple : tfSetNoRipple;
171 fillTransaction(context, tx, accountID, seq, *ledger);
172
173 return true;
174 }
175 }
176 return false;
177 });
178
179 return result;
180}
181
182} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
Value & append(Value const &value)
Append value to array at the end.
A view into a ledger.
Definition ReadView.h:31
virtual Fees const & fees() const =0
Returns the fees for the base ledger.
Json::Value getJson(JsonOptions=JsonOptions::none) const override
Definition STAmount.cpp:744
void setIssuer(AccountID const &uIssuer)
Definition STAmount.h:572
Currency const & getCurrency() const
Definition STAmount.h:476
AccountID const & getIssuer() const
Definition STAmount.h:482
virtual LoadFeeTrack & getFeeTrack()=0
Json::Value jsonClipped() const
Definition XRPAmount.h:197
@ arrayValue
array value (ordered list)
Definition json_value.h:25
@ objectValue
object value (collection of name/value pairs).
Definition json_value.h:26
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:273
Json::Value missing_field_error(std::string const &name)
Definition ErrorCodes.h:231
void inject_error(error_code_i code, Json::Value &json)
Add or update the json update to reflect the error code.
Status lookupLedger(std::shared_ptr< ReadView const > &ledger, JsonContext const &context, Json::Value &result)
Looks up a ledger from a request and fills a Json::Value with ledger data.
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:5
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:602
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:92
base_uint< 256 > uint256
Definition base_uint.h:531
Json::Value doNoRippleCheck(RPC::JsonContext &context)
Json::Value rpcError(error_code_i iError)
Definition RPCErr.cpp:12
XRPAmount scaleFeeLoad(XRPAmount fee, LoadFeeTrack const &feeTrack, Fees const &fees, bool bUnlimited)
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.
static void fillTransaction(RPC::JsonContext &context, Json::Value &txArray, AccountID const &accountID, std::uint32_t &sequence, ReadView const &ledger)
@ rpcACT_NOT_FOUND
Definition ErrorCodes.h:50
@ rpcACT_MALFORMED
Definition ErrorCodes.h:70
Application & app
Definition Context.h:21
unsigned int apiVersion
Definition Context.h:29
Json::Value params
Definition Context.h:43