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