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,
135 std::optional<uint32_t> networkID = 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
229 {
231 }
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
270 {
272
274 LedgerIndex li,
276 std::uint32_t accSeqNext,
277 std::uint32_t accSeqAvail)
278 : validatedLedger{li}
280 , accountSeqNext{accSeqNext}
281 , accountSeqAvail{accSeqAvail}
282 {
283 }
284
289 };
290
297 {
298 return currentLedgerState_;
299 }
300
308 void
310 LedgerIndex validatedLedger,
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
338 {
339 return std::holds_alternative<std::pair<uint256, uint32_t>>(
340 locator);
341 }
342
343 // @return key used to find transaction in nodestore
344 //
345 // Throws if isFound() returns false
346 uint256 const&
348 {
349 return std::get<std::pair<uint256, uint32_t>>(locator).first;
350 }
351
352 // @return sequence of ledger containing the transaction
353 //
354 // Throws is isFound() returns false
355 uint32_t
357 {
358 return std::get<std::pair<uint256, uint32_t>>(locator).second;
359 }
360
361 // @return range of ledgers searched
362 //
363 // Throws if isFound() returns true
366 {
367 return std::get<ClosedInterval<uint32_t>>(locator);
368 }
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:150
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
Definition: Transaction.h:309
std::optional< CurrentLedgerState > currentLedgerState_
Definition: Transaction.h:428
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:99
void setBroadcast()
setBroadcast Set this flag once was broadcasted via network
Definition: Transaction.h:255
void setStatus(TransStatus status)
Definition: Transaction.h:138
void clearSubmitResult()
clearSubmitResult Clear all flags in SubmitResult
Definition: Transaction.h:228
bool getApplying()
Detect if transaction is being batched.
Definition: Transaction.h:166
std::optional< uint32_t > mNetworkID
Definition: Transaction.h:402
std::optional< uint32_t > mTxnSeq
Definition: Transaction.h:401
void setQueued()
setQueued Set this flag once was put into heldtxns queue
Definition: Transaction.h:246
LedgerIndex mLedgerIndex
Definition: Transaction.h:400
pointer const & ref
Definition: Transaction.h:67
void setApplied()
setApplied Set this flag once was applied to open ledger
Definition: Transaction.h:237
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:153
void setKept()
setKept Set this flag once was put to localtxns queue
Definition: Transaction.h:264
std::shared_ptr< STTx const > const & getSTransaction()
Definition: Transaction.h:89
void setLedger(LedgerIndex ledger)
Definition: Transaction.h:144
Application & mApp
Definition: Transaction.h:431
SubmitResult submitResult_
different ways for transaction to be accepted
Definition: Transaction.h:426
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)
Definition: Transaction.cpp:73
bool isValidated() const
Definition: Transaction.h:107
void clearApplying()
Indicate that transaction application has been attempted.
Definition: Transaction.h:177
Json::Value getJson(JsonOptions options, bool binary=false) const
uint256 const & getID() const
Definition: Transaction.h:95
TransStatus mStatus
Definition: Transaction.h:403
std::shared_ptr< STTx const > mTransaction
Definition: Transaction.h:430
void setResult(TER terResult)
Definition: Transaction.h:125
std::optional< CurrentLedgerState > getCurrentLedgerState() const
getCurrentLedgerState Get current ledger state of transaction
Definition: Transaction.h:296
SubmitResult getSubmitResult() const
getSubmitResult Return submit result
Definition: Transaction.h:219
beast::Journal j_
Definition: Transaction.h:432
Set the fee on a JTx.
Definition: fee.h:37
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:38
CurrentLedgerState(LedgerIndex li, XRPAmount fee, std::uint32_t accSeqNext, std::uint32_t accSeqAvail)
Definition: Transaction.h:273
uint256 const & getNodestoreHash()
Definition: Transaction.h:347
std::variant< std::pair< uint256, uint32_t >, ClosedInterval< uint32_t > > locator
Definition: Transaction.h:329
ClosedInterval< uint32_t > const & getLedgerRangeSearched()
Definition: Transaction.h:365
bool any() const
any Get true of any state is true
Definition: Transaction.h:203
void clear()
clear Clear all states
Definition: Transaction.h:190