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/beast/utility/instrumentation.h>
8#include <xrpl/json/json_forwards.h>
9#include <xrpl/json/json_value.h>
10#include <xrpl/ledger/ReadView.h>
11#include <xrpl/ledger/helpers/DirectoryHelpers.h>
12#include <xrpl/protocol/AccountID.h>
13#include <xrpl/protocol/ErrorCodes.h>
14#include <xrpl/protocol/Indexes.h>
15#include <xrpl/protocol/LedgerFormats.h>
16#include <xrpl/protocol/RPCErr.h>
17#include <xrpl/protocol/SField.h>
18#include <xrpl/protocol/TxFlags.h>
19#include <xrpl/protocol/UintTypes.h>
20#include <xrpl/protocol/jss.h>
21#include <xrpl/resource/Fees.h>
22#include <xrpl/server/LoadFeeTrack.h>
23
24#include <cstdint>
25#include <memory>
26#include <string>
27#include <utility>
28
29namespace xrpl {
30
31static void
33 rpc::JsonContext& context,
34 json::Value& txArray,
35 AccountID const& accountID,
36 std::uint32_t& sequence,
37 ReadView const& ledger)
38{
39 txArray[jss::Sequence] = json::UInt(sequence++);
40 txArray[jss::Account] = toBase58(accountID);
41 auto& fees = ledger.fees();
42 // Convert the reference transaction cost in fee units to drops
43 // scaled to represent the current fee load.
44 txArray[jss::Fee] =
45 scaleFeeLoad(fees.base, context.app.getFeeTrack(), fees, false).jsonClipped();
46}
47
48// {
49// account: <account>
50// ledger_hash : <ledger>
51// ledger_index : <ledger_index>
52// limit: integer // optional, number of problems
53// role: gateway|user // account role to assume
54// transactions: true // optional, recommend transactions
55// }
58{
59 auto const& params(context.params);
60 if (!params.isMember(jss::account))
61 return rpc::missingFieldError(jss::account);
62
63 if (!params[jss::account].isString())
64 return rpc::invalidFieldError(jss::account);
65
66 auto id = parseBase58<AccountID>(params[jss::account].asString());
67 if (!id)
68 {
70 }
71 auto const accountID{id.value()};
72
73 if (!params.isMember(jss::role))
74 return rpc::missingFieldError(jss::role);
75
76 bool roleGateway = false;
77 {
78 if (!params[jss::role].isString())
79 return rpc::expectedFieldError(jss::role, "string");
80 std::string const role = params[jss::role].asString();
81 if (role == jss::gateway)
82 {
83 roleGateway = true;
84 }
85 else if (role != jss::user)
86 {
87 return rpc::invalidFieldError(jss::role);
88 }
89 }
90
91 unsigned int limit = 0;
92 if (auto err = readLimitField(limit, rpc::tuning::kNoRippleCheck, context))
93 return *err;
94
95 // API v1 silently accepts any string as `transactions`; v2+ enforces bool.
96 if (context.apiVersion > 1u && params.isMember(jss::transactions) &&
97 !params[jss::transactions].isBool())
98 {
99 return rpc::invalidFieldError(jss::transactions);
100 }
101
102 bool transactions = false;
103 if (params.isMember(jss::transactions))
104 transactions = params[jss::transactions].asBool();
105
107 auto result = rpc::lookupLedger(ledger, context);
108 if (!ledger)
109 return result;
110
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[jss::problems] = json::ValueType::Array);
118
119 bool const defaultRipple = sle->isFlag(lsfDefaultRipple);
120
121 json::Value jvTransactions = json::ValueType::Array;
122
123 if (defaultRipple && !roleGateway)
124 {
125 problems.append(
126 "You appear to have set your default ripple flag even though you "
127 "are not a gateway. This is not recommended unless you are "
128 "experimenting");
129 }
130 else if (roleGateway && !defaultRipple)
131 {
132 problems.append("You should immediately set your default ripple flag");
133 if (transactions)
134 {
135 json::Value& tx = jvTransactions.append(json::ValueType::Object);
136 tx[jss::TransactionType] = jss::AccountSet;
137 tx[jss::SetFlag] = 8;
138 fillTransaction(context, tx, accountID, seq, *ledger);
139 }
140 }
141
142 forEachItemAfter(*ledger, accountID, UInt256(), 0, limit, [&](SLE::ConstRef ownedItem) {
143 if (!ownedItem)
144 {
145 // LCOV_EXCL_START
146 UNREACHABLE("xrpl::doNoRippleCheck : null SLE");
147 return false;
148 // LCOV_EXCL_STOP
149 }
150
151 if (ownedItem->getType() == ltRIPPLE_STATE)
152 {
153 bool const low = accountID == ownedItem->getFieldAmount(sfLowLimit).getIssuer();
154
155 bool const noRipple = ownedItem->isFlag(low ? lsfLowNoRipple : lsfHighNoRipple);
156
157 std::string problem;
158 bool needFix = false;
159 if (noRipple && roleGateway)
160 {
161 problem = "You should clear the no ripple flag on your ";
162 needFix = true;
163 }
164 else if (!roleGateway && !noRipple)
165 {
166 problem = "You should probably set the no ripple flag on your ";
167 needFix = true;
168 }
169 if (needFix)
170 {
171 AccountID const peer =
172 ownedItem->getFieldAmount(low ? sfHighLimit : sfLowLimit).getIssuer();
173 STAmount const peerLimit =
174 ownedItem->getFieldAmount(low ? sfHighLimit : sfLowLimit);
175 problem += to_string(peerLimit.get<Issue>().currency);
176 problem += " line to ";
177 problem += to_string(peerLimit.getIssuer());
178 problems.append(problem);
179
180 STAmount limitAmount(ownedItem->getFieldAmount(low ? sfLowLimit : sfHighLimit));
181 limitAmount.get<Issue>().account = peer;
182
183 if (transactions)
184 {
185 json::Value& tx = jvTransactions.append(json::ValueType::Object);
186 tx[jss::TransactionType] = jss::TrustSet;
187 tx[jss::LimitAmount] = limitAmount.getJson(JsonOptions::Values::None);
188 tx[jss::Flags] = noRipple ? tfClearNoRipple : tfSetNoRipple;
189 fillTransaction(context, tx, accountID, seq, *ledger);
190 }
191
192 return true;
193 }
194 }
195 return false;
196 });
197
198 if (transactions)
199 result[jss::transactions] = std::move(jvTransactions);
200
202 return result;
203}
204
205} // namespace xrpl
Represents a JSON value.
Definition json_value.h:121
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
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:203
unsigned int UInt
@ Array
array value (ordered list)
Definition json_value.h:32
@ Object
object value (collection of name/value pairs).
Definition json_value.h:33
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:233
Charge const kFeeMediumBurdenRpc
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
::rpc::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.
json::Value missingFieldError(std::string const &name)
Definition ErrorCodes.h:243
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:658
json::Value rpcError(ErrorCodeI iError)
Definition RPCErr.cpp:13
BaseUInt< 256 > UInt256
Definition base_uint.h:581
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)
resource::Charge & loadType
Definition Context.h:30
unsigned int apiVersion
Definition Context.h:37
Application & app
Definition Context.h:29
json::Value params
Definition Context.h:51