20 #include <ripple/app/ledger/LedgerMaster.h>
21 #include <ripple/app/ledger/TransactionMaster.h>
22 #include <ripple/app/misc/DeliverMax.h>
23 #include <ripple/app/misc/NetworkOPs.h>
24 #include <ripple/app/misc/Transaction.h>
25 #include <ripple/basics/ToString.h>
26 #include <ripple/net/RPCErr.h>
27 #include <ripple/protocol/ErrorCodes.h>
28 #include <ripple/protocol/NFTSyntheticSerializer.h>
29 #include <ripple/protocol/jss.h>
30 #include <ripple/rpc/CTID.h>
31 #include <ripple/rpc/Context.h>
32 #include <ripple/rpc/DeliveredAmount.h>
33 #include <ripple/rpc/GRPCHandlers.h>
34 #include <ripple/rpc/impl/RPCHelpers.h>
75 Throw<std::runtime_error>(
76 "Called doTxPostgres yet not in reporting mode");
86 "Use of CTIDs on reporting mode is not currently supported."}};
88 JLOG(context.
j.
debug()) <<
"Fetching from postgres";
109 return {res, {
rpcINTERNAL,
"Error making SHAMap node"}};
111 auto item = (
static_cast<SHAMapLeafNode*
>(node.get()))->peekItem();
115 return {res, {
rpcINTERNAL,
"Error reading SHAMap node"}};
119 JLOG(context.
j.
debug()) <<
"Successfully fetched from db";
124 return {res, {
rpcINTERNAL,
"Error deserializing SHAMap node"}};
127 res.
txn = std::make_shared<Transaction>(sttx, reason, context.
app);
135 res.
meta = std::move(blob);
139 res.
meta = std::make_shared<TxMeta>(
147 JLOG(context.
j.
error()) <<
"Failed to fetch from db";
149 return {res, {
rpcINTERNAL,
"Containing SHAMap node not found"}};
152 JLOG(context.
j.
debug()) <<
"tx flat fetch time : "
153 << ((end - start).count() / 1000000000.0);
164 if (min >=
range.lower() && max <=
range.upper())
177 return {res, {
rpcINTERNAL,
"unexpected Postgres response"}};
191 constexpr uint16_t MAX_RANGE = 1000;
214 args.
ctid->first, args.
ctid->second);
233 if (
auto e = std::get_if<TxSearched>(&v))
239 auto [txn, meta] = std::get<TxPair>(v);
252 if (txn->getLedger() == 0)
264 result.
meta = meta->getAsObject().getSerializer().getData();
274 uint32_t lgrSeq = ledger->
info().
seq;
278 if (txnIdx <= 0xFFFFU && netID < 0xFFFFU && lgrSeq < 0x0FFF
'FFFFUL)
280 RPC::encodeCTID(lgrSeq, (uint16_t)txnIdx, (uint16_t)netID);
283 return {result, rpcSUCCESS};
287 populateJsonResponse(
288 std::pair<TxResult, RPC::Status> const& res,
290 RPC::JsonContext const& context)
292 Json::Value response;
293 RPC::Status const& error = res.second;
294 TxResult const& result = res.first;
296 if (error.toErrorCode() != rpcSUCCESS)
298 if (error.toErrorCode() == rpcTXN_NOT_FOUND &&
299 result.searchedAll != TxSearched::unknown)
301 response = Json::Value(Json::objectValue);
302 response[jss::searched_all] =
303 (result.searchedAll == TxSearched::all);
304 error.inject(response);
308 error.inject(response);
314 response = result.txn->getJson(JsonOptions::include_date, args.binary);
315 auto const& sttx = result.txn->getSTransaction();
317 RPC::insertDeliverMax(
318 response, sttx->getTxnType(), context.apiVersion);
320 // populate binary metadata
321 if (auto blob = std::get_if<Blob>(&result.meta))
324 response[jss::meta] = strHex(makeSlice(*blob));
326 // populate meta data
327 else if (auto m = std::get_if<std::shared_ptr<TxMeta>>(&result.meta))
332 response[jss::meta] = meta->getJson(JsonOptions::none);
333 insertDeliveredAmount(
334 response[jss::meta], context, result.txn, *meta);
335 insertNFTSyntheticInJson(response, sttx, *meta);
338 response[jss::validated] = result.validated;
341 response[jss::ctid] = *(result.ctid);
347 doTxJson(RPC::JsonContext& context)
349 if (!context.app.config().useTxTables())
350 return rpcError(rpcNOT_ENABLED);
352 // Deserialize and validate JSON arguments
356 if (context.params.isMember(jss::transaction) &&
357 context.params.isMember(jss::ctid))
358 // specifying both is ambiguous
359 return rpcError(rpcINVALID_PARAMS);
361 if (context.params.isMember(jss::transaction))
364 if (!hash.parseHex(context.params[jss::transaction].asString()))
365 return rpcError(rpcNOT_IMPL);
368 else if (context.params.isMember(jss::ctid))
370 auto ctid = RPC::decodeCTID(context.params[jss::ctid].asString());
372 return rpcError(rpcINVALID_PARAMS);
374 auto const [lgr_seq, txn_idx, net_id] = *ctid;
375 if (net_id != context.app.config().NETWORK_ID)
377 std::stringstream out;
378 out << "Wrong network. You should submit this request to a node "
379 "running on NetworkID: "
381 return RPC::make_error(rpcWRONG_NETWORK, out.str());
383 args.ctid = {lgr_seq, txn_idx};
386 return rpcError(rpcINVALID_PARAMS);
388 args.binary = context.params.isMember(jss::binary) &&
389 context.params[jss::binary].asBool();
391 if (context.params.isMember(jss::min_ledger) &&
392 context.params.isMember(jss::max_ledger))
396 args.ledgerRange = std::make_pair(
397 context.params[jss::min_ledger].asUInt(),
398 context.params[jss::max_ledger].asUInt());
402 // One of the calls to `asUInt ()` failed.
403 return rpcError(rpcINVALID_LGR_RANGE);
407 std::pair<TxResult, RPC::Status> res = doTxHelp(context, args);
408 return populateJsonResponse(res, args, context);
411 } // 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()