20 #include <ripple/app/ledger/LedgerMaster.h>
21 #include <ripple/app/ledger/TransactionMaster.h>
22 #include <ripple/app/misc/NetworkOPs.h>
23 #include <ripple/app/misc/Transaction.h>
24 #include <ripple/basics/ToString.h>
25 #include <ripple/net/RPCErr.h>
26 #include <ripple/protocol/ErrorCodes.h>
27 #include <ripple/protocol/jss.h>
28 #include <ripple/rpc/CTID.h>
29 #include <ripple/rpc/Context.h>
30 #include <ripple/rpc/DeliveredAmount.h>
31 #include <ripple/rpc/GRPCHandlers.h>
32 #include <ripple/rpc/NFTSyntheticSerializer.h>
33 #include <ripple/rpc/impl/RPCHelpers.h>
74 Throw<std::runtime_error>(
75 "Called doTxPostgres yet not in reporting mode");
85 "Use of CTIDs on reporting mode is not currently supported."}};
87 JLOG(context.
j.
debug()) <<
"Fetching from postgres";
108 return {res, {
rpcINTERNAL,
"Error making SHAMap node"}};
110 auto item = (
static_cast<SHAMapLeafNode*
>(node.get()))->peekItem();
114 return {res, {
rpcINTERNAL,
"Error reading SHAMap node"}};
118 JLOG(context.
j.
debug()) <<
"Successfully fetched from db";
123 return {res, {
rpcINTERNAL,
"Error deserializing SHAMap node"}};
126 res.
txn = std::make_shared<Transaction>(sttx, reason, context.
app);
134 res.
meta = std::move(blob);
138 res.
meta = std::make_shared<TxMeta>(
146 JLOG(context.
j.
error()) <<
"Failed to fetch from db";
148 return {res, {
rpcINTERNAL,
"Containing SHAMap node not found"}};
151 JLOG(context.
j.
debug()) <<
"tx flat fetch time : "
152 << ((end - start).count() / 1000000000.0);
163 if (min >=
range.lower() && max <=
range.upper())
176 return {res, {
rpcINTERNAL,
"unexpected Postgres response"}};
190 constexpr uint16_t MAX_RANGE = 1000;
213 args.
ctid->first, args.
ctid->second);
232 if (
auto e = std::get_if<TxSearched>(&v))
238 auto [txn, meta] = std::get<TxPair>(v);
251 if (txn->getLedger() == 0)
263 result.
meta = meta->getAsObject().getSerializer().getData();
273 uint32_t lgrSeq = ledger->
info().
seq;
277 if (txnIdx <= 0xFFFFU && netID < 0xFFFFU && lgrSeq < 0x0FFF
'FFFFUL)
279 RPC::encodeCTID(lgrSeq, (uint16_t)txnIdx, (uint16_t)netID);
282 return {result, rpcSUCCESS};
286 populateJsonResponse(
287 std::pair<TxResult, RPC::Status> const& res,
289 RPC::JsonContext const& context)
291 Json::Value response;
292 RPC::Status const& error = res.second;
293 TxResult const& result = res.first;
295 if (error.toErrorCode() != rpcSUCCESS)
297 if (error.toErrorCode() == rpcTXN_NOT_FOUND &&
298 result.searchedAll != TxSearched::unknown)
300 response = Json::Value(Json::objectValue);
301 response[jss::searched_all] =
302 (result.searchedAll == TxSearched::all);
303 error.inject(response);
307 error.inject(response);
313 response = result.txn->getJson(JsonOptions::include_date, args.binary);
315 // populate binary metadata
316 if (auto blob = std::get_if<Blob>(&result.meta))
319 response[jss::meta] = strHex(makeSlice(*blob));
321 // populate meta data
322 else if (auto m = std::get_if<std::shared_ptr<TxMeta>>(&result.meta))
327 response[jss::meta] = meta->getJson(JsonOptions::none);
328 insertDeliveredAmount(
329 response[jss::meta], context, result.txn, *meta);
330 insertNFTSyntheticInJson(
331 response, context, result.txn->getSTransaction(), *meta);
334 response[jss::validated] = result.validated;
337 response[jss::ctid] = *(result.ctid);
343 doTxJson(RPC::JsonContext& context)
345 if (!context.app.config().useTxTables())
346 return rpcError(rpcNOT_ENABLED);
348 // Deserialize and validate JSON arguments
352 if (context.params.isMember(jss::transaction) &&
353 context.params.isMember(jss::ctid))
354 // specifying both is ambiguous
355 return rpcError(rpcINVALID_PARAMS);
357 if (context.params.isMember(jss::transaction))
360 if (!hash.parseHex(context.params[jss::transaction].asString()))
361 return rpcError(rpcNOT_IMPL);
364 else if (context.params.isMember(jss::ctid))
366 auto ctid = RPC::decodeCTID(context.params[jss::ctid].asString());
368 return rpcError(rpcINVALID_PARAMS);
370 auto const [lgr_seq, txn_idx, net_id] = *ctid;
371 if (net_id != context.app.config().NETWORK_ID)
373 std::stringstream out;
374 out << "Wrong network. You should submit this request to a node "
375 "running on NetworkID: "
377 return RPC::make_error(rpcWRONG_NETWORK, out.str());
379 args.ctid = {lgr_seq, txn_idx};
382 return rpcError(rpcINVALID_PARAMS);
384 args.binary = context.params.isMember(jss::binary) &&
385 context.params[jss::binary].asBool();
387 if (context.params.isMember(jss::min_ledger) &&
388 context.params.isMember(jss::max_ledger))
392 args.ledgerRange = std::make_pair(
393 context.params[jss::min_ledger].asUInt(),
394 context.params[jss::max_ledger].asUInt());
398 // One of the calls to `asUInt ()` failed.
399 return rpcError(rpcINVALID_LGR_RANGE);
403 std::pair<TxResult, RPC::Status> res = doTxHelp(context, args);
404 return populateJsonResponse(res, args, context);
407 } // namespace ripple
virtual Family & getNodeFamily()=0
@ ledgerMaster
ledger master data for signing
std::enable_if_t< std::is_same< T, char >::value||std::is_same< T, unsigned char >::value, Slice > makeSlice(std::array< T, N > const &a)
uint256 const & getNodestoreHash()
void setLedger(LedgerIndex ledger)
LedgerMaster & ledgerMaster
virtual NodeStore::Database & db()=0
std::optional< std::pair< uint32_t, uint16_t > > ctid
LedgerInfo const & info() const override
Returns information about the ledger.
virtual LedgerMaster & getLedgerMaster()=0
std::pair< std::shared_ptr< STTx const >, std::shared_ptr< STObject const > > deserializeTxPlusMeta(SHAMapItem const &item)
Deserialize a SHAMapItem containing STTx + STObject metadata.
LedgerIndex getLedger() const
std::optional< std::pair< uint32_t, uint32_t > > ledgerRange
virtual Config & config()=0
static Locator locate(uint256 const &id, Application &app)
std::shared_ptr< Ledger const > getLedgerBySeq(std::uint32_t index)
std::optional< uint256 > txnIdFromIndex(uint32_t ledgerSeq, uint32_t txnIndex)
ClosedInterval< T > range(T low, T high)
Create a closed range interval.
std::optional< std::string > ctid
const SF_UINT32 sfTransactionIndex
static std::shared_ptr< SHAMapTreeNode > makeFromPrefix(Slice rawNode, SHAMapHash const &hash)
std::optional< uint256 > hash
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::pair< TxResult, RPC::Status > doTxPostgres(RPC::Context &context, TxArgs const &args)
void setStatus(TransStatus status, std::uint32_t ledgerSeq)
std::pair< TxResult, RPC::Status > doTxHelp(RPC::Context &context, TxArgs args)
std::variant< std::shared_ptr< TxMeta >, Blob > meta
std::variant< std::pair< std::shared_ptr< Transaction >, std::shared_ptr< TxMeta > >, TxSearched > fetch(uint256 const &, error_code_i &ec)
std::shared_ptr< NodeObject > fetchNodeObject(uint256 const &hash, std::uint32_t ledgerSeq=0, FetchType fetchType=FetchType::synchronous, bool duplicate=false)
Fetch a node object.
boost::icl::closed_interval< T > ClosedInterval
A closed interval over the domain T.
static bool isValidated(LedgerMaster &ledgerMaster, std::uint32_t seq, uint256 const &hash)
uint32_t getLedgerSequence()
The context of information needed to call an RPC.
virtual TransactionMaster & getMasterTransaction()=0
ClosedInterval< uint32_t > const & getLedgerRangeSearched()