xrpld
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/Tuning.h>
6
7#include <xrpl/json/json_forwards.h>
8#include <xrpl/json/json_value.h>
9#include <xrpl/ledger/ReadView.h>
10#include <xrpl/ledger/helpers/DirectoryHelpers.h>
11#include <xrpl/protocol/AccountID.h>
12#include <xrpl/protocol/ErrorCodes.h>
13#include <xrpl/protocol/Indexes.h>
14#include <xrpl/protocol/LedgerFormats.h>
15#include <xrpl/protocol/RPCErr.h>
16#include <xrpl/protocol/SField.h>
17#include <xrpl/protocol/TxFlags.h>
18#include <xrpl/protocol/UintTypes.h>
19#include <xrpl/protocol/jss.h>
20#include <xrpl/server/LoadFeeTrack.h>
21
22#include <cstdint>
23#include <memory>
24
25namespace xrpl {
26
27static void
29 RPC::JsonContext& context,
30 json::Value& txArray,
31 AccountID const& accountID,
32 std::uint32_t& sequence,
33 ReadView const& ledger)
34{
35 txArray["Sequence"] = json::UInt(sequence++);
36 txArray["Account"] = toBase58(accountID);
37 auto& fees = ledger.fees();
38 // Convert the reference transaction cost in fee units to drops
39 // scaled to represent the current fee load.
40 txArray["Fee"] = scaleFeeLoad(fees.base, context.app.getFeeTrack(), fees, false).jsonClipped();
41}
42
43// {
44// account: <account>
45// ledger_hash : <ledger>
46// ledger_index : <ledger_index>
47// limit: integer // optional, number of problems
48// role: gateway|user // account role to assume
49// transactions: true // optional, recommend transactions
50// }
53{
54 auto const& params(context.params);
55 if (!params.isMember(jss::account))
56 return RPC::missingFieldError("account");
57
58 if (!params.isMember("role"))
59 return RPC::missingFieldError("role");
60
61 if (!params[jss::account].isString())
62 return RPC::invalidFieldError(jss::account);
63
64 bool roleGateway = false;
65 {
66 std::string const role = params["role"].asString();
67 if (role == "gateway")
68 {
69 roleGateway = true;
70 }
71 else if (role != "user")
72 {
73 return RPC::invalidFieldError("role");
74 }
75 }
76
77 unsigned int limit = 0;
78 if (auto err = readLimitField(limit, RPC::Tuning::kNoRippleCheck, context))
79 return *err;
80
81 bool transactions = false;
82 if (params.isMember(jss::transactions))
83 transactions = params["transactions"].asBool();
84
85 // The document[https://xrpl.org/noripple_check.html#noripple_check] states
86 // that transactions params is a boolean value, however, assigning any
87 // string value works. Do not allow this. This check is for api Version 2
88 // onwards only
89 if (context.apiVersion > 1u && params.isMember(jss::transactions) &&
90 !params[jss::transactions].isBool())
91 {
92 return RPC::invalidFieldError(jss::transactions);
93 }
94
96 auto result = RPC::lookupLedger(ledger, context);
97 if (!ledger)
98 return result;
99
100 json::Value dummy; // NOLINT(misc-const-correctness)
101 json::Value& jvTransactions =
102 transactions ? (result[jss::transactions] = json::ValueType::Array) : dummy;
103
104 auto id = parseBase58<AccountID>(params[jss::account].asString());
105 if (!id)
106 {
108 return result;
109 }
110 auto const accountID{id.value()};
111 auto const sle = ledger->read(keylet::account(accountID));
112 if (!sle)
113 return rpcError(RpcActNotFound);
114
115 std::uint32_t seq = sle->getFieldU32(sfSequence);
116
117 json::Value& problems = (result["problems"] = json::ValueType::Array);
118
119 bool const bDefaultRipple = sle->isFlag(lsfDefaultRipple);
120
121 if (bDefaultRipple && !roleGateway)
122 {
123 problems.append(
124 "You appear to have set your default ripple flag even though you "
125 "are not a gateway. This is not recommended unless you are "
126 "experimenting");
127 }
128 else if (roleGateway && !bDefaultRipple)
129 {
130 problems.append("You should immediately set your default ripple flag");
131 if (transactions)
132 {
133 json::Value& tx = jvTransactions.append(json::ValueType::Object);
134 tx["TransactionType"] = jss::AccountSet;
135 tx["SetFlag"] = 8;
136 fillTransaction(context, tx, accountID, seq, *ledger);
137 }
138 }
139
140 forEachItemAfter(*ledger, accountID, uint256(), 0, limit, [&](SLE::const_ref ownedItem) {
141 if (ownedItem->getType() == ltRIPPLE_STATE)
142 {
143 bool const bLow = accountID == ownedItem->getFieldAmount(sfLowLimit).getIssuer();
144
145 bool const bNoRipple = ownedItem->isFlag(bLow ? lsfLowNoRipple : lsfHighNoRipple);
146
147 std::string problem;
148 bool needFix = false;
149 if (bNoRipple && roleGateway)
150 {
151 problem = "You should clear the no ripple flag on your ";
152 needFix = true;
153 }
154 else if (!roleGateway && !bNoRipple)
155 {
156 problem = "You should probably set the no ripple flag on your ";
157 needFix = true;
158 }
159 if (needFix)
160 {
161 AccountID const peer =
162 ownedItem->getFieldAmount(bLow ? sfHighLimit : sfLowLimit).getIssuer();
163 STAmount const peerLimit =
164 ownedItem->getFieldAmount(bLow ? sfHighLimit : sfLowLimit);
165 problem += to_string(peerLimit.get<Issue>().currency);
166 problem += " line to ";
167 problem += to_string(peerLimit.getIssuer());
168 problems.append(problem);
169
170 STAmount limitAmount(ownedItem->getFieldAmount(bLow ? sfLowLimit : sfHighLimit));
171 limitAmount.get<Issue>().account = peer;
172
173 json::Value& tx = jvTransactions.append(json::ValueType::Object);
174 tx["TransactionType"] = jss::TrustSet;
175 tx["LimitAmount"] = limitAmount.getJson(JsonOptions::Values::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 xrpl
Represents a JSON value.
Definition json_value.h:130
Value & append(Value const &value)
Append value to array at the end.
A currency issued by an account.
Definition Issue.h:13
Currency currency
Definition Issue.h:15
A view into a ledger.
Definition ReadView.h:31
virtual Fees const & fees() const =0
Returns the fees for the base ledger.
constexpr TIss const & get() const
json::Value getJson(JsonOptions=JsonOptions::Values::None) const override
Definition STAmount.cpp:734
AccountID const & getIssuer() const
Definition STAmount.h:498
std::shared_ptr< STLedgerEntry const > const & const_ref
virtual LoadFeeTrack & getFeeTrack()=0
json::Value jsonClipped() const
Definition XRPAmount.h:199
unsigned int UInt
@ Array
array value (ordered list)
Definition json_value.h:25
@ Object
object value (collection of name/value pairs).
Definition json_value.h:26
static constexpr LimitRange kNoRippleCheck
Limits for the no_ripple_check command.
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.
void injectError(ErrorCodeI code, json::Value &json)
Add or update the json update to reflect the error code.
json::Value invalidFieldError(std::string const &name)
Definition ErrorCodes.h:273
json::Value missingFieldError(std::string const &name)
Definition ErrorCodes.h:231
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:186
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ RpcActNotFound
Definition ErrorCodes.h:52
@ RpcActMalformed
Definition ErrorCodes.h:72
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
json::Value doNoRippleCheck(RPC::JsonContext &context)
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:93
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:633
json::Value rpcError(ErrorCodeI iError)
Definition RPCErr.cpp:13
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:28
XRPAmount scaleFeeLoad(XRPAmount fee, LoadFeeTrack const &feeTrack, Fees const &fees, bool bUnlimited)
static void fillTransaction(RPC::JsonContext &context, json::Value &txArray, AccountID const &accountID, std::uint32_t &sequence, ReadView const &ledger)
BaseUInt< 256 > uint256
Definition base_uint.h:562
bool forEachItemAfter(ReadView const &view, Keylet const &root, uint256 const &after, std::uint64_t const hint, unsigned int limit, std::function< bool(SLE::const_ref)> const &f)
Iterate all items after an item in the given directory.
Application & app
Definition Context.h:21
unsigned int apiVersion
Definition Context.h:29
json::Value params
Definition Context.h:43