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 xrpl {
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"] = scaleFeeLoad(fees.base, context.app.getFeeTrack(), fees, false).jsonClipped();
31}
32
33// {
34// account: <account>
35// ledger_hash : <ledger>
36// ledger_index : <ledger_index>
37// limit: integer // optional, number of problems
38// role: gateway|user // account role to assume
39// transactions: true // optional, recommend transactions
40// }
43{
44 auto const& params(context.params);
45 if (!params.isMember(jss::account))
46 return RPC::missing_field_error("account");
47
48 if (!params.isMember("role"))
49 return RPC::missing_field_error("role");
50
51 if (!params[jss::account].isString())
52 return RPC::invalid_field_error(jss::account);
53
54 bool roleGateway = false;
55 {
56 std::string const role = params["role"].asString();
57 if (role == "gateway")
58 roleGateway = true;
59 else if (role != "user")
60 return RPC::invalid_field_error("role");
61 }
62
63 unsigned int limit;
64 if (auto err = readLimitField(limit, RPC::Tuning::noRippleCheck, context))
65 return *err;
66
67 bool transactions = false;
68 if (params.isMember(jss::transactions))
69 transactions = params["transactions"].asBool();
70
71 // The document[https://xrpl.org/noripple_check.html#noripple_check] states
72 // that transactions params is a boolean value, however, assigning any
73 // string value works. Do not allow this. This check is for api Version 2
74 // onwards only
75 if (context.apiVersion > 1u && params.isMember(jss::transactions) && !params[jss::transactions].isBool())
76 {
77 return RPC::invalid_field_error(jss::transactions);
78 }
79
81 auto result = RPC::lookupLedger(ledger, context);
82 if (!ledger)
83 return result;
84
85 Json::Value dummy;
86 Json::Value& jvTransactions = transactions ? (result[jss::transactions] = Json::arrayValue) : dummy;
87
88 auto id = parseBase58<AccountID>(params[jss::account].asString());
89 if (!id)
90 {
92 return result;
93 }
94 auto const accountID{std::move(id.value())};
95 auto const sle = ledger->read(keylet::account(accountID));
96 if (!sle)
98
99 std::uint32_t seq = sle->getFieldU32(sfSequence);
100
101 Json::Value& problems = (result["problems"] = Json::arrayValue);
102
103 bool bDefaultRipple = sle->getFieldU32(sfFlags) & lsfDefaultRipple;
104
105 if (bDefaultRipple & !roleGateway)
106 {
107 problems.append(
108 "You appear to have set your default ripple flag even though you "
109 "are not a gateway. This is not recommended unless you are "
110 "experimenting");
111 }
112 else if (roleGateway & !bDefaultRipple)
113 {
114 problems.append("You should immediately set your default ripple flag");
115 if (transactions)
116 {
117 Json::Value& tx = jvTransactions.append(Json::objectValue);
118 tx["TransactionType"] = jss::AccountSet;
119 tx["SetFlag"] = 8;
120 fillTransaction(context, tx, accountID, seq, *ledger);
121 }
122 }
123
124 forEachItemAfter(*ledger, accountID, uint256(), 0, limit, [&](std::shared_ptr<SLE const> const& ownedItem) {
125 if (ownedItem->getType() == ltRIPPLE_STATE)
126 {
127 bool const bLow = accountID == ownedItem->getFieldAmount(sfLowLimit).getIssuer();
128
129 bool const bNoRipple = ownedItem->getFieldU32(sfFlags) & (bLow ? lsfLowNoRipple : lsfHighNoRipple);
130
131 std::string problem;
132 bool needFix = false;
133 if (bNoRipple & roleGateway)
134 {
135 problem = "You should clear the no ripple flag on your ";
136 needFix = true;
137 }
138 else if (!roleGateway & !bNoRipple)
139 {
140 problem = "You should probably set the no ripple flag on your ";
141 needFix = true;
142 }
143 if (needFix)
144 {
145 AccountID peer = ownedItem->getFieldAmount(bLow ? sfHighLimit : sfLowLimit).getIssuer();
146 STAmount peerLimit = ownedItem->getFieldAmount(bLow ? sfHighLimit : sfLowLimit);
147 problem += to_string(peerLimit.getCurrency());
148 problem += " line to ";
149 problem += to_string(peerLimit.getIssuer());
150 problems.append(problem);
151
152 STAmount limitAmount(ownedItem->getFieldAmount(bLow ? sfLowLimit : sfHighLimit));
153 limitAmount.setIssuer(peer);
154
155 Json::Value& tx = jvTransactions.append(Json::objectValue);
156 tx["TransactionType"] = jss::TrustSet;
157 tx["LimitAmount"] = limitAmount.getJson(JsonOptions::none);
158 tx["Flags"] = bNoRipple ? tfClearNoRipple : tfSetNoRipple;
159 fillTransaction(context, tx, accountID, seq, *ledger);
160
161 return true;
162 }
163 }
164 return false;
165 });
166
167 return result;
168}
169
170} // namespace xrpl
Represents a JSON value.
Definition json_value.h:131
Value & append(Value const &value)
Append value to array at the end.
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:721
void setIssuer(AccountID const &uIssuer)
Definition STAmount.h:555
Currency const & getCurrency() const
Definition STAmount.h:461
AccountID const & getIssuer() const
Definition STAmount.h:467
virtual LoadFeeTrack & getFeeTrack()=0
Json::Value jsonClipped() const
Definition XRPAmount.h:197
@ 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:269
Json::Value missing_field_error(std::string const &name)
Definition ErrorCodes.h:227
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:160
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:598
constexpr std::uint32_t tfSetNoRipple
Definition TxFlags.h:97
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:92
base_uint< 256 > uint256
Definition base_uint.h:527
constexpr std::uint32_t tfClearNoRipple
Definition TxFlags.h:98
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.
Definition View.cpp:622
@ lsfDefaultRipple
Json::Value doNoRippleCheck(RPC::JsonContext &)
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:51
@ rpcACT_MALFORMED
Definition ErrorCodes.h:71
Application & app
Definition Context.h:22
unsigned int apiVersion
Definition Context.h:30
Json::Value params
Definition Context.h:44