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