Skip to content

Commit

Permalink
Add chaser_candidate and stub in event handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
evoskuil committed Feb 11, 2024
1 parent 0e6ace6 commit 7f00796
Show file tree
Hide file tree
Showing 21 changed files with 295 additions and 26 deletions.
2 changes: 2 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ src_libbitcoin_node_la_SOURCES = \
src/parser.cpp \
src/settings.cpp \
src/chasers/chaser.cpp \
src/chasers/chaser_candidate.cpp \
src/chasers/chaser_check.cpp \
src/chasers/chaser_confirm.cpp \
src/chasers/chaser_connect.cpp \
Expand Down Expand Up @@ -116,6 +117,7 @@ include_bitcoin_node_HEADERS = \
include_bitcoin_node_chasersdir = ${includedir}/bitcoin/node/chasers
include_bitcoin_node_chasers_HEADERS = \
include/bitcoin/node/chasers/chaser.hpp \
include/bitcoin/node/chasers/chaser_candidate.hpp \
include/bitcoin/node/chasers/chaser_check.hpp \
include/bitcoin/node/chasers/chaser_confirm.hpp \
include/bitcoin/node/chasers/chaser_connect.hpp \
Expand Down
1 change: 1 addition & 0 deletions builds/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ add_library( ${CANONICAL_LIB_NAME}
"../../src/parser.cpp"
"../../src/settings.cpp"
"../../src/chasers/chaser.cpp"
"../../src/chasers/chaser_candidate.cpp"
"../../src/chasers/chaser_check.cpp"
"../../src/chasers/chaser_confirm.cpp"
"../../src/chasers/chaser_connect.cpp"
Expand Down
2 changes: 2 additions & 0 deletions builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
</ImportGroup>
<ItemGroup>
<ClCompile Include="..\..\..\..\src\chasers\chaser.cpp" />
<ClCompile Include="..\..\..\..\src\chasers\chaser_candidate.cpp" />
<ClCompile Include="..\..\..\..\src\chasers\chaser_check.cpp" />
<ClCompile Include="..\..\..\..\src\chasers\chaser_confirm.cpp" />
<ClCompile Include="..\..\..\..\src\chasers\chaser_connect.cpp" />
Expand Down Expand Up @@ -101,6 +102,7 @@
<ItemGroup>
<ClInclude Include="..\..\..\..\include\bitcoin\node.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\chasers\chaser.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\chasers\chaser_candidate.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\chasers\chaser_check.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\chasers\chaser_confirm.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\chasers\chaser_connect.hpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
<ClCompile Include="..\..\..\..\src\chasers\chaser.cpp">
<Filter>src\chasers</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\src\chasers\chaser_candidate.cpp">
<Filter>src\chasers</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\src\chasers\chaser_check.cpp">
<Filter>src\chasers</Filter>
</ClCompile>
Expand Down Expand Up @@ -122,6 +125,9 @@
<ClInclude Include="..\..\..\..\include\bitcoin\node\chasers\chaser.hpp">
<Filter>include\bitcoin\node\chasers</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\node\chasers\chaser_candidate.hpp">
<Filter>include\bitcoin\node\chasers</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\node\chasers\chaser_check.hpp">
<Filter>include\bitcoin\node\chasers</Filter>
</ClInclude>
Expand Down
1 change: 1 addition & 0 deletions include/bitcoin/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <bitcoin/node/settings.hpp>
#include <bitcoin/node/version.hpp>
#include <bitcoin/node/chasers/chaser.hpp>
#include <bitcoin/node/chasers/chaser_candidate.hpp>
#include <bitcoin/node/chasers/chaser_check.hpp>
#include <bitcoin/node/chasers/chaser_confirm.hpp>
#include <bitcoin/node/chasers/chaser_connect.hpp>
Expand Down
67 changes: 57 additions & 10 deletions include/bitcoin/node/chasers/chaser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,81 @@ namespace node {
class full_node;

/// Abstract base chaser.

/// Chasers impose order on blockchain/pool construction as necessary.

/// Each chaser operates on its own strand, implemented here, allowing
/// concurrent chaser operations to the extent that threads are available.

/// Events are passed between chasers using the full_node shared notifier.
/// Notifications are bounced from sink (e.g. chaser) to its strand, and
/// full_node::notify bounces from source (e.g. chaser) to network strand.

/// notify bounces from source (e.g. chaser) to network strand.
class BCN_API chaser
: public network::enable_shared_from_base<chaser>,
public network::reporter
: public network::reporter
{
public:
enum class chase
{
/// Initialize chaser state (no data).
start,

/// A new strong branch exists (strong header link).
/// Issued by 'header' and handled by 'check'.
header,

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

/// A branch has been connected (header link).
/// Issued by 'connect' and handled by 'confirm'.
connected,

/// A branch has been confirmed (top header link).
/// Issued by 'confirm' and handled by 'transaction'.
confirmed,

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

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

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

typedef network::subscriber<chase> event_subscriber;
typedef event_subscriber::handler event_handler;
DELETE_COPY_MOVE(chaser);

/// True if the current thread is on the chaser strand.
virtual bool stranded() const NOEXCEPT;
// TODO: public method to check/store a block.
// TODO: not stranded, thread safe, and posts checked event.

protected:
chaser(full_node& node) NOEXCEPT;
virtual ~chaser() NOEXCEPT;
~chaser() NOEXCEPT;

/// Close the node.
void close(const code& ec) NOEXCEPT;

/// True if the current thread is on the chaser strand.
bool stranded() const NOEXCEPT;

/// Subscribe to chaser events.
code subscribe(event_handler&& handler) NOEXCEPT;

/// Set chaser event (does not require network strand).
void notify(const code& ec, chase value) NOEXCEPT;

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

// These are thread safe (mostly).
full_node& node_;
network::asio::strand strand_;

// This is protected by the network strand.
event_subscriber& subscriber_;
};

} // namespace node
Expand Down
45 changes: 45 additions & 0 deletions include/bitcoin/node/chasers/chaser_candidate.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright (c) 2011-2023 libbitcoin developers (see AUTHORS)
*
* This file is part of libbitcoin.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBBITCOIN_NODE_CHASERS_CHASER_CANDIDATE_HPP
#define LIBBITCOIN_NODE_CHASERS_CHASER_CANDIDATE_HPP

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

namespace libbitcoin {
namespace node {

class full_node;

/// Construct candidate blocks upon modification of the transaction DAG.
/// Notify subscribers with "candidate" event.
class BCN_API chaser_candidate
: public chaser, protected network::tracker<chaser_candidate>
{
public:
typedef std::unique_ptr<chaser_candidate> ptr;

chaser_candidate(full_node& node) NOEXCEPT;
};

} // namespace node
} // namespace libbitcoin

#endif
5 changes: 5 additions & 0 deletions include/bitcoin/node/chasers/chaser_check.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ class BCN_API chaser_check
: public chaser, protected network::tracker<chaser_check>
{
public:
typedef std::unique_ptr<chaser_check> ptr;

chaser_check(full_node& node) NOEXCEPT;

private:
void handle_event(const code& ec, chase value) NOEXCEPT;
};

} // namespace node
Expand Down
2 changes: 2 additions & 0 deletions include/bitcoin/node/chasers/chaser_confirm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class BCN_API chaser_confirm
: public chaser, protected network::tracker<chaser_confirm>
{
public:
typedef std::unique_ptr<chaser_confirm> ptr;

chaser_confirm(full_node& node) NOEXCEPT;
};

Expand Down
4 changes: 3 additions & 1 deletion include/bitcoin/node/chasers/chaser_connect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ class full_node;
/// Chase down blocks in the the candidate header chain for validation.
/// Notify subscribers with the "block connected" event.
class BCN_API chaser_connect
: public chaser, protected network::tracker<chaser_connect>
: public chaser, protected network::tracker<chaser_connect>
{
public:
typedef std::unique_ptr<chaser_connect> ptr;

chaser_connect(full_node& node) NOEXCEPT;
};

Expand Down
4 changes: 3 additions & 1 deletion include/bitcoin/node/chasers/chaser_header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ class full_node;
/// Chase down stronger header branches for the candidate chain.
/// Notify subscribers with "strong header" event.
class BCN_API chaser_header
: public chaser, protected network::tracker<chaser_header>
: public chaser, protected network::tracker<chaser_header>
{
public:
typedef std::unique_ptr<chaser_header> ptr;

chaser_header(full_node& node) NOEXCEPT;
};

Expand Down
4 changes: 3 additions & 1 deletion include/bitcoin/node/chasers/chaser_transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ class full_node;

/// Chase down unconfirmed transactions.
class BCN_API chaser_transaction
: public chaser, protected network::tracker<chaser_transaction>
: public chaser, protected network::tracker<chaser_transaction>
{
public:
typedef std::unique_ptr<chaser_transaction> ptr;

chaser_transaction(full_node& node) NOEXCEPT;
};

Expand Down
1 change: 1 addition & 0 deletions include/bitcoin/node/chasers/chasers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define LIBBITCOIN_NODE_CHASERS_CHASERS_HPP

#include <bitcoin/node/chasers/chaser.hpp>
#include <bitcoin/node/chasers/chaser_candidate.hpp>
#include <bitcoin/node/chasers/chaser_check.hpp>
#include <bitcoin/node/chasers/chaser_confirm.hpp>
#include <bitcoin/node/chasers/chaser_connect.hpp>
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 @@ -38,6 +38,7 @@ enum error_t : uint8_t
{
success,
unknown,
unexpected_event,

// database
store_uninitialized,
Expand Down
24 changes: 16 additions & 8 deletions include/bitcoin/node/full_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <memory>
#include <bitcoin/database.hpp>
#include <bitcoin/network.hpp>
#include <bitcoin/node/chasers/chasers.hpp>
#include <bitcoin/node/configuration.hpp>
#include <bitcoin/node/define.hpp>

Expand All @@ -35,7 +36,6 @@ class BCN_API full_node
typedef std::shared_ptr<full_node> ptr;
typedef database::store<database::map> store;
typedef database::query<store> query;
typedef network::subscriber<> event_subscriber;

/// Constructors.
/// -----------------------------------------------------------------------
Expand All @@ -56,11 +56,6 @@ class BCN_API full_node
/// Close the node.
void close() NOEXCEPT override;

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

// TODO: subscribe/notify.

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

Expand All @@ -70,8 +65,15 @@ class BCN_API full_node
/// Thread safe synchronous archival interface.
query& archive() const NOEXCEPT;

/// Obtain reference to the chaser event subscriber.
chaser::event_subscriber& event_subscriber() NOEXCEPT;

protected:
virtual code create_chasers() NOEXCEPT;
virtual void stop_chasers() NOEXCEPT;

/// Session attachments.
/// -----------------------------------------------------------------------
network::session_manual::ptr attach_manual_session() NOEXCEPT override;
network::session_inbound::ptr attach_inbound_session() NOEXCEPT override;
network::session_outbound::ptr attach_outbound_session() NOEXCEPT override;
Expand All @@ -85,8 +87,14 @@ class BCN_API full_node
const configuration& config_;
query& query_;

// This is protected by strand.
event_subscriber event_subscriber_;
// These are protected by strand.
chaser::event_subscriber event_subscriber_;
chaser_header::ptr chaser_header_{};
chaser_check::ptr chaser_check_{};
chaser_connect::ptr chaser_connect_{};
chaser_confirm::ptr chaser_confirm_{};
chaser_transaction::ptr chaser_transaction_{};
chaser_candidate::ptr chaser_candidate_{};
};

} // namespace node
Expand Down
Loading

0 comments on commit 7f00796

Please sign in to comment.