xrpld
Loading...
Searching...
No Matches
TransactionMaster.cpp
1#include <xrpld/app/ledger/TransactionMaster.h>
2
3#include <xrpld/app/main/Application.h>
4#include <xrpld/app/misc/Transaction.h>
5
6#include <xrpl/basics/RangeSet.h>
7#include <xrpl/basics/TaggedCache.ipp> // IWYU pragma: keep
8#include <xrpl/basics/base_uint.h>
9#include <xrpl/basics/chrono.h>
10#include <xrpl/beast/utility/Zero.h>
11#include <xrpl/protocol/ErrorCodes.h>
12#include <xrpl/protocol/STTx.h>
13#include <xrpl/protocol/Serializer.h>
14#include <xrpl/protocol/TxMeta.h>
15#include <xrpl/protocol/TxSearched.h>
16#include <xrpl/shamap/SHAMapItem.h>
17#include <xrpl/shamap/SHAMapTreeNode.h>
18
19#include <boost/smart_ptr/intrusive_ptr.hpp>
20
21#include <cstdint>
22#include <functional>
23#include <memory>
24#include <optional>
25#include <utility>
26#include <variant>
27
28namespace xrpl {
29
31 : app_(app)
32 , cache_(
33 "TransactionCache",
34 65536,
35 std::chrono::minutes{30},
36 stopwatch(),
37 app_.getJournal("TaggedCache"))
38{
39}
40
41bool
43 uint256 const& hash,
44 std::uint32_t ledger,
47{
48 auto txn = cache_.fetch(hash);
49
50 if (!txn)
51 return false;
52
53 txn->setStatus(TransStatus::COMMITTED, ledger, tseq, netID);
54 return true;
55}
56
59{
60 return cache_.fetch(txnID);
61}
62
65{
67
68 if (auto txn = fetchFromCache(txnID); txn && !txn->isValidated())
69 return std::pair{std::move(txn), nullptr};
70
71 auto v = Transaction::load(txnID, app_, ec);
72
74 return v;
75
76 auto [txn, txnMeta] = std::get<TxPair>(v);
77
78 if (txn)
79 cache_.canonicalizeReplaceClient(txnID, txn);
80
81 return std::pair{std::move(txn), std::move(txnMeta)};
82}
83
86 uint256 const& txnID,
88 ErrorCodeI& ec)
89{
91
92 if (auto txn = fetchFromCache(txnID); txn && !txn->isValidated())
93 return std::pair{std::move(txn), nullptr};
94
95 auto v = Transaction::load(txnID, app_, range, ec);
96
98 return v;
99
100 auto [txn, txnMeta] = std::get<TxPair>(v);
101
102 if (txn)
103 cache_.canonicalizeReplaceClient(txnID, txn);
104
105 return std::pair{std::move(txn), std::move(txnMeta)};
106}
107
110 boost::intrusive_ptr<SHAMapItem> const& item,
111 SHAMapNodeType type,
112 std::uint32_t uCommitLedger)
113{
115 auto iTx = fetchFromCache(item->key());
116
117 if (!iTx)
118 {
120 {
121 SerialIter sit(item->slice());
123 }
124 else if (type == SHAMapNodeType::TnTransactionMd)
125 {
126 auto blob = SerialIter{item->slice()}.getVL();
127 txn = std::make_shared<STTx const>(SerialIter{blob.data(), blob.size()});
128 }
129 }
130 else
131 {
132 if (uCommitLedger != 0u)
133 iTx->setStatus(TransStatus::COMMITTED, uCommitLedger);
134
135 txn = iTx->getSTransaction();
136 }
137
138 return txn;
139}
140
141void
143{
144 uint256 const tid = (*pTransaction)->getID();
145 if (tid != beast::kZero)
146 {
147 auto txn = *pTransaction;
148 // VFALCO NOTE canonicalize can change the value of txn!
149 cache_.canonicalizeReplaceClient(tid, txn);
150 *pTransaction = txn;
151 }
152}
153
154void
156{
157 cache_.sweep();
158}
159
162{
163 return cache_;
164}
165
166} // namespace xrpl
Map/cache combination.
Definition TaggedCache.h:59
TaggedCache< uint256, Transaction > cache_
std::variant< std::pair< std::shared_ptr< Transaction >, std::shared_ptr< TxMeta > >, TxSearched > fetch(uint256 const &, ErrorCodeI &ec)
bool inLedger(uint256 const &hash, std::uint32_t ledger, std::optional< uint32_t > tseq, std::optional< uint32_t > netID)
TransactionMaster(Application &app)
void canonicalize(std::shared_ptr< Transaction > *pTransaction)
TaggedCache< uint256, Transaction > & getCache()
std::shared_ptr< Transaction > fetchFromCache(uint256 const &)
static std::variant< std::pair< std::shared_ptr< Transaction >, std::shared_ptr< TxMeta > >, TxSearched > load(uint256 const &id, Application &app, ErrorCodeI &ec)
T holds_alternative(T... args)
T make_shared(T... args)
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
ErrorCodeI
Definition ErrorCodes.h:22
Stopwatch & stopwatch()
Returns an instance of a wall clock.
Definition chrono.h:94
ClosedInterval< T > range(T low, T high)
Create a closed range interval.
Definition RangeSet.h:34
boost::icl::closed_interval< T > ClosedInterval
A closed interval over the domain T.
Definition RangeSet.h:25
TxSearched
Definition TxSearched.h:5
BaseUInt< 256 > uint256
Definition base_uint.h:562
T ref(T... args)