Skip to content

Commit 1b4b81c

Browse files
authored
Merge pull request #773 from evoskuil/master
Move fork/work related queries to store.
2 parents b549c9f + 2cd897f commit 1b4b81c

File tree

10 files changed

+40
-161
lines changed

10 files changed

+40
-161
lines changed

include/bitcoin/node/chasers/chaser.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,6 @@ class BCN_API chaser
9393
/// Reset store disk full condition.
9494
virtual code reload(const store::event_handler& handler) NOEXCEPT;
9595

96-
/// Get reorganization lock.
97-
virtual lock get_reorganization_lock() const NOEXCEPT;
98-
9996
/// Events.
10097
/// -----------------------------------------------------------------------
10198

include/bitcoin/node/chasers/chaser_confirm.hpp

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ class BCN_API chaser_confirm
4040
code start() NOEXCEPT override;
4141

4242
protected:
43-
using header_links = std_vector<database::header_link>;
44-
typedef network::race_unity<const code&, const database::tx_link&> race;
43+
using header_link = database::header_link;
44+
using header_links = database::header_links;
4545

4646
virtual bool handle_event(const code& ec, chase event_,
4747
event_value value) NOEXCEPT;
@@ -54,27 +54,18 @@ class BCN_API chaser_confirm
5454
virtual void reorganize(header_links& fork, size_t fork_point) NOEXCEPT;
5555
virtual void organize(header_links& fork, const header_links& popped,
5656
size_t fork_point) NOEXCEPT;
57-
virtual bool confirm_block(const database::header_link& link,
57+
virtual bool confirm_block(const header_link& link,
5858
size_t height, const header_links& popped, size_t fork_point) NOEXCEPT;
59-
virtual void complete_block(const code& ec,
60-
const database::header_link& link, size_t height,
61-
bool bypassed) NOEXCEPT;
59+
virtual void complete_block(const code& ec, const header_link& link,
60+
size_t height, bool bypassed) NOEXCEPT;
6261

6362
private:
64-
// setters
65-
bool set_reorganized(const database::header_link& link,
63+
bool set_reorganized(const header_link& link,
6664
height_t confirmed_height) NOEXCEPT;
67-
bool set_organized(const database::header_link& link,
65+
bool set_organized(const header_link& link,
6866
height_t confirmed_height) NOEXCEPT;
6967
bool roll_back(const header_links& popped, size_t fork_point,
7068
size_t top) NOEXCEPT;
71-
72-
// getters
73-
header_links get_fork(height_t fork_top) const NOEXCEPT;
74-
bool get_work(uint256_t& fork_work,
75-
const header_links& fork) const NOEXCEPT;
76-
bool get_strong(bool& strong, const uint256_t& fork_work,
77-
size_t fork_point) const NOEXCEPT;
7869
};
7970

8071
} // namespace node

include/bitcoin/node/define.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
/// Standard includes (do not include directly).
2323
#include <functional>
2424
#include <memory>
25-
#include <mutex>
2625
#include <utility>
2726
#include <variant>
2827

@@ -58,7 +57,6 @@ typedef std::error_code code;
5857
typedef std::function<void(const code&, size_t)> organize_handler;
5958
typedef database::store<database::map> store;
6059
typedef database::query<store> query;
61-
typedef std::unique_lock<std::mutex> lock;
6260

6361
/// Work types.
6462
typedef network::race_all<const code&> job;

include/bitcoin/node/error.hpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ enum error_t : uint8_t
8585
validate6,
8686
validate7,
8787
validate8,
88-
////validate9,
8988
confirm1,
9089
confirm2,
9190
confirm3,
@@ -95,12 +94,10 @@ enum error_t : uint8_t
9594
confirm7,
9695
confirm8,
9796
confirm9,
98-
confirm10
99-
////confirm11,
100-
////confirm12,
101-
////confirm13,
102-
////confirm14,
103-
////confirm15
97+
confirm10,
98+
confirm11,
99+
confirm12,
100+
confirm13
104101
};
105102

106103
// No current need for error_code equivalence mapping.

include/bitcoin/node/full_node.hpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,6 @@ class BCN_API full_node
139139
/// Get the memory resource.
140140
virtual network::memory& get_memory() NOEXCEPT;
141141

142-
/// Get reorganization lock.
143-
/// Used to prevent candidate chain reorganization during confirmation.
144-
virtual lock get_reorganization_lock() const NOEXCEPT;
145-
146142
protected:
147143
/// Session attachments.
148144
/// -----------------------------------------------------------------------
@@ -164,7 +160,6 @@ class BCN_API full_node
164160
event_value value) NOEXCEPT;
165161

166162
// These are thread safe.
167-
mutable std::mutex reorganization_mutex_{};
168163
const configuration& config_;
169164
memory_controller memory_;
170165
query& query_;

include/bitcoin/node/impl/chasers/chaser_organize.ipp

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -232,16 +232,12 @@ void CLASS::do_organize(typename Block::cptr block,
232232

233233
// Pop down to the branch point.
234234
auto index = top_candidate;
235-
if (top_candidate > branch_point)
235+
while (index > branch_point)
236236
{
237-
get_reorganization_lock();
238-
while (index > branch_point)
237+
if (!set_reorganized(index--))
239238
{
240-
if (!set_reorganized(index--))
241-
{
242-
handler(fault(error::organize5), height);
243-
return;
244-
}
239+
handler(fault(error::organize5), height);
240+
return;
245241
}
246242
}
247243

@@ -378,16 +374,12 @@ void CLASS::do_disorganize(header_t link) NOEXCEPT
378374
// ........................................................................
379375

380376
const auto top_candidate = state_->height();
381-
if (top_candidate > fork_point)
377+
for (auto index = top_candidate; index > fork_point; --index)
382378
{
383-
get_reorganization_lock();
384-
for (auto index = top_candidate; index > fork_point; --index)
379+
if (!set_reorganized(index))
385380
{
386-
if (!set_reorganized(index))
387-
{
388-
fault(error::organize11);
389-
return;
390-
}
381+
fault(error::organize11);
382+
return;
391383
}
392384
}
393385

src/chasers/chaser.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,6 @@ code chaser::reload(const store::event_handler& handler) NOEXCEPT
8383
return node_.reload(handler);
8484
}
8585

86-
lock chaser::get_reorganization_lock() const NOEXCEPT
87-
{
88-
return node_.get_reorganization_lock();
89-
}
90-
9186
// Events.
9287
// ----------------------------------------------------------------------------
9388

src/chasers/chaser_confirm.cpp

Lines changed: 17 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ namespace node {
3131
#define CLASS chaser_confirm
3232

3333
using namespace system;
34-
using namespace database;
3534
using namespace std::placeholders;
3635

3736
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
@@ -155,25 +154,26 @@ void chaser_confirm::do_bump(height_t) NOEXCEPT
155154
void chaser_confirm::do_bumped(height_t height) NOEXCEPT
156155
{
157156
BC_ASSERT(stranded());
157+
const auto& query = archive();
158158

159159
if (closed())
160160
return;
161161

162162
// If empty height is not on a candidate fork (may have been reorganized).
163-
auto fork = get_fork(height);
163+
auto fork = query.get_candidate_fork(height);
164164
if (fork.empty())
165165
return;
166166

167167
uint256_t work{};
168-
if (!get_work(work, fork))
168+
if (!query.get_work(work, fork))
169169
{
170170
fault(error::confirm1);
171171
return;
172172
}
173173

174174
bool strong{};
175175
const auto fork_point = height - fork.size();
176-
if (!get_strong(strong, work, fork_point))
176+
if (!query.get_strong(strong, work, fork_point))
177177
{
178178
fault(error::confirm2);
179179
return;
@@ -195,12 +195,12 @@ void chaser_confirm::do_bumped(height_t height) NOEXCEPT
195195
void chaser_confirm::reorganize(header_links& fork, size_t fork_point) NOEXCEPT
196196
{
197197
BC_ASSERT(stranded());
198-
199198
const auto& query = archive();
199+
200200
auto height = query.get_top_confirmed();
201201
if (height < fork_point)
202202
{
203-
fault(error::confirm4);
203+
fault(error::confirm3);
204204
return;
205205
}
206206

@@ -210,14 +210,14 @@ void chaser_confirm::reorganize(header_links& fork, size_t fork_point) NOEXCEPT
210210
const auto link = query.to_confirmed(height);
211211
if (link.is_terminal())
212212
{
213-
fault(error::confirm5);
213+
fault(error::confirm4);
214214
return;
215215
}
216216

217217
popped.push_back(link);
218218
if (!set_reorganized(link, height--))
219219
{
220-
fault(error::confirm6);
220+
fault(error::confirm5);
221221
return;
222222
}
223223
}
@@ -253,7 +253,7 @@ void chaser_confirm::organize(header_links& fork, const header_links& popped,
253253
{
254254
if (!query.set_filter_head(link))
255255
{
256-
fault(error::confirm3);
256+
fault(error::confirm6);
257257
return;
258258
}
259259

@@ -274,15 +274,15 @@ void chaser_confirm::organize(header_links& fork, const header_links& popped,
274274
}
275275
default:
276276
{
277-
fault(error::confirm4);
277+
fault(error::confirm7);
278278
return;
279279
}
280280
}
281281

282282
// After set_block_confirmable.
283283
if (!set_organized(link, height++))
284284
{
285-
fault(error::confirm5);
285+
fault(error::confirm8);
286286
return;
287287
}
288288

@@ -304,19 +304,19 @@ bool chaser_confirm::confirm_block(const header_link& link,
304304
{
305305
if (!query.set_unstrong(link))
306306
{
307-
fault(error::confirm6);
307+
fault(error::confirm9);
308308
return false;
309309
}
310310

311311
if (!query.set_block_unconfirmable(link))
312312
{
313-
fault(error::confirm7);
313+
fault(error::confirm10);
314314
return false;
315315
}
316316

317317
if (!roll_back(popped, fork_point, sub1(height)))
318318
{
319-
fault(error::confirm8);
319+
fault(error::confirm11);
320320
return false;
321321
}
322322

@@ -327,13 +327,13 @@ bool chaser_confirm::confirm_block(const header_link& link,
327327
// Before set_block_confirmable.
328328
if (!query.set_filter_head(link))
329329
{
330-
fault(error::confirm9);
330+
fault(error::confirm12);
331331
return false;
332332
}
333333

334334
if (!query.set_block_confirmable(link))
335335
{
336-
fault(error::confirm10);
336+
fault(error::confirm13);
337337
return false;
338338
}
339339

@@ -367,7 +367,7 @@ void chaser_confirm::complete_block(const code& ec, const header_link& link,
367367
LOGV("Block confirmable: " << height);
368368
}
369369

370-
// Private setters
370+
// private
371371
// ----------------------------------------------------------------------------
372372
// Checkpointed blocks are set strong by archiver, and cannot be reorganized.
373373

@@ -421,84 +421,6 @@ bool chaser_confirm::roll_back(const header_links& popped, size_t fork_point,
421421
return true;
422422
}
423423

424-
// Private getters
425-
// ----------------------------------------------------------------------------
426-
427-
// TODO: move into database library with internal lock.
428-
chaser_confirm::header_links chaser_confirm::get_fork(
429-
height_t fork_top) const NOEXCEPT
430-
{
431-
BC_ASSERT(stranded());
432-
const auto& query = archive();
433-
header_link link{};
434-
header_links out{};
435-
436-
// Prevents organizer from popping candidates (does not block push).
437-
get_reorganization_lock();
438-
439-
// Walk down candidates from fork_top to fork point (highest common).
440-
for (link = query.to_candidate(fork_top);
441-
!link.is_terminal() && !query.is_confirmed_block(link);
442-
link = query.to_candidate(--fork_top))
443-
{
444-
out.push_back(link);
445-
}
446-
447-
// Terminal candidate from previously valid height implies regression.
448-
// This is ok, it just means that the fork is no longer a candidate.
449-
if (link.is_terminal())
450-
out.clear();
451-
452-
return out;
453-
}
454-
455-
bool chaser_confirm::get_work(uint256_t& fork_work,
456-
const header_links& fork) const NOEXCEPT
457-
{
458-
BC_ASSERT(stranded());
459-
const auto& query = archive();
460-
461-
// Walk down candidates from fork_top to fork point (highest common).
462-
for (const auto& link: fork)
463-
{
464-
uint32_t bits{};
465-
if (!query.get_bits(bits, link))
466-
return false;
467-
468-
fork_work += chain::header::proof(bits);
469-
}
470-
471-
return true;
472-
}
473-
474-
// A fork with greater work will cause confirmed reorganization.
475-
bool chaser_confirm::get_strong(bool& strong, const uint256_t& fork_work,
476-
size_t fork_point) const NOEXCEPT
477-
{
478-
BC_ASSERT(stranded());
479-
uint256_t confirmed_work{};
480-
const auto& query = archive();
481-
482-
for (auto height = query.get_top_confirmed(); height > fork_point;
483-
--height)
484-
{
485-
uint32_t bits{};
486-
if (!query.get_bits(bits, query.to_confirmed(height)))
487-
return false;
488-
489-
// Not strong when confirmed_work equals or exceeds fork_work.
490-
confirmed_work += chain::header::proof(bits);
491-
if (confirmed_work >= fork_work)
492-
{
493-
strong = false;
494-
return true;
495-
}
496-
}
497-
498-
strong = true;
499-
return true;
500-
}
501-
502424
BC_POP_WARNING()
503425

504426
} // namespace node

0 commit comments

Comments
 (0)