Skip to content

Commit

Permalink
Refactor bypass using new store.header.bypass field.
Browse files Browse the repository at this point in the history
  • Loading branch information
evoskuil committed Jun 10, 2024
1 parent d4e9a09 commit 4cae6f4
Show file tree
Hide file tree
Showing 15 changed files with 413 additions and 403 deletions.
5 changes: 0 additions & 5 deletions include/bitcoin/node/chase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,6 @@ enum class chase
/// Issued by 'organize' and handled by 'check'.
regressed,

/// Bypass height has changed for all subsequent notifications (height_t).
/// Issued by 'organize' and handled by 'validate', 'confirm', and
/// 'block_in_31800'.
bypass,

/// Late-stage Invalidity.
/// -----------------------------------------------------------------------

Expand Down
9 changes: 1 addition & 8 deletions include/bitcoin/node/chasers/chaser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,7 @@ class BCN_API chaser
/// Header timestamp is within configured span from current time.
bool is_current(uint32_t timestamp) const NOEXCEPT;

/// Bypass (requires strand).
/// -----------------------------------------------------------------------

size_t bypass() const NOEXCEPT;
size_t checkpoint() const NOEXCEPT;
void set_bypass(size_t height) NOEXCEPT;
bool is_bypassed(size_t height) const NOEXCEPT;
/// The height is at or below the top checkpoint.
bool is_under_checkpoint(size_t height) const NOEXCEPT;

/// Position (requires strand).
Expand All @@ -147,7 +141,6 @@ class BCN_API chaser
const size_t top_checkpoint_height_;

// These are protected by strand.
size_t bypass_{};
size_t position_{};
};

Expand Down
24 changes: 15 additions & 9 deletions include/bitcoin/node/chasers/chaser_block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,27 @@ class BCN_API chaser_block
virtual const system::chain::header& get_header(
const system::chain::block& block) const NOEXCEPT;

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

/// True if Block should bypass validation, given its candidate height.
virtual bool get_bypass(const system::chain::block& block,
size_t height) const NOEXCEPT;

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

/// Handle malleted message (nop).
virtual void do_malleated(header_t link) NOEXCEPT;

/// Determine if Block is top of a storable branch.
virtual bool is_storable(const system::chain::block& block,
const system::chain::chain_state& state) const NOEXCEPT;
/// Determine if state is top of a storable branch (always true).
virtual bool is_storable(const chain_state& state) const NOEXCEPT;

// Store Block to database and push to top of candidate chain.
virtual database::header_link push(const system::chain::block& block,
const system::chain::context& context) const NOEXCEPT;
/// Milestone tracking.
virtual void update_milestone(const system::chain::header& header,
size_t height, size_t branch_point) NOEXCEPT;

private:
void set_prevout(const system::chain::input& input) const NOEXCEPT;
Expand Down
42 changes: 36 additions & 6 deletions include/bitcoin/node/chasers/chaser_header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,52 @@ class BCN_API chaser_header

chaser_header(full_node& node) NOEXCEPT;

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

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

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

/// True if Block should bypass validation, given its candidate height.
virtual bool get_bypass(const system::chain::header& header,
size_t height) const NOEXCEPT;

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

/// Disassociate malleated block and notify to redownload header.
virtual void do_malleated(header_t link) NOEXCEPT;

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

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

/// Milestone methods.
bool initialize_milestone() NOEXCEPT;
bool is_under_milestone(size_t height) const NOEXCEPT;

private:
// Storable methods.
bool is_checkpoint(const chain_state& state) const NOEXCEPT;
bool is_milestone(const chain_state& state) const NOEXCEPT;
bool is_current(const chain_state& state) const NOEXCEPT;
bool is_hard(const chain_state& state) const NOEXCEPT;

// This is thread safe.
const system::chain::checkpoint& milestone_;

/// Determine if Block is top of a storable branch.
virtual bool is_storable(const system::chain::header& header,
const system::chain::chain_state& state) const NOEXCEPT;
// This is protected by strand.
size_t active_milestone_height_{};
};

} // namespace node
Expand Down
72 changes: 29 additions & 43 deletions include/bitcoin/node/chasers/chaser_organize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class chaser_organize
DELETE_COPY_MOVE_DESTRUCT(chaser_organize);

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

/// Validate and organize next block in sequence relative to caller peer.
virtual void organize(const typename Block::cptr& block_ptr,
Expand All @@ -66,26 +66,27 @@ class chaser_organize
virtual const system::chain::header& get_header(
const Block& block) const NOEXCEPT = 0;

/// Query store for const pointer to Block instance.
/// Query store for const pointer to Block instance by candidate height.
virtual bool get_block(typename Block::cptr& out,
size_t index) const NOEXCEPT = 0;
size_t height) const NOEXCEPT = 0;

/// True if Block should bypass validation, given its candidate height.
virtual bool get_bypass(const Block& block,
size_t height) const NOEXCEPT = 0;

/// Determine if Block is valid.
virtual code validate(const Block& block,
const chain_state& state) const NOEXCEPT = 0;

/// Determine if Block is top of a storable branch.
virtual bool is_storable(const Block& block,
const chain_state& state) const NOEXCEPT = 0;

/// Properties
/// -----------------------------------------------------------------------
/// Disassociate malleated block and notify repeat header in current job.
virtual void do_malleated(header_t link) NOEXCEPT = 0;

/// Constant access to Block tree.
virtual const block_tree& tree() const NOEXCEPT;
/// Determine if state is top of a storable branch.
virtual bool is_storable(const chain_state& state) const NOEXCEPT = 0;

/// System configuration settings.
virtual const system::settings& settings() const NOEXCEPT;
/// Milestone tracking.
virtual void update_milestone(const system::chain::header& header,
size_t height, size_t branch_point) NOEXCEPT = 0;

/// Methods
/// -----------------------------------------------------------------------
Expand All @@ -94,25 +95,26 @@ class chaser_organize
virtual bool handle_event(const code&, chase event_,
event_value value) NOEXCEPT;

/// Reorganize following strong branch discovery.
/// Organize a discovered Block.
virtual void do_organize(typename Block::cptr& block_ptr,
const organize_handler& handler) NOEXCEPT;

/// Reorganize following Block unconfirmability.
virtual void do_disorganize(header_t header) NOEXCEPT;

/// Disassociate malleated block and notify repeat header in current job.
virtual void do_malleated(header_t link) NOEXCEPT;
/// Properties
/// -----------------------------------------------------------------------

/// Store Block to database and push to top of candidate chain.
virtual database::header_link push(const Block& block,
const system::chain::context& context) const NOEXCEPT;
/// Constant access to Block tree.
virtual const block_tree& tree() const NOEXCEPT;

/// Height represents a candidate block covered by active milestone.
virtual inline bool is_under_milestone(size_t height) const NOEXCEPT;
/// System configuration settings.
virtual const system::settings& settings() const NOEXCEPT;

private:
static constexpr auto flag_bits = to_bits(sizeof(system::chain::flags));
// Template differetiators.
// ------------------------------------------------------------------------

static constexpr bool is_block() NOEXCEPT
{
return is_same_type<Block, system::chain::block>;
Expand Down Expand Up @@ -151,25 +153,11 @@ class chaser_organize
size_t branch_point) const NOEXCEPT;

// Move tree Block to database and push to top of candidate chain.
bool push(const system::hash_digest& key) NOEXCEPT;
bool push_block(const system::hash_digest& key) NOEXCEPT;

// Bypass methods.
// ------------------------------------------------------------------------

// Set milestone cache if exists in candidate chain, send chase::bypass.
bool initialize_bypass() NOEXCEPT;

// Clear milestone cache if its height is above branch_point.
void reset_milestone(size_t branch_point) NOEXCEPT;

// Set milestone cache if configured milestone matches given checkpoint.
void update_milestone(const database::header_link& link,
size_t height) NOEXCEPT;
void update_milestone(const system::hash_digest& hash,
size_t height) NOEXCEPT;

// Notify chase::bypass subscribers of a change in bypass height.
void notify_bypass() const NOEXCEPT;
/// Store Block to database and push to top of candidate chain.
bool push_block(const Block& block,
const system::chain::context& context) const NOEXCEPT;

// Logging.
// ------------------------------------------------------------------------
Expand All @@ -180,11 +168,9 @@ class chaser_organize

// These are thread safe.
const system::settings& settings_;
const system::chain::checkpoint& milestone_;
const system::chain::checkpoints checkpoints_;
const system::chain::checkpoints& checkpoints_;

// These are protected by strand.
size_t active_milestone_height_{};
chain_state::ptr state_{};
block_tree tree_{};
};
Expand Down
4 changes: 0 additions & 4 deletions include/bitcoin/node/chasers/chaser_validate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ class BCN_API chaser_validate
const database::context& ctx) NOEXCEPT;

private:
#if defined (UNDEFINED)
code validate(const database::header_link& link, size_t height) NOEXCEPT;
#endif // UNDEFINED

// neutrino
void update_position(size_t height) NOEXCEPT;
system::hash_digest get_neutrino(size_t height) const NOEXCEPT;
Expand Down
4 changes: 2 additions & 2 deletions include/bitcoin/node/define.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ typedef database::query<store> query;
/// Work types.
typedef network::race_all<const code&> job;
typedef std::shared_ptr<database::associations> map_ptr;
typedef std::function<void(const code&, const map_ptr&, const job::ptr&,
size_t)> map_handler;
typedef std::function<void(const code&, const map_ptr&,
const job::ptr&)> map_handler;

/// Node events.
typedef uint64_t object_key;
Expand Down
Loading

0 comments on commit 4cae6f4

Please sign in to comment.