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 = const pointer&;
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 mApplying = true;
156 }
157
163 bool
165 {
166 return mApplying;
167 }
168
172 void
174 {
175 mApplying = false;
176 }
177
179 {
183 void
185 {
186 applied = false;
187 broadcast = false;
188 queued = false;
189 kept = false;
190 }
191
196 bool
197 any() const
198 {
199 return applied || broadcast || queued || kept;
200 }
201
202 bool applied = false;
203 bool broadcast = false;
204 bool queued = false;
205 bool kept = false;
206 };
207
214 {
215 return submitResult_;
216 }
217
221 void
223 {
225 }
226
230 void
232 {
233 submitResult_.applied = true;
234 }
235
239 void
241 {
242 submitResult_.queued = true;
243 }
244
248 void
250 {
252 }
253
257 void
259 {
260 submitResult_.kept = true;
261 }
262
264 {
266
268 LedgerIndex li,
269 XRPAmount fee,
270 std::uint32_t accSeqNext,
271 std::uint32_t accSeqAvail)
272 : validatedLedger{li}
273 , minFeeRequired{fee}
274 , accountSeqNext{accSeqNext}
275 , accountSeqAvail{accSeqAvail}
276 {
277 }
278
283 };
284
291 {
292 return currentLedgerState_;
293 }
294
302 void
304 LedgerIndex validatedLedger,
305 XRPAmount fee,
306 std::uint32_t accountSeq,
307 std::uint32_t availableSeq)
308 {
309 currentLedgerState_.emplace(
310 validatedLedger, fee, accountSeq, availableSeq);
311 }
312
314 getJson(JsonOptions options, bool binary = false) const;
315
316 // Information used to locate a transaction.
317 // Contains a nodestore hash and ledger sequence pair if the transaction was
318 // found. Otherwise, contains the range of ledgers present in the database
319 // at the time of search.
320 struct Locator
321 {
324
325 // @return true if transaction was found, false otherwise
326 //
327 // Call this function first to determine the type of the contained info.
328 // Calling the wrong getter function will throw an exception.
329 // See documentation for the getter functions for more details
330 bool
332 {
333 return std::holds_alternative<std::pair<uint256, uint32_t>>(
334 locator);
335 }
336
337 // @return key used to find transaction in nodestore
338 //
339 // Throws if isFound() returns false
340 uint256 const&
342 {
343 return std::get<std::pair<uint256, uint32_t>>(locator).first;
344 }
345
346 // @return sequence of ledger containing the transaction
347 //
348 // Throws is isFound() returns false
349 uint32_t
351 {
352 return std::get<std::pair<uint256, uint32_t>>(locator).second;
353 }
354
355 // @return range of ledgers searched
356 //
357 // Throws if isFound() returns true
360 {
361 return std::get<ClosedInterval<uint32_t>>(locator);
362 }
363 };
364
365 static Locator
366 locate(uint256 const& id, Application& app);
367
368 static std::variant<
371 load(uint256 const& id, Application& app, error_code_i& ec);
372
373 static std::variant<
376 load(
377 uint256 const& id,
378 Application& app,
380 error_code_i& ec);
381
382private:
383 static std::variant<
386 load(
387 uint256 const& id,
388 Application& app,
390 error_code_i& ec);
391
393
399 bool mApplying = false;
400
403
405
409};
410
411} // namespace ripple
412
413#endif
Represents a JSON value.
Definition: json_value.h:148
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:303
std::optional< CurrentLedgerState > currentLedgerState_
Definition: Transaction.h:404
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:249
void setStatus(TransStatus status)
Definition: Transaction.h:138
void clearSubmitResult()
clearSubmitResult Clear all flags in SubmitResult
Definition: Transaction.h:222
bool getApplying()
Detect if transaction is being batched.
Definition: Transaction.h:164
std::optional< uint32_t > mNetworkID
Definition: Transaction.h:396
std::optional< uint32_t > mTxnSeq
Definition: Transaction.h:395
void setQueued()
setQueued Set this flag once was put into heldtxns queue
Definition: Transaction.h:240
LedgerIndex mLedgerIndex
Definition: Transaction.h:394
void setApplied()
setApplied Set this flag once was applied to open ledger
Definition: Transaction.h:231
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:258
std::shared_ptr< STTx const > const & getSTransaction()
Definition: Transaction.h:89
void setLedger(LedgerIndex ledger)
Definition: Transaction.h:144
Application & mApp
Definition: Transaction.h:407
SubmitResult submitResult_
different ways for transaction to be accepted
Definition: Transaction.h:402
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:173
Json::Value getJson(JsonOptions options, bool binary=false) const
uint256 const & getID() const
Definition: Transaction.h:95
TransStatus mStatus
Definition: Transaction.h:397
std::shared_ptr< STTx const > mTransaction
Definition: Transaction.h:406
void setResult(TER terResult)
Definition: Transaction.h:125
std::optional< CurrentLedgerState > getCurrentLedgerState() const
getCurrentLedgerState Get current ledger state of transaction
Definition: Transaction.h:290
SubmitResult getSubmitResult() const
getSubmitResult Return submit result
Definition: Transaction.h:213
beast::Journal j_
Definition: Transaction.h:408
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:267
uint256 const & getNodestoreHash()
Definition: Transaction.h:341
std::variant< std::pair< uint256, uint32_t >, ClosedInterval< uint32_t > > locator
Definition: Transaction.h:323
ClosedInterval< uint32_t > const & getLedgerRangeSearched()
Definition: Transaction.h:359
bool any() const
any Get true of any state is true
Definition: Transaction.h:197
void clear()
clear Clear all states
Definition: Transaction.h:184