rippled
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 <ripple/basics/RangeSet.h>
24 #include <ripple/beast/utility/Journal.h>
25 #include <ripple/protocol/ErrorCodes.h>
26 #include <ripple/protocol/Protocol.h>
27 #include <ripple/protocol/STBase.h>
28 #include <ripple/protocol/STTx.h>
29 #include <ripple/protocol/TER.h>
30 #include <ripple/protocol/TxMeta.h>
31 
32 #include <optional>
33 #include <variant>
34 
35 namespace ripple {
36 
37 //
38 // Transactions should be constructed in JSON with. Use STObject::parseJson to
39 // obtain a binary version.
40 //
41 
42 class Application;
43 class Database;
44 class 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 
58 enum class TxSearched { all, some, unknown };
59 
60 // This class is for constructing and examining transactions.
61 // Transactions are static so manipulation functions are unnecessary.
62 class Transaction : public std::enable_shared_from_this<Transaction>,
63  public CountedObject<Transaction>
64 {
65 public:
67  using ref = const pointer&;
68 
71  std::string&,
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
107  isValidated() const
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
131  setStatus(TransStatus status, std::uint32_t ledgerSeq);
132 
133  void
135  {
136  mStatus = status;
137  }
138 
139  void
141  {
142  mLedgerIndex = ledger;
143  }
144 
148  void
150  {
151  mApplying = true;
152  }
153 
159  bool
161  {
162  return mApplying;
163  }
164 
168  void
170  {
171  mApplying = false;
172  }
173 
175  {
179  void
181  {
182  applied = false;
183  broadcast = false;
184  queued = false;
185  kept = false;
186  }
187 
192  bool
193  any() const
194  {
195  return applied || broadcast || queued || kept;
196  }
197 
198  bool applied = false;
199  bool broadcast = false;
200  bool queued = false;
201  bool kept = false;
202  };
203 
210  {
211  return submitResult_;
212  }
213 
217  void
219  {
221  }
222 
226  void
228  {
229  submitResult_.applied = true;
230  }
231 
235  void
237  {
238  submitResult_.queued = true;
239  }
240 
244  void
246  {
247  submitResult_.broadcast = true;
248  }
249 
253  void
255  {
256  submitResult_.kept = true;
257  }
258 
260  {
261  CurrentLedgerState() = delete;
262 
264  LedgerIndex li,
265  XRPAmount fee,
266  std::uint32_t accSeqNext,
267  std::uint32_t accSeqAvail)
268  : validatedLedger{li}
269  , minFeeRequired{fee}
270  , accountSeqNext{accSeqNext}
271  , accountSeqAvail{accSeqAvail}
272  {
273  }
274 
279  };
280 
287  {
288  return currentLedgerState_;
289  }
290 
298  void
300  LedgerIndex validatedLedger,
301  XRPAmount fee,
302  std::uint32_t accountSeq,
303  std::uint32_t availableSeq)
304  {
305  currentLedgerState_.emplace(
306  validatedLedger, fee, accountSeq, availableSeq);
307  }
308 
310  getJson(JsonOptions options, bool binary = false) const;
311 
312  // Information used to locate a transaction.
313  // Contains a nodestore hash and ledger sequence pair if the transaction was
314  // found. Otherwise, contains the range of ledgers present in the database
315  // at the time of search.
316  struct Locator
317  {
320 
321  // @return true if transaction was found, false otherwise
322  //
323  // Call this function first to determine the type of the contained info.
324  // Calling the wrong getter function will throw an exception.
325  // See documentation for the getter functions for more details
326  bool
328  {
329  return std::holds_alternative<std::pair<uint256, uint32_t>>(
330  locator);
331  }
332 
333  // @return key used to find transaction in nodestore
334  //
335  // Throws if isFound() returns false
336  uint256 const&
338  {
339  return std::get<std::pair<uint256, uint32_t>>(locator).first;
340  }
341 
342  // @return sequence of ledger containing the transaction
343  //
344  // Throws is isFound() returns false
345  uint32_t
347  {
348  return std::get<std::pair<uint256, uint32_t>>(locator).second;
349  }
350 
351  // @return range of ledgers searched
352  //
353  // Throws if isFound() returns true
356  {
357  return std::get<ClosedInterval<uint32_t>>(locator);
358  }
359  };
360 
361  static Locator
362  locate(uint256 const& id, Application& app);
363 
364  static std::variant<
366  TxSearched>
367  load(uint256 const& id, Application& app, error_code_i& ec);
368 
369  static std::variant<
371  TxSearched>
372  load(
373  uint256 const& id,
374  Application& app,
376  error_code_i& ec);
377 
378 private:
379  static std::variant<
381  TxSearched>
382  load(
383  uint256 const& id,
384  Application& app,
386  error_code_i& ec);
387 
389 
393  bool mApplying = false;
394 
397 
399 
403 };
404 
405 } // namespace ripple
406 
407 #endif
ripple::COMMITTED
@ COMMITTED
Definition: Transaction.h:51
ripple::Transaction::sqlTransactionStatus
static TransStatus sqlTransactionStatus(boost::optional< std::string > const &status)
Definition: Transaction.cpp:69
ripple::Application
Definition: Application.h:116
ripple::Transaction::CurrentLedgerState::accountSeqAvail
std::uint32_t accountSeqAvail
Definition: Transaction.h:278
ripple::Transaction::transactionFromSQL
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:92
ripple::TxSearched::unknown
@ unknown
ripple::CountedObject
Tracks the number of instances of an object.
Definition: CountedObject.h:124
ripple::Transaction::CurrentLedgerState::minFeeRequired
XRPAmount minFeeRequired
Definition: Transaction.h:276
std::string
STL class.
std::shared_ptr< Transaction >
ripple::Transaction::getJson
Json::Value getJson(JsonOptions options, bool binary=false) const
Definition: Transaction.cpp:168
ripple::Transaction::getSTransaction
std::shared_ptr< STTx const > const & getSTransaction()
Definition: Transaction.h:89
std::pair
ripple::Transaction::SubmitResult::kept
bool kept
Definition: Transaction.h:201
ripple::TxSearched::all
@ all
ripple::Transaction::Locator::getNodestoreHash
uint256 const & getNodestoreHash()
Definition: Transaction.h:337
ripple::TxSearched
TxSearched
Definition: Transaction.h:58
std::vector< unsigned char >
ripple::INCOMPLETE
@ INCOMPLETE
Definition: Transaction.h:55
ripple::Transaction::setLedger
void setLedger(LedgerIndex ledger)
Definition: Transaction.h:140
ripple::Transaction
Definition: Transaction.h:62
ripple::Transaction::currentLedgerState_
std::optional< CurrentLedgerState > currentLedgerState_
Definition: Transaction.h:398
ripple::Transaction::SubmitResult
Definition: Transaction.h:174
ripple::Transaction::CurrentLedgerState
Definition: Transaction.h:259
ripple::NEW
@ NEW
Definition: Transaction.h:47
ripple::HELD
@ HELD
Definition: Transaction.h:52
ripple::Transaction::setApplied
void setApplied()
setApplied Set this flag once was applied to open ledger
Definition: Transaction.h:227
ripple::Transaction::setResult
void setResult(TER terResult)
Definition: Transaction.h:125
ripple::Transaction::mStatus
TransStatus mStatus
Definition: Transaction.h:391
ripple::Transaction::getSubmitResult
SubmitResult getSubmitResult() const
getSubmitResult Return submit result
Definition: Transaction.h:209
ripple::Transaction::getID
uint256 const & getID() const
Definition: Transaction.h:95
ripple::Transaction::CurrentLedgerState::accountSeqNext
std::uint32_t accountSeqNext
Definition: Transaction.h:277
ripple::error_code_i
error_code_i
Definition: ErrorCodes.h:40
ripple::TransStatus
TransStatus
Definition: Transaction.h:46
ripple::Transaction::setStatus
void setStatus(TransStatus status)
Definition: Transaction.h:134
ripple::temUNCERTAIN
@ temUNCERTAIN
Definition: TER.h:122
ripple::Transaction::mTransaction
std::shared_ptr< STTx const > mTransaction
Definition: Transaction.h:400
ripple::base_uint< 256 >
ripple::INCLUDED
@ INCLUDED
Definition: Transaction.h:49
ripple::Transaction::mTransactionID
uint256 mTransactionID
Definition: Transaction.h:388
ripple::Transaction::SubmitResult::clear
void clear()
clear Clear all states
Definition: Transaction.h:180
ripple::Transaction::clearSubmitResult
void clearSubmitResult()
clearSubmitResult Clear all flags in SubmitResult
Definition: Transaction.h:218
ripple::Transaction::getApplying
bool getApplying()
Detect if transaction is being batched.
Definition: Transaction.h:160
ripple::Transaction::SubmitResult::queued
bool queued
Definition: Transaction.h:200
ripple::Transaction::setCurrentLedgerState
void setCurrentLedgerState(LedgerIndex validatedLedger, XRPAmount fee, std::uint32_t accountSeq, std::uint32_t availableSeq)
setCurrentLedgerState Set current ledger state of transaction
Definition: Transaction.h:299
ripple::Transaction::Transaction
Transaction(std::shared_ptr< STTx const > const &, std::string &, Application &) noexcept
Definition: Transaction.cpp:38
ripple::OBSOLETE
@ OBSOLETE
Definition: Transaction.h:54
ripple::Transaction::Locator
Definition: Transaction.h:316
ripple::Transaction::CurrentLedgerState::CurrentLedgerState
CurrentLedgerState()=delete
ripple::Transaction::getLedger
LedgerIndex getLedger() const
Definition: Transaction.h:101
ripple::TERSubset< CanCvtToTER >
ripple::Transaction::mLedgerIndex
LedgerIndex mLedgerIndex
Definition: Transaction.h:390
ripple::Transaction::CurrentLedgerState::CurrentLedgerState
CurrentLedgerState(LedgerIndex li, XRPAmount fee, std::uint32_t accSeqNext, std::uint32_t accSeqAvail)
Definition: Transaction.h:263
ripple::Transaction::setKept
void setKept()
setKept Set this flag once was put to localtxns queue
Definition: Transaction.h:254
std::enable_shared_from_this
ripple::Transaction::locate
static Locator locate(uint256 const &id, Application &app)
Definition: Transaction.cpp:134
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
ripple::Transaction::SubmitResult::any
bool any() const
any Get true of any state is true
Definition: Transaction.h:193
std::uint32_t
ripple::Transaction::submitResult_
SubmitResult submitResult_
different ways for transaction to be accepted
Definition: Transaction.h:396
ripple::CONFLICTED
@ CONFLICTED
Definition: Transaction.h:50
ripple::range
ClosedInterval< T > range(T low, T high)
Create a closed range interval.
Definition: RangeSet.h:54
ripple::Transaction::isValidated
bool isValidated() const
Definition: Transaction.h:107
ripple::Transaction::mResult
TER mResult
Definition: Transaction.h:392
ripple::Transaction::getStatus
TransStatus getStatus() const
Definition: Transaction.h:113
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::REMOVED
@ REMOVED
Definition: Transaction.h:53
ripple::Transaction::load
static std::variant< std::pair< std::shared_ptr< Transaction >, std::shared_ptr< TxMeta > >, TxSearched > load(uint256 const &id, Application &app, error_code_i &ec)
Definition: Transaction.cpp:114
ripple::JsonOptions
Note, should be treated as flags that can be | and &.
Definition: STBase.h:35
ripple::Transaction::Locator::locator
std::variant< std::pair< uint256, uint32_t >, ClosedInterval< uint32_t > > locator
Definition: Transaction.h:319
ripple::Transaction::SubmitResult::applied
bool applied
Definition: Transaction.h:198
ripple::INVALID
@ INVALID
Definition: Transaction.h:48
ripple::Transaction::setStatus
void setStatus(TransStatus status, std::uint32_t ledgerSeq)
Definition: Transaction.cpp:62
ripple::Transaction::mApp
Application & mApp
Definition: Transaction.h:401
optional
ripple::Transaction::j_
beast::Journal j_
Definition: Transaction.h:402
ripple::Transaction::SubmitResult::broadcast
bool broadcast
Definition: Transaction.h:199
ripple::ClosedInterval
boost::icl::closed_interval< T > ClosedInterval
A closed interval over the domain T.
Definition: RangeSet.h:45
ripple::Transaction::mApplying
bool mApplying
Definition: Transaction.h:393
ripple::Transaction::setApplying
void setApplying()
Set this flag once added to a batch.
Definition: Transaction.h:149
ripple::Transaction::getCurrentLedgerState
std::optional< CurrentLedgerState > getCurrentLedgerState() const
getCurrentLedgerState Get current ledger state of transaction
Definition: Transaction.h:286
ripple::Transaction::Locator::getLedgerSequence
uint32_t getLedgerSequence()
Definition: Transaction.h:346
ripple::Transaction::setQueued
void setQueued()
setQueued Set this flag once was put into heldtxns queue
Definition: Transaction.h:236
ripple::Transaction::CurrentLedgerState::validatedLedger
LedgerIndex validatedLedger
Definition: Transaction.h:275
ripple::Transaction::Locator::isFound
bool isFound()
Definition: Transaction.h:327
ripple::Transaction::setBroadcast
void setBroadcast()
setBroadcast Set this flag once was broadcasted via network
Definition: Transaction.h:245
ripple::TxSearched::some
@ some
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::XRPAmount
Definition: XRPAmount.h:46
ripple::Transaction::clearApplying
void clearApplying()
Indicate that transaction application has been attempted.
Definition: Transaction.h:169
ripple::Transaction::getResult
TER getResult()
Definition: Transaction.h:119
variant
ripple::Transaction::Locator::getLedgerRangeSearched
ClosedInterval< uint32_t > const & getLedgerRangeSearched()
Definition: Transaction.h:355