rippled
Loading...
Searching...
No Matches
Transaction.cpp
1#include <xrpld/app/ledger/LedgerMaster.h>
2#include <xrpld/app/main/Application.h>
3#include <xrpld/app/misc/Transaction.h>
4#include <xrpld/rpc/CTID.h>
5
6#include <xrpl/basics/safe_cast.h>
7#include <xrpl/core/HashRouter.h>
8#include <xrpl/protocol/ErrorCodes.h>
9#include <xrpl/protocol/jss.h>
10#include <xrpl/rdb/RelationalDatabase.h>
11#include <xrpl/tx/apply.h>
12
13namespace xrpl {
14
17 std::string& reason,
18 Application& app) noexcept
19 : mTransaction(stx), mApp(app), j_(app.journal("Ledger"))
20{
21 try
22 {
23 mTransactionID = mTransaction->getTransactionID();
24 }
25 catch (std::exception& e)
26 {
27 reason = e.what();
28 return;
29 }
30
31 mStatus = NEW;
32}
33
34//
35// Misc.
36//
37
38void
40 TransStatus ts,
41 std::uint32_t lseq,
44{
45 mStatus = ts;
46 mLedgerIndex = lseq;
47 if (tseq)
48 mTxnSeq = tseq;
49 if (netID)
50 mNetworkID = netID;
51}
52
54Transaction::sqlTransactionStatus(boost::optional<std::string> const& status)
55{
56 char const c = (status) ? (*status)[0] : safe_cast<char>(txnSqlUnknown);
57
58 switch (c)
59 {
60 case txnSqlNew:
61 return NEW;
62 case txnSqlConflict:
63 return CONFLICTED;
64 case txnSqlHeld:
65 return HELD;
66 case txnSqlValidated:
67 return COMMITTED;
68 case txnSqlIncluded:
69 return INCLUDED;
70 }
71
72 XRPL_ASSERT(
73 c == txnSqlUnknown,
74 "xrpl::Transaction::sqlTransactionStatus : unknown transaction "
75 "status");
76 return INVALID;
77}
78
81 boost::optional<std::uint64_t> const& ledgerSeq,
82 boost::optional<std::string> const& status,
83 Blob const& rawTxn,
84 Application& app)
85{
86 std::uint32_t const inLedger = rangeCheckedCast<std::uint32_t>(ledgerSeq.value_or(0));
87
88 SerialIter it(makeSlice(rawTxn));
89 auto txn = std::make_shared<STTx const>(it);
90 std::string reason;
91 auto tr = std::make_shared<Transaction>(txn, reason, app);
92
93 tr->setStatus(sqlTransactionStatus(status));
94 tr->setLedger(inLedger);
95 return tr;
96}
97
100{
101 return load(id, app, std::nullopt, ec);
102}
103
106 uint256 const& id,
107 Application& app,
109 error_code_i& ec)
110{
112
113 return load(id, app, op{range}, ec);
114}
115
118 uint256 const& id,
119 Application& app,
121 error_code_i& ec)
122{
123 auto& db = app.getRelationalDatabase();
124
125 return db.getTransaction(id, range, ec);
126}
127
128// options 1 to include the date of the transaction
130Transaction::getJson(JsonOptions options, bool binary) const
131{
132 // Note, we explicitly suppress `include_date` option here
133 Json::Value ret(mTransaction->getJson(options & ~JsonOptions::include_date, binary));
134
135 // NOTE Binary STTx::getJson output might not be a JSON object
136 if (ret.isObject() && mLedgerIndex)
137 {
138 if (!(options & JsonOptions::disable_API_prior_V2))
139 {
140 // Behaviour before API version 2
141 ret[jss::inLedger] = mLedgerIndex;
142 }
143
144 // TODO: disable_API_prior_V3 to disable output of both `date` and
145 // `ledger_index` elements (taking precedence over include_date)
146 ret[jss::ledger_index] = mLedgerIndex;
147
148 if (options & JsonOptions::include_date)
149 {
151 if (ct)
152 ret[jss::date] = ct->time_since_epoch().count();
153 }
154
155 // compute outgoing CTID
156 // override local network id if it's explicitly in the txn
158 if (mTransaction->isFieldPresent(sfNetworkID))
159 netID = mTransaction->getFieldU32(sfNetworkID);
160
161 if (mTxnSeq && netID)
162 {
164 if (ctid)
165 ret[jss::ctid] = *ctid;
166 }
167 }
168
169 return ret;
170}
171
172} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
bool isObject() const
std::optional< NetClock::time_point > getCloseTimeBySeq(LedgerIndex ledgerIndex)
virtual std::variant< AccountTx, TxSearched > getTransaction(uint256 const &id, std::optional< ClosedInterval< uint32_t > > const &range, error_code_i &ec)=0
getTransaction Returns the transaction with the given hash.
virtual RelationalDatabase & getRelationalDatabase()=0
virtual LedgerMaster & getLedgerMaster()=0
std::shared_ptr< STTx const > mTransaction
Application & mApp
std::optional< uint32_t > mTxnSeq
TransStatus mStatus
LedgerIndex mLedgerIndex
void setStatus(TransStatus status, std::uint32_t ledgerSeq, std::optional< uint32_t > transactionSeq=std::nullopt, std::optional< uint32_t > networkID=std::nullopt)
std::optional< uint32_t > mNetworkID
static Transaction::pointer transactionFromSQL(boost::optional< std::uint64_t > const &ledgerSeq, boost::optional< std::string > const &status, Blob const &rawTxn, Application &app)
static std::variant< std::pair< std::shared_ptr< Transaction >, std::shared_ptr< TxMeta > >, TxSearched > load(uint256 const &id, Application &app, error_code_i &ec)
Json::Value getJson(JsonOptions options, bool binary=false) const
Transaction(std::shared_ptr< STTx const > const &, std::string &, Application &) noexcept
static TransStatus sqlTransactionStatus(boost::optional< std::string > const &status)
T is_same_v
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:33
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
TransStatus
Definition Transaction.h:27
@ INVALID
Definition Transaction.h:29
@ COMMITTED
Definition Transaction.h:32
@ CONFLICTED
Definition Transaction.h:31
@ INCLUDED
Definition Transaction.h:30
ClosedInterval< T > range(T low, T high)
Create a closed range interval.
Definition RangeSet.h:34
@ txnSqlNew
Definition STTx.h:19
@ txnSqlValidated
Definition STTx.h:22
@ txnSqlUnknown
Definition STTx.h:24
@ txnSqlConflict
Definition STTx.h:20
@ txnSqlHeld
Definition STTx.h:21
@ txnSqlIncluded
Definition STTx.h:23
boost::icl::closed_interval< T > ClosedInterval
A closed interval over the domain T.
Definition RangeSet.h:25
TxSearched
Definition TxSearched.h:5
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)
Definition Slice.h:215
error_code_i
Definition ErrorCodes.h:20
Note, should be treated as flags that can be | and &.
Definition STBase.h:17
T what(T... args)