xrpld
Loading...
Searching...
No Matches
Transaction.h
1#pragma once
2
3#include <xrpl/basics/Blob.h>
4#include <xrpl/basics/CountedObject.h>
5#include <xrpl/basics/RangeSet.h>
6#include <xrpl/basics/base_uint.h>
7#include <xrpl/beast/utility/Journal.h>
8#include <xrpl/json/json_value.h>
9#include <xrpl/protocol/ErrorCodes.h>
10#include <xrpl/protocol/Protocol.h>
11#include <xrpl/protocol/STBase.h>
12#include <xrpl/protocol/STTx.h>
13#include <xrpl/protocol/TER.h>
14#include <xrpl/protocol/TxMeta.h>
15#include <xrpl/protocol/TxSearched.h>
16#include <xrpl/protocol/XRPAmount.h>
17
18// boost::optional (not std::optional) appears in the declarations below,
19// because SOCI's into()/use() bindings only support boost::optional.
20#include <boost/optional/optional.hpp>
21
22#include <cstdint>
23#include <memory>
24#include <optional>
25#include <string>
26#include <utility>
27#include <variant>
28
29namespace xrpl {
30
31//
32// Transactions should be constructed in JSON with. Use STObject::parseJson to
33// obtain a binary version.
34//
35
36class Application;
37class Rules;
38
39enum class TransStatus {
40 NEW = 0, // just received / generated
41 INVALID = 1, // no valid signature, insufficient funds
42 INCLUDED = 2, // added to the current ledger
43 CONFLICTED = 3, // losing to a conflicting transaction
44 COMMITTED = 4, // known to be in a ledger
45 HELD = 5, // not valid now, maybe later
46 REMOVED = 6, // taken out of a ledger
47 OBSOLETE = 7, // a compatible transaction has taken precedence
48 INCOMPLETE = 8 // needs more signatures
49};
50
51// This class is for constructing and examining transactions.
52// Transactions are static so manipulation functions are unnecessary.
53class Transaction : public std::enable_shared_from_this<Transaction>,
54 public CountedObject<Transaction>
55{
56public:
58 using ref = pointer const&;
59
61
62 // The two boost::optional parameters are because SOCI requires
63 // boost::optional (not std::optional) parameters.
66 boost::optional<std::uint64_t> const& ledgerSeq,
67 boost::optional<std::string> const& status,
68 Blob const& rawTxn,
69 Application& app);
70
71 // The boost::optional parameter is because SOCI requires
72 // boost::optional (not std::optional) parameters.
73 static TransStatus
74 sqlTransactionStatus(boost::optional<std::string> const& status);
75
78 {
79 return transaction_;
80 }
81
82 uint256 const&
83 getID() const
84 {
85 return transactionID_;
86 }
87
89 getLedger() const
90 {
91 return ledgerIndex_;
92 }
93
94 bool
96 {
97 return ledgerIndex_ != 0;
98 }
99
101 getStatus() const
102 {
103 return status_;
104 }
105
106 TER
108 {
109 return result_;
110 }
111
112 void
113 setResult(TER terResult)
114 {
115 result_ = terResult;
116 }
117
118 void
120 TransStatus status,
121 std::uint32_t ledgerSeq,
122 std::optional<uint32_t> transactionSeq = std::nullopt,
123 std::optional<uint32_t> networkID = std::nullopt);
124
125 void
127 {
128 status_ = status;
129 }
130
131 void
133 {
134 ledgerIndex_ = ledger;
135 }
136
140 void
142 {
143 // Note that all access to applying_ are made by NetworkOPsImp, and must
144 // be done under that class's lock.
145 applying_ = true;
146 }
147
153 bool
155 {
156 // Note that all access to applying_ are made by NetworkOPsImp, and must
157 // be done under that class's lock.
158 return applying_;
159 }
160
164 void
166 {
167 // Note that all access to applying_ are made by NetworkOPsImp, and must
168 // be done under that class's lock.
169 applying_ = false;
170 }
171
173 {
177 void
179 {
180 applied = false;
181 broadcast = false;
182 queued = false;
183 kept = false;
184 }
185
190 [[nodiscard]] bool
191 any() const
192 {
193 return applied || broadcast || queued || kept;
194 }
195
196 bool applied = false;
197 bool broadcast = false;
198 bool queued = false;
199 bool kept = false;
200 };
201
208 {
209 return submitResult_;
210 }
211
215 void
217 {
218 submitResult_.clear();
219 }
220
224 void
226 {
227 submitResult_.applied = true;
228 }
229
233 void
235 {
236 submitResult_.queued = true;
237 }
238
242 void
244 {
245 submitResult_.broadcast = true;
246 }
247
251 void
253 {
254 submitResult_.kept = true;
255 }
256
278
285 {
286 return currentLedgerState_;
287 }
288
296 void
298 LedgerIndex validatedLedger,
299 XRPAmount fee,
300 std::uint32_t accountSeq,
301 std::uint32_t availableSeq)
302 {
303 currentLedgerState_.emplace(validatedLedger, fee, accountSeq, availableSeq);
304 }
305
307 getJson(JsonOptions options, bool binary = false) const;
308
309 // Information used to locate a transaction.
310 // Contains a nodestore hash and ledger sequence pair if the transaction was
311 // found. Otherwise, contains the range of ledgers present in the database
312 // at the time of search.
313 struct Locator
314 {
316
324 [[nodiscard]] bool
329
335 uint256 const&
337 {
338 return std::get<std::pair<uint256, uint32_t>>(locator).first;
339 }
340
346 uint32_t
348 {
349 return std::get<std::pair<uint256, uint32_t>>(locator).second;
350 }
351
359 {
360 return std::get<ClosedInterval<uint32_t>>(locator);
361 }
362 };
363
364 static Locator
365 locate(uint256 const& id, Application& app);
366
367 static std::
368 variant<std::pair<std::shared_ptr<Transaction>, std::shared_ptr<TxMeta>>, TxSearched>
369 load(uint256 const& id, Application& app, ErrorCodeI& ec);
370
371 static std::
372 variant<std::pair<std::shared_ptr<Transaction>, std::shared_ptr<TxMeta>>, TxSearched>
373 load(
374 uint256 const& id,
375 Application& app,
377 ErrorCodeI& ec);
378
379private:
380 static std::
381 variant<std::pair<std::shared_ptr<Transaction>, std::shared_ptr<TxMeta>>, TxSearched>
382 load(
383 uint256 const& id,
384 Application& app,
386 ErrorCodeI& ec);
387
389
395 /* Note that all access to applying_ are made by NetworkOPsImp,
396 and must be done under that class's lock. This avoids the overhead of
397 taking a separate lock, and the consequences of a race condition are
398 nearly-zero.
399
400 1. If there is a race condition, and getApplying returns false when it
401 should be true, the transaction will be processed again. Not that
402 big a deal if it's a rare one-off. Most of the time, it'll get
403 tefALREADY or tefPAST_SEQ.
404 2. On the flip side, if it returns true, when it should be false, then
405 the transaction must have been attempted recently, so no big deal if
406 it doesn't immediately get tried right away.
407 3. If there's a race between setApplying and clearApplying, and the
408 flag ends up set, then a batch is about to try to process the
409 transaction and will call clearApplying later. If it ends up
410 cleared, then it might get attempted again later as is the case with
411 item 1.
412 */
413 bool applying_ = false;
414
419
421
425};
426
427} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
Represents a JSON value.
Definition json_value.h:117
Rules controlling protocol behavior.
Definition Rules.h:40
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:89
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
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:83
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:57
std::shared_ptr< STTx const > const & getSTransaction()
Definition Transaction.h:77
static TransStatus sqlTransactionStatus(boost::optional< std::string > const &status)
bool isValidated() const
Definition Transaction.h:95
pointer const & ref
Definition Transaction.h:58
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:23
std::uint32_t LedgerIndex
A ledger index.
Definition Protocol.h:370
TransStatus
Definition Transaction.h:39
ClosedInterval< T > range(T low, T high)
Create a closed range interval.
Definition RangeSet.h:37
boost::icl::closed_interval< T > ClosedInterval
A closed interval over the domain T.
Definition RangeSet.h:27
TxSearched
Definition TxSearched.h:5
@ temUNCERTAIN
Definition TER.h:111
TERSubset< CanCvtToTER > TER
Definition TER.h:647
std::vector< unsigned char > Blob
Storage for linear binary data.
Definition Blob.h:11
BaseUInt< 256 > uint256
Definition base_uint.h:580
Note, should be treated as flags that can be | and &.
Definition STBase.h:22
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