rippled
Loading...
Searching...
No Matches
TransactionMaster.cpp
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#include <xrpld/app/ledger/TransactionMaster.h>
21#include <xrpld/app/main/Application.h>
22#include <xrpld/app/misc/Transaction.h>
23
24#include <xrpl/basics/TaggedCache.ipp>
25#include <xrpl/basics/chrono.h>
26#include <xrpl/protocol/STTx.h>
27
28namespace ripple {
29
31 : mApp(app)
32 , mCache(
33 "TransactionCache",
34 65536,
35 std::chrono::minutes{30},
36 stopwatch(),
37 mApp.journal("TaggedCache"))
38{
39}
40
41bool
43{
44 auto txn = mCache.fetch(hash);
45
46 if (!txn)
47 return false;
48
49 txn->setStatus(COMMITTED, ledger);
50 return true;
51}
52
55{
56 return mCache.fetch(txnID);
57}
58
63{
64 using TxPair =
66
67 if (auto txn = fetch_from_cache(txnID); txn && !txn->isValidated())
68 return std::pair{std::move(txn), nullptr};
69
70 auto v = Transaction::load(txnID, mApp, ec);
71
72 if (std::holds_alternative<TxSearched>(v))
73 return v;
74
75 auto [txn, txnMeta] = std::get<TxPair>(v);
76
77 if (txn)
78 mCache.canonicalize_replace_client(txnID, txn);
79
80 return std::pair{std::move(txn), std::move(txnMeta)};
81}
82
87 uint256 const& txnID,
89 error_code_i& ec)
90{
91 using TxPair =
93
94 if (auto txn = fetch_from_cache(txnID); txn && !txn->isValidated())
95 return std::pair{std::move(txn), nullptr};
96
97 auto v = Transaction::load(txnID, mApp, range, ec);
98
99 if (std::holds_alternative<TxSearched>(v))
100 return v;
101
102 auto [txn, txnMeta] = std::get<TxPair>(v);
103
104 if (txn)
105 mCache.canonicalize_replace_client(txnID, txn);
106
107 return std::pair{std::move(txn), std::move(txnMeta)};
108}
109
112 boost::intrusive_ptr<SHAMapItem> const& item,
113 SHAMapNodeType type,
114 std::uint32_t uCommitLedger)
115{
117 auto iTx = fetch_from_cache(item->key());
118
119 if (!iTx)
120 {
122 {
123 SerialIter sit(item->slice());
124 txn = std::make_shared<STTx const>(std::ref(sit));
125 }
126 else if (type == SHAMapNodeType::tnTRANSACTION_MD)
127 {
128 auto blob = SerialIter{item->slice()}.getVL();
129 txn = std::make_shared<STTx const>(
130 SerialIter{blob.data(), blob.size()});
131 }
132 }
133 else
134 {
135 if (uCommitLedger)
136 iTx->setStatus(COMMITTED, uCommitLedger);
137
138 txn = iTx->getSTransaction();
139 }
140
141 return txn;
142}
143
144void
146{
147 uint256 const tid = (*pTransaction)->getID();
148 if (tid != beast::zero)
149 {
150 auto txn = *pTransaction;
151 // VFALCO NOTE canonicalize can change the value of txn!
152 mCache.canonicalize_replace_client(tid, txn);
153 *pTransaction = txn;
154 }
155}
156
157void
159{
160 mCache.sweep();
161}
162
165{
166 return mCache;
167}
168
169} // namespace ripple
Map/cache combination.
Definition: TaggedCache.h:62
std::shared_ptr< Transaction > fetch_from_cache(uint256 const &)
bool inLedger(uint256 const &hash, std::uint32_t ledger)
TransactionMaster(Application &app)
std::variant< std::pair< std::shared_ptr< Transaction >, std::shared_ptr< TxMeta > >, TxSearched > fetch(uint256 const &, error_code_i &ec)
TaggedCache< uint256, Transaction > mCache
void canonicalize(std::shared_ptr< Transaction > *pTransaction)
TaggedCache< uint256, Transaction > & getCache()
static std::variant< std::pair< std::shared_ptr< Transaction >, std::shared_ptr< TxMeta > >, TxSearched > load(uint256 const &id, Application &app, error_code_i &ec)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
@ COMMITTED
Definition: Transaction.h:51
error_code_i
Definition: ErrorCodes.h:40
boost::icl::closed_interval< T > ClosedInterval
A closed interval over the domain T.
Definition: RangeSet.h:45
Stopwatch & stopwatch()
Returns an instance of a wall clock.
Definition: chrono.h:119
ClosedInterval< T > range(T low, T high)
Create a closed range interval.
Definition: RangeSet.h:54
STL namespace.
T ref(T... args)