rippled
Loading...
Searching...
No Matches
Transaction.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpld/app/ledger/LedgerMaster.h>
21#include <xrpld/app/main/Application.h>
22#include <xrpld/app/misc/HashRouter.h>
23#include <xrpld/app/misc/Transaction.h>
24#include <xrpld/app/rdb/backend/SQLiteDatabase.h>
25#include <xrpld/app/tx/apply.h>
26#include <xrpl/basics/safe_cast.h>
27#include <xrpl/protocol/ErrorCodes.h>
28#include <xrpl/protocol/jss.h>
29
30namespace ripple {
31
34 std::string& reason,
35 Application& app) noexcept
36 : mTransaction(stx), mApp(app), j_(app.journal("Ledger"))
37{
38 try
39 {
40 mTransactionID = mTransaction->getTransactionID();
41 }
42 catch (std::exception& e)
43 {
44 reason = e.what();
45 return;
46 }
47
48 mStatus = NEW;
49}
50
51//
52// Misc.
53//
54
55void
57{
58 mStatus = ts;
59 mLedgerIndex = lseq;
60}
61
63Transaction::sqlTransactionStatus(boost::optional<std::string> const& status)
64{
65 char const c = (status) ? (*status)[0] : safe_cast<char>(txnSqlUnknown);
66
67 switch (c)
68 {
69 case txnSqlNew:
70 return NEW;
71 case txnSqlConflict:
72 return CONFLICTED;
73 case txnSqlHeld:
74 return HELD;
75 case txnSqlValidated:
76 return COMMITTED;
77 case txnSqlIncluded:
78 return INCLUDED;
79 }
80
81 XRPL_ASSERT(
82 c == txnSqlUnknown,
83 "ripple::Transaction::sqlTransactionStatus : unknown transaction "
84 "status");
85 return INVALID;
86}
87
90 boost::optional<std::uint64_t> const& ledgerSeq,
91 boost::optional<std::string> const& status,
92 Blob const& rawTxn,
93 Application& app)
94{
95 std::uint32_t const inLedger =
96 rangeCheckedCast<std::uint32_t>(ledgerSeq.value_or(0));
97
98 SerialIter it(makeSlice(rawTxn));
99 auto txn = std::make_shared<STTx const>(it);
100 std::string reason;
101 auto tr = std::make_shared<Transaction>(txn, reason, app);
102
103 tr->setStatus(sqlTransactionStatus(status));
104 tr->setLedger(inLedger);
105 return tr;
106}
107
112{
113 return load(id, app, std::nullopt, ec);
114}
115
120 uint256 const& id,
121 Application& app,
123 error_code_i& ec)
124{
126
127 return load(id, app, op{range}, ec);
128}
129
134 uint256 const& id,
135 Application& app,
137 error_code_i& ec)
138{
139 auto const db = dynamic_cast<SQLiteDatabase*>(&app.getRelationalDatabase());
140
141 if (!db)
142 {
143 Throw<std::runtime_error>("Failed to get relational database");
144 }
145
146 return db->getTransaction(id, range, ec);
147}
148
149// options 1 to include the date of the transaction
151Transaction::getJson(JsonOptions options, bool binary) const
152{
153 // Note, we explicitly suppress `include_date` option here
154 Json::Value ret(
155 mTransaction->getJson(options & ~JsonOptions::include_date, binary));
156
157 // NOTE Binary STTx::getJson output might not be a JSON object
158 if (ret.isObject() && mLedgerIndex)
159 {
160 if (!(options & JsonOptions::disable_API_prior_V2))
161 {
162 // Behaviour before API version 2
163 ret[jss::inLedger] = mLedgerIndex;
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] = mLedgerIndex;
169
170 if (options & JsonOptions::include_date)
171 {
173 if (ct)
174 ret[jss::date] = ct->time_since_epoch().count();
175 }
176 }
177
178 return ret;
179}
180
181} // namespace ripple
Represents a JSON value.
Definition: json_value.h:148
bool isObject() const
virtual LedgerMaster & getLedgerMaster()=0
virtual RelationalDatabase & getRelationalDatabase()=0
std::optional< NetClock::time_point > getCloseTimeBySeq(LedgerIndex ledgerIndex)
static Transaction::pointer transactionFromSQL(boost::optional< std::uint64_t > const &ledgerSeq, boost::optional< std::string > const &status, Blob const &rawTxn, Application &app)
Definition: Transaction.cpp:89
Transaction(std::shared_ptr< STTx const > const &, std::string &, Application &) noexcept
Definition: Transaction.cpp:32
LedgerIndex mLedgerIndex
Definition: Transaction.h:390
static std::variant< std::pair< std::shared_ptr< Transaction >, std::shared_ptr< TxMeta > >, TxSearched > load(uint256 const &id, Application &app, error_code_i &ec)
Application & mApp
Definition: Transaction.h:401
static TransStatus sqlTransactionStatus(boost::optional< std::string > const &status)
Definition: Transaction.cpp:63
Json::Value getJson(JsonOptions options, bool binary=false) const
TransStatus mStatus
Definition: Transaction.h:391
std::shared_ptr< STTx const > mTransaction
Definition: Transaction.h:400
void setStatus(TransStatus status, std::uint32_t ledgerSeq)
Definition: Transaction.cpp:56
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
@ INCLUDED
Definition: Transaction.h:49
@ CONFLICTED
Definition: Transaction.h:50
@ COMMITTED
Definition: Transaction.h:51
@ INVALID
Definition: Transaction.h:48
error_code_i
Definition: ErrorCodes.h:40
boost::icl::closed_interval< T > ClosedInterval
A closed interval over the domain T.
Definition: RangeSet.h:45
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:244
@ txnSqlIncluded
Definition: STTx.h:43
@ txnSqlUnknown
Definition: STTx.h:44
@ txnSqlConflict
Definition: STTx.h:40
@ txnSqlHeld
Definition: STTx.h:41
@ txnSqlNew
Definition: STTx.h:39
@ txnSqlValidated
Definition: STTx.h:42
ClosedInterval< T > range(T low, T high)
Create a closed range interval.
Definition: RangeSet.h:54
Note, should be treated as flags that can be | and &.
Definition: STBase.h:38
T what(T... args)