-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathconsensus_forks.ipp
More file actions
169 lines (142 loc) · 5.18 KB
/
Copy pathconsensus_forks.ipp
File metadata and controls
169 lines (142 loc) · 5.18 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
/**
* 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_CONSENSUS_FORKS_IPP
#define LIBBITCOIN_DATABASE_QUERY_CONSENSUS_FORKS_IPP
#include <mutex>
#include <bitcoin/database/define.hpp>
namespace libbitcoin {
namespace database {
// get_confirmed
// ----------------------------------------------------------------------------
TEMPLATE
height_link CLASS::get_confirmed_height(const header_link& link) const NOEXCEPT
{
if (const auto height = get_height(link); !height.is_terminal())
{
// The block is confirmed (by height).
table::height::record confirmed{};
if (store_.confirmed.get(height, confirmed) &&
(confirmed.header_fk == link))
return height;
}
return {};
}
// protected fork readers.
// ----------------------------------------------------------------------------
// Protected against index pop (low contention) to ensure branch consistency.
// node/snapshot (via is_coalesced())
TEMPLATE
size_t CLASS::get_fork() const NOEXCEPT
{
///////////////////////////////////////////////////////////////////////////
std::shared_lock interlock1{ candidate_reorganization_mutex_ };
std::shared_lock interlock2{ confirmed_reorganization_mutex_ };
return get_fork_();
///////////////////////////////////////////////////////////////////////////
}
// node/organizer
TEMPLATE
header_links CLASS::get_candidate_fork(size_t& fork_point) const NOEXCEPT
{
// Reservation may limit allocation to most common single block scenario.
header_links out{};
out.reserve(one);
///////////////////////////////////////////////////////////////////////////
std::shared_lock interlock{ candidate_reorganization_mutex_ };
fork_point = get_fork_();
auto height = add1(fork_point);
auto link = to_candidate(height);
while (!link.is_terminal())
{
out.push_back(link);
link = to_candidate(++height);
}
return out;
///////////////////////////////////////////////////////////////////////////
}
// node/organizer
TEMPLATE
header_links CLASS::get_confirmed_fork(const header_link& fork) const NOEXCEPT
{
if (fork.is_terminal())
return {};
// Reservation may limit allocation to most common single block scenario.
header_links out{};
out.reserve(one);
///////////////////////////////////////////////////////////////////////////
std::shared_lock interlock{ confirmed_reorganization_mutex_ };
// Verify fork block is still confirmed and get its height.
auto height = get_height(fork);
auto link = to_confirmed(height);
if (link != fork)
return out;
// First link above fork.
link = to_confirmed(++height);
while (!link.is_terminal())
{
out.push_back(link);
link = to_confirmed(++height);
}
return out;
///////////////////////////////////////////////////////////////////////////
}
// node/confirmer
TEMPLATE
header_states CLASS::get_validated_fork(size_t& fork_point,
size_t top_checkpoint) const NOEXCEPT
{
// Reservation may limit allocation to most common scenario.
header_states out{};
out.reserve(one);
code ec{};
// Disable optional constraints when the indexes are disabled.
const auto filter = filter_enabled();
const auto silent = silent_enabled();
const auto silent_start = silent_start_height();
///////////////////////////////////////////////////////////////////////////
std::shared_lock interlock{ candidate_reorganization_mutex_ };
fork_point = get_fork_();
auto height = add1(fork_point);
auto link = to_candidate(height);
while (is_block_validated(ec, link, height, top_checkpoint) &&
(!filter || is_filtered_body(link)) &&
(!silent || height < silent_start || is_silent_indexed(link)))
{
out.emplace_back(link, ec);
link = to_candidate(++height);
}
return out;
///////////////////////////////////////////////////////////////////////////
}
// utility
// ----------------------------------------------------------------------------
// private
// Without both interlocks, or caller controlling one side with the other
// locked, this is unsafe, since the compare of heights requires atomicity.
TEMPLATE
size_t CLASS::get_fork_() const NOEXCEPT
{
for (auto height = get_top_confirmed(); is_nonzero(height); --height)
if (to_confirmed(height) == to_candidate(height))
return height;
return zero;
}
} // namespace database
} // namespace libbitcoin
#endif