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 = pointer const&;
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
132 TransStatus status,
133 std::uint32_t ledgerSeq,
134 std::optional<uint32_t> transactionSeq = std::nullopt,
136
137 void
139 {
140 mStatus = status;
141 }
142
143 void
145 {
146 mLedgerIndex = ledger;
147 }
148
152 void
154 {
155 // Note that all access to mApplying are made by NetworkOPsImp, and must
156 // be done under that class's lock.
157 mApplying = true;
158 }
159
165 bool
167 {
168 // Note that all access to mApplying are made by NetworkOPsImp, and must
169 // be done under that class's lock.
170 return mApplying;
171 }
172
176 void
178 {
179 // Note that all access to mApplying are made by NetworkOPsImp, and must
180 // be done under that class's lock.
181 mApplying = false;
182 }
183
185 {
189 void
191 {
192 applied = false;
193 broadcast = false;
194 queued = false;
195 kept = false;
196 }
197
202 bool
203 any() const
204 {
205 return applied || broadcast || queued || kept;
206 }
207
208 bool applied = false;
209 bool broadcast = false;
210 bool queued = false;
211 bool kept = false;
212 };
213
220 {
221 return submitResult_;
222 }
223
227 void
232
236 void
238 {
239 submitResult_.applied = true;
240 }
241
245 void
247 {
248 submitResult_.queued = true;
249 }
250
254 void
256 {
258 }
259
263 void
265 {
266 submitResult_.kept = true;
267 }
268
290
297 {
298 return currentLedgerState_;
299 }
300
308 void
310 LedgerIndex validatedLedger,
311 XRPAmount fee,
312 std::uint32_t accountSeq,
313 std::uint32_t availableSeq)
314 {
315 currentLedgerState_.emplace(
316 validatedLedger, fee, accountSeq, availableSeq);
317 }
318
320 getJson(JsonOptions options, bool binary = false) const;
321
322 // Information used to locate a transaction.
323 // Contains a nodestore hash and ledger sequence pair if the transaction was
324 // found. Otherwise, contains the range of ledgers present in the database
325 // at the time of search.
326 struct Locator
327 {
330
331 // @return true if transaction was found, false otherwise
332 //
333 // Call this function first to determine the type of the contained info.
334 // Calling the wrong getter function will throw an exception.
335 // See documentation for the getter functions for more details
336 bool
342
343 // @return key used to find transaction in nodestore
344 //
345 // Throws if isFound() returns false
346 uint256 const&
351
352 // @return sequence of ledger containing the transaction
353 //
354 // Throws is isFound() returns false
355 uint32_t
360
361 // @return range of ledgers searched
362 //
363 // Throws if isFound() returns true
369 };
370
371 static Locator
372 locate(uint256 const& id, Application& app);
373
374 static std::variant<
377 load(uint256 const& id, Application& app, error_code_i& ec);
378
379 static std::variant<
382 load(
383 uint256 const& id,
384 Application& app,
386 error_code_i& ec);
387
388private:
389 static std::variant<
392 load(
393 uint256 const& id,
394 Application& app,
396 error_code_i& ec);
397
399
405 /* Note that all access to mApplying are made by NetworkOPsImp,
406 and must be done under that class's lock. This avoids the overhead of
407 taking a separate lock, and the consequences of a race condition are
408 nearly-zero.
409
410 1. If there is a race condition, and getApplying returns false when it
411 should be true, the transaction will be processed again. Not that
412 big a deal if it's a rare one-off. Most of the time, it'll get
413 tefALREADY or tefPAST_SEQ.
414 2. On the flip side, if it returns true, when it should be false, then
415 the transaction must have been attempted recently, so no big deal if
416 it doesn't immediately get tried right away.
417 3. If there's a race between setApplying and clearApplying, and the
418 flag ends up set, then a batch is about to try to process the
419 transaction and will call clearApplying later. If it ends up
420 cleared, then it might get attempted again later as is the case with
421 item 1.
422 */
423 bool mApplying = false;
424
427
429
433};
434
435} // namespace ripple
436
437#endif
Represents a JSON value.
Definition json_value.h:149
A generic endpoint for log messages.
Definition Journal.h:60
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
std::optional< CurrentLedgerState > currentLedgerState_
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)
void setBroadcast()
setBroadcast Set this flag once was broadcasted via network
void setStatus(TransStatus status)
void clearSubmitResult()
clearSubmitResult Clear all flags in SubmitResult
bool getApplying()
Detect if transaction is being batched.
std::optional< uint32_t > mNetworkID
std::optional< uint32_t > mTxnSeq
void setQueued()
setQueued Set this flag once was put into heldtxns queue
LedgerIndex mLedgerIndex
pointer const & ref
Definition Transaction.h:67
void setApplied()
setApplied Set this flag once was applied to open ledger
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
LedgerIndex getLedger() const
void setApplying()
Set this flag once added to a batch.
void setKept()
setKept Set this flag once was put to localtxns queue
std::shared_ptr< STTx const > const & getSTransaction()
Definition Transaction.h:89
void setLedger(LedgerIndex ledger)
Application & mApp
SubmitResult submitResult_
different ways for transaction to be accepted
void setStatus(TransStatus status, std::uint32_t ledgerSeq, std::optional< uint32_t > transactionSeq=std::nullopt, std::optional< uint32_t > networkID=std::nullopt)
static TransStatus sqlTransactionStatus(boost::optional< std::string > const &status)
bool isValidated() const
void clearApplying()
Indicate that transaction application has been attempted.
Json::Value getJson(JsonOptions options, bool binary=false) const
uint256 const & getID() const
Definition Transaction.h:95
TransStatus mStatus
std::shared_ptr< STTx const > mTransaction
void setResult(TER terResult)
std::optional< CurrentLedgerState > getCurrentLedgerState() const
getCurrentLedgerState Get current ledger state of transaction
SubmitResult getSubmitResult() const
getSubmitResult Return submit result
beast::Journal j_
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
@ CONFLICTED
Definition Transaction.h:50
@ INCOMPLETE
Definition Transaction.h:55
@ COMMITTED
Definition Transaction.h:51
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:37
CurrentLedgerState(LedgerIndex li, XRPAmount fee, std::uint32_t accSeqNext, std::uint32_t accSeqAvail)
uint256 const & getNodestoreHash()
std::variant< std::pair< uint256, uint32_t >, ClosedInterval< uint32_t > > locator
ClosedInterval< uint32_t > const & getLedgerRangeSearched()
bool any() const
any Get true of any state is true
void clear()
clear Clear all states