xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
xrpld
rpc
handlers
transaction
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
21
namespace
xrpl::rpc
{
22
23
TransactionEntryHandler::TransactionEntryHandler
(
JsonContext
& context) :
context_
(context)
24
{
25
}
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".
30
std::expected<TransactionEntryHandler::Output, ::rpc::Status>
31
TransactionEntryHandler::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
62
void
63
TransactionEntryHandler::writeResult
(
json::Value
& value,
Output
const
& output)
const
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
std::string
json::Value
Represents a JSON value.
Definition
json_value.h:121
xrpl::rpc::TransactionEntryHandler::context_
std::reference_wrapper< JsonContext > context_
Definition
TransactionEntry.h:67
xrpl::rpc::TransactionEntryHandler::writeResult
void writeResult(json::Value &value, Output const &output) const
Write the output of process() into the response.
Definition
TransactionEntry.cpp:63
xrpl::rpc::TransactionEntryHandler::TransactionEntryHandler
TransactionEntryHandler(JsonContext &context)
Construct a handler for a single request.
Definition
TransactionEntry.cpp:23
xrpl::rpc::TransactionEntryHandler::process
std::expected< Output, ::rpc::Status > process(Input const &input) const
Process the request.
Definition
TransactionEntry.cpp:31
xrpl::rpc
The api_version numbers this server serves.
Definition
ApiVersion.h:26
xrpl::rpc::insertDeliverMax
void insertDeliverMax(json::Value &txJson, TxType txnType, unsigned int apiVersion)
Copy Amount field to DeliverMax field in transaction output JSON.
Definition
DeliverMax.cpp:9
xrpl::rpc::getLedger
::rpc::Status getLedger(T &ledger, UInt256 const &ledgerHash, Context const &context)
Retrieves a ledger by its hash.
Definition
RPCLedgerHelpers.cpp:240
xrpl::rpc::injectLedgerFields
void injectLedgerFields(ReadView const &ledger, Context const &context, json::Value &result)
Writes the fields that identify a ledger into a result.
Definition
RPCLedgerHelpers.cpp:391
xrpl::to_string
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition
base_uint.h:658
xrpl::toStringIso
std::string toStringIso(date::sys_time< Duration > tp)
Definition
chrono.h:70
string
xrpl::JsonOptions::Values::None
@ None
Definition
STBase.h:27
xrpl::JsonOptions::Values::DisableApiPriorV2
@ DisableApiPriorV2
Definition
STBase.h:29
xrpl::rpc::JsonContext
Definition
Context.h:41
xrpl::rpc::TransactionEntryHandler::Output
The data a transaction_entry request produces, written out by writeResult().
Definition
TransactionEntry.h:34
xrpl::rpc::TransactionEntryHandler::Output::meta
std::shared_ptr< STObject const > meta
The metadata of the transaction found.
Definition
TransactionEntry.h:38
xrpl::rpc::TransactionEntryHandler::Output::ledger
std::shared_ptr< ReadView const > ledger
The ledger the request selected.
Definition
TransactionEntry.h:35
xrpl::rpc::TransactionEntryHandler::Output::error
std::optional< std::string_view > error
An error written with the ledger fields.
Definition
TransactionEntry.h:36
xrpl::rpc::TransactionEntryHandler::Output::tx
std::shared_ptr< STTx const > tx
The transaction found.
Definition
TransactionEntry.h:37
std::tie
T tie(T... args)
tuple
std::unexpected
T unexpected(T... args)
Generated by
1.17.0