Skip to content

Commit

Permalink
Update event flow, style.
Browse files Browse the repository at this point in the history
  • Loading branch information
evoskuil committed Mar 7, 2024
1 parent 5ae5d5f commit 44ca9fc
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 36 deletions.
16 changes: 12 additions & 4 deletions include/bitcoin/node/chasers/chaser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,31 +56,38 @@ class BCN_API chaser
/// A new strong branch exists (strong height_t).
/// Issued by 'header' and handled by 'check'.
/// The block chaser works with the header-first protocol.
header,
strong,

/// New unassociated blocks exist in the strong branch.
unassociated,
/// New headers without associated txs exist as strong (header_t).
/// Issued by 'check' once hashes queued and handled by 'checked'.
header,

/// A block has been downloaded, checked and stored (header_t).
/// Issued by 'check' and handled by 'connect'.
checked,

/// A downloaded block has failed check.
unchecked,

/// A branch has been connected (header_t|height_t).
/// Issued by 'connect' and handled by 'confirm'.
connected,

/// A checked block has failed connect.
unconnected,

/// A branch has been confirmed (fork header_t|height_t).
/// Issued by 'confirm' and handled by 'transaction'.
confirmed,

/// A connected block has failed confirm.
unconfirmed,

/// A new transaction has been added to the pool (transaction_t).
/// Issued by 'transaction' and handled by 'candidate'.
transaction,

/// A new candidate block has been created (?).
/// A new candidate block (template) has been created.
/// Issued by 'candidate' and handled by miners.
candidate,

Expand All @@ -92,6 +99,7 @@ class BCN_API chaser
using header_t = database::header_link::integer;
using transaction_t = database::tx_link::integer;
using flags_t = database::context::flag::integer;
using count_t = size_t;

typedef std::function<void(const code&, size_t)> organize_handler;
typedef std::variant<uint32_t, uint64_t> link;
Expand Down
4 changes: 2 additions & 2 deletions include/bitcoin/node/chasers/chaser_block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ class BCN_API chaser_block
const system::chain::chain_state::ptr& state) NOEXCEPT;

/// Store block to database and push to top of candidate chain.
virtual database::header_link push(
virtual database::header_link push_block(
const system::chain::block::cptr& block,
const system::chain::context& context) const NOEXCEPT;

/// Move tree header to database and push to top of candidate chain.
virtual bool push(const system::hash_digest& key) NOEXCEPT;
virtual bool push_block(const system::hash_digest& key) NOEXCEPT;

/// Populate block prevouts and metadata from block tree.
virtual void populate(const system::chain::block& block) const NOEXCEPT;
Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/node/chasers/chaser_check.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class BCN_API chaser_check

protected:
virtual void handle_put_hashes(const code&) NOEXCEPT;
virtual void handle_header(height_t branch_point) NOEXCEPT;
virtual void handle_strong(height_t branch_point) NOEXCEPT;
virtual void handle_event(const code& ec, chase event_,
link value) NOEXCEPT;

Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/node/chasers/chaser_header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class BCN_API chaser_header
const system::chain::chain_state::ptr& state) NOEXCEPT;

/// Store header to database and push to top of candidate chain.
virtual database::header_link push(
virtual database::header_link push_header(
const system::chain::header::cptr& header,
const system::chain::context& context) const NOEXCEPT;

Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/node/protocols/protocol_block_in_31800.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class BCN_API protocol_block_in_31800
/// Handle result of performance reporting.
virtual void handle_performance(const code& ec) NOEXCEPT;

virtual void handle_unassociated(chaser::header_t block) NOEXCEPT;
virtual void handle_header(chaser::count_t count) NOEXCEPT;
virtual void handle_event(const code& ec,
chaser::chase event_, chaser::link value) NOEXCEPT;

Expand Down
22 changes: 12 additions & 10 deletions src/chasers/chaser_block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ code chaser_block::start() NOEXCEPT
void chaser_block::handle_event(const code&, chase event_,
link value) NOEXCEPT
{
if (event_ == chase::unconnected)
if (event_ == chase::unconfirmed)
{
POST(handle_unconnected, std::get<height_t>(value));
}
Expand Down Expand Up @@ -117,10 +117,10 @@ void chaser_block::do_organize(const block::cptr& block_ptr,
}

// If header exists test for prior invalidity as a block.
const auto id = query.to_header(hash);
if (!id.is_terminal())
const auto link = query.to_header(hash);
if (!link.is_terminal())
{
const auto ec = query.get_block_state(id);
const auto ec = query.get_block_state(link);
if (ec == database::error::block_unconfirmable)
{
handler(ec, {});
Expand Down Expand Up @@ -255,9 +255,9 @@ void chaser_block::do_organize(const block::cptr& block_ptr,
}

// Push stored strong block headers to candidate chain.
for (const auto& link: views_reverse(store_branch))
for (const auto& id: views_reverse(store_branch))
{
if (!query.push_candidate(link))
if (!query.push_candidate(id))
{
handler(error::store_integrity, height);
return;
Expand All @@ -267,20 +267,22 @@ void chaser_block::do_organize(const block::cptr& block_ptr,
// Store strong tree blocks and push headers to candidate chain.
for (const auto& key: views_reverse(tree_branch))
{
if (!push(key))
if (!push_block(key))
{
handler(error::store_integrity, height);
return;
}
}

// Push new block as top of candidate chain.
if (push(block_ptr, state->context()).is_terminal())
if (push_block(block_ptr, state->context()).is_terminal())
{
handler(error::store_integrity, height);
return;
}

// ------------------------------------------------------------------------

top_state_ = state;
const auto branch_point = possible_narrow_cast<height_t>(point);
notify(error::success, chase::block, { branch_point });
Expand Down Expand Up @@ -380,7 +382,7 @@ void chaser_block::cache(const block::cptr& block,
tree_.insert({ block->hash(), { block, state } });
}

database::header_link chaser_block::push(const block::cptr& block,
database::header_link chaser_block::push_block(const block::cptr& block,
const context& context) const NOEXCEPT
{
auto& query = archive();
Expand All @@ -394,7 +396,7 @@ database::header_link chaser_block::push(const block::cptr& block,
return query.push_candidate(link) ? link : database::header_link{};
}

bool chaser_block::push(const hash_digest& key) NOEXCEPT
bool chaser_block::push_block(const hash_digest& key) NOEXCEPT
{
const auto value = tree_.extract(key);
BC_ASSERT_MSG(!value.empty(), "missing tree value");
Expand Down
1 change: 1 addition & 0 deletions src/chasers/chaser_candidate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ code chaser_candidate::start() NOEXCEPT
void chaser_candidate::handle_event(const code&, chase event_,
link value) NOEXCEPT
{
// TODO: also handle confirmed/unconfirmed.
if (event_ == chase::transaction)
{
POST(handle_transaction, std::get<transaction_t>(value));
Expand Down
13 changes: 8 additions & 5 deletions src/chasers/chaser_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ code chaser_check::start() NOEXCEPT
void chaser_check::handle_event(const code&, chase event_,
link value) NOEXCEPT
{
if (event_ == chase::header)
if (event_ == chase::strong)
{
BC_ASSERT(std::holds_alternative<chaser::height_t>(value));
handle_header(std::get<height_t>(value));
handle_strong(std::get<height_t>(value));
}
}

// Stale branches are just be allowed to complete (still downloaded).
void chaser_check::handle_header(height_t branch_point) NOEXCEPT
void chaser_check::handle_strong(height_t branch_point) NOEXCEPT
{
const auto map = std::make_shared<database::associations>(
archive().get_all_unassociated_above(branch_point));
Expand Down Expand Up @@ -137,15 +137,18 @@ void chaser_check::do_get_hashes(const handler& handler) NOEXCEPT
handler(error::success, map);
}

// TODO: post event causing channels to get some?
// TODO: otherwise completed channels remain idle until header event.
void chaser_check::do_put_hashes(const map_ptr& map,
const network::result_handler& handler) NOEXCEPT
{
BC_ASSERT(stranded());

/// Merge "moves" elements from one table to another.
map_table_.at(0)->merge(*map);

const auto count = map_table_.at(0)->size();
if (!is_zero(count))
notify(error::success, chase::header, count);

handler(error::success);
}

Expand Down
2 changes: 1 addition & 1 deletion src/chasers/chaser_confirm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void chaser_confirm::handle_event(const code&, chase event_,
}
}

// TODO: handle new strong connected branch (may issue 'confirmed').
// TODO: handle new strong connected branch (may issue 'confirmed'/'unconfirmed').
void chaser_confirm::handle_connected(header_t) NOEXCEPT
{
BC_ASSERT(stranded());
Expand Down
4 changes: 2 additions & 2 deletions src/chasers/chaser_connect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ code chaser_connect::start() NOEXCEPT
void chaser_connect::handle_event(const code&, chase event_,
link value) NOEXCEPT
{
if (event_ == chase::connected)
if (event_ == chase::checked)
{
POST(handle_checked, std::get<header_t>(value));
}
}

// TODO: handle the new checked blocks (may issue 'connected').
// TODO: handle the new checked blocks (may issue 'connected'/'unconnected').
void chaser_connect::handle_checked(header_t) NOEXCEPT
{
BC_ASSERT(stranded());
Expand Down
12 changes: 7 additions & 5 deletions src/chasers/chaser_header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ void chaser_header::handle_event(const code&, chase event_,
link value) NOEXCEPT
{
// Posted due to block/header invalidation.
if (event_ == chase::unchecked)
if (event_ == chase::unchecked ||
event_ == chase::unconnected ||
event_ == chase::unconfirmed)
{
POST(handle_unchecked, std::get<height_t>(value));
}
Expand Down Expand Up @@ -224,7 +226,7 @@ void chaser_header::do_organize(const header::cptr& header_ptr,
return;
}

// Reorganize candidate header chain.
// Reorganize candidate chain.
// ------------------------------------------------------------------------

auto top = top_state_->height();
Expand Down Expand Up @@ -265,7 +267,7 @@ void chaser_header::do_organize(const header::cptr& header_ptr,
}

// Push new header as top of candidate chain.
if (push(header_ptr, state->context()).is_terminal())
if (push_header(header_ptr, state->context()).is_terminal())
{
handler(error::store_integrity, height);
return;
Expand All @@ -275,7 +277,7 @@ void chaser_header::do_organize(const header::cptr& header_ptr,

top_state_ = state;
const auto branch_point = possible_narrow_cast<height_t>(point);
notify(error::success, chase::header, { branch_point });
notify(error::success, chase::strong, { branch_point });
handler(error::success, height);
}

Expand Down Expand Up @@ -383,7 +385,7 @@ void chaser_header::cache(const header::cptr& header,
tree_.insert({ header->hash(), { header, state } });
}

database::header_link chaser_header::push(const header::cptr& header,
database::header_link chaser_header::push_header(const header::cptr& header,
const context& context) const NOEXCEPT
{
auto& query = archive();
Expand Down
9 changes: 5 additions & 4 deletions src/protocols/protocol_block_in_31800.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,15 @@ void protocol_block_in_31800::start() NOEXCEPT
void protocol_block_in_31800::handle_event(const code&,
chaser::chase event_, chaser::link value) NOEXCEPT
{
if (event_ == chaser::chase::unassociated)
// There are count blocks to download at/above the given header.
if (event_ == chaser::chase::header)
{
BC_ASSERT(std::holds_alternative<chaser::header_t>(value));
POST(handle_unassociated, std::get<chaser::header_t>(value));
BC_ASSERT(std::holds_alternative<chaser::count_t>(value));
POST(handle_header, std::get<chaser::count_t>(value));
}
}

void protocol_block_in_31800::handle_unassociated(chaser::header_t) NOEXCEPT
void protocol_block_in_31800::handle_header(chaser::count_t) NOEXCEPT
{
BC_ASSERT(stranded());

Expand Down

0 comments on commit 44ca9fc

Please sign in to comment.