Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP on validation/confirmation. #695

Merged
merged 8 commits into from
Nov 22, 2024
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: 3 additions & 0 deletions include/bitcoin/node/chase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ enum class chase
/// Check/Identify.
/// -----------------------------------------------------------------------

/////// A set of blocks is being checked, top block provided (height_t).
////checking,

/// A block has been downloaded, checked and stored (height_t).
/// Issued by 'block_in_31800' or 'populate' and handled by 'connect'.
/// Populate is bypassed for checkpoint/milestone blocks.
Expand Down
2 changes: 2 additions & 0 deletions include/bitcoin/node/chasers/chaser_check.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class BCN_API chaser_check
virtual void do_get_hashes(const map_handler& handler) NOEXCEPT;
virtual void do_put_hashes(const map_ptr& map,
const network::result_handler& handler) NOEXCEPT;
virtual void do_confirmable(height_t height) NOEXCEPT;

private:
typedef std::deque<map_ptr> maps;
Expand All @@ -89,6 +90,7 @@ class BCN_API chaser_check
// These are protected by strand.
size_t inventory_{};
size_t requested_{};
size_t confirmed_{};
job::ptr job_{};
maps maps_{};
};
Expand Down
30 changes: 9 additions & 21 deletions include/bitcoin/node/chasers/chaser_confirm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,16 @@ class BCN_API chaser_confirm
virtual bool handle_event(const code& ec, chase event_,
event_value value) NOEXCEPT;

////virtual void do_checking(height_t height) NOEXCEPT;
virtual void do_regressed(height_t branch_point) NOEXCEPT;
virtual void do_validated(height_t height) NOEXCEPT;
virtual void do_reorganize(size_t height) NOEXCEPT;
virtual void do_organize(size_t height) NOEXCEPT;

virtual void enqueue_block(const database::header_link& link) NOEXCEPT;
virtual void confirm_tx(const database::context& ctx,
const database::tx_link& link, const race::ptr& racer) NOEXCEPT;
virtual void handle_tx(const code& ec, const database::tx_link& tx,
const race::ptr& racer) NOEXCEPT;
virtual void handle_txs(const code& ec, const database::tx_link& tx,
const database::header_link& link, size_t height)NOEXCEPT;
virtual void confirm_block(const code& ec,
const database::header_link& link, size_t height) NOEXCEPT;
virtual void next_block(size_t height) NOEXCEPT;
virtual void do_bump(height_t branch_point) NOEXCEPT;

private:
void reset() NOEXCEPT;
bool busy() const NOEXCEPT;
////virtual void do_reorganize(header_links& fork, size_t fork_point) NOEXCEPT;
////virtual void do_organize(header_links& fork, const header_links& popped,
//// size_t fork_point) NOEXCEPT;

private:
bool set_organized(const database::header_link& link,
height_t height) NOEXCEPT;
bool reset_organized(const database::header_link& link,
Expand All @@ -77,8 +68,6 @@ class BCN_API chaser_confirm
height_t height) NOEXCEPT;
bool roll_back(const header_links& popped, size_t fork_point,
size_t top) NOEXCEPT;
bool roll_back(const header_links& popped, size_t fork_point,
size_t top, const database::header_link& link) NOEXCEPT;

bool get_fork_work(uint256_t& fork_work, header_links& fork,
height_t fork_top) const NOEXCEPT;
Expand All @@ -90,9 +79,8 @@ class BCN_API chaser_confirm

// These are protected by strand.
network::threadpool threadpool_;
header_links popped_{};
header_links fork_{};
size_t fork_point_{};
network::asio::strand strand_;
bool mature_{};
};

} // namespace node
Expand Down
2 changes: 2 additions & 0 deletions include/bitcoin/node/chasers/chaser_validate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ class BCN_API chaser_validate

// These are protected by strand.
network::threadpool threadpool_;
network::asio::strand strand_;
system::hash_digest neutrino_{};
size_t validation_backlog_{};
bool mature_{};
};

} // namespace node
Expand Down
69 changes: 41 additions & 28 deletions include/bitcoin/node/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,39 +53,52 @@ enum error_t : uint8_t
suspended_service,

/// blockchain
branch_error,
orphan_block,
orphan_header,
duplicate_block,
duplicate_header,
////validation_bypass,
////confirmation_bypass,

/// chasers
set_block_unconfirmable,
get_height,
get_branch_work,
get_is_strong,
invalid_branch_point,
pop_candidate,
push_candidate,
invalid_fork_point,
get_candidate_chain_state,
get_block,
get_unassociated,
get_fork_work,
to_confirmed,
pop_confirmed,
get_block_confirmable,
get_block_state,
set_strong,
set_unstrong,
set_organized,
set_block_confirmable,
set_block_valid,
node_confirm,
node_validate,
node_roll_back
/// faults (terminal, code error and store corruption assumed)
protocol1,
header1,
organize1,
organize2,
organize3,
organize4,
organize5,
organize6,
organize7,
organize8,
organize9,
organize10,
organize11,
organize12,
organize13,
organize14,
organize15,
validate1,
validate2,
validate3,
validate4,
validate5,
validate6,
confirm1,
confirm2,
confirm3,
confirm4,
confirm5,
confirm6,
confirm7,
confirm8,
confirm9,
confirm10,
confirm11,
confirm12,
confirm13,
confirm14,
confirm15,
confirm16,
confirm17
};

// No current need for error_code equivalence mapping.
Expand Down
32 changes: 16 additions & 16 deletions include/bitcoin/node/impl/chasers/chaser_organize.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ code CLASS::start() NOEXCEPT

if (!state_)
{
fault(error::get_candidate_chain_state);
return error::get_candidate_chain_state;
fault(error::organize1);
return error::organize1;
}

LOGN("Candidate top [" << system::encode_hash(state_->hash()) << ":"
Expand Down Expand Up @@ -205,14 +205,14 @@ void CLASS::do_organize(typename Block::cptr block,

if (!get_branch_work(work, branch_point, tree_branch, store_branch, header))
{
handler(fault(error::get_branch_work), height);
handler(fault(error::organize2), height);
return;
}

// branch_point is the highest tree-candidate common block.
if (!get_is_strong(strong, work, branch_point))
{
handler(fault(error::get_is_strong), height);
handler(fault(error::organize3), height);
return;
}

Expand All @@ -235,7 +235,7 @@ void CLASS::do_organize(typename Block::cptr block,
const auto top_candidate = state_->height();
if (branch_point > top_candidate)
{
handler(fault(error::invalid_branch_point), height);
handler(fault(error::organize4), height);
return;
}

Expand All @@ -247,7 +247,7 @@ void CLASS::do_organize(typename Block::cptr block,
if ((is_under_milestone(index) && !query.set_unstrong(candidate)) ||
!query.pop_candidate())
{
handler(fault(error::pop_candidate), height);
handler(fault(error::organize5), height);
return;
}

Expand All @@ -264,7 +264,7 @@ void CLASS::do_organize(typename Block::cptr block,
if ((is_under_milestone(index) && !query.set_strong(link)) ||
!query.push_candidate(link))
{
handler(fault(error::push_candidate), height);
handler(fault(error::organize6), height);
return;
}

Expand Down Expand Up @@ -347,15 +347,15 @@ void CLASS::do_disorganize(header_t link) NOEXCEPT
size_t height{};
if (!query.get_height(height, link) || is_zero(height))
{
fault(error::get_height);
fault(error::organize7);
return;
}

// Must reorganize down to fork point, since entire branch is now weak.
const auto fork_point = query.get_fork();
if (height <= fork_point)
{
fault(error::invalid_fork_point);
fault(error::organize8);
return;
}

Expand All @@ -365,7 +365,7 @@ void CLASS::do_disorganize(header_t link) NOEXCEPT
auto state = query.get_candidate_chain_state(settings_, fork_point);
if (!state)
{
fault(error::get_candidate_chain_state);
fault(error::organize9);
return;
}

Expand All @@ -378,7 +378,7 @@ void CLASS::do_disorganize(header_t link) NOEXCEPT
{
if (!get_block(block, index))
{
fault(error::get_block);
fault(error::organize10);
return;
}

Expand All @@ -398,7 +398,7 @@ void CLASS::do_disorganize(header_t link) NOEXCEPT
if ((is_under_milestone(index) && !query.set_unstrong(candidate)) ||
!query.pop_candidate())
{
fault(error::pop_candidate);
fault(error::organize11);
return;
}

Expand All @@ -418,7 +418,7 @@ void CLASS::do_disorganize(header_t link) NOEXCEPT
// Confirmed are already set_strong and must stay that way.
if (!query.push_candidate(query.to_confirmed(index)))
{
fault(error::push_candidate);
fault(error::organize12);
return;
}

Expand All @@ -428,7 +428,7 @@ void CLASS::do_disorganize(header_t link) NOEXCEPT
state = query.get_candidate_chain_state(settings_, top_confirmed);
if (!state)
{
fault(error::get_candidate_chain_state);
fault(error::organize13);
return;
}

Expand Down Expand Up @@ -552,7 +552,7 @@ code CLASS::push_block(const Block& block,
return ec;

if (!query.push_candidate(link))
return error::push_candidate;
return error::organize14;

return ec;
}
Expand All @@ -562,7 +562,7 @@ code CLASS::push_block(const system::hash_digest& key) NOEXCEPT
{
const auto handle = tree_.extract(key);
if (!handle)
return error::branch_error;
return error::organize15;

const auto& value = handle.mapped();
return push_block(*value.block, value.state->context());
Expand Down
24 changes: 21 additions & 3 deletions src/chasers/chaser_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ bool chaser_check::handle_event(const code&, chase event_,
POST(do_headers, std::get<height_t>(value));
break;
}
case chase::confirmable:
{
BC_ASSERT(std::holds_alternative<height_t>(value));
POST(do_confirmable, std::get<height_t>(value));
break;
}
case chase::stop:
{
return false;
Expand All @@ -143,6 +149,15 @@ bool chaser_check::handle_event(const code&, chase event_,
return true;
}

void chaser_check::do_confirmable(height_t height) NOEXCEPT
{
BC_ASSERT(stranded());
confirmed_ = height;

if (confirmed_ == requested_)
do_headers(height_t{});
}

// regression
// ----------------------------------------------------------------------------

Expand Down Expand Up @@ -223,7 +238,7 @@ void chaser_check::do_bump(height_t) NOEXCEPT
query.to_candidate(add1(position()))))
set_position(add1(position()));

set_unassociated();
do_headers(height_t{});
}

// add headers
Expand Down Expand Up @@ -324,8 +339,10 @@ size_t chaser_check::set_unassociated() NOEXCEPT
if (closed() || purging())
return {};

// Defer new work issuance until all gaps are filled.
if (position() < requested_ || requested_ >= maximum_height_)
// Defer new work issuance until gaps filled and confirmation caught up.
if (position() < requested_ ||
requested_ >= maximum_height_
/*||confirmed_ < requested_*/)
return {};

// Inventory size gets set only once.
Expand Down Expand Up @@ -364,6 +381,7 @@ size_t chaser_check::set_unassociated() NOEXCEPT
<< count << ") last ("
<< requested_ << ").");

////notify(error::success, chase::checking, requested_);
return count;
}

Expand Down
Loading
Loading