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#include <string>
25#include <utility>
26
27namespace xrpl {
28
29static void
31 rpc::JsonContext& context,
32 json::Value& txArray,
33 AccountID const& accountID,
34 std::uint32_t& sequence,
35 ReadView const& ledger)
36{
37 txArray[jss::Sequence] = json::UInt(sequence++);
38 txArray[jss::Account] = toBase58(accountID);
39 auto& fees = ledger.fees();
40 // Convert the reference transaction cost in fee units to drops
41 // scaled to represent the current fee load.
42 txArray[jss::Fee] =
43 scaleFeeLoad(fees.base, context.app.getFeeTrack(), fees, false).jsonClipped();
44}
45
46// {
47// account: <account>
48// ledger_hash : <ledger>
49// ledger_index : <ledger_index>
50// limit: integer // optional, number of problems
51// role: gateway|user // account role to assume
52// transactions: true // optional, recommend transactions
53// }
56{
57 auto const& params(context.params);
58 if (!params.isMember(jss::account))
59 return rpc::missingFieldError(jss::account);
60
61 if (!params[jss::account].isString())
62 return rpc::invalidFieldError(jss::account);
63
64 auto id = parseBase58<AccountID>(params[jss::account].asString());
65 if (!id)
66 {
68 }
69 auto const accountID{id.value()};
70
71 if (!params.isMember(jss::role))
72 return rpc::missingFieldError(jss::role);
73
74 bool roleGateway = false;
75 {
76 if (!params[jss::role].isString())
77 return rpc::expectedFieldError(jss::role, "string");
78 std::string const role = params[jss::role].asString();
79 if (role == jss::gateway)
80 {
81 roleGateway = true;
82 }
83 else if (role != jss::user)
84 {
85 return rpc::invalidFieldError(jss::role);
86 }
87 }
88
89 unsigned int limit = 0;
90 if (auto err = readLimitField(limit, rpc::tuning::kNoRippleCheck, context))
91 return *err;
92
93 // API v1 silently accepts any string as `transactions`; v2+ enforces bool.
94 if (context.apiVersion > 1u && params.isMember(jss::transactions) &&
95 !params[jss::transactions].isBool())
96 {
97 return rpc::invalidFieldError(jss::transactions);
98 }
99
100 bool transactions = false;
101 if (params.isMember(jss::transactions))
102 transactions = params[jss::transactions].asBool();
103
105 auto result = rpc::lookupLedger(ledger, context);
106 if (!ledger)
107 return result;
108
109 auto const sle = ledger->read(keylet::account(accountID));
110 if (!sle)
111 return rpcError(RpcActNotFound);
112
113 std::uint32_t seq = sle->getFieldU32(sfSequence);
114
115 json::Value& problems = (result[jss::problems] = json::ValueType::Array);
116
117 bool const defaultRipple = sle->isFlag(lsfDefaultRipple);
118
119 json::Value jvTransactions = json::ValueType::Array;
120
121 if (defaultRipple && !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 && !defaultRipple)
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[jss::TransactionType] = jss::AccountSet;
135 tx[jss::SetFlag] = 8;
136 fillTransaction(context, tx, accountID, seq, *ledger);
137 }
138 }
139
140 forEachItemAfter(*ledger, accountID, UInt256(), 0, limit, [&](SLE::ConstRef ownedItem) {
141 if (ownedItem->getType() == ltRIPPLE_STATE)
142 {
143 bool const low = accountID == ownedItem->getFieldAmount(sfLowLimit).getIssuer();
144
145 bool const noRipple = ownedItem->isFlag(low ? lsfLowNoRipple : lsfHighNoRipple);
146
147 std::string problem;
148 bool needFix = false;
149 if (noRipple && roleGateway)
150 {
151 problem = "You should clear the no ripple flag on your ";
152 needFix = true;
153 }
154 else if (!roleGateway && !noRipple)
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(low ? sfHighLimit : sfLowLimit).getIssuer();
163 STAmount const peerLimit =
164 ownedItem->getFieldAmount(low ? 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(low ? sfLowLimit : sfHighLimit));
171 limitAmount.get<Issue>().account = peer;
172
173 if (transactions)
174 {
175 json::Value& tx = jvTransactions.append(json::ValueType::Object);
176 tx[jss::TransactionType] = jss::TrustSet;
177 tx[jss::LimitAmount] = limitAmount.getJson(JsonOptions::Values::None);
178 tx[jss::Flags] = noRipple ? tfClearNoRipple : tfSetNoRipple;
179 fillTransaction(context, tx, accountID, seq, *ledger);
180 }
181
182 return true;
183 }
184 }
185 return false;
186 });
187
188 if (transactions)
189 result[jss::transactions] = std::move(jvTransactions);
190 return result;
191}
192
193} // namespace xrpl
Represents a JSON value.
Definition json_value.h:117
Value & append(Value const &value)
Append value to array at the end.
A currency issued by an account.
Definition Issue.h:18
Currency currency
Definition Issue.h:20
A view into a ledger.
Definition ReadView.h:41
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:735
AccountID const & getIssuer() const
Definition STAmount.h:516
std::shared_ptr< STLedgerEntry const > const & ConstRef
virtual LoadFeeTrack & getFeeTrack()=0
json::Value jsonClipped() const
Definition XRPAmount.h:210
unsigned int UInt
@ Array
array value (ordered list)
Definition json_value.h:28
@ Object
object value (collection of name/value pairs).
Definition json_value.h:29
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:220
static constexpr LimitRange kNoRippleCheck
Limits for the no_ripple_check command.
json::Value expectedFieldError(std::string const &name, std::string const &type)
Definition ErrorCodes.h:309
json::Value invalidFieldError(std::string const &name)
Definition ErrorCodes.h:285
json::Value missingFieldError(std::string const &name)
Definition ErrorCodes.h:243
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.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ RpcActNotFound
Definition ErrorCodes.h:53
@ RpcActMalformed
Definition ErrorCodes.h:73
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
json::Value doNoRippleCheck(rpc::JsonContext &context)
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:657
json::Value rpcError(ErrorCodeI iError)
Definition RPCErr.cpp:13
BaseUInt< 256 > UInt256
Definition base_uint.h:580
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
bool forEachItemAfter(ReadView const &view, Keylet const &root, UInt256 const &after, std::uint64_t const hint, unsigned int limit, std::function< bool(SLE::ConstRef)> const &f)
Iterate all items after an item in the given directory.
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)
unsigned int apiVersion
Definition Context.h:37
Application & app
Definition Context.h:29
json::Value params
Definition Context.h:51