forked from XRPLF/clio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRPCHelpers.h
344 lines (283 loc) · 10.5 KB
/
RPCHelpers.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//------------------------------------------------------------------------------
/*
This file is part of clio: https://github.com/XRPLF/clio
Copyright (c) 2022, the clio developers.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
/** @file */
#pragma once
/*
* This file contains a variety of utility functions used when executing the handlers.
*/
#include <data/BackendInterface.h>
#include <rpc/Amendments.h>
#include <rpc/JS.h>
#include <rpc/common/Types.h>
#include <util/JsonUtils.h>
#include <web/Context.h>
#include <ripple/protocol/Indexes.h>
#include <ripple/protocol/Rate.h>
#include <ripple/protocol/STLedgerEntry.h>
#include <ripple/protocol/STTx.h>
#include <boost/regex.hpp>
#include <fmt/core.h>
namespace rpc {
enum class NFTokenjson { ENABLE, DISABLE };
std::optional<ripple::AccountID>
accountFromStringStrict(std::string const& account);
bool
isOwnedByAccount(ripple::SLE const& sle, ripple::AccountID const& accountID);
std::uint64_t
getStartHint(ripple::SLE const& sle, ripple::AccountID const& accountID);
std::optional<AccountCursor>
parseAccountCursor(std::optional<std::string> jsonCursor);
// TODO this function should probably be in a different file and namespace
std::pair<std::shared_ptr<ripple::STTx const>, std::shared_ptr<ripple::STObject const>>
deserializeTxPlusMeta(data::TransactionAndMetadata const& blobs);
// TODO this function should probably be in a different file and namespace
std::pair<std::shared_ptr<ripple::STTx const>, std::shared_ptr<ripple::TxMeta const>>
deserializeTxPlusMeta(data::TransactionAndMetadata const& blobs, std::uint32_t seq);
/**
* @brief Convert a TransactionAndMetadata to two JSON objects.
*
* @param blobs The TransactionAndMetadata to convert.
* @param nftEnabled Whether to include NFT information in the JSON.
* @param networkId The network ID to use for ctid, not include ctid if nullopt.
*/
std::pair<boost::json::object, boost::json::object>
toExpandedJson(
data::TransactionAndMetadata const& blobs,
std::uint32_t apiVersion,
NFTokenjson nftEnabled = NFTokenjson::DISABLE,
std::optional<uint16_t> networkId = std::nullopt
);
/**
* @brief Convert a TransactionAndMetadata to JSON object containing tx and metadata data in hex format. According to
* the apiVersion, the key is "tx_blob" and "meta" or "meta_blob".
* @param txnPlusMeta The TransactionAndMetadata to convert.
* @param apiVersion The api version
* @return The JSON object containing tx and metadata data in hex format.
*/
boost::json::object
toJsonWithBinaryTx(data::TransactionAndMetadata const& txnPlusMeta, std::uint32_t apiVersion);
/**
* @brief Add "DeliverMax" which is the alias of "Amount" for "Payment" transaction to transaction json. Remove the
* "Amount" field when version is greater than 1
* @param txJson The transaction json object
* @param apiVersion The api version
*/
void
insertDeliverMaxAlias(boost::json::object& txJson, std::uint32_t apiVersion);
bool
insertDeliveredAmount(
boost::json::object& metaJson,
std::shared_ptr<ripple::STTx const> const& txn,
std::shared_ptr<ripple::TxMeta const> const& meta,
uint32_t date
);
boost::json::object
toJson(ripple::STBase const& obj);
boost::json::object
toJson(ripple::SLE const& sle);
/**
* @brief Convert a LedgerHeader to JSON object.
*
* @param entry The LedgerHeader to convert.
* @param binary Whether to convert in hex format.
* @return The JSON object.
*/
boost::json::object
toJson(ripple::LedgerHeader const& info, bool binary);
boost::json::object
toJson(ripple::TxMeta const& meta);
using RippledJson = Json::Value;
boost::json::value
toBoostJson(RippledJson const& value);
boost::json::object
generatePubLedgerMessage(
ripple::LedgerHeader const& lgrInfo,
ripple::Fees const& fees,
std::string const& ledgerRange,
std::uint32_t txnCount
);
std::variant<Status, ripple::LedgerHeader>
ledgerInfoFromRequest(std::shared_ptr<data::BackendInterface const> const& backend, web::Context const& ctx);
std::variant<Status, ripple::LedgerHeader>
getLedgerInfoFromHashOrSeq(
BackendInterface const& backend,
boost::asio::yield_context yield,
std::optional<std::string> ledgerHash,
std::optional<uint32_t> ledgerIndex,
uint32_t maxSeq
);
std::variant<Status, AccountCursor>
traverseOwnedNodes(
BackendInterface const& backend,
ripple::Keylet const& owner,
ripple::uint256 const& hexMarker,
std::uint32_t startHint,
std::uint32_t sequence,
std::uint32_t limit,
boost::asio::yield_context yield,
std::function<void(ripple::SLE&&)> atOwnedNode
);
// Remove the account check from traverseOwnedNodes
// Account check has been done by framework,remove it from internal function
std::variant<Status, AccountCursor>
traverseOwnedNodes(
BackendInterface const& backend,
ripple::AccountID const& accountID,
std::uint32_t sequence,
std::uint32_t limit,
std::optional<std::string> jsonCursor,
boost::asio::yield_context yield,
std::function<void(ripple::SLE&&)> atOwnedNode,
bool nftIncluded = false
);
std::shared_ptr<ripple::SLE const>
read(
std::shared_ptr<data::BackendInterface const> const& backend,
ripple::Keylet const& keylet,
ripple::LedgerHeader const& lgrInfo,
web::Context const& context
);
std::variant<Status, std::pair<ripple::PublicKey, ripple::SecretKey>>
keypairFromRequst(boost::json::object const& request);
std::vector<ripple::AccountID>
getAccountsFromTransaction(boost::json::object const& transaction);
std::vector<unsigned char>
ledgerInfoToBlob(ripple::LedgerHeader const& info, bool includeHash = false);
bool
isGlobalFrozen(
BackendInterface const& backend,
std::uint32_t seq,
ripple::AccountID const& issuer,
boost::asio::yield_context yield
);
bool
isFrozen(
BackendInterface const& backend,
std::uint32_t sequence,
ripple::AccountID const& account,
ripple::Currency const& currency,
ripple::AccountID const& issuer,
boost::asio::yield_context yield
);
ripple::STAmount
accountFunds(
BackendInterface const& backend,
std::uint32_t sequence,
ripple::STAmount const& amount,
ripple::AccountID const& id,
boost::asio::yield_context yield
);
ripple::STAmount
accountHolds(
BackendInterface const& backend,
std::uint32_t sequence,
ripple::AccountID const& account,
ripple::Currency const& currency,
ripple::AccountID const& issuer,
bool zeroIfFrozen,
boost::asio::yield_context yield
);
ripple::Rate
transferRate(
BackendInterface const& backend,
std::uint32_t sequence,
ripple::AccountID const& issuer,
boost::asio::yield_context yield
);
ripple::XRPAmount
xrpLiquid(
BackendInterface const& backend,
std::uint32_t sequence,
ripple::AccountID const& id,
boost::asio::yield_context yield
);
boost::json::array
postProcessOrderBook(
std::vector<data::LedgerObject> const& offers,
ripple::Book const& book,
ripple::AccountID const& takerID,
data::BackendInterface const& backend,
std::uint32_t ledgerSequence,
boost::asio::yield_context yield
);
std::variant<Status, ripple::Book>
parseBook(ripple::Currency pays, ripple::AccountID payIssuer, ripple::Currency gets, ripple::AccountID getIssuer);
std::variant<Status, ripple::Book>
parseBook(boost::json::object const& request);
std::variant<Status, ripple::AccountID>
parseTaker(boost::json::value const& taker);
bool
specifiesCurrentOrClosedLedger(boost::json::object const& request);
std::variant<ripple::uint256, Status>
getNFTID(boost::json::object const& request);
bool
isAmendmentEnabled(
std::shared_ptr<data::BackendInterface const> const& backend,
boost::asio::yield_context yield,
uint32_t seq,
ripple::uint256 amendmentId
);
std::optional<std::string>
encodeCTID(uint32_t ledgerSeq, uint16_t txnIndex, uint16_t networkId) noexcept;
template <typename T>
inline std::optional<std::tuple<uint32_t, uint16_t, uint16_t>>
decodeCTID(T const ctid) noexcept
{
auto const getCTID64 = [](T const ctid) noexcept -> std::optional<uint64_t> {
if constexpr (std::is_convertible_v<T, std::string>) {
std::string const ctidString(ctid);
static std::size_t constexpr CTID_STRING_LENGTH = 16;
if (ctidString.length() != CTID_STRING_LENGTH)
return {};
if (!boost::regex_match(ctidString, boost::regex("^[0-9A-F]+$")))
return {};
return std::stoull(ctidString, nullptr, 16);
}
if constexpr (std::is_same_v<T, uint64_t>)
return ctid;
return {};
};
auto const ctidValue = getCTID64(ctid).value_or(0);
static uint64_t constexpr CTID_PREFIX = 0xC000'0000'0000'0000ULL;
static uint64_t constexpr CTID_PREFIX_MASK = 0xF000'0000'0000'0000ULL;
if ((ctidValue & CTID_PREFIX_MASK) != CTID_PREFIX)
return {};
uint32_t const ledgerSeq = (ctidValue >> 32) & 0xFFFF'FFFUL;
uint16_t const txnIndex = (ctidValue >> 16) & 0xFFFFU;
uint16_t const networkId = ctidValue & 0xFFFFU;
return {{ledgerSeq, txnIndex, networkId}};
}
template <class T>
void
logDuration(web::Context const& ctx, T const& dur)
{
using boost::json::serialize;
static util::Logger const log{"RPC"};
static std::int64_t constexpr DURATION_ERROR_THRESHOLD_SECONDS = 10;
auto const millis = std::chrono::duration_cast<std::chrono::milliseconds>(dur).count();
auto const seconds = std::chrono::duration_cast<std::chrono::seconds>(dur).count();
auto const msg = fmt::format(
"Request processing duration = {} milliseconds. request = {}", millis, serialize(util::removeSecret(ctx.params))
);
if (seconds > DURATION_ERROR_THRESHOLD_SECONDS) {
LOG(log.error()) << ctx.tag() << msg;
} else if (seconds > 1) {
LOG(log.warn()) << ctx.tag() << msg;
} else
LOG(log.info()) << ctx.tag() << msg;
}
} // namespace rpc