forked from libbitcoin/libbitcoin-database
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patharchive_read.ipp
More file actions
452 lines (374 loc) · 11.1 KB
/
archive_read.ipp
File metadata and controls
452 lines (374 loc) · 11.1 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/**
* Copyright (c) 2011-2026 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBBITCOIN_DATABASE_QUERY_ARCHIVE_READ_IPP
#define LIBBITCOIN_DATABASE_QUERY_ARCHIVE_READ_IPP
#include <atomic>
#include <numeric>
#include <utility>
#include <bitcoin/database/define.hpp>
#include <bitcoin/database/tables/tables.hpp>
namespace libbitcoin {
namespace database {
// Bools.
// ----------------------------------------------------------------------------
TEMPLATE
inline bool CLASS::is_header(const hash_digest& key) const NOEXCEPT
{
return store_.header.exists(key);
}
TEMPLATE
inline bool CLASS::is_block(const hash_digest& key) const NOEXCEPT
{
return is_associated(to_header(key));
}
TEMPLATE
inline bool CLASS::is_tx(const hash_digest& key) const NOEXCEPT
{
return store_.tx.exists(key);
}
TEMPLATE
inline bool CLASS::is_coinbase(const tx_link& link) const NOEXCEPT
{
table::transaction::get_coinbase tx{};
return store_.tx.get(link, tx) && tx.coinbase;
}
TEMPLATE
inline bool CLASS::is_tx_segregated(const tx_link& link) const NOEXCEPT
{
size_t light{}, heavy{};
return get_tx_sizes(light, heavy, link) && heavy != light;
}
TEMPLATE
inline bool CLASS::is_block_segregated(const header_link& link) const NOEXCEPT
{
size_t light{}, heavy{};
return get_block_sizes(light, heavy, link) && heavy != light;
}
TEMPLATE
inline bool CLASS::is_milestone(const header_link& link) const NOEXCEPT
{
table::header::get_milestone header{};
return store_.header.get(link, header) && header.milestone;
}
TEMPLATE
inline bool CLASS::is_associated(const header_link& link) const NOEXCEPT
{
table::txs::get_associated txs{};
return store_.txs.at(to_txs(link), txs) && txs.associated;
}
TEMPLATE
inline bool CLASS::is_confirmable(const header_link& link) const NOEXCEPT
{
return get_header_state(link) == error::block_confirmable;
}
// Empty/null_hash implies fault, zero count implies unassociated.
// ----------------------------------------------------------------------------
TEMPLATE
hash_digest CLASS::get_top_confirmed_hash() const NOEXCEPT
{
return get_header_key(to_confirmed(get_top_confirmed()));
}
TEMPLATE
hash_digest CLASS::get_top_candidate_hash() const NOEXCEPT
{
return get_header_key(to_candidate(get_top_candidate()));
}
TEMPLATE
hashes CLASS::get_tx_keys(const header_link& link) const NOEXCEPT
{
const auto tx_fks = to_transactions(link);
if (tx_fks.empty())
return {};
system::hashes hashes{};
hashes.reserve(tx_fks.size());
for (const auto& tx_fk: tx_fks)
hashes.push_back(get_tx_key(tx_fk));
// Return of any null_hash implies failure.
return hashes;
}
TEMPLATE
size_t CLASS::get_tx_count(const header_link& link) const NOEXCEPT
{
table::txs::get_tx_count txs{};
if (!store_.txs.at(to_txs(link), txs))
return {};
return txs.number;
}
TEMPLATE
inline hash_digest CLASS::get_header_key(const header_link& link) const NOEXCEPT
{
return store_.header.get_key(link);
}
TEMPLATE
inline hash_digest CLASS::get_tx_key(const tx_link& link) const NOEXCEPT
{
return store_.tx.get_key(link);
}
TEMPLATE
inline point_key CLASS::get_point_key(const point_link& link) const NOEXCEPT
{
table::point::get_composed point{};
if (!store_.point.get(link, point))
return {};
return point.key;
}
TEMPLATE
inline hash_digest CLASS::get_point_hash(const point_link& link) const NOEXCEPT
{
table::point::record point{};
if (!store_.point.get(link, point))
return {};
return point.hash;
}
// Position.
// ----------------------------------------------------------------------------
TEMPLATE
bool CLASS::get_tx_height(size_t& out, const tx_link& link) const NOEXCEPT
{
const auto fk = to_strong(link);
return is_confirmed_block(fk) && get_height(out, fk);
}
TEMPLATE
bool CLASS::get_tx_position(size_t& out, const tx_link& link) const NOEXCEPT
{
const auto fk = to_strong(link);
if (!is_confirmed_block(fk))
return false;
// False return below implies an integrity error (tx should be indexed).
table::txs::get_position txs{ {}, link };
if (!store_.txs.at(to_txs(fk), txs))
return false;
out = txs.position;
return true;
}
TEMPLATE
tx_link CLASS::get_position_tx(const header_link& link,
size_t position) const NOEXCEPT
{
table::txs::get_at_position txs{ {}, position };
if (!store_.txs.at(to_txs(link), txs))
return {};
return txs.tx_fk;
}
// Sizes.
// ----------------------------------------------------------------------------
TEMPLATE
bool CLASS::get_tx_size(size_t& out, const tx_link& link,
bool witness) const NOEXCEPT
{
size_t light{}, heavy{};
if (!get_tx_sizes(light, heavy, link))
return false;
out = witness ? heavy : light;
return true;
}
TEMPLATE
bool CLASS::get_block_size(size_t& out, const header_link& link,
bool witness) const NOEXCEPT
{
size_t light{}, heavy{};
if (!get_block_sizes(light, heavy, link))
return false;
out = witness ? heavy : light;
return true;
}
TEMPLATE
bool CLASS::get_tx_sizes(size_t& light, size_t& heavy,
const tx_link& link) const NOEXCEPT
{
table::transaction::get_sizes sizes{};
if (!store_.tx.get(link, sizes))
return false;
light = sizes.light;
heavy = sizes.heavy;
return true;
}
TEMPLATE
bool CLASS::get_block_sizes(size_t& light, size_t& heavy,
const header_link& link) const NOEXCEPT
{
table::txs::get_sizes sizes{};
if (!store_.txs.at(to_txs(link), sizes))
return false;
light = sizes.light;
heavy = sizes.heavy;
return true;
}
// Heights.
// ----------------------------------------------------------------------------
TEMPLATE
height_link CLASS::get_height(const hash_digest& key) const NOEXCEPT
{
table::header::get_height header{};
if (!store_.header.find(key, header))
return {};
return header.height;
}
TEMPLATE
height_link CLASS::get_height(const header_link& link) const NOEXCEPT
{
table::header::get_height header{};
if (!store_.header.get(link, header))
return {};
return header.height;
}
TEMPLATE
bool CLASS::get_height(size_t& out, const hash_digest& key) const NOEXCEPT
{
const auto height = get_height(key);
if (height >= height_link::terminal)
return false;
out = system::possible_narrow_cast<size_t>(height.value);
return true;
}
TEMPLATE
bool CLASS::get_height(size_t& out, const header_link& link) const NOEXCEPT
{
const auto height = get_height(link);
if (height >= height_link::terminal)
return false;
out = system::possible_narrow_cast<size_t>(height.value);
return true;
}
// Values (value, spend, fees).
// ----------------------------------------------------------------------------
TEMPLATE
bool CLASS::get_value(uint64_t& out, const output_link& link) const NOEXCEPT
{
table::output::get_value output{};
if (!store_.output.get(link, output))
return false;
out = output.value;
return true;
}
// protected
TEMPLATE
bool CLASS::get_outputs_total_value(uint64_t& out,
const output_links& links) const NOEXCEPT
{
out = zero;
for (const auto& output_fk: links)
{
uint64_t value{};
if (!get_value(value, output_fk)) return false;
out = system::ceilinged_add(out, value);
}
return true;
}
TEMPLATE
bool CLASS::get_tx_value(uint64_t& out, const tx_link& link) const NOEXCEPT
{
table::transaction::get_coinbase tx{};
if (!store_.tx.get(link, tx))
return false;
// Shortcircuit coinbase prevout read.
if (tx.coinbase)
{
out = zero;
return true;
}
// Optimizable due to sequential tx input links.
const auto links = to_prevouts(link);
return !links.empty() && get_outputs_total_value(out, links);
}
TEMPLATE
bool CLASS::get_tx_spend(uint64_t& out, const tx_link& link) const NOEXCEPT
{
const auto links = to_outputs(link);
return !links.empty() && get_outputs_total_value(out, links);
}
TEMPLATE
bool CLASS::get_tx_fee(uint64_t& out, const tx_link& link) const NOEXCEPT
{
uint64_t value{};
if (!get_tx_value(value, link))
return false;
// Zero input implies either zero output or coinbase (both zero).
if (is_zero(value))
return true;
uint64_t spend{};
if (!get_tx_spend(spend, link) || spend > value)
return false;
out = value - spend;
return true;
}
TEMPLATE
bool CLASS::get_block_value(uint64_t& out,
const header_link& link) const NOEXCEPT
{
table::txs::get_txs txs{};
if (!store_.txs.at(to_txs(link), txs) || (txs.tx_fks.size() < one))
return false;
std::atomic_bool fail{};
const auto begin = std::next(txs.tx_fks.begin());
constexpr auto parallel = poolstl::execution::par;
constexpr auto relaxed = std::memory_order_relaxed;
out = std::transform_reduce(parallel, begin, txs.tx_fks.end(), 0_u64,
[](uint64_t left, uint64_t right) NOEXCEPT
{
return system::ceilinged_add(left, right);
},
[&](const auto& tx_fk) NOEXCEPT
{
uint64_t value{};
if (!fail.load(relaxed) && !get_tx_value(value, tx_fk))
fail.store(true, relaxed);
return value;
});
return !fail.load(relaxed);
}
TEMPLATE
bool CLASS::get_block_spend(uint64_t& out,
const header_link& link) const NOEXCEPT
{
table::txs::get_txs txs{};
if (!store_.txs.at(to_txs(link), txs) || (txs.tx_fks.size() < one))
return false;
std::atomic_bool fail{};
const auto begin = std::next(txs.tx_fks.begin());
constexpr auto parallel = poolstl::execution::par;
constexpr auto relaxed = std::memory_order_relaxed;
out = std::transform_reduce(parallel, begin, txs.tx_fks.end(), 0_u64,
[](uint64_t left, uint64_t right) NOEXCEPT
{
return system::ceilinged_add(left, right);
},
[&](const auto& tx_fk) NOEXCEPT
{
uint64_t spend{};
if (!fail.load(relaxed) && !get_tx_spend(spend, tx_fk))
fail.store(true, relaxed);
return spend;
});
return !fail.load(relaxed);
}
TEMPLATE
bool CLASS::get_block_fee(uint64_t& out,
const header_link& link) const NOEXCEPT
{
uint64_t value{}, spend{};
if (!get_block_value(value, link) || !get_block_spend(spend, link) ||
spend > value)
return false;
out = value - spend;
return true;
}
} // namespace database
} // namespace libbitcoin
#endif