xrpld
Loading...
Searching...
No Matches
OpenView.cpp
1#include <xrpl/ledger/OpenView.h>
2
3#include <xrpl/basics/base_uint.h>
4#include <xrpl/basics/contract.h>
5#include <xrpl/ledger/RawView.h>
6#include <xrpl/ledger/ReadView.h>
7#include <xrpl/protocol/Fees.h>
8#include <xrpl/protocol/Keylet.h>
9#include <xrpl/protocol/SField.h>
10#include <xrpl/protocol/STLedgerEntry.h>
11#include <xrpl/protocol/STObject.h>
12#include <xrpl/protocol/STTx.h>
13#include <xrpl/protocol/Serializer.h>
14#include <xrpl/protocol/XRPAmount.h>
15
16#include <boost/container/pmr/monotonic_buffer_resource.hpp>
17
18#include <cstddef>
19#include <format>
20#include <memory>
21#include <optional>
22#include <stdexcept>
23#include <tuple>
24#include <utility>
25
26namespace xrpl {
27
28class OpenView::TxsIterImpl : public TxsType::IterBase
29{
30private:
32 TxsMap::const_iterator iter_;
33
34public:
35 explicit TxsIterImpl(bool metadata, TxsMap::const_iterator iter)
36 : metadata_(metadata), iter_(iter)
37 {
38 }
39
40 [[nodiscard]] std::unique_ptr<BaseType>
41 copy() const override
42 {
44 }
45
46 [[nodiscard]] bool
47 equal(BaseType const& impl) const override
48 {
49 if (auto const p = dynamic_cast<TxsIterImpl const*>(&impl))
50 return iter_ == p->iter_;
51 return false;
52 }
53
54 void
55 increment() override
56 {
57 ++iter_;
58 }
59
60 [[nodiscard]] 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 , monotonicResource_{std::make_unique<boost::container::pmr::monotonic_buffer_resource>(
85 , rules_{rhs.rules_}
86 , header_{rhs.header_}
87 , base_{rhs.base_}
88 , items_{rhs.items_}
89 , hold_{rhs.hold_}
90 , open_{rhs.open_} {};
91
94 std::make_unique<boost::container::pmr::monotonic_buffer_resource>(kInitialBufferSize)}
96 , rules_(std::move(rules))
97 , header_(base->header())
98 , base_(base)
99 , hold_(std::move(hold))
100{
101 header_.validated = false;
102 header_.accepted = false;
103 header_.seq = base_->header().seq + 1;
104 header_.parentCloseTime = base_->header().closeTime;
105 header_.parentHash = base_->header().hash;
106}
107
110 std::make_unique<boost::container::pmr::monotonic_buffer_resource>(kInitialBufferSize)}
112 , rules_(base->rules())
113 , header_(base->header())
114 , base_(base)
115 , hold_(std::move(hold))
116 , open_(base->open())
117{
118}
119
122{
123 return baseTxCount_ + txs_.size();
124}
125
126void
128{
129 items_.apply(to);
130 for (auto const& item : txs_)
131 to.rawTxInsert(item.first, item.second.txn, item.second.meta);
132}
133
134//---
135
136LedgerHeader const&
138{
139 return header_;
140}
141
142Fees const&
144{
145 return base_->fees();
146}
147
148Rules const&
150{
151 return rules_;
152}
153
154bool
156{
157 return items_.exists(*base_, k);
158}
159
160auto
161OpenView::succ(key_type const& key, std::optional<key_type> const& last) const
163{
164 return items_.succ(*base_, key, last);
165}
166
168OpenView::read(Keylet const& k) const
169{
170 return items_.read(*base_, k);
171}
172
173auto
174OpenView::slesBegin() const -> std::unique_ptr<SlesType::IterBase>
175{
176 return items_.slesBegin(*base_);
177}
178
179auto
180OpenView::slesEnd() const -> std::unique_ptr<SlesType::IterBase>
181{
182 return items_.slesEnd(*base_);
183}
184
185auto
187{
188 return items_.slesUpperBound(*base_, key);
189}
190
191auto
192OpenView::txsBegin() const -> std::unique_ptr<TxsType::IterBase>
193{
194 return std::make_unique<TxsIterImpl>(!open(), txs_.cbegin());
195}
196
197auto
198OpenView::txsEnd() const -> std::unique_ptr<TxsType::IterBase>
199{
200 return std::make_unique<TxsIterImpl>(!open(), txs_.cend());
201}
202
203bool
205{
206 return txs_.contains(key);
207}
208
209auto
210OpenView::txRead(key_type const& key) const -> TxType
211{
212 auto const iter = txs_.find(key);
213 if (iter == txs_.end())
214 return base_->txRead(key);
215 auto const& item = iter->second;
216 auto stx = std::make_shared<STTx const>(SerialIter{item.txn->slice()});
217 decltype(TxType::second) sto;
218 if (item.meta)
219 {
220 sto = std::make_shared<STObject const>(SerialIter{item.meta->slice()}, sfMetadata);
221 }
222 else
223 {
224 sto = nullptr;
225 }
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 header_.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(
267 if (!result.second)
268 Throw<std::logic_error>(std::format("rawTxInsert: duplicate TX id: {}", to_string(key)));
269}
270
271} // namespace xrpl
TxsIterImpl(bool metadata, TxsMap::const_iterator iter)
Definition OpenView.cpp:35
std::unique_ptr< BaseType > copy() const override
Definition OpenView.cpp:41
TxsMap::const_iterator iter_
Definition OpenView.cpp:32
bool equal(BaseType const &impl) const override
Definition OpenView.cpp:47
void increment() override
Definition OpenView.cpp:55
value_type dereference() const override
Definition OpenView.cpp:61
static constexpr size_t kInitialBufferSize
Definition OpenView.h:64
void rawDestroyXRP(XRPAmount const &fee) override
Destroy XRP.
Definition OpenView.cpp:250
std::size_t txCount() const
Return the number of tx inserted since creation.
Definition OpenView.cpp:121
TxType txRead(key_type const &key) const override
Read a transaction from the tx map.
Definition OpenView.cpp:210
OpenView()=delete
LedgerHeader header_
Definition OpenView.h:96
TxsMap txs_
Definition OpenView.h:94
Fees const & fees() const override
Returns the fees for the base ledger.
Definition OpenView.cpp:143
std::unique_ptr< TxsType::IterBase > txsEnd() const override
Definition OpenView.cpp:198
void rawReplace(SLE::Ref sle) override
Unconditionally replace a state item.
Definition OpenView.cpp:244
std::size_t baseTxCount_
In batch mode, the number of transactions already executed.
Definition OpenView.h:104
std::unique_ptr< SlesType::IterBase > slesUpperBound(UInt256 const &key) const override
Definition OpenView.cpp:186
bool txExists(key_type const &key) const override
Returns true if a tx exists in the tx map.
Definition OpenView.cpp:204
std::shared_ptr< void const > hold_
Definition OpenView.h:99
bool open() const override
Returns true if this reflects an open ledger.
Definition OpenView.h:187
std::unique_ptr< SlesType::IterBase > slesBegin() const override
Definition OpenView.cpp:174
SLE::const_pointer read(Keylet const &k) const override
Return the state item associated with a key.
Definition OpenView.cpp:168
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:161
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
ReadView const * base_
Definition OpenView.h:97
void rawInsert(SLE::Ref sle) override
Unconditionally insert a state item.
Definition OpenView.cpp:238
LedgerHeader const & header() const override
Returns information about the ledger.
Definition OpenView.cpp:137
void apply(TxsRawView &to) const
Apply changes.
Definition OpenView.cpp:127
Rules const & rules() const override
Returns the tx processing rules.
Definition OpenView.cpp:149
bool exists(Keylet const &k) const override
Determine if a state item exists.
Definition OpenView.cpp:155
std::unique_ptr< TxsType::IterBase > txsBegin() const override
Definition OpenView.cpp:192
std::unique_ptr< boost::container::pmr::monotonic_buffer_resource > monotonicResource_
Definition OpenView.h:93
void rawErase(SLE::Ref sle) override
Delete an existing state item.
Definition OpenView.cpp:232
std::unique_ptr< SlesType::IterBase > slesEnd() const override
Definition OpenView.cpp:180
detail::RawStateTable items_
Definition OpenView.h:98
UInt256 key_type
Definition ReadView.h:45
LedgerIndex seq() const
Returns the sequence number of the base ledger.
Definition ReadView.h:115
std::pair< std::shared_ptr< STTx const >, std::shared_ptr< STObject const > > TxType
Definition ReadView.h:43
Rules controlling protocol behavior.
Definition Rules.h:40
std::shared_ptr< STLedgerEntry > const & Ref
std::shared_ptr< STLedgerEntry const > const_pointer
Interface for changing ledger entries with transactions.
Definition RawView.h:84
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.
T format(T... args)
T forward_as_tuple(T... args)
T make_shared(T... args)
T make_unique(T... args)
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
T get(Section const &section, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:657
BaseUInt< 256 > UInt256
Definition base_uint.h:580
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:52
T piecewise_construct
Reflects the fee settings for a particular ledger.
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:20
Information about the notional ledger backing the view.
Open ledger construction tag.
Definition OpenView.h:36