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),
95 }
97 , rules_(std::move(rules))
98 , header_(base->header())
99 , base_(base)
100 , hold_(std::move(hold))
101{
102 header_.validated = false;
103 header_.accepted = false;
104 header_.seq = base_->header().seq + 1;
105 header_.parentCloseTime = base_->header().closeTime;
106 header_.parentHash = base_->header().hash;
107}
108
111 std::make_unique<boost::container::pmr::monotonic_buffer_resource>(kInitialBufferSize),
112 }
114 , rules_(base->rules())
115 , header_(base->header())
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
138LedgerHeader const&
140{
141 return header_;
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<SlesType::IterBase>
177{
178 return items_.slesBegin(*base_);
179}
180
181auto
182OpenView::slesEnd() const -> std::unique_ptr<SlesType::IterBase>
183{
184 return items_.slesEnd(*base_);
185}
186
187auto
189{
190 return items_.slesUpperBound(*base_, key);
191}
192
193auto
194OpenView::txsBegin() const -> std::unique_ptr<TxsType::IterBase>
195{
196 return std::make_unique<TxsIterImpl>(!open(), txs_.cbegin());
197}
198
199auto
200OpenView::txsEnd() const -> std::unique_ptr<TxsType::IterBase>
201{
202 return std::make_unique<TxsIterImpl>(!open(), txs_.cend());
203}
204
205bool
207{
208 return txs_.contains(key);
209}
210
211auto
212OpenView::txRead(key_type const& key) const -> TxType
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(TxType::second) sto;
220 if (item.meta)
221 {
222 sto = std::make_shared<STObject const>(SerialIter{item.meta->slice()}, sfMetadata);
223 }
224 else
225 {
226 sto = nullptr;
227 }
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 header_.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(
269 if (!result.second)
270 Throw<std::logic_error>(std::format("rawTxInsert: duplicate TX id: {}", to_string(key)));
271}
272
273} // 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:252
std::size_t txCount() const
Return the number of tx inserted since creation.
Definition OpenView.cpp:123
TxType txRead(key_type const &key) const override
Read a transaction from the tx map.
Definition OpenView.cpp:212
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:145
std::unique_ptr< TxsType::IterBase > txsEnd() const override
Definition OpenView.cpp:200
void rawReplace(SLE::Ref sle) override
Unconditionally replace a state item.
Definition OpenView.cpp:246
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:188
bool txExists(key_type const &key) const override
Returns true if a tx exists in the tx map.
Definition OpenView.cpp:206
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:176
SLE::const_pointer read(Keylet const &k) const override
Return the state item associated with a key.
Definition OpenView.cpp:170
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
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
ReadView const * base_
Definition OpenView.h:97
void rawInsert(SLE::Ref sle) override
Unconditionally insert a state item.
Definition OpenView.cpp:240
LedgerHeader const & header() const override
Returns information about the ledger.
Definition OpenView.cpp:139
void apply(TxsRawView &to) const
Apply changes.
Definition OpenView.cpp:129
Rules const & rules() const override
Returns the tx processing rules.
Definition OpenView.cpp:151
bool exists(Keylet const &k) const override
Determine if a state item exists.
Definition OpenView.cpp:157
std::unique_ptr< TxsType::IterBase > txsBegin() const override
Definition OpenView.cpp:194
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:234
std::unique_ptr< SlesType::IterBase > slesEnd() const override
Definition OpenView.cpp:182
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:658
BaseUInt< 256 > UInt256
Definition base_uint.h:581
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