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

Make chaser strand() and stranded() virtual, update neutrino. #706

Merged
merged 7 commits into from
Jan 5, 2025
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
2 changes: 1 addition & 1 deletion console/executor_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ bool executor::check_store_path(bool create) const
if (create)
{
logger(format(BN_INITIALIZING_CHAIN) % store);
if (auto ec = database::file::create_directory_ex(store))
if (const auto ec = database::file::create_directory_ex(store))
{
logger(format(BN_INITCHAIN_DIRECTORY_ERROR) % store % ec.message());
return false;
Expand Down
18 changes: 12 additions & 6 deletions include/bitcoin/node/chasers/chaser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ class BCN_API chaser
virtual void notify_one(object_key key, const code& ec, chase event_,
event_value value) const NOEXCEPT;

/// Strand.
/// -----------------------------------------------------------------------

/// The chaser's strand (on the network threadpool).
virtual network::asio::strand& strand() NOEXCEPT;

/// True if the current thread is on the chaser strand.
virtual bool stranded() const NOEXCEPT;

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

Expand All @@ -116,12 +125,6 @@ class BCN_API chaser
/// Thread safe synchronous archival interface.
query& archive() const NOEXCEPT;

/// The chaser's strand.
network::asio::strand& strand() NOEXCEPT;

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

/// Top candidate is within configured span from current time.
bool is_current() const NOEXCEPT;

Expand Down Expand Up @@ -153,6 +156,9 @@ class BCN_API chaser
#define SUBSCRIBE_EVENTS(method, ...) \
subscribe_events(BIND(method, __VA_ARGS__))

#define PARALLEL(method, ...) \
boost::asio::post(threadpool_.service(), BIND(method, __VA_ARGS__));

} // namespace node
} // namespace libbitcoin

Expand Down
19 changes: 17 additions & 2 deletions include/bitcoin/node/chasers/chaser_confirm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,20 @@ class BCN_API chaser_confirm
////virtual void do_organize(header_links& fork, const header_links& popped,
//// size_t fork_point) NOEXCEPT;

// Override base class strand because it sits on the network thread pool.
network::asio::strand& strand() NOEXCEPT override;
bool stranded() const NOEXCEPT override;

private:
struct neutrino_header
{
system::hash_digest head{};
database::header_link link{};
};

void reset_position(size_t confirmed_height) NOEXCEPT;
bool update_neutrino(const database::header_link& link) NOEXCEPT;

bool set_organized(const database::header_link& link,
height_t height) NOEXCEPT;
bool reset_organized(const database::header_link& link,
Expand All @@ -74,12 +87,14 @@ class BCN_API chaser_confirm
bool get_is_strong(bool& strong, const uint256_t& fork_work,
size_t fork_point) const NOEXCEPT;

// This is thread safe.
// These are thread safe.
const bool concurrent_;
network::asio::strand independent_strand_;

// These are protected by strand.
network::threadpool threadpool_;
network::asio::strand strand_;
neutrino_header neutrino_{};
bool filters_{};
bool mature_{};
};

Expand Down
24 changes: 16 additions & 8 deletions include/bitcoin/node/chasers/chaser_validate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,33 @@ class BCN_API chaser_validate
virtual void complete_block(const code& ec,
const database::header_link& link, size_t height) NOEXCEPT;

// Override base class strand because it sits on the network thread pool.
network::asio::strand& strand() NOEXCEPT override;
bool stranded() const NOEXCEPT override;

private:
// neutrino
void update_position(size_t height) NOEXCEPT;
system::hash_digest get_neutrino(size_t height) const NOEXCEPT;
bool update_neutrino(const database::header_link& link) NOEXCEPT;
bool update_neutrino(const database::header_link& link,
inline bool unfilled() const NOEXCEPT
{
return backlog_ < maximum_backlog_;
}

bool set_neutrino(const database::header_link& link,
const system::chain::block& block) NOEXCEPT;

bool set_prevouts(size_t height,
const system::chain::block& block) NOEXCEPT;

// These are thread safe.
const bool concurrent_;
const size_t maximum_backlog_;
const uint64_t initial_subsidy_;
const uint32_t subsidy_interval_;
network::asio::strand independent_strand_;

// These are protected by strand.
network::threadpool threadpool_;
network::asio::strand strand_;
system::hash_digest neutrino_{};
size_t validation_backlog_{};
size_t backlog_{};
bool filters_{};
bool mature_{};
};

Expand Down
1 change: 1 addition & 0 deletions include/bitcoin/node/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ enum error_t : uint8_t
validate4,
validate5,
validate6,
validate7,
confirm1,
confirm2,
confirm3,
Expand Down
2 changes: 2 additions & 0 deletions include/bitcoin/node/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,12 @@ class BCN_API settings
uint32_t threads;

/// Helpers.
virtual size_t threads_() const NOEXCEPT;
virtual size_t maximum_height_() const NOEXCEPT;
virtual size_t maximum_concurrency_() const NOEXCEPT;
virtual network::steady_clock::duration sample_period() const NOEXCEPT;
virtual network::wall_clock::duration currency_window() const NOEXCEPT;
virtual network::thread_priority priority_() const NOEXCEPT;
};

} // namespace node
Expand Down
21 changes: 12 additions & 9 deletions src/chasers/chaser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,27 +102,30 @@ void chaser::notify_one(object_key key, const code& ec, chase event_,
node_.notify_one(key, ec, event_, value);
}

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

const node::configuration& chaser::config() const NOEXCEPT
asio::strand& chaser::strand() NOEXCEPT
{
return node_.config();
return strand_;
}

query& chaser::archive() const NOEXCEPT
bool chaser::stranded() const NOEXCEPT
{
return node_.archive();
return strand_.running_in_this_thread();
}

asio::strand& chaser::strand() NOEXCEPT
// Properties.
// ----------------------------------------------------------------------------

const node::configuration& chaser::config() const NOEXCEPT
{
return strand_;
return node_.config();
}

bool chaser::stranded() const NOEXCEPT
query& chaser::archive() const NOEXCEPT
{
return strand_.running_in_this_thread();
return node_.archive();
}

bool chaser::is_current() const NOEXCEPT
Expand Down
91 changes: 67 additions & 24 deletions src/chasers/chaser_confirm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@ BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
chaser_confirm::chaser_confirm(full_node& node) NOEXCEPT
: chaser(node),
concurrent_(node.config().node.concurrent_confirmation),
threadpool_(1_u32, node.config().node.priority_validation ?
network::thread_priority::high : network::thread_priority::normal),
strand_(threadpool_.service().get_executor())
threadpool_(one, node.config().node.priority_()),
independent_strand_(threadpool_.service().get_executor())
{
}

code chaser_confirm::start() NOEXCEPT
{
set_position(archive().get_fork());
const auto& query = archive();
filters_ = query.neutrino_enabled();
reset_position(query.get_fork());
SUBSCRIBE_EVENTS(handle_event, _1, _2, _3);
return error::success;
}
Expand All @@ -71,9 +72,6 @@ void chaser_confirm::stop() NOEXCEPT
}
}

// Protected
// ----------------------------------------------------------------------------

bool chaser_confirm::handle_event(const code&, chase event_,
event_value value) NOEXCEPT
{
Expand Down Expand Up @@ -106,9 +104,7 @@ bool chaser_confirm::handle_event(const code&, chase event_,
{
if (concurrent_ || mature_)
{
////POST(do_bump, height_t{});
boost::asio::post(strand_,
BIND(do_bump, height_t{}));
POST(do_bump, height_t{});
}

break;
Expand All @@ -120,27 +116,21 @@ bool chaser_confirm::handle_event(const code&, chase event_,

if (concurrent_ || mature_)
{
////POST(do_validated, std::get<height_t>(value));
boost::asio::post(strand_,
BIND(do_validated, std::get<height_t>(value)));
POST(do_validated, std::get<height_t>(value));
}

break;
}
case chase::regressed:
{
BC_ASSERT(std::holds_alternative<height_t>(value));
////POST(do_regressed, std::get<height_t>(value));
boost::asio::post(strand_,
BIND(do_regressed, std::get<height_t>(value)));
POST(do_regressed, std::get<height_t>(value));
break;
}
case chase::disorganized:
{
BC_ASSERT(std::holds_alternative<height_t>(value));
////POST(do_regressed, std::get<height_t>(value));
boost::asio::post(strand_,
BIND(do_regressed, std::get<height_t>(value)));
POST(do_regressed, std::get<height_t>(value));
break;
}
case chase::stop:
Expand All @@ -156,6 +146,9 @@ bool chaser_confirm::handle_event(const code&, chase event_,
return true;
}

// track validation
// ----------------------------------------------------------------------------

void chaser_confirm::do_regressed(height_t branch_point) NOEXCEPT
{
BC_ASSERT(stranded());
Expand All @@ -170,14 +163,14 @@ void chaser_confirm::do_regressed(height_t branch_point) NOEXCEPT
}
}

set_position(branch_point);
reset_position(branch_point);
}

// Candidate block validated at given height, if next then confirm/advance.
void chaser_confirm::do_validated(height_t height) NOEXCEPT
{
BC_ASSERT(stranded());

// Candidate block was validated at the given height, confirm/advance.
if (height == add1(position()))
do_bump(height);
}
Expand Down Expand Up @@ -244,9 +237,8 @@ void chaser_confirm::do_bump(height_t) NOEXCEPT
<< ec.message());
return;
}

// TODO: fees.
if (!query.set_block_confirmable(link, {}))

if (!query.set_block_confirmable(link))
{
fault(error::confirm6);
return;
Expand Down Expand Up @@ -283,6 +275,7 @@ void chaser_confirm::do_bump(height_t) NOEXCEPT
return;
}

update_neutrino(link);
set_position(height);
}
}
Expand Down Expand Up @@ -617,6 +610,56 @@ bool chaser_confirm::get_is_strong(bool& strong, const uint256_t& fork_work,
return true;
}

// neutrino
// ----------------------------------------------------------------------------

// This can only fail if prevouts are not fully populated.
bool chaser_confirm::update_neutrino(const header_link& link) NOEXCEPT
{
// neutrino_.link is only used for this assertion, should compile away.
BC_ASSERT(archive().get_height(link) ==
add1(archive().get_height(neutrino_.link)));

if (!filters_)
return true;

data_chunk filter{};
auto& query = archive();
if (!query.get_filter_body(filter, link))
return false;

neutrino_.link = link;
neutrino_.head = neutrino::compute_filter_header(neutrino_.head, filter);
return query.set_filter_head(link, neutrino_.head);
}

// Expects confirmed height.
// Use for startup and regression, to read current filter header from store.
void chaser_confirm::reset_position(size_t confirmed_height) NOEXCEPT
{
set_position(confirmed_height);

if (filters_)
{
const auto& query = archive();
neutrino_.link = query.to_confirmed(confirmed_height);
query.get_filter_head(neutrino_.head, neutrino_.link);
}
}

// Strand.
// ----------------------------------------------------------------------------

network::asio::strand& chaser_confirm::strand() NOEXCEPT
{
return independent_strand_;
}

bool chaser_confirm::stranded() const NOEXCEPT
{
return independent_strand_.running_in_this_thread();
}

BC_POP_WARNING()

} // namespace node
Expand Down
Loading
Loading