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