xrpld
Loading...
Searching...
No Matches
OpenLedger.cpp
1#include <xrpld/app/ledger/OpenLedger.h>
2
3#include <xrpld/app/main/Application.h>
4#include <xrpld/app/misc/TxQ.h>
5#include <xrpld/overlay/Overlay.h>
6
7#include <xrpl/basics/Log.h>
8#include <xrpl/beast/utility/Journal.h>
9#include <xrpl/beast/utility/instrumentation.h>
10#include <xrpl/core/HashRouter.h>
11#include <xrpl/ledger/ApplyView.h>
12#include <xrpl/ledger/Ledger.h>
13#include <xrpl/ledger/OpenView.h>
14#include <xrpl/ledger/ReadView.h>
15#include <xrpl/protocol/Rules.h>
16#include <xrpl/protocol/SField.h> // IWYU pragma: keep
17#include <xrpl/protocol/STObject.h>
18#include <xrpl/protocol/STTx.h>
19#include <xrpl/protocol/Serializer.h>
20#include <xrpl/protocol/TER.h>
21#include <xrpl/protocol/TxFlags.h>
22#include <xrpl/shamap/SHAMap.h>
23#include <xrpl/tx/apply.h>
24
25#include <boost/range/adaptor/transformed.hpp>
26
27#include <xrpl.pb.h>
28
29#include <exception>
30#include <memory>
31#include <mutex>
32#include <sstream>
33#include <string>
34#include <string_view>
35#include <utility>
36#include <vector>
37
38namespace xrpl {
39
42 CachedSLEs& cache,
43 beast::Journal journal)
44 : j_(journal), cache_(cache), current_(create(ledger->rules(), ledger))
45{
46}
47
48bool
50{
52 return current_->txCount() == 0;
53}
54
57{
59 return current_;
60}
61
62bool
64{
65 std::scoped_lock const lock1(modifyMutex_);
67 auto const changed = f(*next, j_);
68 if (changed)
69 {
71 current_ = std::move(next);
72 }
73 return changed;
74}
75
76void
78 Application& app,
79 Rules const& rules,
81 OrderedTxs const& locals,
82 bool retriesFirst,
83 OrderedTxs& retries,
84 ApplyFlags flags,
85 std::string_view suffix,
86 ModifyType const& f)
87{
88 JLOG(j_.trace()) << "accept ledger " << ledger->seq() << " " << suffix;
89 auto next = create(rules, ledger);
90 if (retriesFirst)
91 {
92 // Handle disputed tx, outside lock
94 apply(app, *next, *ledger, Empty{}, retries, flags, j_);
95 }
96 // Block calls to modify, otherwise
97 // new tx going into the open ledger
98 // would get lost.
99 std::scoped_lock const lock1(modifyMutex_);
100 // Apply tx from the current open view
101 if (!current_->txs.empty())
102 {
103 apply(
104 app,
105 *next,
106 *ledger,
107 boost::adaptors::transform(
108 current_->txs,
110 p) { return p.first; }),
111 retries,
112 flags,
113 j_);
114 }
115 // Call the modifier
116 if (f)
117 f(*next, j_);
118 // Apply local tx
119 for (auto const& item : locals)
120 app.getTxQ().apply(app, *next, item.second, flags, j_);
121
122 // If we didn't relay this transaction recently, relay it to all peers
123 for (auto const& txpair : next->txs)
124 {
125 auto const& tx = txpair.first;
126 auto const txId = tx->getTransactionID();
127
128 // skip batch txns
129 // The flag should only be settable if Batch feature is enabled. If
130 // Batch is not enabled, the flag is always invalid, so don't relay it
131 // regardless.
132 // LCOV_EXCL_START
133 if (tx->isFlag(tfInnerBatchTxn))
134 {
135 XRPL_ASSERT(
136 txpair.second && txpair.second->isFieldPresent(sfParentBatchID),
137 "Inner Batch transaction missing sfParentBatchID");
138 continue;
139 }
140 // LCOV_EXCL_STOP
141
142 if (auto const toSkip = app.getHashRouter().shouldRelay(txId))
143 {
144 JLOG(j_.debug()) << "Relaying recovered tx " << txId;
145 protocol::TMTransaction msg;
146 Serializer s;
147
148 tx->add(s);
149 msg.set_rawtransaction(s.data(), s.size());
150 msg.set_status(protocol::tsNEW);
151 app.getOverlay().relay(*tx, msg, *toSkip);
152 }
153 }
154
155 // Switch to the new open view
156 std::scoped_lock const lock2(currentMutex_);
157 current_ = std::move(next);
158}
159
160//------------------------------------------------------------------------------
161
168
169auto
171 Application& app,
172 OpenView& view,
174 bool retry,
175 ApplyFlags flags,
177{
178 if (retry)
179 flags = flags | TapRetry;
180 // If it's in anybody's proposed set, try to keep it in the ledger
181 auto const result = xrpl::apply(app, view, *tx, flags, j);
182 if (result.applied || result.ter == terQUEUED)
183 return Result::Success;
184 if (isTefFailure(result.ter) || isTemMalformed(result.ter) || isTelLocal(result.ter))
185 return Result::Failure;
186 return Result::Retry;
187}
188
189//------------------------------------------------------------------------------
190
193{
195 ss << tx->getTransactionID();
196 return ss.str().substr(0, 4);
197}
198
201{
203 for (auto const& item : set)
204 ss << debugTxstr(item.second) << ", ";
205 return ss.str();
206}
207
210{
212 for (auto const& item : set)
213 {
214 try
215 {
216 SerialIter sit(item.slice());
217 auto const tx = std::make_shared<STTx const>(sit);
218 ss << debugTxstr(tx) << ", ";
219 }
220 catch (std::exception const& ex)
221 {
222 ss << "THROW:" << ex.what() << ", ";
223 }
224 }
225 return ss.str();
226}
227
230{
232 for (auto const& item : view->txs)
233 ss << debugTxstr(item.first) << ", ";
234 return ss.str();
235}
236
237} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
std::optional< std::set< PeerShortID > > shouldRelay(UInt256 const &key)
Determines whether the hashed item should be relayed.
void accept(Application &app, Rules const &rules, std::shared_ptr< Ledger const > const &ledger, OrderedTxs const &locals, bool retriesFirst, OrderedTxs &retries, ApplyFlags flags, std::string_view suffix="", ModifyType const &f={})
Accept a new ledger.
static Result applyOne(Application &app, OpenView &view, std::shared_ptr< STTx const > const &tx, bool retry, ApplyFlags flags, beast::Journal j)
beast::Journal const j_
Definition OpenLedger.h:45
bool empty() const
Returns true if there are no transactions.
std::function< bool(OpenView &, beast::Journal)> ModifyType
Signature for modification functions.
Definition OpenLedger.h:64
std::mutex currentMutex_
Definition OpenLedger.h:48
std::shared_ptr< OpenView const > current() const
Returns a view to the current open ledger.
std::mutex modifyMutex_
Definition OpenLedger.h:47
std::shared_ptr< OpenView > create(Rules const &rules, std::shared_ptr< Ledger const > const &ledger)
std::shared_ptr< OpenView const > current_
Definition OpenLedger.h:49
OpenLedger()=delete
static void apply(Application &app, OpenView &view, ReadView const &check, FwdRange const &txs, OrderedTxs &retries, ApplyFlags flags, beast::Journal j)
Algorithm for applying transactions.
Definition OpenLedger.h:209
CachedSLEs & cache_
Definition OpenLedger.h:46
bool modify(ModifyType const &f)
Modify the open ledger.
Writable ledger view that accumulates state and tx changes.
Definition OpenView.h:59
virtual std::set< Peer::ID > relay(protocol::TMProposeSet const &m, UInt256 const &uid, PublicKey const &validator)=0
Relay a proposal.
Rules controlling protocol behavior.
Definition Rules.h:40
std::size_t size() const noexcept
Definition Serializer.h:147
void const * data() const noexcept
Definition Serializer.h:153
virtual TxQ & getTxQ()=0
virtual HashRouter & getHashRouter()=0
virtual Overlay & getOverlay()=0
ApplyResult apply(Application &app, OpenView &view, std::shared_ptr< STTx const > const &tx, ApplyFlags flags, beast::Journal j)
Add a new transaction to the open ledger, hold it in the queue, or reject it.
Definition TxQ.cpp:1469
T make_shared(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ terQUEUED
Definition TER.h:227
bool set(T &target, std::string const &name, Section const &section)
Set a value from a configuration Section If the named value is not found or doesn't parse as a T,...
constexpr FlagValue tfInnerBatchTxn
Definition TxFlags.h:44
ApplyResult apply(ServiceRegistry &registry, OpenView &view, STTx const &tx, ApplyFlags flags, beast::Journal journal)
Apply a transaction to an OpenView.
Definition apply.cpp:181
TaggedCache< UInt256, SLE const > CachedSLEs
bool isTefFailure(TER x) noexcept
Definition TER.h:674
std::string debugTxstr(std::shared_ptr< STTx const > const &tx)
std::string debugTostr(OrderedTxs const &set)
ApplyFlags
Definition ApplyView.h:27
@ TapRetry
Definition ApplyView.h:36
bool isTelLocal(TER x) noexcept
Definition TER.h:662
constexpr struct xrpl::OpenLedgerT kOpenLedger
bool isTemMalformed(TER x) noexcept
Definition TER.h:668
CanonicalTXSet OrderedTxs
Definition OpenLedger.h:35
T str(T... args)
T what(T... args)