Skip to content

Commit

Permalink
Capture block pointers, WIP, disabled.
Browse files Browse the repository at this point in the history
  • Loading branch information
evoskuil committed Jul 6, 2024
1 parent b045a0a commit 93940fd
Show file tree
Hide file tree
Showing 17 changed files with 244 additions and 72 deletions.
12 changes: 8 additions & 4 deletions include/bitcoin/node/chase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,21 @@ enum class chase
/// Issued by 'organize' and handled by 'validate' (disorgs candidates).
disorganized,

/// Validation.
/// Check/Identify.
/// -----------------------------------------------------------------------

/// A block has been downloaded, checked and stored (height_t).
/// Issued by 'block_in_31800' and handled by 'connect'.
/// Issued by 'block_in_31800' or 'populate' and handled by 'connect'.
/// Populate is bypassed for checkpoint/milestone blocks.
checked,

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

/// Accept/Connect.
/// -----------------------------------------------------------------------

/// A branch has become valid (height_t).
/// Issued by 'validate' and handled by 'confirm'.
valid,
Expand All @@ -107,7 +111,7 @@ enum class chase
/// Issued by 'validate' and handled by 'header'.
unvalid,

/// Confirmation.
/// Confirm (block).
/// -----------------------------------------------------------------------

/// A connected block has become confirmable (header_t).
Expand All @@ -118,7 +122,7 @@ enum class chase
/// Issued by 'confirm' and handled by 'header' (and 'block').
unconfirmable,

/// Confirmed Chain.
/// Confirm (chain).
/// -----------------------------------------------------------------------

/// A confirmable block has been confirmed (header_t).
Expand Down
10 changes: 5 additions & 5 deletions include/bitcoin/node/chasers/chaser_organize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class chaser_organize
/// Initialize chaser state.
virtual code start() NOEXCEPT;

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

protected:
Expand All @@ -54,7 +54,7 @@ class chaser_organize
chain_state::ptr state;
};
using block_tree = std::unordered_map<system::hash_digest, block_state>;
using header_links = std::vector<database::header_link>;
using header_links = std_vector<database::header_link>;

/// Protected constructor for abstract base.
chaser_organize(full_node& node) NOEXCEPT;
Expand Down Expand Up @@ -96,7 +96,7 @@ class chaser_organize
event_value value) NOEXCEPT;

/// Organize a discovered Block.
virtual void do_organize(typename Block::cptr block_ptr,
virtual void do_organize(typename Block::cptr block,
const organize_handler& handler) NOEXCEPT;

/// Reorganize following Block unconfirmability.
Expand Down Expand Up @@ -136,7 +136,7 @@ class chaser_organize
// ------------------------------------------------------------------------

// Store Block into logical tree cache.
void cache(const typename Block::cptr& block_ptr,
void cache(const typename Block::cptr& block,
const chain_state::ptr& state) NOEXCEPT;

// Obtain chain state for given previous hash, nullptr if not found.
Expand Down
16 changes: 14 additions & 2 deletions include/bitcoin/node/chasers/chaser_populate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#ifndef LIBBITCOIN_NODE_CHASERS_CHASER_POPULATE_HPP
#define LIBBITCOIN_NODE_CHASERS_CHASER_POPULATE_HPP

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

Expand All @@ -39,14 +42,23 @@ class BCN_API chaser_populate
/// Initialize chaser state.
code start() NOEXCEPT override;

/// Populate a candidate block for validation.
virtual void populate(const system::chain::block::cptr& block,
const database::header_link& link, size_t height,
network::result_handler&& complete) NOEXCEPT;

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

virtual void do_checked(height_t height) NOEXCEPT;
virtual void do_populate(const system::chain::block::cptr& block,
database::header_link::integer link, size_t height,
const network::result_handler& complete) NOEXCEPT;

private:
// TODO:
// These are protected by strand.
network::threadpool threadpool_;
network::asio::strand independent_strand_;
};

} // namespace node
Expand Down
8 changes: 8 additions & 0 deletions include/bitcoin/node/chasers/chaser_validate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define LIBBITCOIN_NODE_CHASERS_CHASER_VALIDATE_HPP

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

Expand All @@ -39,12 +40,19 @@ class BCN_API chaser_validate

code start() NOEXCEPT override;

/// Validate a populated candidate block.
virtual void validate(const system::chain::block::cptr& block,
const database::header_link& link, size_t height) NOEXCEPT;

protected:
typedef network::race_unity<const code&, const database::tx_link&> race;

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

virtual void do_validate(const system::chain::block::cptr& block,
database::header_link::integer link, size_t height) NOEXCEPT;

virtual void do_regressed(height_t branch_point) NOEXCEPT;
virtual void do_checked(height_t height) NOEXCEPT;
virtual void do_bump(height_t height) NOEXCEPT;
Expand Down
20 changes: 3 additions & 17 deletions include/bitcoin/node/define.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,32 +74,18 @@ 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>,
std::variant<uint32_t, size_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>>>;
std::variant<uint64_t, size_t>,
std::variant<uint64_t, uint32_t, size_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;

Expand Down
13 changes: 13 additions & 0 deletions include/bitcoin/node/full_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ class BCN_API full_node
/// Unsubscribe from chaser events.
virtual void unsubscribe_events(object_key key) NOEXCEPT;

/// Blocks.
/// -----------------------------------------------------------------------

/// Populate a candidate block for validation.
virtual void populate(const system::chain::block::cptr& block,
const database::header_link& link, size_t height,
network::result_handler&& complete) NOEXCEPT;

/// Validate a populated candidate block.
virtual void validate(const system::chain::block::cptr& block,
const database::header_link& link, size_t height) NOEXCEPT;

/// Suspensions.
/// -----------------------------------------------------------------------

Expand Down Expand Up @@ -156,6 +168,7 @@ class BCN_API full_node
chaser_block chaser_block_;
chaser_header chaser_header_;
chaser_check chaser_check_;
////chaser_populate chaser_populate_;
chaser_validate chaser_validate_;
chaser_confirm chaser_confirm_;
chaser_transaction chaser_transaction_;
Expand Down
30 changes: 15 additions & 15 deletions include/bitcoin/node/impl/chasers/chaser_organize.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ code CLASS::start() NOEXCEPT
}

TEMPLATE
void CLASS::organize(const typename Block::cptr& block_ptr,
void CLASS::organize(const typename Block::cptr& block,
organize_handler&& handler) NOEXCEPT
{
if (closed())
return;

POST(do_organize, block_ptr, std::move(handler));
POST(do_organize, block, std::move(handler));
}

// Methods
Expand Down Expand Up @@ -114,14 +114,14 @@ bool CLASS::handle_event(const code&, chase event_, event_value value) NOEXCEPT
}

TEMPLATE
void CLASS::do_organize(typename Block::cptr block_ptr,
void CLASS::do_organize(typename Block::cptr block,
const organize_handler& handler) NOEXCEPT
{
using namespace system;
BC_ASSERT(stranded());

const auto& hash = block_ptr->get_hash();
const auto& header = get_header(*block_ptr);
const auto& hash = block->get_hash();
const auto& header = get_header(*block);
auto& query = archive();

// Skip existing/orphan, get state.
Expand Down Expand Up @@ -174,7 +174,7 @@ void CLASS::do_organize(typename Block::cptr block_ptr,

// Headers are late validated, with malleations ignored upon download.
// Blocks are fully validated (not confirmed), so malleation is non-issue.
if ((ec = validate(*block_ptr, *state)))
if ((ec = validate(*block, *state)))
{
handler(ec, height);
return;
Expand All @@ -184,7 +184,7 @@ void CLASS::do_organize(typename Block::cptr block_ptr,
if (!is_storable(*state))
{
log_state_change(*parent, *state);
cache(block_ptr, state);
cache(block, state);
handler(error::success, height);
return;
}
Expand Down Expand Up @@ -215,7 +215,7 @@ void CLASS::do_organize(typename Block::cptr block_ptr,
if (!strong)
{
log_state_change(*parent, *state);
cache(block_ptr, state);
cache(block, state);
handler(error::success, height);
return;
}
Expand Down Expand Up @@ -283,7 +283,7 @@ void CLASS::do_organize(typename Block::cptr block_ptr,

// Push new header as top of candidate chain.
{
if ((ec = push_block(*block_ptr, state->context())))
if ((ec = push_block(*block, state->context())))
{
handler(fault(ec), height);
return;
Expand Down Expand Up @@ -368,18 +368,18 @@ void CLASS::do_disorganize(header_t link) NOEXCEPT
// ........................................................................
// Forward order is required to advance chain state for tree.

typename Block::cptr block_ptr{};
typename Block::cptr block{};
for (auto index = add1(fork_point); index < height; ++index)
{
if (!get_block(block_ptr, index))
if (!get_block(block, index))
{
fault(error::get_block);
return;
}

const auto& header = get_header(*block_ptr);
const auto& header = get_header(*block);
state.reset(new chain::chain_state{ *state, header, settings_ });
cache(block_ptr, state);
cache(block, state);
}

// Pop candidates from top candidate down to above fork point.
Expand Down Expand Up @@ -439,10 +439,10 @@ void CLASS::do_disorganize(header_t link) NOEXCEPT
// ----------------------------------------------------------------------------

TEMPLATE
void CLASS::cache(const typename Block::cptr& block_ptr,
void CLASS::cache(const typename Block::cptr& block,
const chain_state::ptr& state) NOEXCEPT
{
tree_.insert({ block_ptr->hash(), { block_ptr, state } });
tree_.insert({ block->hash(), { block, state } });
}

TEMPLATE
Expand Down
13 changes: 13 additions & 0 deletions include/bitcoin/node/protocols/protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define LIBBITCOIN_NODE_PROTOCOLS_PROTOCOL_HPP

// Individual session.hpp inclusion to prevent cycle (can't forward declare).
#include <bitcoin/database.hpp>
#include <bitcoin/network.hpp>
#include <bitcoin/node/define.hpp>
#include <bitcoin/node/sessions/session.hpp>
Expand Down Expand Up @@ -96,6 +97,18 @@ class BCN_API protocol
/// Get the subscription key (for notify_one).
virtual object_key events_key() const NOEXCEPT;

/// Blocks.
/// -----------------------------------------------------------------------

/// Populate a candidate block for validation.
virtual void populate(const system::chain::block::cptr& block,
const database::header_link& link, size_t height,
network::result_handler&& complete) NOEXCEPT;

/// Validate a populated candidate block.
virtual void validate(const system::chain::block::cptr& block,
const database::header_link& link, size_t height) NOEXCEPT;

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

Expand Down
6 changes: 6 additions & 0 deletions include/bitcoin/node/protocols/protocol_block_in_31800.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ class BCN_API protocol_block_in_31800
private:
using type_id = network::messages::inventory::type_id;

// HACK
void complete(const code& ec, const system::chain::block::cptr& block,
size_t height) const NOEXCEPT;

code check(const system::chain::block& block,
const system::chain::context& ctx, bool bypass) const NOEXCEPT;

Expand All @@ -97,6 +101,8 @@ class BCN_API protocol_block_in_31800
// These are protected by strand.
map_ptr map_;
job::ptr job_{};

std_vector<system::chain::block::cptr> blocks_{};
};

} // namespace node
Expand Down
13 changes: 13 additions & 0 deletions include/bitcoin/node/sessions/session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define LIBBITCOIN_NODE_SESSIONS_SESSION_HPP

#include <memory>
#include <bitcoin/database.hpp>
#include <bitcoin/network.hpp>
#include <bitcoin/node/define.hpp>
#include <bitcoin/node/full_node.hpp>
Expand Down Expand Up @@ -73,6 +74,18 @@ class BCN_API session
/// Unsubscribe from chaser events.
virtual void unsubscribe_events(object_key key) NOEXCEPT;

/// Blocks.
/// -----------------------------------------------------------------------

/// Populate a candidate block for validation.
virtual void populate(const system::chain::block::cptr& block,
const database::header_link& link, size_t height,
network::result_handler&& complete) NOEXCEPT;

/// Validate a populated candidate block.
virtual void validate(const system::chain::block::cptr& block,
const database::header_link& link, size_t height) NOEXCEPT;

/// Methods.
/// -----------------------------------------------------------------------

Expand Down
3 changes: 1 addition & 2 deletions src/chasers/chaser_confirm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,7 @@ bool chaser_confirm::enqueue_block(const header_link& link) NOEXCEPT

for (auto tx = std::next(txs.begin()); tx != txs.end(); ++tx)
boost::asio::post(threadpool_.service(),
std::bind(&chaser_confirm::confirm_tx,
this, ctx, *tx, racer));
BIND(confirm_tx, ctx, *tx, racer));

return true;
}
Expand Down
Loading

0 comments on commit 93940fd

Please sign in to comment.