Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions include/bitcoin/node/chasers/chaser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,6 @@ class BCN_API chaser
/// Reset store disk full condition.
virtual code reload(const store::event_handler& handler) NOEXCEPT;

/// Get reorganization lock.
virtual lock get_reorganization_lock() const NOEXCEPT;

/// Events.
/// -----------------------------------------------------------------------

Expand Down
23 changes: 7 additions & 16 deletions include/bitcoin/node/chasers/chaser_confirm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class BCN_API chaser_confirm
code start() NOEXCEPT override;

protected:
using header_links = std_vector<database::header_link>;
typedef network::race_unity<const code&, const database::tx_link&> race;
using header_link = database::header_link;
using header_links = database::header_links;

virtual bool handle_event(const code& ec, chase event_,
event_value value) NOEXCEPT;
Expand All @@ -54,27 +54,18 @@ class BCN_API chaser_confirm
virtual void reorganize(header_links& fork, size_t fork_point) NOEXCEPT;
virtual void organize(header_links& fork, const header_links& popped,
size_t fork_point) NOEXCEPT;
virtual bool confirm_block(const database::header_link& link,
virtual bool confirm_block(const header_link& link,
size_t height, const header_links& popped, size_t fork_point) NOEXCEPT;
virtual void complete_block(const code& ec,
const database::header_link& link, size_t height,
bool bypassed) NOEXCEPT;
virtual void complete_block(const code& ec, const header_link& link,
size_t height, bool bypassed) NOEXCEPT;

private:
// setters
bool set_reorganized(const database::header_link& link,
bool set_reorganized(const header_link& link,
height_t confirmed_height) NOEXCEPT;
bool set_organized(const database::header_link& link,
bool set_organized(const header_link& link,
height_t confirmed_height) NOEXCEPT;
bool roll_back(const header_links& popped, size_t fork_point,
size_t top) NOEXCEPT;

// getters
header_links get_fork(height_t fork_top) const NOEXCEPT;
bool get_work(uint256_t& fork_work,
const header_links& fork) const NOEXCEPT;
bool get_strong(bool& strong, const uint256_t& fork_work,
size_t fork_point) const NOEXCEPT;
};

} // namespace node
Expand Down
2 changes: 0 additions & 2 deletions include/bitcoin/node/define.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
/// Standard includes (do not include directly).
#include <functional>
#include <memory>
#include <mutex>
#include <utility>
#include <variant>

Expand Down Expand Up @@ -58,7 +57,6 @@ typedef std::error_code code;
typedef std::function<void(const code&, size_t)> organize_handler;
typedef database::store<database::map> store;
typedef database::query<store> query;
typedef std::unique_lock<std::mutex> lock;

/// Work types.
typedef network::race_all<const code&> job;
Expand Down
11 changes: 4 additions & 7 deletions include/bitcoin/node/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ enum error_t : uint8_t
validate6,
validate7,
validate8,
////validate9,
confirm1,
confirm2,
confirm3,
Expand All @@ -95,12 +94,10 @@ enum error_t : uint8_t
confirm7,
confirm8,
confirm9,
confirm10
////confirm11,
////confirm12,
////confirm13,
////confirm14,
////confirm15
confirm10,
confirm11,
confirm12,
confirm13
};

// No current need for error_code equivalence mapping.
Expand Down
5 changes: 0 additions & 5 deletions include/bitcoin/node/full_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,6 @@ class BCN_API full_node
/// Get the memory resource.
virtual network::memory& get_memory() NOEXCEPT;

/// Get reorganization lock.
/// Used to prevent candidate chain reorganization during confirmation.
virtual lock get_reorganization_lock() const NOEXCEPT;

protected:
/// Session attachments.
/// -----------------------------------------------------------------------
Expand All @@ -164,7 +160,6 @@ class BCN_API full_node
event_value value) NOEXCEPT;

// These are thread safe.
mutable std::mutex reorganization_mutex_{};
const configuration& config_;
memory_controller memory_;
query& query_;
Expand Down
24 changes: 8 additions & 16 deletions include/bitcoin/node/impl/chasers/chaser_organize.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -232,16 +232,12 @@ void CLASS::do_organize(typename Block::cptr block,

// Pop down to the branch point.
auto index = top_candidate;
if (top_candidate > branch_point)
while (index > branch_point)
{
get_reorganization_lock();
while (index > branch_point)
if (!set_reorganized(index--))
{
if (!set_reorganized(index--))
{
handler(fault(error::organize5), height);
return;
}
handler(fault(error::organize5), height);
return;
}
}

Expand Down Expand Up @@ -378,16 +374,12 @@ void CLASS::do_disorganize(header_t link) NOEXCEPT
// ........................................................................

const auto top_candidate = state_->height();
if (top_candidate > fork_point)
for (auto index = top_candidate; index > fork_point; --index)
{
get_reorganization_lock();
for (auto index = top_candidate; index > fork_point; --index)
if (!set_reorganized(index))
{
if (!set_reorganized(index))
{
fault(error::organize11);
return;
}
fault(error::organize11);
return;
}
}

Expand Down
5 changes: 0 additions & 5 deletions src/chasers/chaser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,6 @@ code chaser::reload(const store::event_handler& handler) NOEXCEPT
return node_.reload(handler);
}

lock chaser::get_reorganization_lock() const NOEXCEPT
{
return node_.get_reorganization_lock();
}

// Events.
// ----------------------------------------------------------------------------

Expand Down
112 changes: 17 additions & 95 deletions src/chasers/chaser_confirm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ namespace node {
#define CLASS chaser_confirm

using namespace system;
using namespace database;
using namespace std::placeholders;

BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
Expand Down Expand Up @@ -155,25 +154,26 @@ void chaser_confirm::do_bump(height_t) NOEXCEPT
void chaser_confirm::do_bumped(height_t height) NOEXCEPT
{
BC_ASSERT(stranded());
const auto& query = archive();

if (closed())
return;

// If empty height is not on a candidate fork (may have been reorganized).
auto fork = get_fork(height);
auto fork = query.get_candidate_fork(height);
if (fork.empty())
return;

uint256_t work{};
if (!get_work(work, fork))
if (!query.get_work(work, fork))
{
fault(error::confirm1);
return;
}

bool strong{};
const auto fork_point = height - fork.size();
if (!get_strong(strong, work, fork_point))
if (!query.get_strong(strong, work, fork_point))
{
fault(error::confirm2);
return;
Expand All @@ -195,12 +195,12 @@ void chaser_confirm::do_bumped(height_t height) NOEXCEPT
void chaser_confirm::reorganize(header_links& fork, size_t fork_point) NOEXCEPT
{
BC_ASSERT(stranded());

const auto& query = archive();

auto height = query.get_top_confirmed();
if (height < fork_point)
{
fault(error::confirm4);
fault(error::confirm3);
return;
}

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

popped.push_back(link);
if (!set_reorganized(link, height--))
{
fault(error::confirm6);
fault(error::confirm5);
return;
}
}
Expand Down Expand Up @@ -253,7 +253,7 @@ void chaser_confirm::organize(header_links& fork, const header_links& popped,
{
if (!query.set_filter_head(link))
{
fault(error::confirm3);
fault(error::confirm6);
return;
}

Expand All @@ -274,15 +274,15 @@ void chaser_confirm::organize(header_links& fork, const header_links& popped,
}
default:
{
fault(error::confirm4);
fault(error::confirm7);
return;
}
}

// After set_block_confirmable.
if (!set_organized(link, height++))
{
fault(error::confirm5);
fault(error::confirm8);
return;
}

Expand All @@ -304,19 +304,19 @@ bool chaser_confirm::confirm_block(const header_link& link,
{
if (!query.set_unstrong(link))
{
fault(error::confirm6);
fault(error::confirm9);
return false;
}

if (!query.set_block_unconfirmable(link))
{
fault(error::confirm7);
fault(error::confirm10);
return false;
}

if (!roll_back(popped, fork_point, sub1(height)))
{
fault(error::confirm8);
fault(error::confirm11);
return false;
}

Expand All @@ -327,13 +327,13 @@ bool chaser_confirm::confirm_block(const header_link& link,
// Before set_block_confirmable.
if (!query.set_filter_head(link))
{
fault(error::confirm9);
fault(error::confirm12);
return false;
}

if (!query.set_block_confirmable(link))
{
fault(error::confirm10);
fault(error::confirm13);
return false;
}

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

// Private setters
// private
// ----------------------------------------------------------------------------
// Checkpointed blocks are set strong by archiver, and cannot be reorganized.

Expand Down Expand Up @@ -421,84 +421,6 @@ bool chaser_confirm::roll_back(const header_links& popped, size_t fork_point,
return true;
}

// Private getters
// ----------------------------------------------------------------------------

// TODO: move into database library with internal lock.
chaser_confirm::header_links chaser_confirm::get_fork(
height_t fork_top) const NOEXCEPT
{
BC_ASSERT(stranded());
const auto& query = archive();
header_link link{};
header_links out{};

// Prevents organizer from popping candidates (does not block push).
get_reorganization_lock();

// Walk down candidates from fork_top to fork point (highest common).
for (link = query.to_candidate(fork_top);
!link.is_terminal() && !query.is_confirmed_block(link);
link = query.to_candidate(--fork_top))
{
out.push_back(link);
}

// Terminal candidate from previously valid height implies regression.
// This is ok, it just means that the fork is no longer a candidate.
if (link.is_terminal())
out.clear();

return out;
}

bool chaser_confirm::get_work(uint256_t& fork_work,
const header_links& fork) const NOEXCEPT
{
BC_ASSERT(stranded());
const auto& query = archive();

// Walk down candidates from fork_top to fork point (highest common).
for (const auto& link: fork)
{
uint32_t bits{};
if (!query.get_bits(bits, link))
return false;

fork_work += chain::header::proof(bits);
}

return true;
}

// A fork with greater work will cause confirmed reorganization.
bool chaser_confirm::get_strong(bool& strong, const uint256_t& fork_work,
size_t fork_point) const NOEXCEPT
{
BC_ASSERT(stranded());
uint256_t confirmed_work{};
const auto& query = archive();

for (auto height = query.get_top_confirmed(); height > fork_point;
--height)
{
uint32_t bits{};
if (!query.get_bits(bits, query.to_confirmed(height)))
return false;

// Not strong when confirmed_work equals or exceeds fork_work.
confirmed_work += chain::header::proof(bits);
if (confirmed_work >= fork_work)
{
strong = false;
return true;
}
}

strong = true;
return true;
}

BC_POP_WARNING()

} // namespace node
Expand Down
Loading
Loading