xrpld
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 Rules;
25
26enum class TransStatus {
27 NEW = 0, // just received / generated
28 INVALID = 1, // no valid signature, insufficient funds
29 INCLUDED = 2, // added to the current ledger
30 CONFLICTED = 3, // losing to a conflicting transaction
31 COMMITTED = 4, // known to be in a ledger
32 HELD = 5, // not valid now, maybe later
33 REMOVED = 6, // taken out of a ledger
34 OBSOLETE = 7, // a compatible transaction has taken precedence
35 INCOMPLETE = 8 // needs more signatures
36};
37
38// This class is for constructing and examining transactions.
39// Transactions are static so manipulation functions are unnecessary.
40class Transaction : public std::enable_shared_from_this<Transaction>,
41 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 transaction_;
67 }
68
69 uint256 const&
70 getID() const
71 {
72 return transactionID_;
73 }
74
76 getLedger() const
77 {
78 return ledgerIndex_;
79 }
80
81 bool
83 {
84 return ledgerIndex_ != 0;
85 }
86
88 getStatus() const
89 {
90 return status_;
91 }
92
93 TER
95 {
96 return result_;
97 }
98
99 void
100 setResult(TER terResult)
101 {
102 result_ = terResult;
103 }
104
105 void
107 TransStatus status,
108 std::uint32_t ledgerSeq,
109 std::optional<uint32_t> transactionSeq = std::nullopt,
110 std::optional<uint32_t> networkID = std::nullopt);
111
112 void
114 {
115 status_ = status;
116 }
117
118 void
120 {
121 ledgerIndex_ = ledger;
122 }
123
127 void
129 {
130 // Note that all access to applying_ are made by NetworkOPsImp, and must
131 // be done under that class's lock.
132 applying_ = true;
133 }
134
140 bool
142 {
143 // Note that all access to applying_ are made by NetworkOPsImp, and must
144 // be done under that class's lock.
145 return applying_;
146 }
147
151 void
153 {
154 // Note that all access to applying_ are made by NetworkOPsImp, and must
155 // be done under that class's lock.
156 applying_ = false;
157 }
158
160 {
164 void
166 {
167 applied = false;
168 broadcast = false;
169 queued = false;
170 kept = false;
171 }
172
177 [[nodiscard]] 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
204 {
205 submitResult_.clear();
206 }
207
211 void
213 {
214 submitResult_.applied = true;
215 }
216
220 void
222 {
223 submitResult_.queued = true;
224 }
225
229 void
231 {
232 submitResult_.broadcast = true;
233 }
234
238 void
240 {
241 submitResult_.kept = true;
242 }
243
265
272 {
273 return currentLedgerState_;
274 }
275
283 void
285 LedgerIndex validatedLedger,
286 XRPAmount fee,
287 std::uint32_t accountSeq,
288 std::uint32_t availableSeq)
289 {
290 currentLedgerState_.emplace(validatedLedger, fee, accountSeq, availableSeq);
291 }
292
294 getJson(JsonOptions options, bool binary = false) const;
295
296 // Information used to locate a transaction.
297 // Contains a nodestore hash and ledger sequence pair if the transaction was
298 // found. Otherwise, contains the range of ledgers present in the database
299 // at the time of search.
300 struct Locator
301 {
303
304 // @return true if transaction was found, false otherwise
305 //
306 // Call this function first to determine the type of the contained info.
307 // Calling the wrong getter function will throw an exception.
308 // See documentation for the getter functions for more details
309 [[nodiscard]] bool
314
315 // @return key used to find transaction in nodestore
316 //
317 // Throws if isFound() returns false
318 uint256 const&
320 {
321 return std::get<std::pair<uint256, uint32_t>>(locator).first;
322 }
323
324 // @return sequence of ledger containing the transaction
325 //
326 // Throws is isFound() returns false
327 uint32_t
329 {
330 return std::get<std::pair<uint256, uint32_t>>(locator).second;
331 }
332
333 // @return range of ledgers searched
334 //
335 // Throws if isFound() returns true
338 {
339 return std::get<ClosedInterval<uint32_t>>(locator);
340 }
341 };
342
343 static Locator
344 locate(uint256 const& id, Application& app);
345
346 static std::
347 variant<std::pair<std::shared_ptr<Transaction>, std::shared_ptr<TxMeta>>, TxSearched>
348 load(uint256 const& id, Application& app, ErrorCodeI& ec);
349
350 static std::
351 variant<std::pair<std::shared_ptr<Transaction>, std::shared_ptr<TxMeta>>, TxSearched>
352 load(
353 uint256 const& id,
354 Application& app,
356 ErrorCodeI& ec);
357
358private:
359 static std::
360 variant<std::pair<std::shared_ptr<Transaction>, std::shared_ptr<TxMeta>>, TxSearched>
361 load(
362 uint256 const& id,
363 Application& app,
365 ErrorCodeI& ec);
366
368
374 /* Note that all access to applying_ are made by NetworkOPsImp,
375 and must be done under that class's lock. This avoids the overhead of
376 taking a separate lock, and the consequences of a race condition are
377 nearly-zero.
378
379 1. If there is a race condition, and getApplying returns false when it
380 should be true, the transaction will be processed again. Not that
381 big a deal if it's a rare one-off. Most of the time, it'll get
382 tefALREADY or tefPAST_SEQ.
383 2. On the flip side, if it returns true, when it should be false, then
384 the transaction must have been attempted recently, so no big deal if
385 it doesn't immediately get tried right away.
386 3. If there's a race between setApplying and clearApplying, and the
387 flag ends up set, then a batch is about to try to process the
388 transaction and will call clearApplying later. If it ends up
389 cleared, then it might get attempted again later as is the case with
390 item 1.
391 */
392 bool applying_ = false;
393
396
398
402};
403
404} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:38
Represents a JSON value.
Definition json_value.h:130
Rules controlling protocol behavior.
Definition Rules.h:33
TransStatus status_
void setApplying()
Set this flag once added to a batch.
std::optional< CurrentLedgerState > getCurrentLedgerState() const
getCurrentLedgerState Get current ledger state of transaction
json::Value getJson(JsonOptions options, bool binary=false) const
static std::variant< std::pair< std::shared_ptr< Transaction >, std::shared_ptr< TxMeta > >, TxSearched > load(uint256 const &id, Application &app, ErrorCodeI &ec)
void setKept()
setKept Set this flag once was put to localtxns queue
LedgerIndex getLedger() const
Definition Transaction.h:76
LedgerIndex ledgerIndex_
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
beast::Journal j_
bool getApplying() const
Detect if transaction is being batched.
SubmitResult submitResult_
different ways for transaction to be accepted
std::shared_ptr< STTx const > transaction_
TransStatus getStatus() const
Definition Transaction.h:88
std::optional< uint32_t > txnSeq_
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 Locator locate(uint256 const &id, Application &app)
std::optional< uint32_t > networkID_
Application & app_
Transaction(std::shared_ptr< STTx const > const &, std::string &, Application &) noexcept
std::shared_ptr< Transaction > pointer
Definition Transaction.h:44
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
T holds_alternative(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
ErrorCodeI
Definition ErrorCodes.h:22
std::uint32_t LedgerIndex
A ledger index.
Definition Protocol.h:259
TransStatus
Definition Transaction.h:26
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:109
TERSubset< CanCvtToTER > TER
Definition TER.h:634
std::vector< unsigned char > Blob
Storage for linear binary data.
Definition Blob.h:10
BaseUInt< 256 > uint256
Definition base_uint.h:562
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