3#include "data/BackendInterface.hpp"
4#include "data/Types.hpp"
5#include "etl/ETLServiceInterface.hpp"
8#include "rpc/common/SpecBackend.hpp"
9#include "rpc/common/Types.hpp"
10#include "util/Assert.hpp"
12#include <boost/asio/spawn.hpp>
13#include <boost/json/conversion.hpp>
14#include <boost/json/object.hpp>
15#include <boost/json/value.hpp>
16#include <boost/json/value_to.hpp>
17#include <fmt/format.h>
18#include <rpcspec/Errors.hpp>
19#include <rpcspec/handlers/tx/Types.hpp>
20#include <xrpl/basics/chrono.h>
21#include <xrpl/basics/strHex.h>
22#include <xrpl/protocol/LedgerHeader.h>
23#include <xrpl/protocol/jss.h>
39 std::shared_ptr<BackendInterface> sharedPtrBackend_;
40 std::shared_ptr<etl::ETLServiceInterface const> etl_;
56 std::optional<boost::json::object>
meta =
61 std::optional<boost::json::object>
tx =
71 std::optional<std::string>
txStr =
76 std::optional<std::string>
ctid =
100 std::shared_ptr<BackendInterface> sharedPtrBackend,
101 std::shared_ptr<etl::ETLServiceInterface const>
const&
etl
103 : sharedPtrBackend_(std::move(sharedPtrBackend)), etl_(
etl)
117 if (input.ctid && input.transaction)
118 return Error{Status{XrpldError::RpcInvalidParams}};
120 if (!input.ctid && !input.transaction)
121 return Error{Status{XrpldError::RpcInvalidParams}};
123 static constexpr auto kMaxLedgerRange = 1000u;
124 auto const rangeSupplied = input.minLedger && input.maxLedger;
127 if (*input.minLedger > *input.maxLedger)
128 return Error{Status{XrpldError::RpcInvalidLgrRange}};
130 if (*input.maxLedger - *input.minLedger > kMaxLedgerRange)
131 return Error{Status{XrpldError::RpcExcessiveLgrRange}};
134 std::optional<uint32_t> currentNetId = std::nullopt;
135 if (
auto const& etlState = etl_->getETLState(); etlState.has_value())
136 currentNetId = etlState->networkID;
138 std::optional<data::TransactionAndMetadata> dbResponse;
143 return Error{Status{XrpldError::RpcInvalidParams}};
145 auto const [lgrSeq, txnIdx, netId] = *ctid;
147 if (currentNetId && netId != *currentNetId) {
149 XrpldError::RpcWrongNetwork,
151 "Wrong network. You should submit this request to a node running on "
158 dbResponse = fetchTxViaCtid(lgrSeq, txnIdx, ctx.
yield);
160 dbResponse = sharedPtrBackend_->fetchTransaction(*input.transaction, ctx.
yield);
166 if (rangeSupplied && input.transaction)
168 auto const range = sharedPtrBackend_->fetchLedgerRange();
169 ASSERT(range.has_value(),
"Tx's ledger range must be available");
172 auto const searchedAll = range->maxSequence >= *input.maxLedger &&
173 range->minSequence <= *input.minLedger;
177 Status{XrpldError::RpcTxnNotFound, ExtraInfo{{
"searched_all", searchedAll}}}
181 return Error{Status{XrpldError::RpcTxnNotFound}};
184 auto const [txn, meta] =
191 output.txStr = xrpl::strHex(dbResponse->transaction);
192 output.metaStr = xrpl::strHex(dbResponse->metadata);
195 if (txn.contains(JS(hash)))
196 output.hash = txn.at(JS(hash)).as_string();
200 auto const txnIdx = boost::json::value_to<uint64_t>(meta.at(
"TransactionIndex"));
201 if (txnIdx <= 0xFFFFU && dbResponse->ledgerSequence < 0x0FFF'FFFFUL && currentNetId &&
202 *currentNetId <= 0xFFFFU) {
204 dbResponse->ledgerSequence,
205 static_cast<uint16_t
>(txnIdx),
206 static_cast<uint16_t
>(*currentNetId)
210 output.date = dbResponse->date;
211 output.ledgerIndex = dbResponse->ledgerSequence;
215 output.ledgerHeader =
216 sharedPtrBackend_->fetchLedgerBySequence(dbResponse->ledgerSequence, ctx.
yield);
223 [[nodiscard]] std::optional<data::TransactionAndMetadata>
224 fetchTxViaCtid(uint32_t ledgerSeq, uint32_t txId, boost::asio::yield_context yield)
const
226 auto const txs = sharedPtrBackend_->fetchAllTransactionsInLedger(ledgerSeq, yield);
228 for (
auto const& tx : txs) {
231 if (meta->getIndex() == txId)
239 tag_invoke(boost::json::value_from_tag, boost::json::value& jv,
Output const& output)
241 auto const getJsonV1 = [&] {
242 auto obj = boost::json::object{};
246 obj[JS(meta)] = *output.meta;
248 obj[JS(meta)] = *output.metaStr;
249 obj[JS(tx)] = *output.txStr;
250 obj[JS(hash)] = output.hash;
253 obj[JS(validated)] = output.validated;
254 obj[JS(date)] = output.date;
255 obj[JS(ledger_index)] = output.ledgerIndex;
256 obj[JS(inLedger)] = output.ledgerIndex;
260 auto const getJsonV2 = [&] {
261 auto obj = boost::json::object{};
264 obj[JS(tx_json)] = *output.tx;
265 obj[JS(tx_json)].as_object()[JS(date)] = output.date;
267 obj[JS(tx_json)].as_object()[JS(ctid)] = *output.ctid;
269 obj[JS(tx_json)].as_object()[JS(ledger_index)] = output.ledgerIndex;
271 if (obj[JS(tx_json)].as_object().contains(JS(hash))) {
272 obj[JS(hash)] = obj[JS(tx_json)].as_object()[JS(hash)];
273 obj[JS(tx_json)].as_object().erase(JS(hash));
275 obj[JS(meta)] = *output.meta;
277 obj[JS(meta_blob)] = *output.metaStr;
278 obj[JS(tx_blob)] = *output.txStr;
279 obj[JS(hash)] = output.hash;
282 obj[JS(validated)] = output.validated;
283 obj[JS(ledger_index)] = output.ledgerIndex;
285 if (output.ledgerHeader) {
286 obj[JS(ledger_hash)] = xrpl::strHex(output.ledgerHeader->hash);
287 obj[JS(close_time_iso)] = xrpl::toStringIso(output.ledgerHeader->closeTime);
292 auto obj = output.apiVersion > 1u ? getJsonV2() : getJsonV1();
295 obj[JS(ctid)] = *output.ctid;
TxHandler(std::shared_ptr< BackendInterface > sharedPtrBackend, std::shared_ptr< etl::ETLServiceInterface const > const &etl)
Construct a new TxHandler object.
Definition Tx.hpp:99
Result process(Input const &input, Context const &ctx) const
Process the Tx command.
Definition Tx.hpp:115
HandlerReturnType< Output > Result
The result type of the command: either the Output or an error status.
Definition Tx.hpp:91
This namespace contains everything to do with the ETL and ETL sources.
Definition AmendmentBlockHandlerInterface.hpp:6
This namespace contains all the RPC logic and handlers.
Definition AMMHelpers.cpp:17
std::pair< boost::json::object, boost::json::object > toExpandedJson(data::TransactionAndMetadata const &blobs, std::uint32_t const apiVersion, NFTokenjson nftEnabled, std::optional< uint32_t > networkId)
Convert a TransactionAndMetadata to two JSON objects.
Definition RPCHelpers.cpp:235
std::expected< OutputType, Status > HandlerReturnType
Return type for each individual handler.
Definition Types.hpp:63
std::optional< std::tuple< uint32_t, uint16_t, uint16_t > > decodeCTID(T const ctid) noexcept
Decode the CTID from a string or a uint64_t.
Definition RPCHelpers.hpp:799
spec::HandlerFor< InputT, boost::json::value > HandlerFor
The spec-driven base every typed handler inherits.
Definition SpecBackend.hpp:24
std::unexpected< Status > Error
The type that represents just the error part of MaybeError.
Definition Types.hpp:55
std::optional< std::string > encodeCTID(uint32_t ledgerSeq, uint16_t txnIndex, uint32_t networkId) noexcept
Encode CTID as string.
Definition RPCHelpers.cpp:272
@ ENABLE
Insert synthetic NFT fields (e.g. nftoken_id, offer_id) into the metadata.
Definition RPCHelpers.hpp:73
std::pair< std::shared_ptr< xrpl::STTx const >, std::shared_ptr< xrpl::STObject const > > deserializeTxPlusMeta(data::TransactionAndMetadata const &blobs)
Deserialize a TransactionAndMetadata into a pair of STTx and STObject.
Definition RPCHelpers.cpp:186
Context of an RPC call.
Definition Types.hpp:102
uint32_t apiVersion
The API version requested by the client; 0 means invalid/unset.
Definition Types.hpp:126
boost::asio::yield_context yield
The coroutine context the RPC call is executed in.
Definition Types.hpp:106
Result type used to return responses or error statuses to the Webserver subsystem.
Definition Types.hpp:132
A struct to hold the output data of the command.
Definition Tx.hpp:46
std::string hash
Transaction hash, only set when binary was requested.
Definition Tx.hpp:51
std::optional< boost::json::object > meta
Metadata as JSON, unless binary was requested.
Definition Tx.hpp:56
std::optional< std::string > metaStr
Hex-encoded metadata, if binary was requested.
Definition Tx.hpp:66
std::optional< std::string > ctid
Compact transaction ID, if the network ID is known and the CTID can be encoded.
Definition Tx.hpp:76
uint32_t apiVersion
API version of the request.
Definition Tx.hpp:84
uint32_t ledgerIndex
Sequence of the ledger containing the transaction.
Definition Tx.hpp:52
std::optional< std::string > txStr
Hex-encoded transaction, if binary was requested.
Definition Tx.hpp:71
uint32_t date
Close time of the ledger containing the transaction.
Definition Tx.hpp:47
std::optional< xrpl::LedgerHeader > ledgerHeader
Header of the containing ledger (API version 2+).
Definition Tx.hpp:81
bool validated
Whether the ledger is validated.
Definition Tx.hpp:85
std::optional< boost::json::object > tx
Transaction as JSON, unless binary was requested.
Definition Tx.hpp:61