xrpld
Loading...
Searching...
No Matches
Transaction.cpp
1#include <xrpld/app/misc/Transaction.h>
2
3#include <xrpld/app/ledger/LedgerMaster.h>
4#include <xrpld/app/main/Application.h>
5#include <xrpld/rpc/CTID.h>
6
7#include <xrpl/basics/Blob.h>
8#include <xrpl/basics/RangeSet.h>
9#include <xrpl/basics/Slice.h>
10#include <xrpl/basics/safe_cast.h>
11#include <xrpl/beast/utility/instrumentation.h>
12#include <xrpl/json/json_value.h>
13#include <xrpl/protocol/ErrorCodes.h>
14#include <xrpl/protocol/SField.h>
15#include <xrpl/protocol/STBase.h>
16#include <xrpl/protocol/STTx.h>
17#include <xrpl/protocol/Serializer.h>
18#include <xrpl/protocol/TxMeta.h>
19#include <xrpl/protocol/TxSearched.h>
20#include <xrpl/protocol/jss.h>
21#include <xrpl/rdb/RelationalDatabase.h>
22
23#include <boost/optional/optional.hpp> // IWYU pragma: keep
24
25#include <cstdint>
26#include <exception>
27#include <memory>
28#include <optional>
29#include <string>
30#include <utility>
31#include <variant>
32
33namespace xrpl {
34
37 std::string& reason,
38 Application& app) noexcept
39 : transaction_(stx), app_(app), j_(app.getJournal("Ledger"))
40{
41 try
42 {
43 transactionID_ = transaction_->getTransactionID();
44 }
45 catch (std::exception& e)
46 {
47 reason = e.what();
48 return;
49 }
50
52}
53
54//
55// Misc.
56//
57
58void
60 TransStatus ts,
61 std::uint32_t lseq,
64{
65 status_ = ts;
66 ledgerIndex_ = lseq;
67 if (tseq)
68 txnSeq_ = tseq;
69 if (netID)
70 networkID_ = netID;
71}
72
74Transaction::sqlTransactionStatus(boost::optional<std::string> const& status)
75{
76 auto const c = status ? safeCast<TxnSql>((*status)[0]) : TxnSql::Unknown;
77
78 switch (static_cast<TxnSql>(c))
79 {
80 case TxnSql::New:
81 return TransStatus::NEW;
84 case TxnSql::Held:
85 return TransStatus::HELD;
90 default:
91 XRPL_ASSERT(
92 c == TxnSql::Unknown,
93 "xrpl::Transaction::sqlTransactionStatus : unknown transaction status");
94 }
95
97}
98
101 boost::optional<std::uint64_t> const& ledgerSeq,
102 boost::optional<std::string> const& status,
103 Blob const& rawTxn,
104 Application& app)
105{
106 std::uint32_t const inLedger = rangeCheckedCast<std::uint32_t>(ledgerSeq.value_or(0));
107
108 SerialIter it(makeSlice(rawTxn));
109 auto txn = std::make_shared<STTx const>(it);
110 std::string reason;
111 auto tr = std::make_shared<Transaction>(txn, reason, app);
112
113 tr->setStatus(sqlTransactionStatus(status));
114 tr->setLedger(inLedger);
115 return tr;
116}
117
120{
121 return load(id, app, std::nullopt, ec);
122}
123
126 uint256 const& id,
127 Application& app,
129 ErrorCodeI& ec)
130{
132
133 return load(id, app, op{range}, ec);
134}
135
138 uint256 const& id,
139 Application& app,
141 ErrorCodeI& ec)
142{
143 auto& db = app.getRelationalDatabase();
144
145 return db.getTransaction(id, range, ec);
146}
147
148// options 1 to include the date of the transaction
150Transaction::getJson(JsonOptions options, bool binary) const
151{
152 // Note, we explicitly suppress `include_date` option here
153 json::Value ret(transaction_->getJson(
155 binary));
156
157 // NOTE Binary STTx::getJson output might not be a JSON object
158 if (ret.isObject() && (ledgerIndex_ != 0u))
159 {
161 {
162 // Behaviour before API version 2
163 ret[jss::inLedger] = ledgerIndex_;
164 }
165
166 // TODO: disable_API_prior_V3 to disable output of both `date` and
167 // `ledger_index` elements (taking precedence over include_date)
168 ret[jss::ledger_index] = ledgerIndex_;
169
171 {
172 auto ct = app_.getLedgerMaster().getCloseTimeBySeq(ledgerIndex_);
173 if (ct)
174 ret[jss::date] = ct->time_since_epoch().count();
175 }
176
177 // compute outgoing CTID
178 // override local network id if it's explicitly in the txn
180 if (transaction_->isFieldPresent(sfNetworkID))
181 netID = transaction_->getFieldU32(sfNetworkID);
182
183 if (txnSeq_ && netID)
184 {
186 if (ctid)
187 ret[jss::ctid] = *ctid;
188 }
189 }
190
191 return ret;
192}
193
194} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
bool isObject() const
virtual std::variant< AccountTx, TxSearched > getTransaction(uint256 const &id, std::optional< ClosedInterval< uint32_t > > const &range, ErrorCodeI &ec)=0
getTransaction Returns the transaction with the given hash.
virtual RelationalDatabase & getRelationalDatabase()=0
TransStatus status_
json::Value getJson(JsonOptions options, bool binary=false) const
static std::variant< std::pair< std::shared_ptr< Transaction >, std::shared_ptr< TxMeta > >, TxSearched > load(uint256 const &id, Application &app, ErrorCodeI &ec)
LedgerIndex ledgerIndex_
void setStatus(TransStatus status, std::uint32_t ledgerSeq, std::optional< uint32_t > transactionSeq=std::nullopt, std::optional< uint32_t > networkID=std::nullopt)
beast::Journal j_
std::shared_ptr< STTx const > transaction_
std::optional< uint32_t > txnSeq_
static Transaction::pointer transactionFromSQL(boost::optional< std::uint64_t > const &ledgerSeq, boost::optional< std::string > const &status, Blob const &rawTxn, Application &app)
std::optional< uint32_t > networkID_
Application & app_
Transaction(std::shared_ptr< STTx const > const &, std::string &, Application &) noexcept
std::shared_ptr< Transaction > pointer
Definition Transaction.h:44
static TransStatus sqlTransactionStatus(boost::optional< std::string > const &status)
T make_shared(T... args)
std::optional< std::string > encodeCTID(uint32_t ledgerSeq, uint32_t txnIndex, uint32_t networkID) noexcept
Encodes ledger sequence, transaction index, and network ID into a CTID string.
Definition CTID.h:31
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
ErrorCodeI
Definition ErrorCodes.h:22
TransStatus
Definition Transaction.h:26
ClosedInterval< T > range(T low, T high)
Create a closed range interval.
Definition RangeSet.h:34
T rangeCheckedCast(C c)
constexpr std::enable_if_t< std::is_integral_v< Dest > &&std::is_integral_v< Src >, Dest > safeCast(Src s) noexcept
Definition safe_cast.h:21
TxnSql
Definition STTx.h:18
@ Validated
Definition STTx.h:22
@ Unknown
Definition STTx.h:24
@ Included
Definition STTx.h:23
@ Conflict
Definition STTx.h:20
boost::icl::closed_interval< T > ClosedInterval
A closed interval over the domain T.
Definition RangeSet.h:25
TxSearched
Definition TxSearched.h:5
std::vector< unsigned char > Blob
Storage for linear binary data.
Definition Blob.h:10
BaseUInt< 256 > uint256
Definition base_uint.h:562
std::enable_if_t< std::is_same_v< T, char >||std::is_same_v< T, unsigned char >, Slice > makeSlice(std::array< T, N > const &a)
Definition Slice.h:215
Note, should be treated as flags that can be | and &.
Definition STBase.h:17
unsigned int underlying_t
Definition STBase.h:18
T what(T... args)