Skip to content

Commit

Permalink
Merge pull request #568 from evoskuil/master
Browse files Browse the repository at this point in the history
Add intermediate templated chaser for consensus critical common code.
  • Loading branch information
evoskuil authored Mar 14, 2024
2 parents 4f63530 + 7dc9490 commit 61f5467
Show file tree
Hide file tree
Showing 14 changed files with 881 additions and 1,150 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ jobs:
$BC_TEST_SINGLETON = $BC_TEST_EXES.FullName;
Write-Host "Executing $BC_TEST_SINGLETON $env:BOOST_UNIT_TEST_OPTIONS" -ForegroundColor Yellow;
try {
Invoke-Expression "$BC_TEST_SINGLETON --log_level=warning --run_test=${{ matrix.tests }} $env:BOOST_UNIT_TEST_OPTIONS"
Invoke-Expression "$BC_TEST_SINGLETON --run_test=${{ matrix.tests }} $env:BOOST_UNIT_TEST_OPTIONS"
}
catch {
$ERR = $_;
Expand Down
5 changes: 5 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,14 @@ include_bitcoin_node_chasers_HEADERS = \
include/bitcoin/node/chasers/chaser_confirm.hpp \
include/bitcoin/node/chasers/chaser_connect.hpp \
include/bitcoin/node/chasers/chaser_header.hpp \
include/bitcoin/node/chasers/chaser_organize.hpp \
include/bitcoin/node/chasers/chaser_transaction.hpp \
include/bitcoin/node/chasers/chasers.hpp

include_bitcoin_node_impl_chasersdir = ${includedir}/bitcoin/node/impl/chasers
include_bitcoin_node_impl_chasers_HEADERS = \
include/bitcoin/node/impl/chasers/chaser_organize.ipp

include_bitcoin_node_protocolsdir = ${includedir}/bitcoin/node/protocols
include_bitcoin_node_protocols_HEADERS = \
include/bitcoin/node/protocols/protocol.hpp \
Expand Down
1 change: 0 additions & 1 deletion builds/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,6 @@ if (with-tests)

add_test( NAME libbitcoin-node-test COMMAND libbitcoin-node-test
--run_test=*
--log_level=warning
--show_progress=no
--detect_memory_leak=0
--report_level=no
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PreprocessorDefinitions Condition="'$(DefaultLinkage)' == 'dynamic'">BOOST_TEST_DYN_LINK;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<PostBuildEvent Condition="'$(DebugOrRelease)' == 'release'">
<Command>"$(TargetPath)" --log_level=warning --run_test=* --show_progress=no --build_info=yes</Command>
<Command>"$(TargetPath)" --run_test=* --show_progress=no --build_info=yes</Command>
</PostBuildEvent>
</ItemDefinitionGroup>

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 @@ -110,6 +110,7 @@
<ClInclude Include="..\..\..\..\include\bitcoin\node\chasers\chaser_confirm.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\chasers\chaser_connect.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_transaction.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\chasers\chasers.hpp" />
<ClInclude Include="..\..\..\..\include\bitcoin\node\configuration.hpp" />
Expand Down Expand Up @@ -139,6 +140,7 @@
<ClInclude Include="..\..\resource.h" />
</ItemGroup>
<ItemGroup>
<None Include="..\..\..\..\include\bitcoin\node\impl\chasers\chaser_organize.ipp" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
Expand Down
18 changes: 15 additions & 3 deletions builds/msvc/vs2022/libbitcoin-node/libbitcoin-node.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,21 @@
<Filter Include="include\bitcoin\node\chasers">
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-000000000007}</UniqueIdentifier>
</Filter>
<Filter Include="include\bitcoin\node\protocols">
<Filter Include="include\bitcoin\node\impl">
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-000000000008}</UniqueIdentifier>
</Filter>
<Filter Include="include\bitcoin\node\sessions">
<Filter Include="include\bitcoin\node\impl\chasers">
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-00000000000B}</UniqueIdentifier>
</Filter>
<Filter Include="include\bitcoin\node\protocols">
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-000000000009}</UniqueIdentifier>
</Filter>
<Filter Include="resource">
<Filter Include="include\bitcoin\node\sessions">
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-00000000000A}</UniqueIdentifier>
</Filter>
<Filter Include="resource">
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-00000000000C}</UniqueIdentifier>
</Filter>
<Filter Include="src">
<UniqueIdentifier>{5FFB5F52-0772-4404-0000-000000000000}</UniqueIdentifier>
</Filter>
Expand Down Expand Up @@ -149,6 +155,9 @@
<ClInclude Include="..\..\..\..\include\bitcoin\node\chasers\chaser_header.hpp">
<Filter>include\bitcoin\node\chasers</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\node\chasers\chaser_organize.hpp">
<Filter>include\bitcoin\node\chasers</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\include\bitcoin\node\chasers\chaser_transaction.hpp">
<Filter>include\bitcoin\node\chasers</Filter>
</ClInclude>
Expand Down Expand Up @@ -232,6 +241,9 @@
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\..\..\include\bitcoin\node\impl\chasers\chaser_organize.ipp">
<Filter>include\bitcoin\node\impl\chasers</Filter>
</None>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions include/bitcoin/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <bitcoin/node/chasers/chaser_confirm.hpp>
#include <bitcoin/node/chasers/chaser_connect.hpp>
#include <bitcoin/node/chasers/chaser_header.hpp>
#include <bitcoin/node/chasers/chaser_organize.hpp>
#include <bitcoin/node/chasers/chaser_transaction.hpp>
#include <bitcoin/node/chasers/chasers.hpp>
#include <bitcoin/node/protocols/protocol.hpp>
Expand Down
65 changes: 19 additions & 46 deletions include/bitcoin/node/chasers/chaser_block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,72 +19,45 @@
#ifndef LIBBITCOIN_NODE_CHASERS_CHASER_BLOCK_HPP
#define LIBBITCOIN_NODE_CHASERS_CHASER_BLOCK_HPP

#include <unordered_map>
#include <bitcoin/database.hpp>
#include <bitcoin/network.hpp>
#include <bitcoin/system.hpp>
#include <bitcoin/node/chasers/chaser_organize.hpp>
#include <bitcoin/node/define.hpp>
#include <bitcoin/node/chasers/chaser.hpp>

namespace libbitcoin {
namespace node {

class full_node;

/// Chase down stronger block branches for the confirmed chain.
/// Weak branches are retained in a hash table if not store populated.
/// Strong branches reorganize the candidate chain and fire the 'connect' event.
class BCN_API chaser_block
: public chaser
: public chaser_organize<system::chain::block>
{
public:
DELETE_COPY_MOVE_DESTRUCT(chaser_block);

chaser_block(full_node& node) NOEXCEPT;

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

/// Validate and organize next block in sequence relative to caller peer.
virtual void organize(const system::chain::block::cptr& block_ptr,
organize_handler&& handler) NOEXCEPT;

protected:
virtual void handle_event(const code& ec, chase event_, link value) NOEXCEPT;
virtual void do_disorganize(header_t height) NOEXCEPT;
virtual void do_organize(const system::chain::block::cptr& block,
const organize_handler& handler) NOEXCEPT;
/// Get header from Block instance.
virtual const system::chain::header& get_header(
const system::chain::block& block) const NOEXCEPT;

private:
struct block_state
{
system::chain::block::cptr block;
system::chain::chain_state::ptr state;
};
using block_tree = std::unordered_map<system::hash_digest, block_state>;
using header_links = std::vector<database::header_link>;
/// Query store for const pointer to Block instance.
virtual bool get_block(system::chain::block::cptr& out,
size_t index) const NOEXCEPT;

system::chain::chain_state::ptr get_chain_state(
const system::hash_digest& hash) const NOEXCEPT;
bool get_branch_work(uint256_t& work, size_t& branch_point,
system::hashes& tree_branch, header_links& store_branch,
const system::chain::header& header) const NOEXCEPT;
bool get_is_strong(bool& strong, const uint256_t& work,
size_t branch_point) const NOEXCEPT;
void cache(const system::chain::block::cptr& block,
const system::chain::chain_state::ptr& state) NOEXCEPT;
database::header_link push_block(
const system::chain::block::cptr& block,
const system::chain::context& context) const NOEXCEPT;
bool push_block(const system::hash_digest& key) NOEXCEPT;
void populate(const system::chain::block& block) const NOEXCEPT;
void set_prevout(const system::chain::input& input) const NOEXCEPT;
/// Determine if Block is valid.
virtual code validate(const system::chain::block& block,
const system::chain::chain_state& state) const NOEXCEPT;

// This is thread safe.
const system::settings& settings_;
/// Determine if Block is top of a storable branch.
virtual bool is_storable(const system::chain::block& block,
size_t height, const system::hash_digest& hash,
const system::chain::chain_state& state) const NOEXCEPT;

// This is protected by strand.
system::chain::chain_state::ptr state_{};
block_tree tree_{};
private:
void set_prevout(const system::chain::input& input) const NOEXCEPT;
void populate(const system::chain::block& block) const NOEXCEPT;
};

} // namespace node
Expand Down
60 changes: 16 additions & 44 deletions include/bitcoin/node/chasers/chaser_header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@
#ifndef LIBBITCOIN_NODE_CHASERS_CHASER_HEADER_HPP
#define LIBBITCOIN_NODE_CHASERS_CHASER_HEADER_HPP

#include <unordered_map>
#include <bitcoin/database.hpp>
#include <bitcoin/network.hpp>
#include <bitcoin/node/chasers/chaser.hpp>
#include <bitcoin/node/configuration.hpp>
#include <bitcoin/system.hpp>
#include <bitcoin/node/chasers/chaser_organize.hpp>
#include <bitcoin/node/define.hpp>

namespace libbitcoin {
Expand All @@ -35,55 +32,30 @@ class full_node;
/// 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_organize<system::chain::header>
{
public:
DELETE_COPY_MOVE_DESTRUCT(chaser_header);

chaser_header(full_node& node) NOEXCEPT;

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

/// Validate and organize next header in sequence relative to caller peer.
virtual void organize(const system::chain::header::cptr& header_ptr,
organize_handler&& handler) NOEXCEPT;

protected:
virtual void handle_event(const code& ec, chase event_, link value) NOEXCEPT;
virtual void do_disorganize(header_t height) NOEXCEPT;
virtual void do_organize(const system::chain::header::cptr& header,
const organize_handler& handler) NOEXCEPT;

private:
struct header_state
{
system::chain::header::cptr header;
system::chain::chain_state::ptr state;
};
using header_tree = std::unordered_map<system::hash_digest, header_state>;
using header_links = std::vector<database::header_link>;

system::chain::chain_state::ptr get_chain_state(
const system::hash_digest& hash) const NOEXCEPT;
bool get_branch_work(uint256_t& work, size_t& branch_point,
system::hashes& tree_branch, header_links& store_branch,
/// Get header from Block instance.
virtual const system::chain::header& get_header(
const system::chain::header& header) const NOEXCEPT;
bool get_is_strong(bool& strong, const uint256_t& work,
size_t branch_point) const NOEXCEPT;
void cache(const system::chain::header::cptr& header,
const system::chain::chain_state::ptr& state) NOEXCEPT;
database::header_link push_header(
const system::chain::header::cptr& header,
const system::chain::context& context) const NOEXCEPT;
bool push_header(const system::hash_digest& key) NOEXCEPT;

// This is thread safe.
const system::settings& settings_;
/// Query store for const pointer to Block instance.
virtual bool get_block(system::chain::header::cptr& out,
size_t index) const NOEXCEPT;

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

// These are protected by strand.
system::chain::chain_state::ptr state_{};
header_tree tree_{};
/// Determine if Block is top of a storable branch.
virtual bool is_storable(const system::chain::header& header,
size_t height, const system::hash_digest& hash,
const system::chain::chain_state& state) const NOEXCEPT;
};

} // namespace node
Expand Down
Loading

0 comments on commit 61f5467

Please sign in to comment.