xrpld
Loading...
Searching...
No Matches
TransactionEntry.cpp
1#include <xrpld/rpc/handlers/transaction/TransactionEntry.h>
2
3#include <xrpld/app/ledger/LedgerMaster.h>
4#include <xrpld/app/misc/DeliverMax.h>
5#include <xrpld/rpc/Context.h>
6#include <xrpld/rpc/detail/RPCLedgerHelpers.h>
7
8#include <xrpl/basics/base_uint.h>
9#include <xrpl/basics/chrono.h>
10#include <xrpl/json/json_value.h>
11#include <xrpl/ledger/ReadView.h>
12#include <xrpl/protocol/jss.h>
13
14#include <rpcspec/Errors.hpp>
15#include <rpcspec/handlers/transaction_entry/Types.hpp>
16
17#include <expected>
18#include <string>
19#include <tuple>
20
21namespace xrpl::rpc {
22
26
27// With no ledger specified, this uses the current ledger, as every other method
28// does. An open ledger is refused with notYetImplemented, so a request with no
29// ledger fails the same way as one that asks for "current".
30std::expected<TransactionEntryHandler::Output, ::rpc::Status>
31TransactionEntryHandler::process(Input const& input) const
32{
33 Output output;
34 if (auto const status = getLedger(output.ledger, input.ledger, context_.get());
35 status != ::rpc::Status::kOK)
36 return std::unexpected{status};
37
38 if (!input.txHash &&
39 input.txHash.error() == ::rpc::spec::handlers::transaction_entry::TxHashError::Missing)
40 {
41 output.error = "fieldNotFoundTransaction";
42 }
43 else if (output.ledger->open())
44 {
45 // We don't work on ledger current.
46 output.error = "notYetImplemented";
47 }
48 else if (!input.txHash)
49 {
50 output.error = "malformedRequest";
51 }
52 else
53 {
54 std::tie(output.tx, output.meta) = output.ledger->txRead(*input.txHash);
55 if (!output.tx)
56 output.error = "transactionNotFound";
57 }
58
59 return output;
60}
61
62void
64{
65 auto const& context = context_.get();
66 auto const& ledger = *output.ledger;
67
68 injectLedgerFields(ledger, context, value);
69
70 if (output.error)
71 {
72 value[jss::error] = std::string{*output.error};
73 return;
74 }
75
76 if (context.apiVersion > 1)
77 {
78 value[jss::tx_json] = output.tx->getJson(JsonOptions::Values::DisableApiPriorV2);
79 value[jss::hash] = to_string(output.tx->getTransactionID());
80
81 if (!ledger.open())
82 value[jss::ledger_hash] = to_string(context.ledgerMaster.getHashBySeq(ledger.seq()));
83
84 bool const validated = context.ledgerMaster.isValidated(ledger);
85
86 value[jss::validated] = validated;
87 if (validated)
88 {
89 value[jss::ledger_index] = ledger.seq();
90 if (auto closeTime = context.ledgerMaster.getCloseTimeBySeq(ledger.seq()))
91 value[jss::close_time_iso] = toStringIso(*closeTime);
92 }
93 }
94 else
95 {
96 value[jss::tx_json] = output.tx->getJson(JsonOptions::Values::None);
97 }
98
99 insertDeliverMax(value[jss::tx_json], output.tx->getTxnType(), context.apiVersion);
100
101 auto const jsonMeta = (context.apiVersion > 1 ? jss::meta : jss::metadata);
102 if (output.meta)
103 value[jsonMeta] = output.meta->getJson(JsonOptions::Values::None);
104}
105
106} // namespace xrpl::rpc
Represents a JSON value.
Definition json_value.h:121
std::reference_wrapper< JsonContext > context_
void writeResult(json::Value &value, Output const &output) const
Write the output of process() into the response.
TransactionEntryHandler(JsonContext &context)
Construct a handler for a single request.
std::expected< Output, ::rpc::Status > process(Input const &input) const
Process the request.
The api_version numbers this server serves.
Definition ApiVersion.h:26
void insertDeliverMax(json::Value &txJson, TxType txnType, unsigned int apiVersion)
Copy Amount field to DeliverMax field in transaction output JSON.
Definition DeliverMax.cpp:9
::rpc::Status getLedger(T &ledger, UInt256 const &ledgerHash, Context const &context)
Retrieves a ledger by its hash.
void injectLedgerFields(ReadView const &ledger, Context const &context, json::Value &result)
Writes the fields that identify a ledger into a result.
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:658
std::string toStringIso(date::sys_time< Duration > tp)
Definition chrono.h:70
The data a transaction_entry request produces, written out by writeResult().
std::shared_ptr< STObject const > meta
The metadata of the transaction found.
std::shared_ptr< ReadView const > ledger
The ledger the request selected.
std::optional< std::string_view > error
An error written with the ledger fields.
std::shared_ptr< STTx const > tx
The transaction found.
T tie(T... args)
T unexpected(T... args)