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