rippled
Loading...
Searching...
No Matches
Transaction.h
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#ifndef RIPPLE_APP_MISC_TRANSACTION_H_INCLUDED
21#define RIPPLE_APP_MISC_TRANSACTION_H_INCLUDED
22
23#include <xrpl/basics/RangeSet.h>
24#include <xrpl/beast/utility/Journal.h>
25#include <xrpl/protocol/ErrorCodes.h>
26#include <xrpl/protocol/Protocol.h>
27#include <xrpl/protocol/STBase.h>
28#include <xrpl/protocol/STTx.h>
29#include <xrpl/protocol/TER.h>
30#include <xrpl/protocol/TxMeta.h>
31
32#include <optional>
33#include <variant>
34
35namespace ripple {
36
37//
38// Transactions should be constructed in JSON with. Use STObject::parseJson to
39// obtain a binary version.
40//
41
42class Application;
43class Database;
44class Rules;
45
47 NEW = 0, // just received / generated
48 INVALID = 1, // no valid signature, insufficient funds
49 INCLUDED = 2, // added to the current ledger
50 CONFLICTED = 3, // losing to a conflicting transaction
51 COMMITTED = 4, // known to be in a ledger
52 HELD = 5, // not valid now, maybe later
53 REMOVED = 6, // taken out of a ledger
54 OBSOLETE = 7, // a compatible transaction has taken precedence
55 INCOMPLETE = 8 // needs more signatures
56};
57
58enum class TxSearched { all, some, unknown };
59
60// This class is for constructing and examining transactions.
61// Transactions are static so manipulation functions are unnecessary.
62class Transaction : public std::enable_shared_from_this<Transaction>,
63 public CountedObject<Transaction>
64{
65public:
67 using ref = const pointer&;
68
72 Application&) noexcept;
73
74 // The two boost::optional parameters are because SOCI requires
75 // boost::optional (not std::optional) parameters.
78 boost::optional<std::uint64_t> const& ledgerSeq,
79 boost::optional<std::string> const& status,
80 Blob const& rawTxn,
81 Application& app);
82
83 // The boost::optional parameter is because SOCI requires
84 // boost::optional (not std::optional) parameters.
85 static TransStatus
86 sqlTransactionStatus(boost::optional<std::string> const& status);
87
90 {
91 return mTransaction;
92 }
93
94 uint256 const&
95 getID() const
96 {
97 return mTransactionID;
98 }
99
101 getLedger() const
102 {
103 return mLedgerIndex;
104 }
105
106 bool
108 {
109 return mLedgerIndex != 0;
110 }
111
113 getStatus() const
114 {
115 return mStatus;
116 }
117
118 TER
120 {
121 return mResult;
122 }
123
124 void
125 setResult(TER terResult)
126 {
127 mResult = terResult;
128 }
129
130 void
131 setStatus(TransStatus status, std::uint32_t ledgerSeq);
132
133 void
135 {
136 mStatus = status;
137 }
138
139 void
141 {
142 mLedgerIndex = ledger;
143 }
144
148 void
150 {
151 mApplying = true;
152 }
153
159 bool
161 {
162 return mApplying;
163 }
164
168 void
170 {
171 mApplying = false;
172 }
173
175 {
179 void
181 {
182 applied = false;
183 broadcast = false;
184 queued = false;
185 kept = false;
186 }
187
192 bool
193 any() const
194 {
195 return applied || broadcast || queued || kept;
196 }
197
198 bool applied = false;
199 bool broadcast = false;
200 bool queued = false;
201 bool kept = false;
202 };
203
210 {
211 return submitResult_;
212 }
213
217 void
219 {
221 }
222
226 void
228 {
229 submitResult_.applied = true;
230 }
231
235 void
237 {
238 submitResult_.queued = true;
239 }
240
244 void
246 {
248 }
249
253 void
255 {
256 submitResult_.kept = true;
257 }
258
260 {
262
264 LedgerIndex li,
265 XRPAmount fee,
266 std::uint32_t accSeqNext,
267 std::uint32_t accSeqAvail)
268 : validatedLedger{li}
269 , minFeeRequired{fee}
270 , accountSeqNext{accSeqNext}
271 , accountSeqAvail{accSeqAvail}
272 {
273 }
274
279 };
280
287 {
288 return currentLedgerState_;
289 }
290
298 void
300 LedgerIndex validatedLedger,
301 XRPAmount fee,
302 std::uint32_t accountSeq,
303 std::uint32_t availableSeq)
304 {
305 currentLedgerState_.emplace(
306 validatedLedger, fee, accountSeq, availableSeq);
307 }
308
310 getJson(JsonOptions options, bool binary = false) const;
311
312 // Information used to locate a transaction.
313 // Contains a nodestore hash and ledger sequence pair if the transaction was
314 // found. Otherwise, contains the range of ledgers present in the database
315 // at the time of search.
316 struct Locator
317 {
320
321 // @return true if transaction was found, false otherwise
322 //
323 // Call this function first to determine the type of the contained info.
324 // Calling the wrong getter function will throw an exception.
325 // See documentation for the getter functions for more details
326 bool
328 {
329 return std::holds_alternative<std::pair<uint256, uint32_t>>(
330 locator);
331 }
332
333 // @return key used to find transaction in nodestore
334 //
335 // Throws if isFound() returns false
336 uint256 const&
338 {
339 return std::get<std::pair<uint256, uint32_t>>(locator).first;
340 }
341
342 // @return sequence of ledger containing the transaction
343 //
344 // Throws is isFound() returns false
345 uint32_t
347 {
348 return std::get<std::pair<uint256, uint32_t>>(locator).second;
349 }
350
351 // @return range of ledgers searched
352 //
353 // Throws if isFound() returns true
356 {
357 return std::get<ClosedInterval<uint32_t>>(locator);
358 }
359 };
360
361 static Locator
362 locate(uint256 const& id, Application& app);
363
364 static std::variant<
367 load(uint256 const& id, Application& app, error_code_i& ec);
368
369 static std::variant<
372 load(
373 uint256 const& id,
374 Application& app,
376 error_code_i& ec);
377
378private:
379 static std::variant<
382 load(
383 uint256 const& id,
384 Application& app,
386 error_code_i& ec);
387
389
393 bool mApplying = false;
394
397
399
403};
404
405} // namespace ripple
406
407#endif
Represents a JSON value.
Definition: json_value.h:147
A generic endpoint for log messages.
Definition: Journal.h:59
Tracks the number of instances of an object.
void setCurrentLedgerState(LedgerIndex validatedLedger, XRPAmount fee, std::uint32_t accountSeq, std::uint32_t availableSeq)
setCurrentLedgerState Set current ledger state of transaction
Definition: Transaction.h:299
std::optional< CurrentLedgerState > currentLedgerState_
Definition: Transaction.h:398
static Locator locate(uint256 const &id, Application &app)
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
void setBroadcast()
setBroadcast Set this flag once was broadcasted via network
Definition: Transaction.h:245
void setStatus(TransStatus status)
Definition: Transaction.h:134
void clearSubmitResult()
clearSubmitResult Clear all flags in SubmitResult
Definition: Transaction.h:218
bool getApplying()
Detect if transaction is being batched.
Definition: Transaction.h:160
void setQueued()
setQueued Set this flag once was put into heldtxns queue
Definition: Transaction.h:236
LedgerIndex mLedgerIndex
Definition: Transaction.h:390
void setApplied()
setApplied Set this flag once was applied to open ledger
Definition: Transaction.h:227
static std::variant< std::pair< std::shared_ptr< Transaction >, std::shared_ptr< TxMeta > >, TxSearched > load(uint256 const &id, Application &app, error_code_i &ec)
TransStatus getStatus() const
Definition: Transaction.h:113
LedgerIndex getLedger() const
Definition: Transaction.h:101
void setApplying()
Set this flag once added to a batch.
Definition: Transaction.h:149
void setKept()
setKept Set this flag once was put to localtxns queue
Definition: Transaction.h:254
std::shared_ptr< STTx const > const & getSTransaction()
Definition: Transaction.h:89
void setLedger(LedgerIndex ledger)
Definition: Transaction.h:140
Application & mApp
Definition: Transaction.h:401
SubmitResult submitResult_
different ways for transaction to be accepted
Definition: Transaction.h:396
static TransStatus sqlTransactionStatus(boost::optional< std::string > const &status)
Definition: Transaction.cpp:67
bool isValidated() const
Definition: Transaction.h:107
void clearApplying()
Indicate that transaction application has been attempted.
Definition: Transaction.h:169
Json::Value getJson(JsonOptions options, bool binary=false) const
uint256 const & getID() const
Definition: Transaction.h:95
TransStatus mStatus
Definition: Transaction.h:391
std::shared_ptr< STTx const > mTransaction
Definition: Transaction.h:400
void setResult(TER terResult)
Definition: Transaction.h:125
std::optional< CurrentLedgerState > getCurrentLedgerState() const
getCurrentLedgerState Get current ledger state of transaction
Definition: Transaction.h:286
void setStatus(TransStatus status, std::uint32_t ledgerSeq)
Definition: Transaction.cpp:60
SubmitResult getSubmitResult() const
getSubmitResult Return submit result
Definition: Transaction.h:209
beast::Journal j_
Definition: Transaction.h:402
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
@ INCOMPLETE
Definition: Transaction.h:55
@ OBSOLETE
Definition: Transaction.h:54
@ COMMITTED
Definition: Transaction.h:51
@ REMOVED
Definition: Transaction.h:53
@ 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
ClosedInterval< T > range(T low, T high)
Create a closed range interval.
Definition: RangeSet.h:54
@ temUNCERTAIN
Definition: TER.h:123
Note, should be treated as flags that can be | and &.
Definition: STBase.h:36
CurrentLedgerState(LedgerIndex li, XRPAmount fee, std::uint32_t accSeqNext, std::uint32_t accSeqAvail)
Definition: Transaction.h:263
uint256 const & getNodestoreHash()
Definition: Transaction.h:337
std::variant< std::pair< uint256, uint32_t >, ClosedInterval< uint32_t > > locator
Definition: Transaction.h:319
ClosedInterval< uint32_t > const & getLedgerRangeSearched()
Definition: Transaction.h:355
bool any() const
any Get true of any state is true
Definition: Transaction.h:193
void clear()
clear Clear all states
Definition: Transaction.h:180