forked from libbitcoin/libbitcoin-database
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathquery.hpp
More file actions
845 lines (709 loc) · 37.4 KB
/
query.hpp
File metadata and controls
845 lines (709 loc) · 37.4 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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
/**
* 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_HPP
#define LIBBITCOIN_DATABASE_QUERY_HPP
#include <atomic>
#include <mutex>
#include <optional>
#include <utility>
#include <bitcoin/database/define.hpp>
#include <bitcoin/database/types.hpp>
namespace libbitcoin {
namespace database {
// Writers (non-const) are only: push_, pop_, set_ and initialize.
template <typename Store>
class query
{
public:
DELETE_COPY_MOVE_DESTRUCT(query);
/// Query type aliases.
using block = system::chain::block;
using point = system::chain::point;
using input = system::chain::input;
using output = system::chain::output;
using header = system::chain::header;
using script = system::chain::script;
using witness = system::chain::witness;
using headers = system::chain::header_cptrs;
using transaction = system::chain::transaction;
using transactions = system::chain::transaction_cptrs;
using inputs_ptr = system::chain::inputs_ptr;
using outputs_ptr = system::chain::outputs_ptr;
using transactions_ptr = system::chain::transactions_ptr;
using chain_state = system::chain::chain_state;
using chain_state_cptr = system::chain::chain_state::cptr;
using chain_context = system::chain::context;
using index = table::transaction::ix::integer;
using sizes = std::pair<size_t, size_t>;
using heights = std_vector<size_t>;
using filter = system::data_chunk;
query(Store& store) NOEXCEPT;
/// Store management from query-holder (not store owner) context.
/// -----------------------------------------------------------------------
/// Get first fault code, or disk_full if none and full, or success.
code get_code() const NOEXCEPT;
/// Get first fault code.
code get_fault() const NOEXCEPT;
/// True if there is a fault condition (disk full is not a fault).
bool is_fault() const NOEXCEPT;
/// True if there is a disk full condition.
bool is_full() const NOEXCEPT;
/// True if candidate top is fork point header.
bool is_coalesced() const NOEXCEPT;
/// Get the space required to clear the disk full condition.
size_t get_space() const NOEXCEPT;
/// Resume from disk full condition.
code reload(const typename Store::event_handler& handler) const NOEXCEPT;
/// Prune the store while running.
code prune(const typename Store::event_handler& handler) const NOEXCEPT;
/// Snapshot the store while running.
code snapshot(const typename Store::event_handler& handler) const NOEXCEPT;
/// Count of puts resulting in table body search to detect duplication.
size_t positive_search_count() const NOEXCEPT;
/// Count of puts not resulting in table body search to detect duplication.
size_t negative_search_count() const NOEXCEPT;
/// Store extent.
/// -----------------------------------------------------------------------
/// Full store (all data) logical byte sizes.
size_t store_size() const NOEXCEPT;
size_t store_head_size() const NOEXCEPT;
size_t store_body_size() const NOEXCEPT;
/// Full archive (primary data) logical byte sizes.
size_t archive_size() const NOEXCEPT;
size_t archive_head_size() const NOEXCEPT;
size_t archive_body_size() const NOEXCEPT;
/// Table head logical byte sizes.
size_t header_head_size() const NOEXCEPT;
size_t output_head_size() const NOEXCEPT;
size_t input_head_size() const NOEXCEPT;
size_t point_head_size() const NOEXCEPT;
size_t ins_head_size() const NOEXCEPT;
size_t outs_head_size() const NOEXCEPT;
size_t txs_head_size() const NOEXCEPT;
size_t tx_head_size() const NOEXCEPT;
size_t candidate_head_size() const NOEXCEPT;
size_t confirmed_head_size() const NOEXCEPT;
size_t strong_tx_head_size() const NOEXCEPT;
size_t duplicate_head_size() const NOEXCEPT;
size_t prevout_head_size() const NOEXCEPT;
size_t validated_bk_head_size() const NOEXCEPT;
size_t validated_tx_head_size() const NOEXCEPT;
size_t filter_bk_head_size() const NOEXCEPT;
size_t filter_tx_head_size() const NOEXCEPT;
size_t address_head_size() const NOEXCEPT;
/// Table body logical byte sizes.
size_t header_body_size() const NOEXCEPT;
size_t output_body_size() const NOEXCEPT;
size_t input_body_size() const NOEXCEPT;
size_t point_body_size() const NOEXCEPT;
size_t ins_body_size() const NOEXCEPT;
size_t outs_body_size() const NOEXCEPT;
size_t txs_body_size() const NOEXCEPT;
size_t tx_body_size() const NOEXCEPT;
size_t candidate_body_size() const NOEXCEPT;
size_t confirmed_body_size() const NOEXCEPT;
size_t strong_tx_body_size() const NOEXCEPT;
size_t duplicate_body_size() const NOEXCEPT;
size_t prevout_body_size() const NOEXCEPT;
size_t validated_bk_body_size() const NOEXCEPT;
size_t validated_tx_body_size() const NOEXCEPT;
size_t filter_bk_body_size() const NOEXCEPT;
size_t filter_tx_body_size() const NOEXCEPT;
size_t address_body_size() const NOEXCEPT;
/// Table (head + body) logical byte sizes.
size_t header_size() const NOEXCEPT;
size_t output_size() const NOEXCEPT;
size_t input_size() const NOEXCEPT;
size_t point_size() const NOEXCEPT;
size_t ins_size() const NOEXCEPT;
size_t outs_size() const NOEXCEPT;
size_t txs_size() const NOEXCEPT;
size_t tx_size() const NOEXCEPT;
size_t candidate_size() const NOEXCEPT;
size_t confirmed_size() const NOEXCEPT;
size_t strong_tx_size() const NOEXCEPT;
size_t duplicate_size() const NOEXCEPT;
size_t prevout_size() const NOEXCEPT;
size_t validated_bk_size() const NOEXCEPT;
size_t validated_tx_size() const NOEXCEPT;
size_t filter_bk_size() const NOEXCEPT;
size_t filter_tx_size() const NOEXCEPT;
size_t address_size() const NOEXCEPT;
/// Buckets (hashmap + arraymap).
size_t header_buckets() const NOEXCEPT;
size_t point_buckets() const NOEXCEPT;
size_t txs_buckets() const NOEXCEPT;
size_t tx_buckets() const NOEXCEPT;
size_t strong_tx_buckets() const NOEXCEPT;
size_t duplicate_buckets() const NOEXCEPT;
size_t prevout_buckets() const NOEXCEPT;
size_t validated_bk_buckets() const NOEXCEPT;
size_t validated_tx_buckets() const NOEXCEPT;
size_t filter_bk_buckets() const NOEXCEPT;
size_t filter_tx_buckets() const NOEXCEPT;
size_t address_buckets() const NOEXCEPT;
/// Records.
size_t header_records() const NOEXCEPT;
size_t point_records() const NOEXCEPT;
size_t ins_records() const NOEXCEPT;
size_t outs_records() const NOEXCEPT;
size_t tx_records() const NOEXCEPT;
size_t candidate_records() const NOEXCEPT;
size_t confirmed_records() const NOEXCEPT;
size_t strong_tx_records() const NOEXCEPT;
size_t duplicate_records() const NOEXCEPT;
size_t filter_bk_records() const NOEXCEPT;
size_t address_records() const NOEXCEPT;
/// Counters (archive slabs - txs/puts/filter_tx can be derived).
size_t input_count(const tx_link& link) const NOEXCEPT;
size_t output_count(const tx_link& link) const NOEXCEPT;
two_counts put_counts(const tx_link& link) const NOEXCEPT;
size_t input_count(const tx_links& txs) const NOEXCEPT;
size_t output_count(const tx_links& txs) const NOEXCEPT;
two_counts put_counts(const tx_links& txs) const NOEXCEPT;
/// Optional/configured table state.
bool address_enabled() const NOEXCEPT;
bool filter_enabled() const NOEXCEPT;
size_t interval_span() const NOEXCEPT;
/// Initialization (natural-keyed).
/// -----------------------------------------------------------------------
/// Not reliable during organization.
inline bool is_initialized() const NOEXCEPT;
inline size_t get_top_candidate() const NOEXCEPT;
inline size_t get_top_confirmed() const NOEXCEPT;
size_t get_fork() const NOEXCEPT;
size_t get_top_associated() const NOEXCEPT;
size_t get_top_associated_from(size_t height) const NOEXCEPT;
associations get_all_unassociated() const NOEXCEPT;
associations get_unassociated_above(size_t height) const NOEXCEPT;
associations get_unassociated_above(size_t height,
size_t count) const NOEXCEPT;
associations get_unassociated_above(size_t height, size_t count,
size_t last) const NOEXCEPT;
size_t get_unassociated_count() const NOEXCEPT;
size_t get_unassociated_count_above(size_t height) const NOEXCEPT;
size_t get_unassociated_count_above(size_t height,
size_t last) const NOEXCEPT;
/// Translation (key/link to link/s).
/// -----------------------------------------------------------------------
/// to_input not provided as input_link cannot produce chain::input.
/// search key (entry)
inline header_link to_candidate(size_t height) const NOEXCEPT;
inline header_link to_confirmed(size_t height) const NOEXCEPT;
inline header_link to_header(const hash_digest& key) const NOEXCEPT;
inline tx_link to_tx(const hash_digest& key) const NOEXCEPT;
inline filter_link to_filter(const header_link& key) const NOEXCEPT;
inline output_link to_output(const point& prevout) const NOEXCEPT;
inline output_link to_output(const hash_digest& key,
uint32_t output_index) const NOEXCEPT;
/// put to tx (reverse navigation)
tx_link to_output_tx(const output_link& link) const NOEXCEPT;
tx_link to_prevout_tx(const point_link& link) const NOEXCEPT;
tx_link to_spending_tx(const point_link& link) const NOEXCEPT;
/// point to put (forward navigation)
point_link to_point(const tx_link& link,
uint32_t input_index) const NOEXCEPT;
output_link to_output(const tx_link& link,
uint32_t output_index) const NOEXCEPT;
output_link to_previous_output(const point_link& link) const NOEXCEPT;
/// block/tx to block (reverse navigation)
tx_link to_strong_tx(const tx_link& link) const NOEXCEPT;
tx_link to_strong_tx(const hash_digest& tx_hash) const NOEXCEPT;
header_link to_strong(const tx_link& link) const NOEXCEPT;
header_link to_strong(const hash_digest& tx_hash) const NOEXCEPT;
header_link to_parent(const header_link& link) const NOEXCEPT;
/// to confirmed objects (reverse navigation)
header_link to_confirmed_block(const hash_digest& tx_hash) const NOEXCEPT;
point_link to_confirmed_spender(const point& prevout) const NOEXCEPT;
/// output to spenders (reverse navigation)
point_links to_spenders(const point& prevout) const NOEXCEPT;
point_links to_spenders(const output_link& link) const NOEXCEPT;
point_links to_spenders(const hash_digest& point_hash,
uint32_t output_index) const NOEXCEPT;
point_links to_spenders(const tx_link& output_tx,
uint32_t output_index) const NOEXCEPT;
/// tx to puts (forward navigation)
point_links to_points(const tx_link& link) const NOEXCEPT;
output_links to_outputs(const tx_link& link) const NOEXCEPT;
output_links to_prevouts(const tx_link& link) const NOEXCEPT;
/// txs to puts (forward navigation)
point_links to_points(const tx_links& txs) const NOEXCEPT;
output_links to_outputs(const tx_links& txs) const NOEXCEPT;
output_links to_prevouts(const tx_links& txs) const NOEXCEPT;
/// block to puts (forward navigation)
point_links to_block_points(const header_link& link) const NOEXCEPT;
output_links to_block_outputs(const header_link& link) const NOEXCEPT;
output_links to_block_prevouts(const header_link& link) const NOEXCEPT;
/// block to txs (forward navigation)
tx_links to_transactions(const header_link& link) const NOEXCEPT;
tx_links to_spending_txs(const header_link& link) const NOEXCEPT;
tx_link to_coinbase(const header_link& link) const NOEXCEPT;
tx_link to_transaction(const header_link& link,
size_t position) const NOEXCEPT;
/// header to arraymap tables (guard domain transitions)
constexpr size_t to_validated_bk(const header_link& link) const NOEXCEPT;
constexpr size_t to_filter_bk(const header_link& link) const NOEXCEPT;
constexpr size_t to_filter_tx(const header_link& link) const NOEXCEPT;
constexpr size_t to_prevout(const header_link& link) const NOEXCEPT;
constexpr size_t to_txs(const header_link& link) const NOEXCEPT;
/// hashmap enumeration
header_link top_header(size_t bucket) const NOEXCEPT;
point_link top_point(size_t bucket) const NOEXCEPT;
tx_link top_tx(size_t bucket) const NOEXCEPT;
/// optional enumeration
code to_address_outputs(std::atomic_bool& cancel, output_links& out,
const hash_digest& key) const NOEXCEPT;
/// Archive reads.
/// -----------------------------------------------------------------------
// Bools.
inline bool is_header(const hash_digest& key) const NOEXCEPT;
inline bool is_block(const hash_digest& key) const NOEXCEPT;
inline bool is_tx(const hash_digest& key) const NOEXCEPT;
inline bool is_coinbase(const tx_link& link) const NOEXCEPT;
inline bool is_tx_segregated(const tx_link& link) const NOEXCEPT;
inline bool is_block_segregated(const header_link& link) const NOEXCEPT;
inline bool is_milestone(const header_link& link) const NOEXCEPT;
inline bool is_associated(const header_link& link) const NOEXCEPT;
inline bool is_confirmable(const header_link& link) const NOEXCEPT;
inline bool is_validated(const header_link& link) const NOEXCEPT;
/// Empty/null_hash implies fault, zero count implies unassociated.
hash_digest get_top_confirmed_hash() const NOEXCEPT;
hash_digest get_top_candidate_hash() const NOEXCEPT;
hashes get_tx_keys(const header_link& link) const NOEXCEPT;
size_t get_tx_count(const header_link& link) const NOEXCEPT;
inline hash_digest get_header_key(const header_link& link) const NOEXCEPT;
inline hash_digest get_tx_key(const tx_link& link) const NOEXCEPT;
inline point_key get_point_key(const point_link& link) const NOEXCEPT;
inline hash_digest get_point_hash(const point_link& link) const NOEXCEPT;
/// False position implies not confirmed (or fault).
bool get_tx_height(size_t& out, const tx_link& link) const NOEXCEPT;
bool get_tx_position(size_t& out, const tx_link& link) const NOEXCEPT;
tx_link get_position_tx(const header_link& link,
size_t position) const NOEXCEPT;
/// Sizes.
bool get_tx_size(size_t& out, const tx_link& link,
bool witness) const NOEXCEPT;
bool get_block_size(size_t& out, const header_link& link,
bool witness) const NOEXCEPT;
bool get_block_sizes(size_t& light, size_t& heavy,
const header_link& link) const NOEXCEPT;
bool get_tx_sizes(size_t& light, size_t& heavy,
const tx_link& link) const NOEXCEPT;
/// Heights.
height_link get_height(const hash_digest& key) const NOEXCEPT;
height_link get_height(const header_link& link) const NOEXCEPT;
bool get_height(size_t& out, const hash_digest& key) const NOEXCEPT;
bool get_height(size_t& out, const header_link& link) const NOEXCEPT;
/// Values (value, spend, fees).
bool get_value(uint64_t& out, const output_link& link) const NOEXCEPT;
bool get_tx_value(uint64_t& out, const tx_link& link) const NOEXCEPT;
bool get_tx_spend(uint64_t& out, const tx_link& link) const NOEXCEPT;
bool get_tx_fee(uint64_t& out, const tx_link& link) const NOEXCEPT;
bool get_block_value(uint64_t& out, const header_link& link) const NOEXCEPT;
bool get_block_spend(uint64_t& out, const header_link& link) const NOEXCEPT;
bool get_block_fee(uint64_t& out, const header_link& link) const NOEXCEPT;
/// Objects.
/// -----------------------------------------------------------------------
inputs_ptr get_inputs(const tx_link& link, bool witness) const NOEXCEPT;
outputs_ptr get_outputs(const tx_link& link) const NOEXCEPT;
transactions_ptr get_transactions(const header_link& link,
bool witness) const NOEXCEPT;
header::cptr get_header(const header_link& link) const NOEXCEPT;
block::cptr get_block(const header_link& link, bool witness) const NOEXCEPT;
transaction::cptr get_transaction(const tx_link& link,
bool witness) const NOEXCEPT;
point get_point(const point_link& link) const NOEXCEPT;
witness::cptr get_witness(const point_link& link) const NOEXCEPT;
script::cptr get_input_script(const point_link& link) const NOEXCEPT;
input::cptr get_input(const point_link& link, bool witness) const NOEXCEPT;
input::cptr get_input(const tx_link& link, uint32_t index,
bool witness) const NOEXCEPT;
script::cptr get_output_script(const output_link& link) const NOEXCEPT;
output::cptr get_output(const output_link& link) const NOEXCEPT;
output::cptr get_output(const tx_link& link, uint32_t index) const NOEXCEPT;
inputs_ptr get_spenders(const output_link& link,
bool witness) const NOEXCEPT;
/// Inpoint and outpoint result sets.
outpoint get_spent(const output_link& link) const NOEXCEPT;
inpoint get_spender(const point_link& link) const NOEXCEPT;
inpoints get_spenders(const point& point) const NOEXCEPT;
/// False implies missing prevouts, node input.metadata is populated.
bool populate_with_metadata(const input& input) const NOEXCEPT;
bool populate_with_metadata(const block& block) const NOEXCEPT;
bool populate_with_metadata(const transaction& tx) const NOEXCEPT;
/// False implies missing prevouts, input.metadata is not populated.
bool populate_without_metadata(const input& input) const NOEXCEPT;
bool populate_without_metadata(const block& block) const NOEXCEPT;
bool populate_without_metadata(const transaction& tx) const NOEXCEPT;
/// Fees.
/// -----------------------------------------------------------------------
/// Virtual size incorporates witnesses if archived.
bool get_tx_virtual_size(size_t& out, const tx_link& link) const NOEXCEPT;
bool get_block_virtual_size(size_t& out, const header_link& link) const NOEXCEPT;
/// Fee rate tuples by tx, block or branch.
bool get_tx_fees(fee_rate& out, const tx_link& link) const NOEXCEPT;
bool get_block_fees(fee_rates& out, const header_link& link) const NOEXCEPT;
bool get_branch_fees(std::atomic_bool& cancel, fee_rate_sets& out,
size_t start, size_t count) const NOEXCEPT;
/// Merkle.
/// -----------------------------------------------------------------------
/// Merkle computations over the index of confirmed headers.
hash_digest get_merkle_root(size_t height) const NOEXCEPT;
code get_merkle_root_and_proof(hash_digest& root, hashes& proof,
size_t target, size_t checkpoint) const NOEXCEPT;
/// Archive writes.
/// -----------------------------------------------------------------------
/// Bool returns.
bool set(const header& header, const chain_context& ctx,
bool milestone) NOEXCEPT;
bool set(const header& header, const context& ctx,
bool milestone) NOEXCEPT;
bool set(const block& block, const chain_context& ctx,
bool milestone, bool strong) NOEXCEPT;
bool set(const block& block, const context& ctx,
bool milestone, bool strong) NOEXCEPT;
bool set(const transaction& tx) NOEXCEPT;
bool set(const block& block, bool strong, bool bypass) NOEXCEPT;
/// Set transaction.
code set_code(const transaction& tx) NOEXCEPT;
/// Set header (headers-first).
code set_code(const header& header, const context& ctx,
bool milestone) NOEXCEPT;
code set_code(const header& header, const chain_context& ctx,
bool milestone) NOEXCEPT;
code set_code(header_link& out_fk, const header& header,
const context& ctx, bool milestone, bool=false) NOEXCEPT;
code set_code(header_link& out_fk, const header& header,
const chain_context& ctx, bool milestone, bool=false) NOEXCEPT;
/// Set full block (blocks-first).
code set_code(const block& block, const context& ctx, bool milestone,
bool strong) NOEXCEPT;
code set_code(const block& block, const chain_context& ctx, bool milestone,
bool strong) NOEXCEPT;
code set_code(header_link& out_fk, const block& block, const context& ctx,
bool milestone, bool strong) NOEXCEPT;
code set_code(header_link& out_fk, const block& block,
const chain_context& ctx, bool milestone, bool strong) NOEXCEPT;
/// Set block.txs (headers-first).
code set_code(const block& block, bool strong, bool bypass) NOEXCEPT;
code set_code(header_link& out_fk, const block& block, bool strong,
bool bypass) NOEXCEPT;
code set_code(const block& block, const header_link& key, bool strong,
bool bypass, size_t height) NOEXCEPT;
/// Context.
/// -----------------------------------------------------------------------
chain_state_cptr get_chain_state(const system::settings& settings,
const hash_digest& hash) const NOEXCEPT;
chain_state_cptr get_candidate_chain_state(
const system::settings& settings) const NOEXCEPT;
chain_state_cptr get_candidate_chain_state(const system::settings& settings,
size_t height) const NOEXCEPT;
chain_state_cptr get_candidate_chain_state(const system::settings& settings,
const header_link& link, size_t height) const NOEXCEPT;
chain_state_cptr get_candidate_chain_state(const system::settings& settings,
const header& header, const header_link& link,
size_t height) const NOEXCEPT;
/// Validation.
/// -----------------------------------------------------------------------
/// States.
bool is_validateable(size_t height) const NOEXCEPT;
code get_block_state(const header_link& link) const NOEXCEPT;
code get_header_state(const header_link& link) const NOEXCEPT;
code get_tx_state(const tx_link& link, const context& ctx) const NOEXCEPT;
code get_tx_state(uint64_t& fee, size_t& sigops, const tx_link& link,
const context& ctx) const NOEXCEPT;
/// Values.
// get_context(chain::context) sets only flags, median_time_past, height.
uint32_t get_top_timestamp(bool confirmed) const NOEXCEPT;
bool get_timestamp(uint32_t& timestamp, const header_link& link) const NOEXCEPT;
bool get_version(uint32_t& version, const header_link& link) const NOEXCEPT;
bool get_work(uint256_t& work, const header_link& link) const NOEXCEPT;
bool get_bits(uint32_t& bits, const header_link& link) const NOEXCEPT;
bool get_context(context& ctx, const header_link& link) const NOEXCEPT;
bool get_context(system::chain::context& ctx,
const header_link& link) const NOEXCEPT;
/// Setters.
bool set_block_valid(const header_link& link) NOEXCEPT;
bool set_block_unconfirmable(const header_link& link) NOEXCEPT;
bool set_block_confirmable(const header_link& link) NOEXCEPT;
bool set_block_unknown(const header_link& link) NOEXCEPT;
bool set_tx_unknown(const tx_link& link) NOEXCEPT;
bool set_tx_disconnected(const tx_link& link, const context& ctx) NOEXCEPT;
bool set_tx_connected(const tx_link& link, const context& ctx,
uint64_t fee, size_t sigops) NOEXCEPT;
/// Confirmation.
/// -----------------------------------------------------------------------
/// These are not used in consensus confirmation.
/// These compare strong with height index.
bool is_candidate_header(const header_link& link) const NOEXCEPT;
bool is_confirmed_block(const header_link& link) const NOEXCEPT;
bool is_confirmed_tx(const tx_link& link) const NOEXCEPT;
bool is_confirmed_input(const point_link& link) const NOEXCEPT;
bool is_confirmed_output(const output_link& link) const NOEXCEPT;
bool is_spent_output(const output_link& link) const NOEXCEPT;
/// Height index not used by these.
bool is_strong_tx(const tx_link& link) const NOEXCEPT;
bool is_strong_block(const header_link& link) const NOEXCEPT;
bool is_unconfirmable(const header_link& link) const NOEXCEPT;
/// Consensus.
/// -----------------------------------------------------------------------
/// These are used in consensus confirmation.
code block_confirmable(const header_link& link) const NOEXCEPT;
bool is_prevouts_cached(const header_link& link) const NOEXCEPT;
bool is_strong(const tx_link& link) const NOEXCEPT;
bool set_strong(const header_link& link) NOEXCEPT;
bool set_unstrong(const header_link& link) NOEXCEPT;
bool set_prevouts(const header_link& link, const block& block) NOEXCEPT;
bool get_branch(header_states& branch, const hash_digest& hash) const NOEXCEPT;
bool get_work(uint256_t& work, const header_states& states) const NOEXCEPT;
bool get_strong_branch(bool& strong, const uint256_t& branch_work,
size_t branch_point) const NOEXCEPT;
bool get_strong_fork(bool& strong, const uint256_t& fork_work,
size_t fork_point) const NOEXCEPT;
/// Height indexation.
/// -----------------------------------------------------------------------
size_t get_candidate_size() const NOEXCEPT;
size_t get_candidate_size(size_t top) const NOEXCEPT;
size_t get_confirmed_size() const NOEXCEPT;
size_t get_confirmed_size(size_t top) const NOEXCEPT;
hashes get_candidate_hashes(const heights& heights) const NOEXCEPT;
hashes get_confirmed_hashes(const heights& heights) const NOEXCEPT;
hashes get_confirmed_hashes(size_t first, size_t count) const NOEXCEPT;
header_links get_confirmed_headers(size_t first,
size_t limit) const NOEXCEPT;
header_links get_confirmed_fork(const header_link& fork) const NOEXCEPT;
header_links get_candidate_fork(size_t& fork_point) const NOEXCEPT;
header_states get_validated_fork(size_t& fork_point,
size_t top_checkpoint=zero, bool filter=false) const NOEXCEPT;
bool initialize(const block& genesis) NOEXCEPT;
bool push_candidate(const header_link& link) NOEXCEPT;
bool push_confirmed(const header_link& link, bool strong) NOEXCEPT;
bool pop_candidate() NOEXCEPT;
bool pop_confirmed() NOEXCEPT;
/// Populate message payloads from locator.
headers get_headers(const hashes& locator, const hash_digest& stop,
size_t limit) const NOEXCEPT;
hashes get_blocks(const hashes& locator, const hash_digest& stop,
size_t limit) const NOEXCEPT;
/// Get descending list of ancestry starting with descendant (inclusive).
bool get_ancestry(header_links& ancestry, const header_link& descendant,
size_t count) const NOEXCEPT;
/// Optional Tables.
/// -----------------------------------------------------------------------
code get_address_outputs(std::atomic_bool& cancel, outpoints& out,
const hash_digest& key, bool turbo=false) const NOEXCEPT;
code get_confirmed_unspent_outputs(std::atomic_bool& cancel, outpoints& out,
const hash_digest& key, bool turbo=false) const NOEXCEPT;
code get_minimum_unspent_outputs(std::atomic_bool& cancel, outpoints& out,
const hash_digest& key, uint64_t value, bool turbo=false) const NOEXCEPT;
code get_confirmed_balance(std::atomic_bool& cancel,
uint64_t& balance, const hash_digest& key,
bool turbo=false) const NOEXCEPT;
bool is_filtered_body(const header_link& link) const NOEXCEPT;
bool get_filter_body(filter& out, const header_link& link) const NOEXCEPT;
bool is_filtered_head(const header_link& link) const NOEXCEPT;
bool get_filter_head(hash_digest& out, const header_link& link) const NOEXCEPT;
bool get_filter_hash(hash_digest& out, const header_link& link) const NOEXCEPT;
bool get_filter_hashes(hashes& filter_hashes, hash_digest& previous_header,
const header_link& stop_link, size_t count) const NOEXCEPT;
bool get_filter_heads(hashes& filter_heads, size_t stop_height,
size_t interval) const NOEXCEPT;
bool set_filter_body(const header_link& link, const block& block) NOEXCEPT;
bool set_filter_body(const header_link& link, const filter& body) NOEXCEPT;
bool set_filter_head(const header_link& link) NOEXCEPT;
bool set_filter_head(const header_link& link, const hash_digest& head,
const hash_digest& hash) NOEXCEPT;
protected:
using hash_option = std::optional<hash_digest>;
struct span
{
size_t size() const NOEXCEPT { return end - begin; }
size_t begin;
size_t end;
};
/// Network
/// -----------------------------------------------------------------------
/// Height of highest confirmed block (assumes locator descending).
size_t get_fork(const hashes& locator) const NOEXCEPT;
/// Height of highest confirmed block (assumes locator descending).
span get_locator_span(const hashes& locator, const hash_digest& stop,
size_t limit) const NOEXCEPT;
/// Support unassociated gathering.
bool get_unassociated(association& out,
const header_link& link) const NOEXCEPT;
/// Translate.
/// -----------------------------------------------------------------------
header_link to_block(const tx_link& link) const NOEXCEPT;
uint32_t to_input_index(const tx_link& parent_fk,
const point_link& point_fk) const NOEXCEPT;
uint32_t to_output_index(const tx_link& parent_fk,
const output_link& output_fk) const NOEXCEPT;
/// Objects.
/// -----------------------------------------------------------------------
static inline point::cptr make_point(hash_digest&& hash,
uint32_t index) NOEXCEPT;
bool get_outputs_total_value(uint64_t& out,
const output_links& links) const NOEXCEPT;
/// Validate.
/// -----------------------------------------------------------------------
inline code to_block_code(linkage<schema::code>::integer value) const NOEXCEPT;
inline code to_tx_code(linkage<schema::code>::integer value) const NOEXCEPT;
inline bool is_sufficient(const context& current,
const context& evaluated) const NOEXCEPT;
/// Called by confirmation chaser.
bool is_block_validated(code& state, const header_link& link,
size_t height, size_t checkpoint) const NOEXCEPT;
/// Setters.
/// -----------------------------------------------------------------------
bool set_block_state(const header_link& link,
schema::block_state state) NOEXCEPT;
bool set_tx_state(const tx_link& link, const context& ctx,
uint64_t fee, size_t sigops, schema::tx_state state) NOEXCEPT;
/// Confirm.
/// -----------------------------------------------------------------------
bool is_confirmed_unspent(const output_link& link) const NOEXCEPT;
/// Consensus.
/// -----------------------------------------------------------------------
/// Called by block_confirmable (check bip30)
bool is_spent_coinbase(const tx_link& link) const NOEXCEPT;
code unspent_duplicates(const header_link& coinbase,
const context& ctx) const NOEXCEPT;
/// Called by block_confirmable (populate and check double spends).
code unspendable(uint32_t sequence, bool coinbase,
const tx_link& prevout_tx, uint32_t version,
const context& ctx) const NOEXCEPT;
/// Called by block_confirmable (populate and check double spends).
code populate_prevouts(point_sets& sets, size_t points,
const header_link& link) const NOEXCEPT;
/// TODO: apply these to compact block confirmation, as the block will
/// TODO: associate existing txs, making it impossible to rely on the
/// TODO: duplicates table. The full query approach is used instead.
bool get_double_spenders(tx_links& out, const block& block) const NOEXCEPT;
bool get_double_spenders(tx_links& out, const point& point,
const point_link& self) const NOEXCEPT;
/// Support set_strong and set_unstrong writers.
bool set_strong(const header_link& link, size_t count,
const tx_link& first_fk, bool positive) NOEXCEPT;
/// Get all tx links for any point of block that is also in duplicate table.
bool get_doubles(tx_links& out, const block& block) const NOEXCEPT;
bool get_doubles(tx_links& out, const point& point) const NOEXCEPT;
/// Context.
/// -----------------------------------------------------------------------
/// any blocks
bool populate_bits(chain_state::data& data,
const chain_state::map& map, header_link link) const NOEXCEPT;
bool populate_versions(chain_state::data& data,
const chain_state::map& map, header_link link) const NOEXCEPT;
bool populate_timestamps(chain_state::data& data,
const chain_state::map& map, header_link link) const NOEXCEPT;
bool populate_retarget(chain_state::data& data,
const chain_state::map& map, header_link link) const NOEXCEPT;
bool populate_hashes(chain_state::data& data,
const chain_state::map& map) const NOEXCEPT;
bool populate_work(chain_state::data& data,
header_link link) const NOEXCEPT;
bool populate_all(chain_state::data& data,
const system::settings& settings, const header_link& link,
size_t height) const NOEXCEPT;
/// candidate blocks
bool populate_candidate_bits(chain_state::data& data,
const chain_state::map& map, const header& header) const NOEXCEPT;
bool populate_candidate_versions(chain_state::data& data,
const chain_state::map& map, const header& header) const NOEXCEPT;
bool populate_candidate_timestamps(chain_state::data& data,
const chain_state::map& map, const header& header) const NOEXCEPT;
bool populate_candidate_retarget(chain_state::data& data,
const chain_state::map& map, const header& header) const NOEXCEPT;
bool populate_candidate_work(chain_state::data& data,
const header& header) const NOEXCEPT;
bool populate_candidate_all(chain_state::data& data,
const system::settings& settings, const header& header,
const header_link& link, size_t height) const NOEXCEPT;
/// address
/// -----------------------------------------------------------------------
code get_address_outputs_turbo(std::atomic_bool& cancel,
outpoints& out, const hash_digest& key) const NOEXCEPT;
code get_confirmed_unspent_outputs_turbo(std::atomic_bool& cancel,
outpoints& out, const hash_digest& key) const NOEXCEPT;
code get_minimum_unspent_outputs_turbo(std::atomic_bool& cancel,
outpoints& out, const hash_digest& key,
uint64_t minimum) const NOEXCEPT;
/// merkle
/// -----------------------------------------------------------------------
struct position { size_t sibling; size_t width; };
using positions = std::vector<position>;
// merkle related utilities
static hash_digest partial_subroot(hashes&& tree, size_t span) NOEXCEPT;
static void merge_merkle(hashes& path, hashes&& leaves, size_t first,
size_t lift) NOEXCEPT;
static positions merkle_branch(size_t leaf, size_t leaves,
bool compress=false) NOEXCEPT;
// merkle related configuration
size_t interval_depth() const NOEXCEPT;
size_t initialize_span() const NOEXCEPT;
hash_option get_confirmed_interval(size_t height) const NOEXCEPT;
hash_option create_interval(header_link link, size_t height) const NOEXCEPT;
code get_merkle_subroots(hashes& roots, size_t waypoint) const NOEXCEPT;
code get_merkle_proof(hashes& proof, hashes roots, size_t target,
size_t waypoint) const NOEXCEPT;
/// tx_fk must be allocated.
/// -----------------------------------------------------------------------
code set_code(const tx_link& tx_fk, const transaction& tx,
bool bypass) NOEXCEPT;
private:
// Chain objects.
template <typename Bool>
static inline bool push_bool(std_vector<Bool>& stack,
const Bool& element) NOEXCEPT;
template <typename Functor>
static inline code parallel_address_transform(std::atomic_bool& cancel,
outpoints& out, const output_links& links, Functor&& functor) NOEXCEPT;
// Not thread safe.
size_t get_fork_() const NOEXCEPT;
// These are thread safe.
mutable std::shared_mutex candidate_reorganization_mutex_{};
mutable std::shared_mutex confirmed_reorganization_mutex_{};
mutable std::atomic<size_t> span_{};
Store& store_;
};
} // namespace database
} // namespace libbitcoin
#define TEMPLATE template <typename Store>
#define CLASS query<Store>
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
#include <bitcoin/database/impl/query/query.ipp>
#include <bitcoin/database/impl/query/archive_read.ipp>
#include <bitcoin/database/impl/query/archive_write.ipp>
#include <bitcoin/database/impl/query/confirm.ipp>
#include <bitcoin/database/impl/query/consensus.ipp>
#include <bitcoin/database/impl/query/context.ipp>
#include <bitcoin/database/impl/query/extent.ipp>
#include <bitcoin/database/impl/query/fees.ipp>
#include <bitcoin/database/impl/query/height.ipp>
#include <bitcoin/database/impl/query/initialize.ipp>
#include <bitcoin/database/impl/query/network.ipp>
#include <bitcoin/database/impl/query/objects.ipp>
#include <bitcoin/database/impl/query/optional.ipp>
#include <bitcoin/database/impl/query/merkle.ipp>
#include <bitcoin/database/impl/query/translate.ipp>
#include <bitcoin/database/impl/query/validate.ipp>
BC_POP_WARNING()
#undef CLASS
#undef TEMPLATE
#endif