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 uint256 const& hash,
44 std::uint32_t ledger,
47{
48 auto txn = mCache.fetch(hash);
49
50 if (!txn)
51 return false;
52
53 txn->setStatus(COMMITTED, ledger, tseq, netID);
54 return true;
55}
56
59{
60 return mCache.fetch(txnID);
61}
62
67{
68 using TxPair =
70
71 if (auto txn = fetch_from_cache(txnID); txn && !txn->isValidated())
72 return std::pair{std::move(txn), nullptr};
73
74 auto v = Transaction::load(txnID, mApp, ec);
75
77 return v;
78
79 auto [txn, txnMeta] = std::get<TxPair>(v);
80
81 if (txn)
82 mCache.canonicalize_replace_client(txnID, txn);
83
84 return std::pair{std::move(txn), std::move(txnMeta)};
85}
86
91 uint256 const& txnID,
93 error_code_i& ec)
94{
95 using TxPair =
97
98 if (auto txn = fetch_from_cache(txnID); txn && !txn->isValidated())
99 return std::pair{std::move(txn), nullptr};
100
101 auto v = Transaction::load(txnID, mApp, range, ec);
102
104 return v;
105
106 auto [txn, txnMeta] = std::get<TxPair>(v);
107
108 if (txn)
109 mCache.canonicalize_replace_client(txnID, txn);
110
111 return std::pair{std::move(txn), std::move(txnMeta)};
112}
113
116 boost::intrusive_ptr<SHAMapItem> const& item,
117 SHAMapNodeType type,
118 std::uint32_t uCommitLedger)
119{
121 auto iTx = fetch_from_cache(item->key());
122
123 if (!iTx)
124 {
126 {
127 SerialIter sit(item->slice());
129 }
130 else if (type == SHAMapNodeType::tnTRANSACTION_MD)
131 {
132 auto blob = SerialIter{item->slice()}.getVL();
134 SerialIter{blob.data(), blob.size()});
135 }
136 }
137 else
138 {
139 if (uCommitLedger)
140 iTx->setStatus(COMMITTED, uCommitLedger);
141
142 txn = iTx->getSTransaction();
143 }
144
145 return txn;
146}
147
148void
150{
151 uint256 const tid = (*pTransaction)->getID();
152 if (tid != beast::zero)
153 {
154 auto txn = *pTransaction;
155 // VFALCO NOTE canonicalize can change the value of txn!
156 mCache.canonicalize_replace_client(tid, txn);
157 *pTransaction = txn;
158 }
159}
160
161void
163{
164 mCache.sweep();
165}
166
169{
170 return mCache;
171}
172
173} // namespace ripple
Map/cache combination.
Definition TaggedCache.h:62
std::shared_ptr< Transaction > fetch_from_cache(uint256 const &)
TransactionMaster(Application &app)
std::variant< std::pair< std::shared_ptr< Transaction >, std::shared_ptr< TxMeta > >, TxSearched > fetch(uint256 const &, error_code_i &ec)
bool inLedger(uint256 const &hash, std::uint32_t ledger, std::optional< uint32_t > tseq, std::optional< uint32_t > netID)
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)
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
@ COMMITTED
Definition Transaction.h:51
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)