|
| 1 | +/** |
| 2 | + * Copyright (c) 2011-2025 libbitcoin developers (see AUTHORS) |
| 3 | + * |
| 4 | + * This file is part of libbitcoin. |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Affero General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Affero General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Affero General Public License |
| 17 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | +#ifndef LIBBITCOIN_DATABASE_QUERY_NETWORK_IPP |
| 20 | +#define LIBBITCOIN_DATABASE_QUERY_NETWORK_IPP |
| 21 | + |
| 22 | +#include <algorithm> |
| 23 | +#include <bitcoin/system.hpp> |
| 24 | +#include <bitcoin/database/define.hpp> |
| 25 | + |
| 26 | +namespace libbitcoin { |
| 27 | +namespace database { |
| 28 | + |
| 29 | +// locator readers |
| 30 | +// ---------------------------------------------------------------------------- |
| 31 | +// These do not require strict consistency. |
| 32 | + |
| 33 | +TEMPLATE |
| 34 | +CLASS::headers CLASS::get_headers(const hashes& locator, |
| 35 | + const hash_digest& stop, size_t limit) const NOEXCEPT |
| 36 | +{ |
| 37 | + headers out{}; |
| 38 | + const auto span = get_locator_span(locator, stop, limit); |
| 39 | + out.reserve(span.size()); |
| 40 | + |
| 41 | + for (auto height = span.begin; height < span.end; ++height) |
| 42 | + { |
| 43 | + // Terminal implies intervening reorganization. |
| 44 | + const auto link = to_confirmed(height); |
| 45 | + if (link.is_terminal()) |
| 46 | + return {}; |
| 47 | + |
| 48 | + out.push_back(get_header(link)); |
| 49 | + BC_ASSERT(!is_null(out.back())); |
| 50 | + } |
| 51 | + |
| 52 | + return out; |
| 53 | +} |
| 54 | + |
| 55 | +TEMPLATE |
| 56 | +hashes CLASS::get_blocks(const hashes& locator, |
| 57 | + const hash_digest& stop, size_t limit) const NOEXCEPT |
| 58 | +{ |
| 59 | + hashes out{}; |
| 60 | + const auto span = get_locator_span(locator, stop, limit); |
| 61 | + out.reserve(span.size()); |
| 62 | + |
| 63 | + for (auto height = span.begin; height < span.end; ++height) |
| 64 | + { |
| 65 | + // Terminal implies intervening reorganization. |
| 66 | + const auto link = to_confirmed(height); |
| 67 | + if (link.is_terminal()) |
| 68 | + return {}; |
| 69 | + |
| 70 | + out.push_back(get_header_key(link)); |
| 71 | + BC_ASSERT(out.back() != system::null_hash); |
| 72 | + } |
| 73 | + |
| 74 | + return out; |
| 75 | +} |
| 76 | + |
| 77 | +TEMPLATE |
| 78 | +CLASS::span CLASS::get_locator_span(const hashes& locator, |
| 79 | + const hash_digest& stop, size_t limit) const NOEXCEPT |
| 80 | +{ |
| 81 | + using namespace system; |
| 82 | + span out{}; |
| 83 | + |
| 84 | + // Start at fork point, stop at given header (both excluded). |
| 85 | + const auto start = add1(get_fork(locator)); |
| 86 | + const auto last1 = (stop == null_hash) ? max_uint32 : |
| 87 | + get_height(to_header(stop)).value; |
| 88 | + |
| 89 | + // Determine number of headers requested, limited by max allowed. |
| 90 | + const auto request = floored_subtract<size_t>(last1, start); |
| 91 | + const auto allowed = std::min(request, limit); |
| 92 | + |
| 93 | + // Set end to (start + allowed), limited by (top + 1). |
| 94 | + const auto top1 = ceilinged_add(get_top_confirmed(), one); |
| 95 | + const auto end = std::min(ceilinged_add(start, allowed), top1); |
| 96 | + |
| 97 | + // Convert negative range to empty. |
| 98 | + out.end = std::max(start, end); |
| 99 | + return out; |
| 100 | +} |
| 101 | + |
| 102 | +// protected |
| 103 | +TEMPLATE |
| 104 | +size_t CLASS::get_fork(const hashes& locator) const NOEXCEPT |
| 105 | +{ |
| 106 | + // Locator is presumed (by convention) to be in order by height. |
| 107 | + for (const auto& hash: locator) |
| 108 | + { |
| 109 | + const auto link = to_header(hash); |
| 110 | + const auto height = get_height(link); |
| 111 | + |
| 112 | + table::height::record confirmed{}; |
| 113 | + if (store_.confirmed.get(height, confirmed) && |
| 114 | + confirmed.header_fk == link) |
| 115 | + return height; |
| 116 | + } |
| 117 | + |
| 118 | + return zero; |
| 119 | +} |
| 120 | + |
| 121 | +} // namespace database |
| 122 | +} // namespace libbitcoin |
| 123 | + |
| 124 | +#endif |
0 commit comments