rippled
Loading...
Searching...
No Matches
OpenView.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/ledger/OpenView.h>
21
22#include <xrpl/basics/contract.h>
23
24namespace ripple {
25
26class OpenView::txs_iter_impl : public txs_type::iter_base
27{
28private:
30 txs_map::const_iterator iter_;
31
32public:
33 explicit txs_iter_impl(bool metadata, txs_map::const_iterator iter)
34 : metadata_(metadata), iter_(iter)
35 {
36 }
37
39 copy() const override
40 {
41 return std::make_unique<txs_iter_impl>(metadata_, iter_);
42 }
43
44 bool
45 equal(base_type const& impl) const override
46 {
47 if (auto const p = dynamic_cast<txs_iter_impl const*>(&impl))
48 return iter_ == p->iter_;
49 return false;
50 }
51
52 void
53 increment() override
54 {
55 ++iter_;
56 }
57
58 value_type
59 dereference() const override
60 {
61 value_type result;
62 {
63 SerialIter sit(iter_->second.txn->slice());
64 result.first = std::make_shared<STTx const>(sit);
65 }
66 if (metadata_)
67 {
68 SerialIter sit(iter_->second.meta->slice());
69 result.second = std::make_shared<STObject const>(sit, sfMetadata);
70 }
71 return result;
72 }
73};
74
75//------------------------------------------------------------------------------
76
78 : ReadView(rhs)
79 , TxsRawView(rhs)
80 , monotonic_resource_{std::make_unique<
81 boost::container::pmr::monotonic_buffer_resource>(initialBufferSize)}
82 , txs_{rhs.txs_, monotonic_resource_.get()}
83 , rules_{rhs.rules_}
84 , info_{rhs.info_}
85 , base_{rhs.base_}
86 , items_{rhs.items_}
87 , hold_{rhs.hold_}
88 , open_{rhs.open_} {};
89
92 ReadView const* base,
93 Rules const& rules,
95 : monotonic_resource_{std::make_unique<
96 boost::container::pmr::monotonic_buffer_resource>(initialBufferSize)}
97 , txs_{monotonic_resource_.get()}
98 , rules_(rules)
99 , info_(base->info())
100 , base_(base)
101 , hold_(std::move(hold))
102{
103 info_.validated = false;
104 info_.accepted = false;
105 info_.seq = base_->info().seq + 1;
108}
109
111 : monotonic_resource_{std::make_unique<
112 boost::container::pmr::monotonic_buffer_resource>(initialBufferSize)}
113 , txs_{monotonic_resource_.get()}
114 , rules_(base->rules())
115 , info_(base->info())
116 , base_(base)
117 , hold_(std::move(hold))
118 , open_(base->open())
119{
120}
121
124{
125 return baseTxCount_ + txs_.size();
126}
127
128void
130{
131 items_.apply(to);
132 for (auto const& item : txs_)
133 to.rawTxInsert(item.first, item.second.txn, item.second.meta);
134}
135
136//---
137
138LedgerInfo const&
140{
141 return info_;
142}
143
144Fees const&
146{
147 return base_->fees();
148}
149
150Rules const&
152{
153 return rules_;
154}
155
156bool
158{
159 return items_.exists(*base_, k);
160}
161
162auto
163OpenView::succ(key_type const& key, std::optional<key_type> const& last) const
165{
166 return items_.succ(*base_, key, last);
167}
168
170OpenView::read(Keylet const& k) const
171{
172 return items_.read(*base_, k);
173}
174
175auto
176OpenView::slesBegin() const -> std::unique_ptr<sles_type::iter_base>
177{
178 return items_.slesBegin(*base_);
179}
180
181auto
182OpenView::slesEnd() const -> std::unique_ptr<sles_type::iter_base>
183{
184 return items_.slesEnd(*base_);
185}
186
187auto
190{
191 return items_.slesUpperBound(*base_, key);
192}
193
194auto
195OpenView::txsBegin() const -> std::unique_ptr<txs_type::iter_base>
196{
197 return std::make_unique<txs_iter_impl>(!open(), txs_.cbegin());
198}
199
200auto
201OpenView::txsEnd() const -> std::unique_ptr<txs_type::iter_base>
202{
203 return std::make_unique<txs_iter_impl>(!open(), txs_.cend());
204}
205
206bool
208{
209 return txs_.find(key) != txs_.end();
210}
211
212auto
214{
215 auto const iter = txs_.find(key);
216 if (iter == txs_.end())
217 return base_->txRead(key);
218 auto const& item = iter->second;
219 auto stx = std::make_shared<STTx const>(SerialIter{item.txn->slice()});
220 decltype(tx_type::second) sto;
221 if (item.meta)
222 sto = std::make_shared<STObject const>(
223 SerialIter{item.meta->slice()}, sfMetadata);
224 else
225 sto = nullptr;
226 return {std::move(stx), std::move(sto)};
227}
228
229//---
230
231void
233{
234 items_.erase(sle);
235}
236
237void
239{
240 items_.insert(sle);
241}
242
243void
245{
246 items_.replace(sle);
247}
248
249void
251{
252 items_.destroyXRP(fee);
253 // VFALCO Deduct from info_.totalDrops ?
254 // What about child views?
255}
256
257//---
258
259void
261 key_type const& key,
263 std::shared_ptr<Serializer const> const& metaData)
264{
265 auto const result = txs_.emplace(
268 std::forward_as_tuple(txn, metaData));
269 if (!result.second)
270 LogicError("rawTxInsert: duplicate TX id: " + to_string(key));
271}
272
273} // namespace ripple
T cbegin(T... args)
std::unique_ptr< base_type > copy() const override
Definition: OpenView.cpp:39
txs_iter_impl(bool metadata, txs_map::const_iterator iter)
Definition: OpenView.cpp:33
bool equal(base_type const &impl) const override
Definition: OpenView.cpp:45
value_type dereference() const override
Definition: OpenView.cpp:59
txs_map::const_iterator iter_
Definition: OpenView.cpp:30
Writable ledger view that accumulates state and tx changes.
Definition: OpenView.h:66
std::size_t txCount() const
Return the number of tx inserted since creation.
Definition: OpenView.cpp:123
std::unique_ptr< sles_type::iter_base > slesUpperBound(uint256 const &key) const override
Definition: OpenView.cpp:188
tx_type txRead(key_type const &key) const override
Read a transaction from the tx map.
Definition: OpenView.cpp:213
bool txExists(key_type const &key) const override
Returns true if a tx exists in the tx map.
Definition: OpenView.cpp:207
ReadView const * base_
Definition: OpenView.h:106
LedgerInfo const & info() const override
Returns information about the ledger.
Definition: OpenView.cpp:139
std::unique_ptr< sles_type::iter_base > slesEnd() const override
Definition: OpenView.cpp:182
detail::RawStateTable items_
Definition: OpenView.h:107
std::optional< key_type > succ(key_type const &key, std::optional< key_type > const &last=std::nullopt) const override
Return the key of the next state item.
Definition: OpenView.cpp:163
LedgerInfo info_
Definition: OpenView.h:105
void rawDestroyXRP(XRPAmount const &fee) override
Destroy XRP.
Definition: OpenView.cpp:250
bool exists(Keylet const &k) const override
Determine if a state item exists.
Definition: OpenView.cpp:157
Rules const & rules() const override
Returns the tx processing rules.
Definition: OpenView.cpp:151
std::shared_ptr< SLE const > read(Keylet const &k) const override
Return the state item associated with a key.
Definition: OpenView.cpp:170
std::unique_ptr< sles_type::iter_base > slesBegin() const override
Definition: OpenView.cpp:176
void rawTxInsert(key_type const &key, std::shared_ptr< Serializer const > const &txn, std::shared_ptr< Serializer const > const &metaData) override
Add a transaction to the tx map.
Definition: OpenView.cpp:260
void rawErase(std::shared_ptr< SLE > const &sle) override
Delete an existing state item.
Definition: OpenView.cpp:232
std::size_t baseTxCount_
In batch mode, the number of transactions already executed.
Definition: OpenView.h:111
void rawReplace(std::shared_ptr< SLE > const &sle) override
Unconditionally replace a state item.
Definition: OpenView.cpp:244
void rawInsert(std::shared_ptr< SLE > const &sle) override
Unconditionally insert a state item.
Definition: OpenView.cpp:238
txs_map txs_
Definition: OpenView.h:103
bool open() const override
Returns true if this reflects an open ledger.
Definition: OpenView.h:192
std::unique_ptr< txs_type::iter_base > txsBegin() const override
Definition: OpenView.cpp:195
Fees const & fees() const override
Returns the fees for the base ledger.
Definition: OpenView.cpp:145
void apply(TxsRawView &to) const
Apply changes.
Definition: OpenView.cpp:129
std::unique_ptr< txs_type::iter_base > txsEnd() const override
Definition: OpenView.cpp:201
A view into a ledger.
Definition: ReadView.h:52
virtual Fees const & fees() const =0
Returns the fees for the base ledger.
virtual LedgerInfo const & info() const =0
Returns information about the ledger.
Rules controlling protocol behavior.
Definition: Rules.h:38
Interface for changing ledger entries with transactions.
Definition: RawView.h:96
virtual void rawTxInsert(ReadView::key_type const &key, std::shared_ptr< Serializer const > const &txn, std::shared_ptr< Serializer const > const &metaData)=0
Add a transaction to the tx map.
void destroyXRP(XRPAmount const &fee)
bool exists(ReadView const &base, Keylet const &k) const
void apply(RawView &to) const
void insert(std::shared_ptr< SLE > const &sle)
void erase(std::shared_ptr< SLE > const &sle)
std::shared_ptr< SLE const > read(ReadView const &base, Keylet const &k) const
std::unique_ptr< ReadView::sles_type::iter_base > slesBegin(ReadView const &base) const
void replace(std::shared_ptr< SLE > const &sle)
std::unique_ptr< ReadView::sles_type::iter_base > slesEnd(ReadView const &base) const
T emplace(T... args)
T cend(T... args)
T find(T... args)
T forward_as_tuple(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:25
@ open
We haven't closed our ledger yet, but others might have.
std::string to_string(base_uint< Bits, Tag > const &a)
Definition: base_uint.h:630
T get(Section const &section, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
Definition: BasicConfig.h:355
void LogicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
STL namespace.
T piecewise_construct
T size(T... args)
Reflects the fee settings for a particular ledger.
Definition: protocol/Fees.h:33
A pair of SHAMap key and LedgerEntryType.
Definition: Keylet.h:39
Information about the notional ledger backing the view.
Definition: LedgerHeader.h:34
NetClock::time_point closeTime
Definition: LedgerHeader.h:72
NetClock::time_point parentCloseTime
Definition: LedgerHeader.h:42
Open ledger construction tag.
Definition: OpenView.h:45