/
ReadView.h
288 lines (228 loc) · 8.26 KB
/
ReadView.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifndef RIPPLE_LEDGER_READVIEW_H_INCLUDED
#define RIPPLE_LEDGER_READVIEW_H_INCLUDED
#include <ripple/basics/FeeUnits.h>
#include <ripple/basics/IOUAmount.h>
#include <ripple/basics/XRPAmount.h>
#include <ripple/basics/chrono.h>
#include <ripple/beast/hash/uhash.h>
#include <ripple/beast/utility/Journal.h>
#include <ripple/ledger/detail/ReadViewFwdRange.h>
#include <ripple/protocol/Fees.h>
#include <ripple/protocol/Indexes.h>
#include <ripple/protocol/LedgerHeader.h>
#include <ripple/protocol/Protocol.h>
#include <ripple/protocol/Rules.h>
#include <ripple/protocol/STAmount.h>
#include <ripple/protocol/STLedgerEntry.h>
#include <ripple/protocol/STTx.h>
#include <cassert>
#include <cstdint>
#include <memory>
#include <optional>
#include <unordered_set>
namespace ripple {
//------------------------------------------------------------------------------
/** A view into a ledger.
This interface provides read access to state
and transaction items. There is no checkpointing
or calculation of metadata.
*/
class ReadView
{
public:
using tx_type =
std::pair<std::shared_ptr<STTx const>, std::shared_ptr<STObject const>>;
using key_type = uint256;
using mapped_type = std::shared_ptr<SLE const>;
struct sles_type : detail::ReadViewFwdRange<std::shared_ptr<SLE const>>
{
explicit sles_type(ReadView const& view);
iterator
begin() const;
iterator
end() const;
iterator
upper_bound(key_type const& key) const;
};
struct txs_type : detail::ReadViewFwdRange<tx_type>
{
explicit txs_type(ReadView const& view);
bool
empty() const;
iterator
begin() const;
iterator
end() const;
};
virtual ~ReadView() = default;
ReadView&
operator=(ReadView&& other) = delete;
ReadView&
operator=(ReadView const& other) = delete;
ReadView() : sles(*this), txs(*this)
{
}
ReadView(ReadView const& other) : sles(*this), txs(*this)
{
}
ReadView(ReadView&& other) : sles(*this), txs(*this)
{
}
/** Returns information about the ledger. */
virtual LedgerInfo const&
info() const = 0;
/** Returns true if this reflects an open ledger. */
virtual bool
open() const = 0;
/** Returns the close time of the previous ledger. */
NetClock::time_point
parentCloseTime() const
{
return info().parentCloseTime;
}
/** Returns the sequence number of the base ledger. */
LedgerIndex
seq() const
{
return info().seq;
}
/** Returns the fees for the base ledger. */
virtual Fees const&
fees() const = 0;
/** Returns the tx processing rules. */
virtual Rules const&
rules() const = 0;
/** Determine if a state item exists.
@note This can be more efficient than calling read.
@return `true` if a SLE is associated with the
specified key.
*/
virtual bool
exists(Keylet const& k) const = 0;
/** Return the key of the next state item.
This returns the key of the first state item
whose key is greater than the specified key. If
no such key is present, std::nullopt is returned.
If `last` is engaged, returns std::nullopt when
the key returned would be outside the open
interval (key, last).
*/
virtual std::optional<key_type>
succ(
key_type const& key,
std::optional<key_type> const& last = std::nullopt) const = 0;
/** Return the state item associated with a key.
Effects:
If the key exists, gives the caller ownership
of the non-modifiable corresponding SLE.
@note While the returned SLE is `const` from the
perspective of the caller, it can be changed
by other callers through raw operations.
@return `nullptr` if the key is not present or
if the type does not match.
*/
virtual std::shared_ptr<SLE const>
read(Keylet const& k) const = 0;
// Accounts in a payment are not allowed to use assets acquired during that
// payment. The PaymentSandbox tracks the debits, credits, and owner count
// changes that accounts make during a payment. `balanceHook` adjusts
// balances so newly acquired assets are not counted toward the balance.
// This is required to support PaymentSandbox.
virtual STAmount
balanceHook(
AccountID const& account,
AccountID const& issuer,
STAmount const& amount) const
{
return amount;
}
// Accounts in a payment are not allowed to use assets acquired during that
// payment. The PaymentSandbox tracks the debits, credits, and owner count
// changes that accounts make during a payment. `ownerCountHook` adjusts the
// ownerCount so it returns the max value of the ownerCount so far.
// This is required to support PaymentSandbox.
virtual std::uint32_t
ownerCountHook(AccountID const& account, std::uint32_t count) const
{
return count;
}
// used by the implementation
virtual std::unique_ptr<sles_type::iter_base>
slesBegin() const = 0;
// used by the implementation
virtual std::unique_ptr<sles_type::iter_base>
slesEnd() const = 0;
// used by the implementation
virtual std::unique_ptr<sles_type::iter_base>
slesUpperBound(key_type const& key) const = 0;
// used by the implementation
virtual std::unique_ptr<txs_type::iter_base>
txsBegin() const = 0;
// used by the implementation
virtual std::unique_ptr<txs_type::iter_base>
txsEnd() const = 0;
/** Returns `true` if a tx exists in the tx map.
A tx exists in the map if it is part of the
base ledger, or if it is a newly inserted tx.
*/
virtual bool
txExists(key_type const& key) const = 0;
/** Read a transaction from the tx map.
If the view represents an open ledger,
the metadata object will be empty.
@return A pair of nullptr if the
key is not found in the tx map.
*/
virtual tx_type
txRead(key_type const& key) const = 0;
//
// Memberspaces
//
/** Iterable range of ledger state items.
@note Visiting each state entry in the ledger can
become quite expensive as the ledger grows.
*/
sles_type sles;
// The range of transactions
txs_type txs;
};
//------------------------------------------------------------------------------
/** ReadView that associates keys with digests. */
class DigestAwareReadView : public ReadView
{
public:
using digest_type = uint256;
DigestAwareReadView() = default;
DigestAwareReadView(const DigestAwareReadView&) = default;
/** Return the digest associated with the key.
@return std::nullopt if the item does not exist.
*/
virtual std::optional<digest_type>
digest(key_type const& key) const = 0;
};
//------------------------------------------------------------------------------
Rules
makeRulesGivenLedger(DigestAwareReadView const& ledger, Rules const& current);
Rules
makeRulesGivenLedger(
DigestAwareReadView const& ledger,
std::unordered_set<uint256, beast::uhash<>> const& presets);
} // namespace ripple
#include <ripple/ledger/detail/ReadViewFwdRange.ipp>
#endif