rippled
Loading...
Searching...
No Matches
OpenLedger.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/OpenLedger.h>
21#include <xrpld/app/main/Application.h>
22#include <xrpld/app/misc/HashRouter.h>
23#include <xrpld/app/misc/TxQ.h>
24#include <xrpld/app/tx/apply.h>
25#include <xrpld/ledger/CachedView.h>
26#include <xrpld/overlay/Message.h>
27#include <xrpld/overlay/Overlay.h>
28
29#include <boost/range/adaptor/transformed.hpp>
30
31namespace ripple {
32
35 CachedSLEs& cache,
36 beast::Journal journal)
37 : j_(journal), cache_(cache), current_(create(ledger->rules(), ledger))
38{
39}
40
41bool
43{
45 return current_->txCount() == 0;
46}
47
50{
52 return current_;
53}
54
55bool
57{
59 auto next = std::make_shared<OpenView>(*current_);
60 auto const changed = f(*next, j_);
61 if (changed)
62 {
64 current_ = std::move(next);
65 }
66 return changed;
67}
68
69void
71 Application& app,
72 Rules const& rules,
74 OrderedTxs const& locals,
75 bool retriesFirst,
76 OrderedTxs& retries,
77 ApplyFlags flags,
78 std::string const& suffix,
79 modify_type const& f)
80{
81 JLOG(j_.trace()) << "accept ledger " << ledger->seq() << " " << suffix;
82 auto next = create(rules, ledger);
83 if (retriesFirst)
84 {
85 // Handle disputed tx, outside lock
87 apply(app, *next, *ledger, empty{}, retries, flags, j_);
88 }
89 // Block calls to modify, otherwise
90 // new tx going into the open ledger
91 // would get lost.
93 // Apply tx from the current open view
94 if (!current_->txs.empty())
95 {
96 apply(
97 app,
98 *next,
99 *ledger,
100 boost::adaptors::transform(
101 current_->txs,
102 [](std::pair<
105 return p.first;
106 }),
107 retries,
108 flags,
109 j_);
110 }
111 // Call the modifier
112 if (f)
113 f(*next, j_);
114 // Apply local tx
115 for (auto const& item : locals)
116 app.getTxQ().apply(app, *next, item.second, flags, j_);
117
118 // If we didn't relay this transaction recently, relay it to all peers
119 for (auto const& txpair : next->txs)
120 {
121 auto const& tx = txpair.first;
122 auto const txId = tx->getTransactionID();
123 if (auto const toSkip = app.getHashRouter().shouldRelay(txId))
124 {
125 JLOG(j_.debug()) << "Relaying recovered tx " << txId;
126 protocol::TMTransaction msg;
127 Serializer s;
128
129 tx->add(s);
130 msg.set_rawtransaction(s.data(), s.size());
131 msg.set_status(protocol::tsNEW);
132 msg.set_receivetimestamp(
133 app.timeKeeper().now().time_since_epoch().count());
134 app.overlay().relay(txId, msg, *toSkip);
135 }
136 }
137
138 // Switch to the new open view
140 current_ = std::move(next);
141}
142
143//------------------------------------------------------------------------------
144
147 Rules const& rules,
148 std::shared_ptr<Ledger const> const& ledger)
149{
150 return std::make_shared<OpenView>(
152 rules,
153 std::make_shared<CachedLedger const>(ledger, cache_));
154}
155
156auto
158 Application& app,
159 OpenView& view,
161 bool retry,
162 ApplyFlags flags,
164{
165 if (retry)
166 flags = flags | tapRETRY;
167 // If it's in anybody's proposed set, try to keep it in the ledger
168 auto const result = ripple::apply(app, view, *tx, flags, j);
169 if (result.applied || result.ter == terQUEUED)
170 return Result::success;
171 if (isTefFailure(result.ter) || isTemMalformed(result.ter) ||
172 isTelLocal(result.ter))
173 return Result::failure;
174 return Result::retry;
175}
176
177//------------------------------------------------------------------------------
178
181{
183 ss << tx->getTransactionID();
184 return ss.str().substr(0, 4);
185}
186
189{
191 for (auto const& item : set)
192 ss << debugTxstr(item.second) << ", ";
193 return ss.str();
194}
195
198{
200 for (auto const& item : set)
201 {
202 try
203 {
204 SerialIter sit(item.slice());
205 auto const tx = std::make_shared<STTx const>(sit);
206 ss << debugTxstr(tx) << ", ";
207 }
208 catch (std::exception const& ex)
209 {
210 ss << "THROW:" << ex.what() << ", ";
211 }
212 }
213 return ss.str();
214}
215
218{
220 for (auto const& item : view->txs)
221 ss << debugTxstr(item.first) << ", ";
222 return ss.str();
223}
224
225} // namespace ripple
A generic endpoint for log messages.
Definition: Journal.h:60
Stream debug() const
Definition: Journal.h:328
Stream trace() const
Severity stream access functions.
Definition: Journal.h:322
virtual Overlay & overlay()=0
virtual TimeKeeper & timeKeeper()=0
virtual TxQ & getTxQ()=0
virtual HashRouter & getHashRouter()=0
Holds transactions which were deferred to the next pass of consensus.
std::optional< std::set< PeerShortID > > shouldRelay(uint256 const &key)
Determines whether the hashed item should be relayed.
Definition: HashRouter.cpp:118
bool modify(modify_type const &f)
Modify the open ledger.
Definition: OpenLedger.cpp:56
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 const &suffix="", modify_type const &f={})
Accept a new ledger.
Definition: OpenLedger.cpp:70
CachedSLEs & cache_
Definition: OpenLedger.h:55
std::shared_ptr< OpenView > create(Rules const &rules, std::shared_ptr< Ledger const > const &ledger)
Definition: OpenLedger.cpp:146
bool empty() const
Returns true if there are no transactions.
Definition: OpenLedger.cpp:42
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:211
std::mutex modify_mutex_
Definition: OpenLedger.h:56
static Result apply_one(Application &app, OpenView &view, std::shared_ptr< STTx const > const &tx, bool retry, ApplyFlags flags, beast::Journal j)
Definition: OpenLedger.cpp:157
std::shared_ptr< OpenView const > current_
Definition: OpenLedger.h:58
beast::Journal const j_
Definition: OpenLedger.h:54
std::shared_ptr< OpenView const > current() const
Returns a view to the current open ledger.
Definition: OpenLedger.cpp:49
std::mutex current_mutex_
Definition: OpenLedger.h:57
Writable ledger view that accumulates state and tx changes.
Definition: OpenView.h:57
virtual std::set< Peer::id_t > relay(protocol::TMProposeSet &m, uint256 const &uid, PublicKey const &validator)=0
Relay a proposal.
Rules controlling protocol behavior.
Definition: Rules.h:35
A SHAMap is both a radix tree with a fan-out of 16 and a Merkle tree.
Definition: SHAMap.h:98
std::size_t size() const noexcept
Definition: Serializer.h:73
void const * data() const noexcept
Definition: Serializer.h:79
Map/cache combination.
Definition: TaggedCache.h:62
time_point now() const override
Returns the current time, using the server's clock.
Definition: TimeKeeper.h:64
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:730
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
bool isTelLocal(TER x)
Definition: TER.h:632
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,...
Definition: BasicConfig.h:315
bool isTemMalformed(TER x)
Definition: TER.h:638
std::string debugTostr(OrderedTxs const &set)
Definition: OpenLedger.cpp:188
open_ledger_t const open_ledger
Definition: OpenView.cpp:26
ApplyResult apply(Application &app, OpenView &view, STTx const &tx, ApplyFlags flags, beast::Journal journal)
Apply a transaction to an OpenView.
Definition: apply.cpp:110
bool isTefFailure(TER x)
Definition: TER.h:644
ApplyFlags
Definition: ApplyView.h:31
@ tapRETRY
Definition: ApplyView.h:40
@ terQUEUED
Definition: TER.h:225
std::string debugTxstr(std::shared_ptr< STTx const > const &tx)
Definition: OpenLedger.cpp:180
T str(T... args)
T what(T... args)