Skip to content

Commit

Permalink
Merge pull request #545 from evoskuil/master
Browse files Browse the repository at this point in the history
Stub in parallel download, comments.
  • Loading branch information
evoskuil authored Feb 26, 2024
2 parents fbec074 + 523f928 commit 0afb56a
Show file tree
Hide file tree
Showing 21 changed files with 276 additions and 264 deletions.
15 changes: 14 additions & 1 deletion include/bitcoin/node/chasers/chaser_check.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,34 @@ class BCN_API chaser_check
: public chaser
{
public:
// context_map casts header_fk into context.minimum_block_version.
using map = database::context_map;
typedef std::function<void(const code&, const map&)> handler;

DELETE_COPY_MOVE(chaser_check);

chaser_check(full_node& node) NOEXCEPT;
virtual ~chaser_check() NOEXCEPT;

virtual code start() NOEXCEPT;
virtual void checked(const system::chain::block::cptr& block) NOEXCEPT;

virtual void get_hashes(handler&& handler) NOEXCEPT;
virtual void put_hashes(const map& map,
network::result_handler&& handler) NOEXCEPT;

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

virtual void do_get_hashes(const handler& handler) NOEXCEPT;
virtual void do_put_hashes(const map& map,
const network::result_handler& handler) NOEXCEPT;

private:
void do_handle_event(const code& ec, chase event_, link value) NOEXCEPT;

map map_{};
};

} // namespace node
Expand Down
5 changes: 5 additions & 0 deletions include/bitcoin/node/full_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ class BCN_API full_node
virtual void organize(const system::chain::block::cptr& block,
network::result_handler&& handler) NOEXCEPT;

/// Manage download queue.
virtual void get_hashes(chaser_check::handler&& handler) NOEXCEPT;
virtual void put_hashes(const chaser_check::map& map,
network::result_handler&& handler) NOEXCEPT;

/// Properties.
/// -----------------------------------------------------------------------

Expand Down
6 changes: 6 additions & 0 deletions include/bitcoin/node/protocols/protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define LIBBITCOIN_NODE_PROTOCOLS_PROTOCOL_HPP

#include <bitcoin/network.hpp>
#include <bitcoin/node/chasers/chasers.hpp>
#include <bitcoin/node/configuration.hpp>
#include <bitcoin/node/define.hpp>
#include <bitcoin/node/full_node.hpp>
Expand Down Expand Up @@ -61,6 +62,11 @@ class BCN_API protocol
virtual void organize(const system::chain::block::cptr& block,
network::result_handler&& handler) NOEXCEPT;

/// Manage download queue.
virtual void get_hashes(chaser_check::handler&& handler) NOEXCEPT;
virtual void put_hashes(const chaser_check::map& map,
network::result_handler&& handler) NOEXCEPT;

/// Configuration settings for all libraries.
const configuration& config() const NOEXCEPT;

Expand Down
48 changes: 14 additions & 34 deletions include/bitcoin/node/protocols/protocol_block_in_31800.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
#ifndef LIBBITCOIN_NODE_PROTOCOLS_PROTOCOL_BLOCK_IN_31800_HPP
#define LIBBITCOIN_NODE_PROTOCOLS_PROTOCOL_BLOCK_IN_31800_HPP

#include <functional>
#include <memory>
#include <bitcoin/network.hpp>
#include <bitcoin/node/chasers/chasers.hpp>
#include <bitcoin/node/define.hpp>
#include <bitcoin/node/protocols/protocol.hpp>

Expand Down Expand Up @@ -56,59 +59,36 @@ class BCN_API protocol_block_in_31800
void stopping(const code& ec) NOEXCEPT override;

protected:
struct track
{
const size_t announced;
const system::hash_digest last;
system::hashes hashes;
};

typedef std::shared_ptr<track> track_ptr;

/// Recieved incoming inventory message.
virtual bool handle_receive_inventory(const code& ec,
const network::messages::inventory::cptr& message) NOEXCEPT;

/// Recieved incoming block message.
virtual bool handle_receive_block(const code& ec,
const network::messages::block::cptr& message,
const track_ptr& tracker) NOEXCEPT;
const network::messages::block::cptr& message) NOEXCEPT;

/// Handle performance timer event.
virtual void handle_performance_timer(const code& ec) NOEXCEPT;

/// Handle result of performance reporting.
virtual void handle_performance(const code& ec) NOEXCEPT;

/// Invoked when initial blocks sync is complete.
virtual void complete() NOEXCEPT;

/// Handle organize result.
virtual void handle_organize(const code& ec, size_t height,
const system::chain::block::cptr& block_ptr) NOEXCEPT;
/// Manage download queue.
virtual void handle_put_hashes(const code& ec) NOEXCEPT;
virtual void handle_get_hashes(const code& ec,
const chaser_check::map& map) NOEXCEPT;

private:
static system::hashes to_hashes(
const network::messages::get_data& getter) NOEXCEPT;

network::messages::get_blocks create_get_inventory() const NOEXCEPT;
network::messages::get_blocks create_get_inventory(
const system::hash_digest& last) const NOEXCEPT;
network::messages::get_blocks create_get_inventory(
system::hashes&& start_hashes) const NOEXCEPT;

network::messages::get_data create_get_data(
const network::messages::inventory& message) const NOEXCEPT;
const chaser_check::map& map) const NOEXCEPT;

void do_handle_performance(const code& ec) NOEXCEPT;
void do_handle_get_hashes(const code& ec,
const chaser_check::map& map) NOEXCEPT;

// Thread safe.
// These are thread safe.
const bool report_performance_;
const network::messages::inventory::type_id block_type_;

// Protected by strand.
// These are protected by strand.
uint64_t bytes_{ zero };
system::chain::checkpoint top_{};
chaser_check::map map_{};
network::steady_clock::time_point start_{};
network::deadline::ptr performance_timer_;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class BCN_API protocol_header_in_31800
const system::chain::header::cptr& header_ptr) NOEXCEPT;

private:
network::messages::get_headers create_get_headers() NOEXCEPT;
network::messages::get_headers create_get_headers() const NOEXCEPT;
network::messages::get_headers create_get_headers(
const system::hash_digest& last) const NOEXCEPT;
network::messages::get_headers create_get_headers(
Expand Down
6 changes: 6 additions & 0 deletions include/bitcoin/node/sessions/session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define LIBBITCOIN_NODE_SESSIONS_SESSION_HPP

#include <bitcoin/network.hpp>
#include <bitcoin/node/chasers/chasers.hpp>
#include <bitcoin/node/define.hpp>
#include <bitcoin/node/full_node.hpp>

Expand All @@ -44,6 +45,11 @@ class BCN_API session
virtual void organize(const system::chain::block::cptr& block,
network::result_handler&& handler) NOEXCEPT;

/// Manage download queue.
virtual void get_hashes(chaser_check::handler&& handler) NOEXCEPT;
virtual void put_hashes(const chaser_check::map& map,
network::result_handler&& handler) NOEXCEPT;

/// Configuration settings for all libraries.
const configuration& config() const NOEXCEPT;

Expand Down
8 changes: 4 additions & 4 deletions src/chasers/chaser_block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ chaser_block::~chaser_block() NOEXCEPT
{
}

// protected
code chaser_block::start() NOEXCEPT
{
BC_ASSERT_MSG(node_stranded(), "chaser_block");

state_ = archive().get_candidate_chain_state(config().bitcoin);
BC_ASSERT_MSG(state_, "Store not initialized.");

return subscribe(std::bind(&chaser_block::handle_event,
this, _1, _2, _3));
return subscribe(
std::bind(&chaser_block::handle_event,
this, _1, _2, _3));
}

// protected
Expand Down Expand Up @@ -117,7 +117,7 @@ void chaser_block::do_organize(const block::cptr& block_ptr,
}

// Peer processing should have precluded orphan submission.
// Results from running headers-first and then restarting to blocks-first.
// Results from running headers-first and then blocks-first.
if (!tree_.contains(previous) && !query.is_block(previous))
{
handler(error::orphan_block);
Expand Down
5 changes: 3 additions & 2 deletions src/chasers/chaser_candidate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ chaser_candidate::~chaser_candidate() NOEXCEPT
code chaser_candidate::start() NOEXCEPT
{
BC_ASSERT_MSG(node_stranded(), "chaser_check");
return subscribe(std::bind(&chaser_candidate::handle_event,
this, _1, _2, _3));
return subscribe(
std::bind(&chaser_candidate::handle_event,
this, _1, _2, _3));
}

void chaser_candidate::handle_event(const code& ec, chase event_,
Expand Down
88 changes: 61 additions & 27 deletions src/chasers/chaser_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,21 @@

namespace libbitcoin {
namespace node {


using namespace network;
using namespace system;
using namespace system::chain;
using namespace std::placeholders;

BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)

// Requires subscriber_ protection (call from node construct or node.strand).
/// We need to be able to perform block.check(ctx) while we still have the
/// deserialized block, because of the witness commitment check (hash).
/// Requires timestamp (header) height, mtp, flags. These can be cached on the
/// block hash registry maintained by this chaser or queried from the stored
/// header. Caching requries rolling forward through all states as the registry
/// is initialized. Store query is simpler and may be as fast.

chaser_check::chaser_check(full_node& node) NOEXCEPT
: chaser(node)
{
Expand All @@ -43,56 +51,82 @@ chaser_check::~chaser_check() NOEXCEPT
{
}

// TODO: initialize check state.
code chaser_check::start() NOEXCEPT
{
BC_ASSERT_MSG(node_stranded(), "chaser_check");

// get_all_unassociated_above(0)
return subscribe(std::bind(&chaser_check::handle_event,
this, _1, _2, _3));
// Initialize from genesis block.
handle_header(zero);

return subscribe(
std::bind(&chaser_check::handle_event,
this, _1, _2, _3));
}

// protected
void chaser_check::handle_event(const code& ec, chase event_,
link value) NOEXCEPT
{
boost::asio::post(strand(),
std::bind(&chaser_check::do_handle_event, this, ec, event_, value));
std::bind(&chaser_check::do_handle_event,
this, ec, event_, value));
}

void chaser_check::do_handle_event(const code& ec, chase event_,
// private
void chaser_check::do_handle_event(const code&, chase event_,
link value) NOEXCEPT
{
BC_ASSERT_MSG(stranded(), "chaser_check");

if (ec)
return;

switch (event_)
if (event_ == chase::header)
{
case chase::header:
{
BC_ASSERT(std::holds_alternative<height_t>(value));
handle_header(std::get<height_t>(value));
break;
}
default:
return;
BC_ASSERT(std::holds_alternative<height_t>(value));
handle_header(std::get<height_t>(value));
}
}

// TODO: handle the new strong branch (may issue 'checked').
void chaser_check::handle_header(height_t) NOEXCEPT
void chaser_check::get_hashes(handler&& handler) NOEXCEPT
{
boost::asio::post(strand(),
std::bind(&chaser_check::do_get_hashes,
this, std::move(handler)));
}

void chaser_check::put_hashes(const chaser_check::map& map,
network::result_handler&& handler) NOEXCEPT
{
boost::asio::post(strand(),
std::bind(&chaser_check::do_put_hashes,
this, map, std::move(handler)));
}

// protected
// ----------------------------------------------------------------------------

void chaser_check::do_get_hashes(const handler&) NOEXCEPT
{
BC_ASSERT_MSG(stranded(), "chaser_check");
}

void chaser_check::do_put_hashes(const chaser_check::map&,
const network::result_handler&) NOEXCEPT
{
BC_ASSERT_MSG(stranded(), "chaser_check");
////LOGN("Handle candidate organization above height (" << branch_point << ").");
// get_all_unassociated_above(branch_point)
}

void chaser_check::checked(const block::cptr&) NOEXCEPT
void chaser_check::handle_header(height_t branch_point) NOEXCEPT
{
// Push checked block into store and issue 'checked' event so that connect
// can connect the next blocks in order. Executes in caller thread.
BC_ASSERT_MSG(stranded(), "chaser_check");

// Map and peer maps may have newly stale blocks.
// All stale branches can just be allowed to complete.
// The connect chaser will verify proper advancement.

// get_all_unassociated_above(branch_point) and add to map.
const auto& query = archive();
const auto top = query.get_top_candidate();
const auto last = query.get_last_associated_from(branch_point);
map_.merge(query.get_all_unassociated_above(last));
}

BC_POP_WARNING()
Expand Down
5 changes: 3 additions & 2 deletions src/chasers/chaser_confirm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ chaser_confirm::~chaser_confirm() NOEXCEPT
code chaser_confirm::start() NOEXCEPT
{
BC_ASSERT_MSG(node_stranded(), "chaser_confirm");
return subscribe(std::bind(&chaser_confirm::handle_event,
this, _1, _2, _3));
return subscribe(
std::bind(&chaser_confirm::handle_event,
this, _1, _2, _3));
}

void chaser_confirm::handle_event(const code& ec, chase event_,
Expand Down
5 changes: 3 additions & 2 deletions src/chasers/chaser_connect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ chaser_connect::~chaser_connect() NOEXCEPT
code chaser_connect::start() NOEXCEPT
{
BC_ASSERT_MSG(node_stranded(), "chaser_connect");
return subscribe(std::bind(&chaser_connect::handle_event,
this, _1, _2, _3));
return subscribe(
std::bind(&chaser_connect::handle_event,
this, _1, _2, _3));
}

void chaser_connect::handle_event(const code& ec, chase event_,
Expand Down
6 changes: 4 additions & 2 deletions src/chasers/chaser_header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ code chaser_header::start() NOEXCEPT
state_ = archive().get_candidate_chain_state(config().bitcoin);
BC_ASSERT_MSG(state_, "Store not initialized.");

return subscribe(std::bind(&chaser_header::handle_event,
this, _1, _2, _3));
return subscribe(
std::bind(&chaser_header::handle_event,
this, _1, _2, _3));
}

// protected
Expand Down Expand Up @@ -255,6 +256,7 @@ void chaser_header::do_organize(const header::cptr& header_ptr,
// Notify candidate reorganization with branch point.
// ------------------------------------------------------------------------

// New branch organized, queue up candidate downloads from branch point.
notify(error::success, chase::header,
{ possible_narrow_cast<height_t>(point) });

Expand Down
Loading

0 comments on commit 0afb56a

Please sign in to comment.