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

Avoid std parallel set_strong/unstrong, use variant events. #656

Merged
merged 16 commits into from
Jun 27, 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
3 changes: 3 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ src_libbitcoin_node_la_SOURCES = \
src/chasers/chaser_check.cpp \
src/chasers/chaser_confirm.cpp \
src/chasers/chaser_header.cpp \
src/chasers/chaser_populate.cpp \
src/chasers/chaser_snapshot.cpp \
src/chasers/chaser_storage.cpp \
src/chasers/chaser_template.cpp \
Expand Down Expand Up @@ -89,6 +90,7 @@ test_libbitcoin_node_test_SOURCES = \
test/chasers/chaser_check.cpp \
test/chasers/chaser_confirm.cpp \
test/chasers/chaser_header.cpp \
test/chasers/chaser_populate.cpp \
test/chasers/chaser_template.cpp \
test/chasers/chaser_transaction.cpp \
test/chasers/chaser_validate.cpp \
Expand Down Expand Up @@ -140,6 +142,7 @@ include_bitcoin_node_chasers_HEADERS = \
include/bitcoin/node/chasers/chaser_confirm.hpp \
include/bitcoin/node/chasers/chaser_header.hpp \
include/bitcoin/node/chasers/chaser_organize.hpp \
include/bitcoin/node/chasers/chaser_populate.hpp \
include/bitcoin/node/chasers/chaser_snapshot.hpp \
include/bitcoin/node/chasers/chaser_storage.hpp \
include/bitcoin/node/chasers/chaser_template.hpp \
Expand Down
2 changes: 2 additions & 0 deletions builds/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ add_library( ${CANONICAL_LIB_NAME}
"../../src/chasers/chaser_check.cpp"
"../../src/chasers/chaser_confirm.cpp"
"../../src/chasers/chaser_header.cpp"
"../../src/chasers/chaser_populate.cpp"
"../../src/chasers/chaser_snapshot.cpp"
"../../src/chasers/chaser_storage.cpp"
"../../src/chasers/chaser_template.cpp"
Expand Down Expand Up @@ -329,6 +330,7 @@ if (with-tests)
"../../test/chasers/chaser_check.cpp"
"../../test/chasers/chaser_confirm.cpp"
"../../test/chasers/chaser_header.cpp"
"../../test/chasers/chaser_populate.cpp"
"../../test/chasers/chaser_template.cpp"
"../../test/chasers/chaser_transaction.cpp"
"../../test/chasers/chaser_validate.cpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
<ClCompile Include="..\..\..\..\test\chasers\chaser_check.cpp" />
<ClCompile Include="..\..\..\..\test\chasers\chaser_confirm.cpp" />
<ClCompile Include="..\..\..\..\test\chasers\chaser_header.cpp" />
<ClCompile Include="..\..\..\..\test\chasers\chaser_populate.cpp" />
<ClCompile Include="..\..\..\..\test\chasers\chaser_template.cpp" />
<ClCompile Include="..\..\..\..\test\chasers\chaser_transaction.cpp" />
<ClCompile Include="..\..\..\..\test\chasers\chaser_validate.cpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
<ClCompile Include="..\..\..\..\test\chasers\chaser_header.cpp">
<Filter>src\chasers</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\chasers\chaser_populate.cpp">
<Filter>src\chasers</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\test\chasers\chaser_template.cpp">
<Filter>src\chasers</Filter>
</ClCompile>
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 @@ -78,6 +78,7 @@
<ClCompile Include="..\..\..\..\src\chasers\chaser_check.cpp" />
<ClCompile Include="..\..\..\..\src\chasers\chaser_confirm.cpp" />
<ClCompile Include="..\..\..\..\src\chasers\chaser_header.cpp" />
<ClCompile Include="..\..\..\..\src\chasers\chaser_populate.cpp" />
<ClCompile Include="..\..\..\..\src\chasers\chaser_snapshot.cpp" />
<ClCompile Include="..\..\..\..\src\chasers\chaser_storage.cpp" />
<ClCompile Include="..\..\..\..\src\chasers\chaser_template.cpp" />
Expand Down Expand Up @@ -114,6 +115,7 @@
<ClInclude Include="..\..\..\..\include\bitcoin\node\chasers\chaser_confirm.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\chasers\chaser_header.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\chasers\chaser_organize.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\chasers\chaser_populate.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\chasers\chaser_snapshot.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\chasers\chaser_storage.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\chasers\chaser_template.hpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
<ClCompile Include="..\..\..\..\src\chasers\chaser_header.cpp">
<Filter>src\chasers</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\src\chasers\chaser_populate.cpp">
<Filter>src\chasers</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\src\chasers\chaser_snapshot.cpp">
<Filter>src\chasers</Filter>
</ClCompile>
Expand Down Expand Up @@ -167,6 +170,9 @@
<ClInclude Include="..\..\..\..\include\bitcoin\node\chasers\chaser_organize.hpp">
<Filter>include\bitcoin\node\chasers</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\node\chasers\chaser_populate.hpp">
<Filter>include\bitcoin\node\chasers</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\node\chasers\chaser_snapshot.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 @@ -31,6 +31,7 @@
#include <bitcoin/node/chasers/chaser_confirm.hpp>
#include <bitcoin/node/chasers/chaser_header.hpp>
#include <bitcoin/node/chasers/chaser_organize.hpp>
#include <bitcoin/node/chasers/chaser_populate.hpp>
#include <bitcoin/node/chasers/chaser_snapshot.hpp>
#include <bitcoin/node/chasers/chaser_storage.hpp>
#include <bitcoin/node/chasers/chaser_template.hpp>
Expand Down
26 changes: 13 additions & 13 deletions include/bitcoin/node/chasers/chaser_block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,33 @@ class BCN_API chaser_block

protected:
/// Get header from Block instance.
virtual const system::chain::header& get_header(
const system::chain::block& block) const NOEXCEPT;
const system::chain::header& get_header(
const system::chain::block& block) const NOEXCEPT override;

/// Query store for const pointer to Block instance by candidate height.
virtual bool get_block(system::chain::block::cptr& out,
size_t height) const NOEXCEPT;
bool get_block(system::chain::block::cptr& out,
size_t height) const NOEXCEPT override;

/// Determine if Block is a duplicate (success for not duplicate).
virtual code duplicate(size_t& height,
const system::hash_digest& hash) const NOEXCEPT;
code duplicate(size_t& height,
const system::hash_digest& hash) const NOEXCEPT override;

/// Determine if Block is valid.
virtual code validate(const system::chain::block& block,
const chain_state& state) const NOEXCEPT;
code validate(const system::chain::block& block,
const chain_state& state) const NOEXCEPT override;

/// Notify check chaser to redownload the block (nop).
virtual void do_malleated(header_t link) NOEXCEPT;
void do_malleated(header_t link) NOEXCEPT override;

/// Determine if state is top of a storable branch (always true).
virtual bool is_storable(const chain_state& state) const NOEXCEPT;
bool is_storable(const chain_state& state) const NOEXCEPT override;

/// True if Block is on a milestone-covered branch.
virtual bool is_under_milestone(size_t height) const NOEXCEPT;
bool is_under_milestone(size_t height) const NOEXCEPT override;

/// Milestone tracking.
virtual void update_milestone(const system::chain::header& header,
size_t height, size_t branch_point) NOEXCEPT;
void update_milestone(const system::chain::header& header,
size_t height, size_t branch_point) NOEXCEPT override;

private:
void set_prevout(const system::chain::input& input) const NOEXCEPT;
Expand Down
14 changes: 8 additions & 6 deletions include/bitcoin/node/chasers/chaser_confirm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ class BCN_API chaser_confirm
event_value value) NOEXCEPT;

virtual void do_validated(height_t height) NOEXCEPT;
#if defined(SEQUENTIAL)
virtual void do_organize(size_t height) NOEXCEPT;
#else
virtual void do_reorganize(size_t height) NOEXCEPT;
virtual void do_organize(size_t height) NOEXCEPT;
virtual bool enqueue_block(const database::header_link& link) NOEXCEPT;
virtual void confirm_tx(const database::context& ctx,
Expand All @@ -63,17 +61,21 @@ class BCN_API chaser_confirm
virtual void confirm_block(const code& ec,
const database::header_link& link, size_t height) NOEXCEPT;
virtual void next_block(size_t height) NOEXCEPT;
#endif // SEQUENTIAL

private:
void reset() NOEXCEPT;
bool busy() const NOEXCEPT;

bool set_organized(const database::header_link& link,
height_t height) NOEXCEPT;
bool reset_organized(const database::header_link& link,
height_t height) NOEXCEPT;
bool set_reorganized(const database::header_link& link,
height_t height) NOEXCEPT;
bool roll_back(header_links& popped, const database::header_link& link,
size_t fork_point, size_t top) NOEXCEPT;
bool roll_back(const header_links& popped, size_t fork_point,
size_t top) NOEXCEPT;
bool roll_back(const header_links& popped, size_t fork_point,
size_t top, const database::header_link& link) NOEXCEPT;

bool get_fork_work(uint256_t& fork_work, header_links& fork,
height_t fork_top) const NOEXCEPT;
Expand Down
26 changes: 13 additions & 13 deletions include/bitcoin/node/chasers/chaser_header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,33 +44,33 @@ class BCN_API chaser_header

protected:
/// Get header from Block instance.
virtual const system::chain::header& get_header(
const system::chain::header& header) const NOEXCEPT;
const system::chain::header& get_header(
const system::chain::header& header) const NOEXCEPT override;

/// Query store for const pointer to Block instance by candidate height.
virtual bool get_block(system::chain::header::cptr& out,
size_t height) const NOEXCEPT;
bool get_block(system::chain::header::cptr& out,
size_t height) const NOEXCEPT override;

/// Determine if Block is a duplicate (success for not duplicate).
virtual code duplicate(size_t& height,
const system::hash_digest& hash) const NOEXCEPT;
code duplicate(size_t& height,
const system::hash_digest& hash) const NOEXCEPT override;

/// Determine if Block is valid.
virtual code validate(const system::chain::header& header,
const chain_state& state) const NOEXCEPT;
code validate(const system::chain::header& header,
const chain_state& state) const NOEXCEPT override;

/// Notify check chaser to redownload the block.
virtual void do_malleated(header_t link) NOEXCEPT;
void do_malleated(header_t link) NOEXCEPT override;

/// Determine if state is top of a storable branch.
virtual bool is_storable(const chain_state& state) const NOEXCEPT;
bool is_storable(const chain_state& state) const NOEXCEPT override;

/// True if Block is on a milestone-covered branch.
virtual bool is_under_milestone(size_t height) const NOEXCEPT;
bool is_under_milestone(size_t height) const NOEXCEPT override;

/// Milestone tracking.
virtual void update_milestone(const system::chain::header& header,
size_t height, size_t branch_point) NOEXCEPT;
void update_milestone(const system::chain::header& header,
size_t height, size_t branch_point) NOEXCEPT override;

private:
bool is_checkpoint(const chain_state& state) const NOEXCEPT;
Expand Down
55 changes: 55 additions & 0 deletions include/bitcoin/node/chasers/chaser_populate.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* 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_POPULATE_HPP
#define LIBBITCOIN_NODE_CHASERS_CHASER_POPULATE_HPP

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

namespace libbitcoin {
namespace node {

class full_node;

/// Order and populate downloaded non-bypass blocks for validation.
class BCN_API chaser_populate
: public chaser
{
public:
DELETE_COPY_MOVE_DESTRUCT(chaser_populate);

chaser_populate(full_node& node) NOEXCEPT;

/// Initialize chaser state.
code start() NOEXCEPT override;

protected:
virtual bool handle_event(const code& ec, chase event_,
event_value value) NOEXCEPT;

virtual void do_checked(height_t height) NOEXCEPT;

private:
// TODO:
};

} // namespace node
} // namespace libbitcoin

#endif
3 changes: 2 additions & 1 deletion include/bitcoin/node/chasers/chasers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@
#include <bitcoin/node/chasers/chaser_confirm.hpp>
#include <bitcoin/node/chasers/chaser_header.hpp>
#include <bitcoin/node/chasers/chaser_organize.hpp>
#include <bitcoin/node/chasers/chaser_validate.hpp>
#include <bitcoin/node/chasers/chaser_populate.hpp>
#include <bitcoin/node/chasers/chaser_snapshot.hpp>
#include <bitcoin/node/chasers/chaser_storage.hpp>
#include <bitcoin/node/chasers/chaser_template.hpp>
#include <bitcoin/node/chasers/chaser_transaction.hpp>
#include <bitcoin/node/chasers/chaser_validate.hpp>

#endif
51 changes: 39 additions & 12 deletions include/bitcoin/node/define.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <functional>
#include <memory>
#include <utility>
#include <variant>

/// Pulls in common /node headers (excluding settings/config/parser/full_node).
#include <bitcoin/node/chase.hpp>
Expand Down Expand Up @@ -63,25 +64,51 @@ typedef std::shared_ptr<database::associations> map_ptr;
typedef std::function<void(const code&, const map_ptr&,
const job::ptr&)> map_handler;

/// Node events.
typedef uint64_t object_key;
typedef uint64_t event_value;
typedef network::desubscriber<object_key, chase, event_value> event_subscriber;
typedef event_subscriber::handler event_notifier;
typedef event_subscriber::completer event_completer;
/// Event desubscriber key type.
using object_key = uint64_t;

/// Use for event_value variants (all unsigned integral integers).
/// std::variant is inconsistent with interpretation of size_t as redundant or
/// unique with respect to uint32_t and/or uint64_t (specifically macOS). So
/// instead these are implicitly casted to event_value (uint64_t) and explicitly
/// casted from event_value using system::possible_narrow_cast. This is no less
/// type-safe as using std::variant in cases where the types are overloaded.
/// Event value types.
using count_t = size_t;
using height_t = size_t;
using channel_t = uint64_t;
using object_t = object_key;
using header_t = database::header_link::integer;
using transaction_t = database::tx_link::integer;
typedef system::chain::block::cptr block_t;
////typedef struct
////{
//// size_t height;
//// database::header_link link;
//// system::chain::block::cptr block;
////} xblock_t;

/// std::variant types must be distinct, and xcode size_t is neither uint32_t
/// nor uint64_t, so this ensures we have the distinct set of necessary types.
using event_value =
iif<is_same_type<std::size_t, uint64_t>,
std::variant<uint32_t, size_t, block_t>,
iif<is_same_type<std::size_t, uint32_t>,
std::variant<uint64_t, size_t, block_t>,
std::variant<uint64_t, uint32_t, size_t, block_t>>>;
////using xevent_value =
//// iif<is_same_type<std::size_t, uint64_t>,
//// std::variant<uint32_t, size_t, xblock_t>,
//// iif<is_same_type<std::size_t, uint32_t>,
//// std::variant<uint64_t, size_t, xblock_t>,
//// std::variant<uint64_t, uint32_t, size_t, xblock_t>>>;

/// Event desubscriber.
typedef network::desubscriber<object_key, chase, event_value> event_subscriber;
////typedef network::desubscriber<object_key, chase, xevent_value> xevent_subscriber;
typedef event_subscriber::handler event_notifier;
typedef event_subscriber::completer event_completer;

// NDEBUG MSVC
////static_assert(sizeof(uint64_t) == 8u);
////static_assert(sizeof(block_t) == 16u);
////static_assert(sizeof(xblock_t) == 32u);
////static_assert(sizeof(event_value) == 24u);
////static_assert(sizeof(xevent_value) == 40u);

} // namespace node
} // namespace libbitcoin
Expand Down
6 changes: 4 additions & 2 deletions include/bitcoin/node/impl/chasers/chaser_organize.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,15 @@ bool CLASS::handle_event(const code&, chase event_, event_value value) NOEXCEPT
case chase::unconfirmable:
{
// Roll back the candidate chain to confirmed top (via fork point).
POST(do_disorganize, possible_narrow_cast<header_t>(value));
BC_ASSERT(std::holds_alternative<header_t>(value));
POST(do_disorganize, std::get<header_t>(value));
break;
}
case chase::malleated:
{
// Re-obtain the malleated block if it is still a candidate (virtual).
POST(do_malleated, possible_narrow_cast<header_t>(value));
BC_ASSERT(std::holds_alternative<header_t>(value));
POST(do_malleated, std::get<header_t>(value));
break;
}
case chase::stop:
Expand Down
Loading
Loading