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

Clean up chaser start/stop and error handling. #537

Merged
merged 4 commits into from
Feb 16, 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
11 changes: 10 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,16 @@ test_libbitcoin_node_test_SOURCES = \
test/node.cpp \
test/settings.cpp \
test/test.cpp \
test/test.hpp
test/test.hpp \
test/chasers/chaser.cpp \
test/chasers/chaser_candidate.cpp \
test/chasers/chaser_check.cpp \
test/chasers/chaser_confirm.cpp \
test/chasers/chaser_connect.cpp \
test/chasers/chaser_header.cpp \
test/chasers/chaser_transaction.cpp \
test/protocols/protocol.cpp \
test/sessions/session.cpp

endif WITH_TESTS

Expand Down
11 changes: 10 additions & 1 deletion builds/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,16 @@ if (with-tests)
"../../test/node.cpp"
"../../test/settings.cpp"
"../../test/test.cpp"
"../../test/test.hpp" )
"../../test/test.hpp"
"../../test/chasers/chaser.cpp"
"../../test/chasers/chaser_candidate.cpp"
"../../test/chasers/chaser_check.cpp"
"../../test/chasers/chaser_confirm.cpp"
"../../test/chasers/chaser_connect.cpp"
"../../test/chasers/chaser_header.cpp"
"../../test/chasers/chaser_transaction.cpp"
"../../test/protocols/protocol.cpp"
"../../test/sessions/session.cpp" )

add_test( NAME libbitcoin-node-test COMMAND libbitcoin-node-test
--run_test=*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,19 @@
<Import Project="$(ProjectDir)$(ProjectName).props" />
</ImportGroup>
<ItemGroup>
<ClCompile Include="..\..\..\..\test\chasers\chaser.cpp" />
<ClCompile Include="..\..\..\..\test\chasers\chaser_candidate.cpp" />
<ClCompile Include="..\..\..\..\test\chasers\chaser_check.cpp" />
<ClCompile Include="..\..\..\..\test\chasers\chaser_confirm.cpp" />
<ClCompile Include="..\..\..\..\test\chasers\chaser_connect.cpp" />
<ClCompile Include="..\..\..\..\test\chasers\chaser_header.cpp" />
<ClCompile Include="..\..\..\..\test\chasers\chaser_transaction.cpp" />
<ClCompile Include="..\..\..\..\test\configuration.cpp" />
<ClCompile Include="..\..\..\..\test\error.cpp" />
<ClCompile Include="..\..\..\..\test\main.cpp" />
<ClCompile Include="..\..\..\..\test\node.cpp" />
<ClCompile Include="..\..\..\..\test\protocols\protocol.cpp" />
<ClCompile Include="..\..\..\..\test\sessions\session.cpp" />
<ClCompile Include="..\..\..\..\test\settings.cpp" />
<ClCompile Include="..\..\..\..\test\test.cpp" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,38 @@
<Filter Include="src">
<UniqueIdentifier>{4BD50864-D3BC-4F64-0000-000000000000}</UniqueIdentifier>
</Filter>
<Filter Include="src\chasers">
<UniqueIdentifier>{4BD50864-D3BC-4F64-0000-000000000001}</UniqueIdentifier>
</Filter>
<Filter Include="src\protocols">
<UniqueIdentifier>{4BD50864-D3BC-4F64-0000-000000000002}</UniqueIdentifier>
</Filter>
<Filter Include="src\sessions">
<UniqueIdentifier>{4BD50864-D3BC-4F64-0000-000000000003}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\..\..\test\chasers\chaser.cpp">
<Filter>src\chasers</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\chasers\chaser_candidate.cpp">
<Filter>src\chasers</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\chasers\chaser_check.cpp">
<Filter>src\chasers</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\chasers\chaser_confirm.cpp">
<Filter>src\chasers</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\chasers\chaser_connect.cpp">
<Filter>src\chasers</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\chasers\chaser_header.cpp">
<Filter>src\chasers</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\chasers\chaser_transaction.cpp">
<Filter>src\chasers</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\configuration.cpp">
<Filter>src</Filter>
</ClCompile>
Expand All @@ -24,6 +54,12 @@
<ClCompile Include="..\..\..\..\test\node.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\protocols\protocol.cpp">
<Filter>src\protocols</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\sessions\session.cpp">
<Filter>src\sessions</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\settings.cpp">
<Filter>src</Filter>
</ClCompile>
Expand Down
11 changes: 7 additions & 4 deletions include/bitcoin/node/chasers/chaser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace node {

class full_node;

/// Abstract base chaser.
/// Abstract base chaser for thread safe chain state management classes.
/// 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.
Expand Down Expand Up @@ -84,7 +84,7 @@ class BCN_API chaser
DELETE_COPY_MOVE(chaser);

/// Synchronously subscribe to notify and asynchronously initialize state.
virtual bool start() NOEXCEPT = 0;
virtual code start() NOEXCEPT = 0;

protected:
chaser(full_node& node) NOEXCEPT;
Expand All @@ -99,13 +99,16 @@ class BCN_API chaser
/// True if the current thread is on the chaser strand.
bool stranded() const NOEXCEPT;

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

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

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

/// Close the node.
/// Close the node in case of failure.
void stop(const code& ec) NOEXCEPT;

private:
Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/node/chasers/chaser_candidate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class BCN_API chaser_candidate
public:
chaser_candidate(full_node& node) NOEXCEPT;

virtual bool start() NOEXCEPT;
virtual code start() NOEXCEPT;

protected:
virtual void handle_transaction(transaction_t value) 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 @@ -36,7 +36,7 @@ class BCN_API chaser_check
public:
chaser_check(full_node& node) NOEXCEPT;

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

protected:
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 @@ -36,7 +36,7 @@ class BCN_API chaser_confirm
public:
chaser_confirm(full_node& node) NOEXCEPT;

virtual bool start() NOEXCEPT;
virtual code start() NOEXCEPT;

protected:
virtual void handle_connected(header_t block) 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 @@ -36,7 +36,7 @@ class BCN_API chaser_connect
public:
chaser_connect(full_node& node) NOEXCEPT;

virtual bool start() NOEXCEPT;
virtual code start() NOEXCEPT;

protected:
virtual void handle_checked(header_t block) NOEXCEPT;
Expand Down
22 changes: 11 additions & 11 deletions include/bitcoin/node/chasers/chaser_header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#ifndef LIBBITCOIN_NODE_CHASERS_CHASER_HEADER_HPP
#define LIBBITCOIN_NODE_CHASERS_CHASER_HEADER_HPP

#include <functional>
#include <unordered_map>
#include <bitcoin/database.hpp>
#include <bitcoin/network.hpp>
Expand All @@ -32,15 +31,15 @@ namespace node {
class full_node;

/// Chase down stronger header branches for the candidate chain.
/// Weak branches are retained in a hash table unless already store populated.
/// Weak branches are retained in a hash table if not store populated.
/// Strong branches reorganize the candidate chain and fire the 'header' event.
class BCN_API chaser_header
: public chaser
{
public:
chaser_header(full_node& node) NOEXCEPT;

virtual bool start() NOEXCEPT;
virtual code start() NOEXCEPT;

/// Organize the next header in sequence, relative to caller's peer.
/// Causes a fault/stop if preceding headers have not been stored.
Expand All @@ -49,8 +48,16 @@ class BCN_API chaser_header
system::chain::context&& context) NOEXCEPT;

protected:
struct proposed_header
{
database::context context;
system::chain::header::cptr header;
};
typedef std::vector<database::header_link> header_links;

// This is protected by strand.
std::unordered_map<system::hash_digest, proposed_header> tree_{};

/// Handlers.
virtual void handle_event(const code& ec, chase event_,
link value) NOEXCEPT;
Expand Down Expand Up @@ -82,18 +89,11 @@ class BCN_API chaser_header
virtual bool push(const system::hash_digest& key) NOEXCEPT;

private:
struct proposed_header
{
database::context context;
system::chain::header::cptr header;
};

void do_handle_event(const code& ec, chase event_, link value) NOEXCEPT;
void do_organize(const system::chain::header::cptr& header,
const system::chain::context& context) NOEXCEPT;

// These are protected by strand.
std::unordered_map<system::hash_digest, proposed_header> tree_{};
// These are thread safe.
const network::wall_clock::duration currency_window_;
const bool use_currency_window_;
};
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 @@ -36,7 +36,7 @@ class BCN_API chaser_transaction
public:
chaser_transaction(full_node& node) NOEXCEPT;

virtual bool start() NOEXCEPT;
code start() NOEXCEPT override;
virtual void store(const system::chain::transaction::cptr& block) NOEXCEPT;

protected:
Expand Down
23 changes: 10 additions & 13 deletions src/chasers/chaser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,15 @@ bool chaser::stranded() const NOEXCEPT
return strand_.running_in_this_thread();
}

bool chaser::subscribe(event_handler&& handler) NOEXCEPT
bool chaser::node_stranded() const NOEXCEPT
{
BC_ASSERT_MSG(node_.stranded(), "chaser");
const auto ec = subscriber_.subscribe(std::move(handler));

if (ec)
{
LOGF("Chaser subscribe fault, " << ec.message());
return false;
}
return node_.stranded();
}

return true;
code chaser::subscribe(event_handler&& handler) NOEXCEPT
{
BC_ASSERT_MSG(node_stranded(), "chaser");
return subscriber_.subscribe(std::move(handler));
}

// Posts to network strand (call from chaser strands).
Expand All @@ -82,13 +79,13 @@ void chaser::notify(const code& ec, chase event_, link value) NOEXCEPT
// Executed on network strand (handler should bounce to chaser strand).
void chaser::do_notify(const code& ec, chase event_, link value) NOEXCEPT
{
BC_ASSERT_MSG(node_.stranded(), "chaser");
BC_ASSERT_MSG(node_stranded(), "chaser");
subscriber_.notify(ec, event_, value);
}

void chaser::stop(const code&) NOEXCEPT
void chaser::stop(const code& ec) NOEXCEPT
{
////LOGF("Chaser fault, " << ec.message());
LOGF("Chaser fault, " << ec.message());
node_.close();
}

Expand Down
15 changes: 8 additions & 7 deletions src/chasers/chaser_candidate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ chaser_candidate::chaser_candidate(full_node& node) NOEXCEPT
{
}

// TODO: initialize candidate state.
code chaser_candidate::start() NOEXCEPT
{
BC_ASSERT_MSG(node_stranded(), "chaser_check");
return subscribe(std::bind(&chaser_candidate::handle_event,
this, _1, _2, _3));
}

void chaser_candidate::handle_event(const code& ec, chase event_,
link value) NOEXCEPT
{
Expand Down Expand Up @@ -66,13 +74,6 @@ void chaser_candidate::do_handle_event(const code& ec, chase event_,
}
}

// TODO: initialize candidate state.
bool chaser_candidate::start() NOEXCEPT
{
return subscribe(std::bind(&chaser_candidate::handle_event,
this, _1, _2, _3));
}

// TODO: handle transaction graph change (may issue 'candidate').
void chaser_candidate::handle_transaction(transaction_t tx) NOEXCEPT
{
Expand Down
18 changes: 10 additions & 8 deletions src/chasers/chaser_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ chaser_check::chaser_check(full_node& node) 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));
}

void chaser_check::handle_event(const code& ec, chase event_,
link value) NOEXCEPT
{
Expand Down Expand Up @@ -67,14 +77,6 @@ void chaser_check::do_handle_event(const code& ec, chase event_,
}
}

// TODO: initialize check state.
bool chaser_check::start() NOEXCEPT
{
// get_all_unassociated_above(0)
return subscribe(std::bind(&chaser_check::handle_event,
this, _1, _2, _3));
}

// TODO: handle the new strong branch (may issue 'checked').
void chaser_check::handle_header(height_t branch_point) NOEXCEPT
{
Expand Down
15 changes: 8 additions & 7 deletions src/chasers/chaser_confirm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ chaser_confirm::chaser_confirm(full_node& node) NOEXCEPT
{
}

// TODO: initialize confirm state.
code chaser_confirm::start() NOEXCEPT
{
BC_ASSERT_MSG(node_stranded(), "chaser_confirm");
return subscribe(std::bind(&chaser_confirm::handle_event,
this, _1, _2, _3));
}

void chaser_confirm::handle_event(const code& ec, chase event_,
link value) NOEXCEPT
{
Expand Down Expand Up @@ -65,13 +73,6 @@ void chaser_confirm::do_handle_event(const code& ec, chase event_,
}
}

// TODO: initialize confirm state.
bool chaser_confirm::start() NOEXCEPT
{
return subscribe(std::bind(&chaser_confirm::handle_event,
this, _1, _2, _3));
}

// TODO: handle new strong connected branch (may issue 'confirmed').
void chaser_confirm::handle_connected(header_t block) NOEXCEPT
{
Expand Down
Loading