Skip to content

Commit

Permalink
Merge pull request #556 from evoskuil/master
Browse files Browse the repository at this point in the history
Work in progress on chaser eventing.
  • Loading branch information
evoskuil authored Mar 10, 2024
2 parents b0b1f54 + 3287b9c commit 7d65e94
Show file tree
Hide file tree
Showing 28 changed files with 480 additions and 264 deletions.
25 changes: 13 additions & 12 deletions console/executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ using namespace system;
using namespace std::placeholders;

// const executor statics
const std::string executor::quit_{ "q" };
// "c" avoids conflict with network "quit" messages.
const std::string executor::name_{ "bn" };
const std::string executor::close_{ "c" };
const std::unordered_map<uint8_t, bool> executor::defined_
{
{ levels::application, true },
Expand All @@ -68,7 +69,7 @@ const std::unordered_map<uint8_t, std::string> executor::display_
{ levels::wire, "toggle Wire shark" }, // not implemented
{ levels::remote, "toggle Remote fault" },
{ levels::fault, "toggle internal Fault" },
{ levels::quit, "Quit" }
{ levels::quit, "toggle Quitting" }
};
const std::unordered_map<std::string, uint8_t> executor::keys_
{
Expand All @@ -81,7 +82,7 @@ const std::unordered_map<std::string, uint8_t> executor::keys_
{ "w", levels::wire },
{ "r", levels::remote },
{ "f", levels::fault },
{ quit_, levels::quit }
{ "q", levels::quit }
};
const std::unordered_map<database::event_t, std::string> executor::events_
{
Expand Down Expand Up @@ -1601,22 +1602,22 @@ void executor::subscribe_capture()
capture_.subscribe([&](const code& ec, const std::string& line)
{
const auto token = system::trim_copy(line);

// Close (this isn't a toggle).
if (token == close_)
{
logger("CONSOLE: Close");
stop(error::success);
return false;
}

if (!keys_.contains(token))
{
logger("CONSOLE: '" + line + "'");
return !ec;
}

const auto index = keys_.at(token);

// Quit (this level isn't a toggle).
if (index == levels::quit)
{
logger("CONSOLE: " + display_.at(index));
stop(error::success);
return false;
}

if (defined_.at(index))
{
toggle_.at(index) = !toggle_.at(index);
Expand Down
4 changes: 2 additions & 2 deletions console/executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ class executor
void read_test() const;
void write_test();

static const std::string quit_;
static const std::string name_;
static const std::string close_;
static const std::unordered_map<uint8_t, bool> defined_;
static const std::unordered_map<uint8_t, std::string> display_;
static const std::unordered_map<std::string, uint8_t> keys_;
Expand All @@ -103,7 +103,7 @@ class executor
std::istream& input_;
std::ostream& output_;
network::logger log_{};
network::capture capture_{ input_, quit_ };
network::capture capture_{ input_, close_ };
std_array<std::atomic_bool, logs> toggle_
{
true, // application
Expand Down
58 changes: 41 additions & 17 deletions include/bitcoin/node/chasers/chaser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,51 +47,59 @@ class BCN_API chaser
public:
enum class chase
{
/// A new strong branch exists (strong height_t).
/// Issued by 'block' and handled by 'confirm'.
/// The block chaser works with the blocks-first protocol.
/// Bocks first performs header/checked/connected stages.
block,

/// A new strong branch exists (strong height_t).
/// A new candidate branch exists (height_t).
/// Issued by 'header' and handled by 'check'.
/// The block chaser works with the header-first protocol.
header,

/// New unassociated blocks exist in the strong branch.
unassociated,
/// New candidate headers without txs exist (count_t).
/// Issued by 'check' and handled by 'block_in_31800'.
download,

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

/// A downloaded block has failed check (header_t).
/// Issued by 'block_in_31800' and handled by 'header'.
unchecked,

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

/// A checked block has failed connect (header_t).
/// Issued by 'connect' and handled by 'header'.
unconnected,

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

/// A connected block has failed confirm (header_t).
/// Issued by 'confirm' and handled by 'header' (and 'block').
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 (?).
/// Issued by 'candidate' and handled by miners.
/// A new candidate block (template) has been created ().
/// Issued by 'candidate' and handled by [miners].
candidate,

/// Service is stopping (accompanied by error::service_stopped).
/// Legacy: A new strong branch exists (branch height_t).
/// Issued by 'block' and handled by 'confirm'.
block,

/// Service is stopping (accompanied by error::service_stopped), ().
stop
};

using height_t = database::height_link::integer;
using header_t = database::header_link::integer;
using transaction_t = database::tx_link::integer;
using flags_t = database::context::flag::integer;
using count_t = height_t;

typedef std::function<void(const code&, size_t)> organize_handler;
typedef std::variant<uint32_t, uint64_t> link;
Expand All @@ -106,6 +114,12 @@ class BCN_API chaser
virtual code start() NOEXCEPT = 0;

protected:
////using channel_notifier = network::p2p::channel_notifier;
////using channel_completer = network::p2p::channel_completer;
using stop_handler = network::p2p::stop_handler;
using stop_completer = network::p2p::stop_completer;
using key = network::p2p::object_key;

/// Bind a method (use BIND).
template <class Derived, typename Method, typename... Args>
auto bind(Method&& method, Args&&... args) NOEXCEPT
Expand Down Expand Up @@ -148,6 +162,16 @@ class BCN_API chaser
/// Set chaser event (does not require node strand).
void notify(const code& ec, chase event_, link value) NOEXCEPT;

/////// Subscribe to connection creation.
/////// A call after close invokes handlers with error::subscriber_stopped.
////void subscribe_connect(channel_notifier&& handler,
//// channel_completer&& complete) NOEXCEPT;

/// Subscribe to service stop.
/// A call after close invokes handlers with error::subscriber_stopped.
void subscribe_close(stop_handler&& handler,
stop_completer&& complete) NOEXCEPT;

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

Expand Down
6 changes: 3 additions & 3 deletions include/bitcoin/node/chasers/chaser_block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class BCN_API chaser_block
link value) NOEXCEPT;

// Handle events.
virtual void handle_unconnected(height_t height) NOEXCEPT;
virtual void handle_unconnected(header_t height) NOEXCEPT;

/// Sum of work from header to fork point (excluded).
virtual bool get_branch_work(uint256_t& work, size_t& point,
Expand All @@ -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
15 changes: 13 additions & 2 deletions include/bitcoin/node/chasers/chaser_check.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <functional>
#include <memory>
#include <queue>
#include <bitcoin/network.hpp>
#include <bitcoin/node/define.hpp>
#include <bitcoin/node/chasers/chaser.hpp>
Expand All @@ -37,6 +38,7 @@ class BCN_API chaser_check
public:
typedef std::shared_ptr<database::associations> map_ptr;
typedef std::function<void(const code&, const map_ptr&)> handler;
typedef std::list<map_ptr> maps;

DELETE_COPY_MOVE(chaser_check);

Expand All @@ -50,6 +52,8 @@ class BCN_API chaser_check
network::result_handler&& handler) NOEXCEPT;

protected:
virtual bool handle_close(const code& ec) NOEXCEPT;
virtual void handle_subscribed(const code& ec, const key& id) NOEXCEPT;
virtual void handle_put_hashes(const code&) NOEXCEPT;
virtual void handle_header(height_t branch_point) NOEXCEPT;
virtual void handle_event(const code& ec, chase event_,
Expand All @@ -59,11 +63,18 @@ class BCN_API chaser_check
virtual void do_put_hashes(const map_ptr& map,
const network::result_handler& handler) NOEXCEPT;

// This is thread safe.
private:
void initialize_map(maps& table) const NOEXCEPT;
size_t count_map(const maps& table) const NOEXCEPT;
map_ptr make_map(size_t start, size_t count=max_size_t) const NOEXCEPT;
map_ptr get_map(maps& table) NOEXCEPT;

// These are thread safe.
const size_t connections_;
const size_t inventory_;

// This is protected by strand.
database::associations map_{};
maps map_table_{};
};

} // namespace node
Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/node/chasers/chaser_confirm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class BCN_API chaser_confirm
virtual code start() NOEXCEPT;

protected:
virtual void handle_connected(header_t block) NOEXCEPT;
virtual void handle_connected(height_t block) 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_connect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class BCN_API chaser_connect
virtual code start() NOEXCEPT;

protected:
virtual void handle_checked(header_t block) NOEXCEPT;
virtual void handle_checked(height_t block) NOEXCEPT;
virtual void handle_event(const code& ec, chase event_,
link value) NOEXCEPT;
};
Expand Down
6 changes: 3 additions & 3 deletions include/bitcoin/node/chasers/chaser_header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class BCN_API chaser_header
link value) NOEXCEPT;

// Handle events.
virtual void handle_unchecked(height_t height) NOEXCEPT;
virtual void handle_unchecked(header_t height) NOEXCEPT;

/// Sum of work from header to branch point (excluded).
virtual bool get_branch_work(uint256_t& work, size_t& point,
Expand All @@ -90,12 +90,12 @@ 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;

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

/// Validate and organize next header in sequence relative to caller peer.
virtual void do_organize(const system::chain::header::cptr& header,
Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/node/chasers/chaser_transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class BCN_API chaser_transaction
virtual void store(const system::chain::transaction::cptr& block) NOEXCEPT;

protected:
virtual void handle_confirmed(header_t block) NOEXCEPT;
virtual void handle_confirmed(height_t block) NOEXCEPT;
virtual void handle_event(const code& ec, chase event_,
link value) NOEXCEPT;

Expand Down
1 change: 1 addition & 0 deletions include/bitcoin/node/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ enum error_t : uint8_t
// network
slow_channel,
stalled_channel,
exhausted_channel,

// blockchain
orphan_block,
Expand Down
25 changes: 10 additions & 15 deletions include/bitcoin/node/protocols/protocol_block_in_31800.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,32 +61,27 @@ class BCN_API protocol_block_in_31800
void stopping(const code& ec) NOEXCEPT override;

protected:
/// Request blocks from peer.
/// Methods.
virtual void restore(const map_ptr& map) NOEXCEPT;
virtual void start_performance() NOEXCEPT;
virtual void pause_performance() NOEXCEPT;
virtual void stop_performance() NOEXCEPT;
virtual void send_performance(uint64_t rate) NOEXCEPT;
virtual void send_get_data(const map_ptr& map) NOEXCEPT;

/// Recieved incoming block message.
/// Handlers.
virtual void handle_performance_timer(const code& ec) NOEXCEPT;
virtual void handle_send_performance(const code& ec) NOEXCEPT;
virtual bool handle_receive_block(const code& ec,
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;

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

/// Manage download queue.
virtual void handle_put_hashes(const code& ec) NOEXCEPT;
virtual void handle_get_hashes(const code& ec,
const map_ptr& map) NOEXCEPT;

private:
void reset_performance() NOEXCEPT;
void set_performance(uint64_t rate) NOEXCEPT;

void do_handle_performance(const code& ec) NOEXCEPT;
network::messages::get_data create_get_data(
const map_ptr& map) const NOEXCEPT;
Expand Down
12 changes: 12 additions & 0 deletions src/chasers/chaser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ void chaser::do_notify(const code& ec, chase event_, link value) NOEXCEPT
subscriber_.notify(ec, event_, value);
}

////void chaser::subscribe_connect(channel_notifier&& handler,
//// channel_completer&& complete) NOEXCEPT
////{
//// node_.subscribe_connect(std::move(handler), std::move(complete));
////}

void chaser::subscribe_close(stop_handler&& handler,
stop_completer&& complete) NOEXCEPT
{
node_.subscribe_close(std::move(handler), std::move(complete));
}

BC_POP_WARNING()

} // namespace database
Expand Down
Loading

0 comments on commit 7d65e94

Please sign in to comment.