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
27#include <xrpl/basics/safe_cast.h>
28#include <xrpl/protocol/ErrorCodes.h>
29#include <xrpl/protocol/jss.h>
30
31namespace ripple {
32
35 std::string& reason,
36 Application& app) noexcept
37 : mTransaction(stx), mApp(app), j_(app.journal("Ledger"))
38{
39 try
40 {
41 mTransactionID = mTransaction->getTransactionID();
42 }
43 catch (std::exception& e)
44 {
45 reason = e.what();
46 return;
47 }
48
49 mStatus = NEW;
50}
51
52//
53// Misc.
54//
55
56void
58{
59 mStatus = ts;
60 mLedgerIndex = lseq;
61}
62
64Transaction::sqlTransactionStatus(boost::optional<std::string> const& status)
65{
66 char const c = (status) ? (*status)[0] : safe_cast<char>(txnSqlUnknown);
67
68 switch (c)
69 {
70 case txnSqlNew:
71 return NEW;
72 case txnSqlConflict:
73 return CONFLICTED;
74 case txnSqlHeld:
75 return HELD;
76 case txnSqlValidated:
77 return COMMITTED;
78 case txnSqlIncluded:
79 return INCLUDED;
80 }
81
82 XRPL_ASSERT(
83 c == txnSqlUnknown,
84 "ripple::Transaction::sqlTransactionStatus : unknown transaction "
85 "status");
86 return INVALID;
87}
88
91 boost::optional<std::uint64_t> const& ledgerSeq,
92 boost::optional<std::string> const& status,
93 Blob const& rawTxn,
94 Application& app)
95{
96 std::uint32_t const inLedger =
97 rangeCheckedCast<std::uint32_t>(ledgerSeq.value_or(0));
98
99 SerialIter it(makeSlice(rawTxn));
100 auto txn = std::make_shared<STTx const>(it);
101 std::string reason;
102 auto tr = std::make_shared<Transaction>(txn, reason, app);
103
104 tr->setStatus(sqlTransactionStatus(status));
105 tr->setLedger(inLedger);
106 return tr;
107}
108
113{
114 return load(id, app, std::nullopt, ec);
115}
116
121 uint256 const& id,
122 Application& app,
124 error_code_i& ec)
125{
127
128 return load(id, app, op{range}, ec);
129}
130
135 uint256 const& id,
136 Application& app,
138 error_code_i& ec)
139{
140 auto const db = dynamic_cast<SQLiteDatabase*>(&app.getRelationalDatabase());
141
142 if (!db)
143 {
144 Throw<std::runtime_error>("Failed to get relational database");
145 }
146
147 return db->getTransaction(id, range, ec);
148}
149
150// options 1 to include the date of the transaction
152Transaction::getJson(JsonOptions options, bool binary) const
153{
154 // Note, we explicitly suppress `include_date` option here
155 Json::Value ret(
156 mTransaction->getJson(options & ~JsonOptions::include_date, binary));
157
158 // NOTE Binary STTx::getJson output might not be a JSON object
159 if (ret.isObject() && mLedgerIndex)
160 {
161 if (!(options & JsonOptions::disable_API_prior_V2))
162 {
163 // Behaviour before API version 2
164 ret[jss::inLedger] = mLedgerIndex;
165 }
166
167 // TODO: disable_API_prior_V3 to disable output of both `date` and
168 // `ledger_index` elements (taking precedence over include_date)
169 ret[jss::ledger_index] = mLedgerIndex;
170
171 if (options & JsonOptions::include_date)
172 {
174 if (ct)
175 ret[jss::date] = ct->time_since_epoch().count();
176 }
177 }
178
179 return ret;
180}
181
182} // 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:90
Transaction(std::shared_ptr< STTx const > const &, std::string &, Application &) noexcept
Definition: Transaction.cpp:33
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:64
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:57
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)