forked from libbitcoin/libbitcoin-database
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtranslate.ipp
More file actions
556 lines (456 loc) · 14 KB
/
translate.ipp
File metadata and controls
556 lines (456 loc) · 14 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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/**
* Copyright (c) 2011-2025 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_TRANSLATE_IPP
#define LIBBITCOIN_DATABASE_QUERY_TRANSLATE_IPP
#include <algorithm>
#include <iterator>
#include <numeric>
#include <unordered_map>
#include <utility>
#include <bitcoin/system.hpp>
#include <bitcoin/database/define.hpp>
namespace libbitcoin {
namespace database {
// natural key (entry)
// ----------------------------------------------------------------------------
TEMPLATE
inline header_link CLASS::to_candidate(size_t height) const NOEXCEPT
{
using namespace system;
using link = table::height::header::integer;
if (height >= store_.candidate.count())
return {};
table::height::record index{};
if (!store_.candidate.get(possible_narrow_cast<link>(height), index))
return {};
return index.header_fk;
}
TEMPLATE
inline header_link CLASS::to_confirmed(size_t height) const NOEXCEPT
{
using namespace system;
using link = table::height::header::integer;
if (height >= store_.confirmed.count())
return {};
table::height::record index{};
if (!store_.confirmed.get(possible_narrow_cast<link>(height), index))
return {};
return index.header_fk;
}
TEMPLATE
inline header_link CLASS::to_header(const hash_digest& key) const NOEXCEPT
{
return store_.header.first(key);
}
TEMPLATE
inline tx_link CLASS::to_tx(const hash_digest& key) const NOEXCEPT
{
return store_.tx.first(key);
}
TEMPLATE
inline filter_link CLASS::to_filter(const header_link& key) const NOEXCEPT
{
return store_.filter_tx.first(key);
}
TEMPLATE
output_link CLASS::to_output(const point& prevout) const NOEXCEPT
{
return to_output(prevout.hash(), prevout.index());
}
TEMPLATE
output_link CLASS::to_output(const hash_digest& key,
uint32_t input_index) const NOEXCEPT
{
return to_output(to_tx(key), input_index);
}
// put to tx (reverse navigation)
// ----------------------------------------------------------------------------
TEMPLATE
tx_link CLASS::to_output_tx(const output_link& link) const NOEXCEPT
{
table::output::get_parent out{};
if (!store_.output.get(link, out))
return {};
return out.parent_fk;
}
TEMPLATE
tx_link CLASS::to_prevout_tx(const point_link& link) const NOEXCEPT
{
return to_tx(get_point_hash(link));
}
TEMPLATE
tx_link CLASS::to_spending_tx(const point_link& link) const NOEXCEPT
{
table::ins::get_parent ins{};
if (!store_.ins.get(link, ins))
return {};
return ins.parent_fk;
}
// point to put (forward navigation)
// ----------------------------------------------------------------------------
TEMPLATE
point_link CLASS::to_point(const tx_link& link,
uint32_t input_index) const NOEXCEPT
{
table::transaction::get_point tx{ {}, input_index };
if (!store_.tx.get(link, tx))
return {};
return tx.point_fk;
}
TEMPLATE
output_link CLASS::to_output(const tx_link& link,
uint32_t output_index) const NOEXCEPT
{
table::transaction::get_output tx{ {}, output_index };
if (!store_.tx.get(link, tx))
return {};
table::outs::get_output outs{};
if (!store_.outs.get(tx.outs_fk, outs))
return {};
return outs.out_fk;
}
TEMPLATE
output_link CLASS::to_prevout(const point_link& link) const NOEXCEPT
{
table::point::record point{};
if (!store_.point.get(link, point))
return {};
return to_output(to_tx(point.hash), point.index);
}
// block/tx to block (reverse navigation)
// ----------------------------------------------------------------------------
TEMPLATE
tx_link CLASS::to_strong_tx(const tx_link& link) const NOEXCEPT
{
return to_strong_tx(get_tx_key(link));
}
TEMPLATE
tx_link CLASS::to_strong_tx(const hash_digest& tx_hash) const NOEXCEPT
{
// Get all tx links for tx_hash.
tx_links txs{};
for (auto it = store_.tx.it(tx_hash); it; ++it)
txs.push_back(*it);
// Find the first strong tx of the set and return its link.
for (const auto& tx : txs)
if (!to_block(tx).is_terminal())
return tx;
return {};
}
// protected (weak association)
// Required for confirmation processing.
TEMPLATE
header_link CLASS::to_block(const tx_link& link) const NOEXCEPT
{
table::strong_tx::record strong{};
if (!store_.strong_tx.find(link, strong) || !strong.positive())
return {};
// Terminal implies not in strong block (reorganized).
return strong.header_fk();
}
TEMPLATE
header_link CLASS::to_strong(const tx_link& link) const NOEXCEPT
{
return to_strong(get_tx_key(link));
}
// Required for confirmation processing.
TEMPLATE
header_link CLASS::to_strong(const hash_digest& tx_hash) const NOEXCEPT
{
// Get all tx links for tx_hash.
tx_links txs{};
for (auto it = store_.tx.it(tx_hash); it; ++it)
txs.push_back(*it);
// Find the first strong tx of the set and return its block.
for (const auto& tx: txs)
if (const auto block = to_block(tx); !block.is_terminal())
return block;
return {};
}
TEMPLATE
header_link CLASS::to_parent(const header_link& link) const NOEXCEPT
{
table::header::get_parent_fk header{};
if (!store_.header.get(link, header))
return {};
// Terminal implies genesis (no parent).
return header.parent_fk;
}
// to confirmed objects (reverse navigation)
// ----------------------------------------------------------------------------
TEMPLATE
header_link CLASS::to_confirmed_block(
const hash_digest& tx_hash) const NOEXCEPT
{
const auto block = to_strong(tx_hash);
if (!is_confirmed_block(block))
return {};
return block;
}
TEMPLATE
point_link CLASS::to_confirmed_spender(const point& prevout) const NOEXCEPT
{
// see is_spent_output().
for (const auto& in: to_spenders(prevout))
if (is_confirmed_input(in))
return in;
return {};
}
// output to spenders (reverse navigation)
// ----------------------------------------------------------------------------
// protected/unused (symmetry)
TEMPLATE
uint32_t CLASS::to_input_index(const tx_link& parent_fk,
const point_link& point_fk) const NOEXCEPT
{
uint32_t index{};
for (const auto& in_fk: to_points(parent_fk))
{
if (in_fk == point_fk) return index;
++index;
}
return point::null_index;
}
// protected/to_spenders
TEMPLATE
uint32_t CLASS::to_output_index(const tx_link& parent_fk,
const output_link& output_fk) const NOEXCEPT
{
uint32_t index{};
for (const auto& out_fk: to_outputs(parent_fk))
{
if (out_fk == output_fk) return index;
++index;
}
return point::null_index;
}
TEMPLATE
point_links CLASS::to_spenders(const output_link& link) const NOEXCEPT
{
table::output::get_parent out{};
if (!store_.output.get(link, out))
return {};
// This results in two reads to the tx table, so could be optimized.
return to_spenders(out.parent_fk, to_output_index(out.parent_fk, link));
}
TEMPLATE
point_links CLASS::to_spenders(const tx_link& output_tx,
uint32_t output_index) const NOEXCEPT
{
return to_spenders(get_tx_key(output_tx), output_index);
}
TEMPLATE
point_links CLASS::to_spenders(const hash_digest& point_hash,
uint32_t output_index) const NOEXCEPT
{
return to_spenders({ point_hash, output_index });
}
TEMPLATE
point_links CLASS::to_spenders(const point& point) const NOEXCEPT
{
// Avoid returning spend links for coinbase inputs (not spenders).
if (point.index() == point::null_index)
return {};
point_links points{};
for (auto it = store_.point.it(point); it; ++it)
points.push_back(*it);
return points;
}
// tx to puts (forward navigation)
// ----------------------------------------------------------------------------
TEMPLATE
point_links CLASS::to_points(const tx_link& link) const NOEXCEPT
{
table::transaction::get_point tx{};
if (!store_.tx.get(link, tx))
return {};
// Transaction points are stored in a contiguous array of records.
point_links points(tx.number);
for (auto& point: points)
point = tx.point_fk++;
return points;
}
TEMPLATE
output_links CLASS::to_outputs(const tx_link& link) const NOEXCEPT
{
table::transaction::get_output tx{};
if (!store_.tx.get(link, tx))
return {};
table::outs::record outs{};
outs.out_fks.resize(tx.number);
if (!store_.outs.get(tx.outs_fk, outs))
return {};
return std::move(outs.out_fks);
}
TEMPLATE
output_links CLASS::to_prevouts(const tx_link& link) const NOEXCEPT
{
const auto points = to_points(link);
if (points.empty())
return {};
// TODO: to_prevout()
output_links prevouts{};
prevouts.reserve(points.size());
for (const auto& point: points)
prevouts.push_back(to_prevout(point));
return prevouts;
}
// txs to puts (forward navigation)
// ----------------------------------------------------------------------------
// to_ins()
TEMPLATE
point_links CLASS::to_points(const tx_links& txs) const NOEXCEPT
{
point_links points{};
for (const auto& tx: txs)
{
const auto tx_points = to_points(tx);
points.insert(points.end(), tx_points.begin(), tx_points.end());
}
return points;
}
TEMPLATE
output_links CLASS::to_outputs(const tx_links& txs) const NOEXCEPT
{
output_links outputs{};
for (const auto& tx: txs)
{
const auto tx_outputs = to_outputs(tx);
outputs.insert(outputs.end(), tx_outputs.begin(), tx_outputs.end());
}
return outputs;
}
TEMPLATE
output_links CLASS::to_prevouts(const tx_links& txs) const NOEXCEPT
{
constexpr auto parallel = poolstl::execution::par;
// to_prevout()
const auto ins = to_points(txs);
output_links outs(ins.size());
const auto fn = [this](auto spend) NOEXCEPT{ return to_prevout(spend); };
std::transform(parallel, ins.begin(), ins.end(), outs.begin(), fn);
return outs;
}
// block to puts (forward navigation)
// ----------------------------------------------------------------------------
TEMPLATE
point_links CLASS::to_block_points(const header_link& link) const NOEXCEPT
{
return to_points(to_spending_txs(link));
}
TEMPLATE
output_links CLASS::to_block_outputs(const header_link& link) const NOEXCEPT
{
return to_outputs(to_transactions(link));
}
TEMPLATE
output_links CLASS::to_block_prevouts(const header_link& link) const NOEXCEPT
{
return to_prevouts(to_spending_txs(link));
}
// block to txs (forward navigation)
// ----------------------------------------------------------------------------
TEMPLATE
tx_links CLASS::to_transactions(const header_link& link) const NOEXCEPT
{
table::txs::get_txs txs{};
if (!store_.txs.at(to_txs(link), txs))
return {};
return std::move(txs.tx_fks);
}
TEMPLATE
tx_links CLASS::to_spending_txs(const header_link& link) const NOEXCEPT
{
table::txs::get_spending_txs txs{};
if (!store_.txs.at(to_txs(link), txs))
return {};
return std::move(txs.tx_fks);
}
TEMPLATE
tx_link CLASS::to_coinbase(const header_link& link) const NOEXCEPT
{
table::txs::get_coinbase txs{};
if (!store_.txs.at(to_txs(link), txs))
return {};
return txs.coinbase_fk;
}
TEMPLATE
tx_link CLASS::to_transaction(const header_link& link,
size_t position) const NOEXCEPT
{
table::txs::get_tx txs{ {}, position };
if (!store_.txs.at(to_txs(link), txs))
return {};
return txs.tx_fk;
}
// header to arraymap tables (guard domain transitions)
// ----------------------------------------------------------------------------
TEMPLATE
constexpr size_t CLASS::to_validated_bk(const header_link& link) const NOEXCEPT
{
static_assert(header_link::terminal <= table::validated_bk::link::terminal);
return link.is_terminal() ? table::validated_bk::link::terminal : link.value;
}
TEMPLATE
constexpr size_t CLASS::to_filter_bk(const header_link& link) const NOEXCEPT
{
static_assert(header_link::terminal <= table::filter_bk::link::terminal);
return link.is_terminal() ? table::filter_bk::link::terminal : link.value;
}
TEMPLATE
constexpr size_t CLASS::to_filter_tx(const header_link& link) const NOEXCEPT
{
static_assert(header_link::terminal <= table::filter_tx::link::terminal);
return link.is_terminal() ? table::filter_tx::link::terminal : link.value;
}
TEMPLATE
constexpr size_t CLASS::to_prevout(const header_link& link) const NOEXCEPT
{
static_assert(header_link::terminal <= table::prevout::link::terminal);
return link.is_terminal() ? table::prevout::link::terminal : link.value;
}
TEMPLATE
constexpr size_t CLASS::to_txs(const header_link& link) const NOEXCEPT
{
static_assert(header_link::terminal <= table::txs::link::terminal);
return link.is_terminal() ? table::txs::link::terminal : link.value;
}
// hashmap enumeration
// ----------------------------------------------------------------------------
TEMPLATE
header_link CLASS::top_header(size_t bucket) const NOEXCEPT
{
using namespace system;
return store_.header.top(possible_narrow_cast<header_link::integer>(bucket));
}
TEMPLATE
point_link CLASS::top_point(size_t bucket) const NOEXCEPT
{
using namespace system;
return store_.point.top(possible_narrow_cast<point_link::integer>(bucket));
}
TEMPLATE
tx_link CLASS::top_tx(size_t bucket) const NOEXCEPT
{
using namespace system;
return store_.tx.top(possible_narrow_cast<tx_link::integer>(bucket));
}
} // namespace database
} // namespace libbitcoin
#endif