rippled
Loading...
Searching...
No Matches
Transaction.h
1#pragma once
2
3#include <xrpl/basics/RangeSet.h>
4#include <xrpl/beast/utility/Journal.h>
5#include <xrpl/protocol/ErrorCodes.h>
6#include <xrpl/protocol/Protocol.h>
7#include <xrpl/protocol/STBase.h>
8#include <xrpl/protocol/STTx.h>
9#include <xrpl/protocol/TER.h>
10#include <xrpl/protocol/TxMeta.h>
11#include <xrpl/protocol/TxSearched.h>
12
13#include <optional>
14#include <variant>
15
16namespace xrpl {
17
18//
19// Transactions should be constructed in JSON with. Use STObject::parseJson to
20// obtain a binary version.
21//
22
23class Application;
24class Database;
25class Rules;
26
28 NEW = 0, // just received / generated
29 INVALID = 1, // no valid signature, insufficient funds
30 INCLUDED = 2, // added to the current ledger
31 CONFLICTED = 3, // losing to a conflicting transaction
32 COMMITTED = 4, // known to be in a ledger
33 HELD = 5, // not valid now, maybe later
34 REMOVED = 6, // taken out of a ledger
35 OBSOLETE = 7, // a compatible transaction has taken precedence
36 INCOMPLETE = 8 // needs more signatures
37};
38
39// This class is for constructing and examining transactions.
40// Transactions are static so manipulation functions are unnecessary.
41class Transaction : public std::enable_shared_from_this<Transaction>, public CountedObject<Transaction>
42{
43public:
45 using ref = pointer const&;
46
48
49 // The two boost::optional parameters are because SOCI requires
50 // boost::optional (not std::optional) parameters.
53 boost::optional<std::uint64_t> const& ledgerSeq,
54 boost::optional<std::string> const& status,
55 Blob const& rawTxn,
56 Application& app);
57
58 // The boost::optional parameter is because SOCI requires
59 // boost::optional (not std::optional) parameters.
60 static TransStatus
61 sqlTransactionStatus(boost::optional<std::string> const& status);
62
65 {
66 return mTransaction;
67 }
68
69 uint256 const&
70 getID() const
71 {
72 return mTransactionID;
73 }
74
76 getLedger() const
77 {
78 return mLedgerIndex;
79 }
80
81 bool
83 {
84 return mLedgerIndex != 0;
85 }
86
88 getStatus() const
89 {
90 return mStatus;
91 }
92
93 TER
95 {
96 return mResult;
97 }
98
99 void
100 setResult(TER terResult)
101 {
102 mResult = terResult;
103 }
104
105 void
107 TransStatus status,
108 std::uint32_t ledgerSeq,
109 std::optional<uint32_t> transactionSeq = std::nullopt,
111
112 void
114 {
115 mStatus = status;
116 }
117
118 void
120 {
121 mLedgerIndex = ledger;
122 }
123
127 void
129 {
130 // Note that all access to mApplying are made by NetworkOPsImp, and must
131 // be done under that class's lock.
132 mApplying = true;
133 }
134
140 bool
142 {
143 // Note that all access to mApplying are made by NetworkOPsImp, and must
144 // be done under that class's lock.
145 return mApplying;
146 }
147
151 void
153 {
154 // Note that all access to mApplying are made by NetworkOPsImp, and must
155 // be done under that class's lock.
156 mApplying = false;
157 }
158
160 {
164 void
166 {
167 applied = false;
168 broadcast = false;
169 queued = false;
170 kept = false;
171 }
172
177 bool
178 any() const
179 {
180 return applied || broadcast || queued || kept;
181 }
182
183 bool applied = false;
184 bool broadcast = false;
185 bool queued = false;
186 bool kept = false;
187 };
188
195 {
196 return submitResult_;
197 }
198
202 void
207
211 void
213 {
214 submitResult_.applied = true;
215 }
216
220 void
222 {
223 submitResult_.queued = true;
224 }
225
229 void
231 {
233 }
234
238 void
240 {
241 submitResult_.kept = true;
242 }
243
258
265 {
266 return currentLedgerState_;
267 }
268
276 void
278 LedgerIndex validatedLedger,
279 XRPAmount fee,
280 std::uint32_t accountSeq,
281 std::uint32_t availableSeq)
282 {
283 currentLedgerState_.emplace(validatedLedger, fee, accountSeq, availableSeq);
284 }
285
287 getJson(JsonOptions options, bool binary = false) const;
288
289 // Information used to locate a transaction.
290 // Contains a nodestore hash and ledger sequence pair if the transaction was
291 // found. Otherwise, contains the range of ledgers present in the database
292 // at the time of search.
293 struct Locator
294 {
296
297 // @return true if transaction was found, false otherwise
298 //
299 // Call this function first to determine the type of the contained info.
300 // Calling the wrong getter function will throw an exception.
301 // See documentation for the getter functions for more details
302 bool
307
308 // @return key used to find transaction in nodestore
309 //
310 // Throws if isFound() returns false
311 uint256 const&
316
317 // @return sequence of ledger containing the transaction
318 //
319 // Throws is isFound() returns false
320 uint32_t
325
326 // @return range of ledgers searched
327 //
328 // Throws if isFound() returns true
334 };
335
336 static Locator
337 locate(uint256 const& id, Application& app);
338
340 load(uint256 const& id, Application& app, error_code_i& ec);
341
344
345private:
348
350
356 /* Note that all access to mApplying are made by NetworkOPsImp,
357 and must be done under that class's lock. This avoids the overhead of
358 taking a separate lock, and the consequences of a race condition are
359 nearly-zero.
360
361 1. If there is a race condition, and getApplying returns false when it
362 should be true, the transaction will be processed again. Not that
363 big a deal if it's a rare one-off. Most of the time, it'll get
364 tefALREADY or tefPAST_SEQ.
365 2. On the flip side, if it returns true, when it should be false, then
366 the transaction must have been attempted recently, so no big deal if
367 it doesn't immediately get tried right away.
368 3. If there's a race between setApplying and clearApplying, and the
369 flag ends up set, then a batch is about to try to process the
370 transaction and will call clearApplying later. If it ends up
371 cleared, then it might get attempted again later as is the case with
372 item 1.
373 */
374 bool mApplying = false;
375
378
380
384};
385
386} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
A generic endpoint for log messages.
Definition Journal.h:40
Tracks the number of instances of an object.
std::shared_ptr< STTx const > mTransaction
void setApplying()
Set this flag once added to a batch.
Application & mApp
std::optional< uint32_t > mTxnSeq
std::optional< CurrentLedgerState > getCurrentLedgerState() const
getCurrentLedgerState Get current ledger state of transaction
TransStatus mStatus
void setKept()
setKept Set this flag once was put to localtxns queue
LedgerIndex mLedgerIndex
LedgerIndex getLedger() const
Definition Transaction.h:76
void setQueued()
setQueued Set this flag once was put into held-txns queue
void clearSubmitResult()
clearSubmitResult Clear all flags in SubmitResult
void clearApplying()
Indicate that transaction application has been attempted.
void setApplied()
setApplied Set this flag once was applied to open ledger
void setStatus(TransStatus status, std::uint32_t ledgerSeq, std::optional< uint32_t > transactionSeq=std::nullopt, std::optional< uint32_t > networkID=std::nullopt)
SubmitResult getSubmitResult() const
getSubmitResult Return submit result
std::optional< uint32_t > mNetworkID
beast::Journal j_
SubmitResult submitResult_
different ways for transaction to be accepted
TransStatus getStatus() const
Definition Transaction.h:88
void setCurrentLedgerState(LedgerIndex validatedLedger, XRPAmount fee, std::uint32_t accountSeq, std::uint32_t availableSeq)
setCurrentLedgerState Set current ledger state of transaction
void setResult(TER terResult)
static Transaction::pointer transactionFromSQL(boost::optional< std::uint64_t > const &ledgerSeq, boost::optional< std::string > const &status, Blob const &rawTxn, Application &app)
std::optional< CurrentLedgerState > currentLedgerState_
void setStatus(TransStatus status)
void setBroadcast()
setBroadcast Set this flag once was broadcasted via network
uint256 const & getID() const
Definition Transaction.h:70
void setLedger(LedgerIndex 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)
Json::Value getJson(JsonOptions options, bool binary=false) const
static Locator locate(uint256 const &id, Application &app)
std::shared_ptr< STTx const > const & getSTransaction()
Definition Transaction.h:64
static TransStatus sqlTransactionStatus(boost::optional< std::string > const &status)
bool isValidated() const
Definition Transaction.h:82
pointer const & ref
Definition Transaction.h:45
bool getApplying()
Detect if transaction is being batched.
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
TransStatus
Definition Transaction.h:27
@ REMOVED
Definition Transaction.h:34
@ INVALID
Definition Transaction.h:29
@ OBSOLETE
Definition Transaction.h:35
@ COMMITTED
Definition Transaction.h:32
@ CONFLICTED
Definition Transaction.h:31
@ INCOMPLETE
Definition Transaction.h:36
@ INCLUDED
Definition Transaction.h:30
ClosedInterval< T > range(T low, T high)
Create a closed range interval.
Definition RangeSet.h:34
boost::icl::closed_interval< T > ClosedInterval
A closed interval over the domain T.
Definition RangeSet.h:25
TxSearched
Definition TxSearched.h:5
@ temUNCERTAIN
Definition TER.h:103
error_code_i
Definition ErrorCodes.h:20
Note, should be treated as flags that can be | and &.
Definition STBase.h:17
CurrentLedgerState(LedgerIndex li, XRPAmount fee, std::uint32_t accSeqNext, std::uint32_t accSeqAvail)
uint256 const & getNodestoreHash()
ClosedInterval< uint32_t > const & getLedgerRangeSearched()
std::variant< std::pair< uint256, uint32_t >, ClosedInterval< uint32_t > > locator
bool any() const
any Get true of any state is true
void clear()
clear Clear all states