forked from libbitcoin/libbitcoin-database
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoptional.ipp
More file actions
478 lines (401 loc) · 13.4 KB
/
optional.ipp
File metadata and controls
478 lines (401 loc) · 13.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
/**
* 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_OPTIONAL_IPP
#define LIBBITCOIN_DATABASE_QUERY_OPTIONAL_IPP
#include <atomic>
#include <algorithm>
#include <ranges>
#include <utility>
#include <bitcoin/system.hpp>
#include <bitcoin/database/define.hpp>
// TODO: address table could use point keys to compress the multimap.
namespace libbitcoin {
namespace database {
// Address (natural-keyed).
// ----------------------------------------------------------------------------
// private/static
TEMPLATE
template <typename Functor>
inline code CLASS::parallel_address_transform(const std::atomic_bool& cancel,
outpoints& out, const output_links& links, Functor&& functor) NOEXCEPT
{
constexpr auto parallel = poolstl::execution::par;
std::atomic_bool fail{};
std::vector<outpoint> outpoints(links.size());
std::transform(parallel, links.begin(), links.end(), outpoints.begin(),
[&functor, &fail](const auto& link) NOEXCEPT
{
return functor(link, fail);
});
out.clear();
if (fail) return error::integrity;
if (cancel) return error::canceled;
for (auto& outpoint: outpoints)
{
if (cancel) return error::canceled;
if (outpoint.point().index() != point::null_index)
out.insert(std::move(outpoint));
}
return error::success;
}
// protected
TEMPLATE
code CLASS::to_address_outputs(const std::atomic_bool& cancel,
output_links& out, const hash_digest& key) const NOEXCEPT
{
// Pushing into the vector is more efficient than precomputation of size.
out.clear();
for (auto it = store_.address.it(key); it; ++it)
{
if (cancel)
return error::canceled;
table::address::record address{};
if (!store_.address.get(it, address))
return error::integrity;
out.push_back(address.output_fk);
}
return error::success;
}
// protected
TEMPLATE
code CLASS::get_address_outputs_turbo(const std::atomic_bool& cancel,
outpoints& out, const hash_digest& key) const NOEXCEPT
{
out.clear();
output_links links{};
if (const code ec = to_address_outputs(cancel, links, key))
return ec;
return parallel_address_transform(cancel, out, links,
[this, &cancel](const auto& link, auto& fail) NOEXCEPT
{
if (cancel || fail) return outpoint{};
auto outpoint = get_spent(link);
fail = (outpoint.point().index() == point::null_index);
return outpoint;
});
}
TEMPLATE
code CLASS::get_address_outputs(const std::atomic_bool& cancel,
outpoints& out, const hash_digest& key, bool turbo) const NOEXCEPT
{
if (turbo && store_.turbo())
return get_address_outputs_turbo(cancel, out, key);
out.clear();
for (auto it = store_.address.it(key); it; ++it)
{
if (cancel)
return error::canceled;
table::address::record address{};
if (!store_.address.get(it, address))
return error::integrity;
out.insert(get_spent(address.output_fk));
}
return error::success;
}
// protected
TEMPLATE
code CLASS::get_confirmed_unspent_outputs_turbo(const std::atomic_bool& cancel,
outpoints& out, const hash_digest& key) const NOEXCEPT
{
out.clear();
output_links links{};
if (const code ec = to_address_outputs(cancel, links, key))
return ec;
return parallel_address_transform(cancel, out, links,
[this, &cancel](const auto& link, auto& fail) NOEXCEPT
{
if (cancel || fail || !is_confirmed_unspent(link))
return outpoint{};
auto outpoint = get_spent(link);
fail = (outpoint.point().index() == point::null_index);
return outpoint;
});
}
TEMPLATE
code CLASS::get_confirmed_unspent_outputs(const std::atomic_bool& cancel,
outpoints& out, const hash_digest& key, bool turbo) const NOEXCEPT
{
if (turbo && store_.turbo())
return get_confirmed_unspent_outputs_turbo(cancel, out, key);
out.clear();
for (auto it = store_.address.it(key); it; ++it)
{
if (cancel)
return error::canceled;
table::address::record address{};
if (!store_.address.get(it, address))
return error::integrity;
if (is_confirmed_unspent(address.output_fk))
out.insert(get_spent(address.output_fk));
}
return error::success;
}
// protected
TEMPLATE
code CLASS::get_minimum_unspent_outputs_turbo(const std::atomic_bool& cancel,
outpoints& out, const hash_digest& key, uint64_t minimum) const NOEXCEPT
{
out.clear();
output_links links{};
if (const code ec = to_address_outputs(cancel, links, key))
return ec;
return parallel_address_transform(cancel, out, links,
[this, &cancel, minimum](const auto& link, auto& fail) NOEXCEPT
{
if (cancel || fail || !is_confirmed_unspent(link))
return outpoint{};
uint64_t value{};
if (!get_value(value, link))
{
fail = true;
return outpoint{};
}
if (value < minimum)
return outpoint{};
auto outpoint = get_spent(link);
fail = (outpoint.point().index() == point::null_index);
return outpoint;
});
}
TEMPLATE
code CLASS::get_minimum_unspent_outputs(const std::atomic_bool& cancel,
outpoints& out, const hash_digest& key,
uint64_t minimum, bool turbo) const NOEXCEPT
{
if (turbo && store_.turbo())
return get_minimum_unspent_outputs_turbo(cancel, out, key, minimum);
out.clear();
for (auto it = store_.address.it(key); it; ++it)
{
if (cancel)
return error::canceled;
table::address::record address{};
if (!store_.address.get(it, address))
return error::integrity;
if (is_confirmed_unspent(address.output_fk))
{
uint64_t value{};
if (!get_value(value, address.output_fk))
return error::integrity;
if (value >= minimum)
out.insert(get_spent(address.output_fk));
}
}
return error::success;
}
TEMPLATE
code CLASS::get_confirmed_balance(const std::atomic_bool& cancel,
uint64_t& balance, const hash_digest& key, bool turbo) const NOEXCEPT
{
outpoints outs{};
if (code ec = get_confirmed_unspent_outputs(cancel, outs, key, turbo))
{
balance = zero;
return ec;
}
// Use of to_confirmed_unspent_outputs() provides necessary deduplication.
balance = std::accumulate(outs.begin(), outs.end(), zero,
[](size_t total, const outpoint& out) NOEXCEPT
{
return system::ceilinged_add(total, out.value());
});
return error::success;
}
////TEMPLATE
////bool CLASS::set_address_output(const output& output,
//// const output_link& link) NOEXCEPT
////{
//// return set_address_output(output.script().hash(), link);
////}
////
////TEMPLATE
////bool CLASS::set_address_output(const hash_digest& key,
//// const output_link& link) NOEXCEPT
////{
//// if (link.is_terminal())
//// return false;
////
//// // ========================================================================
//// const auto scope = store_.get_transactor();
////
//// // Clean single allocation failure (e.g. disk full).
//// return store_.address.put(key, table::address::record
//// {
//// {},
//// link
//// });
//// // ========================================================================
////}
// filter_tx (surrogate-keyed).
// ----------------------------------------------------------------------------
TEMPLATE
bool CLASS::is_filtered_body(const header_link& link) const NOEXCEPT
{
return store_.filter_tx.exists(to_filter_tx(link));
}
TEMPLATE
bool CLASS::get_filter_body(filter& out, const header_link& link) const NOEXCEPT
{
table::filter_tx::get_filter filter_tx{};
if (!store_.filter_tx.at(to_filter_tx(link), filter_tx))
return false;
out = std::move(filter_tx.filter);
return true;
}
// filter_bk (surrogate-keyed).
// ----------------------------------------------------------------------------
TEMPLATE
bool CLASS::is_filtered_head(const header_link& link) const NOEXCEPT
{
return store_.filter_bk.exists(to_filter_bk(link));
}
TEMPLATE
bool CLASS::get_filter_head(hash_digest& out,
const header_link& link) const NOEXCEPT
{
table::filter_bk::get_head_only filter_bk{};
if (!store_.filter_bk.at(to_filter_bk(link), filter_bk))
return false;
out = std::move(filter_bk.head);
return true;
}
TEMPLATE
bool CLASS::get_filter_hash(hash_digest& out,
const header_link& link) const NOEXCEPT
{
table::filter_bk::get_hash_only filter_bk{};
if (!store_.filter_bk.at(to_filter_bk(link), filter_bk))
return false;
out = std::move(filter_bk.hash);
return true;
}
TEMPLATE
bool CLASS::get_filter_hashes(hashes& filter_hashes,
hash_digest& previous_header, const header_link& stop_link,
size_t count) const NOEXCEPT
{
size_t height{};
if (!get_height(height, stop_link))
return false;
count = std::min(add1(height), count);
filter_hashes.resize(count);
auto link = stop_link;
// Reversal allows ancenstry population into forward vector.
for (auto& hash: std::views::reverse(filter_hashes))
{
// Implies that stop_link is not a filtered block.
if (!get_filter_hash(hash, link))
return false;
// Ancestry from stop link (included) ensures continuity without locks.
link = to_parent(link);
}
// There's no trailing increment without at least one loop iteration.
if (is_zero(count))
link = to_parent(link);
// link is genesis, previous is null.
if (link.is_terminal())
{
previous_header = system::null_hash;
return true;
}
// Obtaining previous from ancestry eansures its continuity as well.
return get_filter_head(previous_header, link);
}
TEMPLATE
bool CLASS::get_filter_heads(hashes& filter_heads,
size_t stop_height, size_t interval) const NOEXCEPT
{
size_t height{};
filter_heads.resize(system::floored_divide(stop_height, interval));
for (auto& head: filter_heads)
if (!get_filter_head(head, to_confirmed((height += interval))))
return false;
return true;
}
// set_filter_body
// ----------------------------------------------------------------------------
TEMPLATE
bool CLASS::set_filter_body(const header_link& link,
const block& block) NOEXCEPT
{
using namespace system::neutrino;
if (!filter_enabled())
return true;
// Compute the current filter from the block and store under the link.
filter body{};
return compute_filter(body, block) && set_filter_body(link, body);
}
TEMPLATE
bool CLASS::set_filter_body(const header_link& link,
const filter& filter) NOEXCEPT
{
if (!filter_enabled())
return true;
// ========================================================================
const auto scope = store_.get_transactor();
// Clean single allocation failure (e.g. disk full).
return store_.filter_tx.put(to_filter_tx(link), table::filter_tx::put_ref
{
{},
filter
});
// ========================================================================
}
// set_filter_head
// ----------------------------------------------------------------------------
TEMPLATE
bool CLASS::set_filter_head(const header_link& link) NOEXCEPT
{
using namespace system::neutrino;
if (!filter_enabled())
return true;
// The filter body must have been previously stored under the block link.
filter body{};
if (!get_filter_body(body, link))
return false;
// If genesis then parent is terminal (returns null_hash).
hash_digest previous{};
const auto parent = to_parent(link);
if (!parent.is_terminal())
if (!get_filter_head(previous, parent))
return false;
// Use previous head and current body to compute current hash and head.
hash_digest hash{};
return set_filter_head(link, compute_header(hash, previous, body), hash);
}
TEMPLATE
bool CLASS::set_filter_head(const header_link& link, const hash_digest& head,
const hash_digest& hash) NOEXCEPT
{
if (!filter_enabled())
return true;
// ========================================================================
const auto scope = store_.get_transactor();
// Clean single allocation failure (e.g. disk full).
return store_.filter_bk.put(to_filter_bk(link), table::filter_bk::put_ref
{
{},
hash,
head
});
// ========================================================================
}
} // namespace database
} // namespace libbitcoin
#endif