Skip to content

Commit 0980db2

Browse files
committed
Implement candidate reorganization interlock.
1 parent ed7c64e commit 0980db2

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

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

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

235235
// Pop down to the branch point.
236236
auto index = top_candidate;
237-
while (index > branch_point)
237+
if (top_candidate > branch_point)
238238
{
239-
if (!set_reorganized(index--))
239+
get_reorganization_lock();
240+
while (index > branch_point)
240241
{
241-
handler(fault(error::organize5), height);
242-
return;
242+
if (!set_reorganized(index--))
243+
{
244+
handler(fault(error::organize5), height);
245+
return;
246+
}
243247
}
244248
}
245249

@@ -376,12 +380,16 @@ void CLASS::do_disorganize(header_t link) NOEXCEPT
376380
// ........................................................................
377381

378382
const auto top_candidate = state_->height();
379-
for (auto index = top_candidate; index > fork_point; --index)
383+
if (top_candidate > fork_point)
380384
{
381-
if (!set_reorganized(index))
385+
get_reorganization_lock();
386+
for (auto index = top_candidate; index > fork_point; --index)
382387
{
383-
fault(error::organize11);
384-
return;
388+
if (!set_reorganized(index))
389+
{
390+
fault(error::organize11);
391+
return;
392+
}
385393
}
386394
}
387395

src/chasers/chaser_confirm.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ void chaser_confirm::do_bumped(height_t height) NOEXCEPT
159159
if (closed())
160160
return;
161161

162+
// Prevents organizer from popping candidates (does not block push).
163+
get_reorganization_lock();
164+
162165
// Scan from candidate height to first confirmed, return links and work.
163166
uint256_t work{};
164167
header_links fork{};
@@ -291,6 +294,7 @@ void chaser_confirm::organize(header_links& fork, const header_links& popped,
291294
}
292295

293296
// Prevent stall by posting internal event, avoiding external handlers.
297+
// Posts new work, preventing recursion and releasing reorganization lock.
294298
handle_event(error::success, chase::bump, height_t{});
295299
}
296300

0 commit comments

Comments
 (0)