Skip to content

Commit

Permalink
Merge pull request #610 from evoskuil/master
Browse files Browse the repository at this point in the history
Implement max advance and query.get_code().
  • Loading branch information
evoskuil committed May 10, 2024
2 parents 3e941b5 + cb5f1a4 commit 6936e06
Show file tree
Hide file tree
Showing 24 changed files with 158 additions and 250 deletions.
3 changes: 2 additions & 1 deletion include/bitcoin/node/chasers/chaser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ class BCN_API chaser
virtual bool closed() const NOEXCEPT;

/// Suspend all existing and future network connections.
virtual void suspend(const code& ec) const NOEXCEPT;
/// A race condition could result in an unsuspended connection.
virtual code suspend(const code& ec) const NOEXCEPT;

/// Events.
/// -----------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion include/bitcoin/node/chasers/chaser_check.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ class BCN_API chaser_check
size_t get_unassociated() NOEXCEPT;

// These are thread safe.
const size_t maximum_block_;
const size_t maximum_advance_;
const size_t maximum_height_;
const size_t connections_;
const size_t inventory_;

Expand Down
5 changes: 3 additions & 2 deletions include/bitcoin/node/full_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ class BCN_API full_node
/// Suspensions.
/// -----------------------------------------------------------------------

/// Suspend nework connections.
void suspend(const code& ec) NOEXCEPT override;
/// Suspend all existing and future network connections.
/// A race condition could result in an unsuspended connection.
code suspend(const code& ec) NOEXCEPT override;

/// Resume nework connections.
void resume() NOEXCEPT override;
Expand Down
65 changes: 9 additions & 56 deletions include/bitcoin/node/impl/chasers/chaser_organize.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ void CLASS::do_organize(typename Block::cptr& block_ptr,
size_t height{};
if (!query.get_height(height, id))
{
handler(error::store_integrity, {});
suspend(error::store_integrity);
handler(suspend(error::store_integrity), {});
return;
}

Expand Down Expand Up @@ -239,8 +238,7 @@ void CLASS::do_organize(typename Block::cptr& block_ptr,
if (!get_branch_work(work, branch_point, tree_branch, store_branch, header) ||
!get_is_strong(strong, work, branch_point))
{
handler(error::store_integrity, height);
suspend(error::store_integrity);
handler(suspend(error::store_integrity), height);
return;
}

Expand All @@ -259,8 +257,7 @@ void CLASS::do_organize(typename Block::cptr& block_ptr,
const auto top_candidate = state_->height();
if (branch_point > top_candidate)
{
handler(error::store_integrity, height);
suspend(error::store_integrity);
handler(suspend(error::store_integrity), height);
return;
}

Expand All @@ -276,15 +273,7 @@ void CLASS::do_organize(typename Block::cptr& block_ptr,
{
if (!query.pop_candidate())
{
if (query.is_full())
{
handler(database::error::disk_full, height);
suspend(database::error::disk_full);
return;
}

handler(error::store_integrity, height);
suspend(error::store_integrity);
handler(suspend(query.get_code()), height);
return;
}

Expand All @@ -299,15 +288,7 @@ void CLASS::do_organize(typename Block::cptr& block_ptr,
{
if (!query.push_candidate(link))
{
if (query.is_full())
{
handler(database::error::disk_full, height);
suspend(database::error::disk_full);
return;
}

handler(error::store_integrity, height);
suspend(error::store_integrity);
handler(suspend(query.get_code()), height);
return;
}

Expand All @@ -319,15 +300,7 @@ void CLASS::do_organize(typename Block::cptr& block_ptr,
{
if (!push(key))
{
if (query.is_full())
{
handler(database::error::disk_full, height);
suspend(database::error::disk_full);
return;
}

handler(error::store_integrity, height);
suspend(error::store_integrity);
handler(suspend(query.get_code()), height);
return;
}

Expand All @@ -339,15 +312,7 @@ void CLASS::do_organize(typename Block::cptr& block_ptr,
{
if (push(block, state->context()).is_terminal())
{
if (query.is_full())
{
handler(database::error::disk_full, height);
suspend(database::error::disk_full);
return;
}

handler(error::store_integrity, height);
suspend(error::store_integrity);
handler(suspend(query.get_code()), height);
return;
}

Expand Down Expand Up @@ -452,13 +417,7 @@ void CLASS::do_disorganize(header_t link) NOEXCEPT
{
if (!query.pop_candidate())
{
if (query.is_full())
{
suspend(database::error::disk_full);
return;
}

suspend(error::store_integrity);
suspend(query.get_code());
return;
}

Expand All @@ -473,13 +432,7 @@ void CLASS::do_disorganize(header_t link) NOEXCEPT
{
if (!query.push_candidate(query.to_confirmed(index)))
{
if (query.is_full())
{
suspend(database::error::disk_full);
return;
}

suspend(error::store_integrity);
suspend(query.get_code());
return;
}

Expand Down
5 changes: 3 additions & 2 deletions include/bitcoin/node/protocols/protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ class BCN_API protocol
virtual void performance(uint64_t channel, uint64_t speed,
network::result_handler&& handler) const NOEXCEPT;

/// Suspend all connections.
virtual void suspend(const code& ec) NOEXCEPT;
/// Suspend all existing and future network connections.
/// A race condition could result in an unsuspended connection.
virtual code suspend(const code& ec) NOEXCEPT;

/// Events.
/// -----------------------------------------------------------------------
Expand Down
7 changes: 3 additions & 4 deletions include/bitcoin/node/protocols/protocol_block_in_31800.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class BCN_API protocol_block_in_31800
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
template <typename SessionPtr>
protocol_block_in_31800(const SessionPtr& session,
const channel_ptr& channel, bool performance) NOEXCEPT
: protocol_performer(session, channel, performance),
const channel_ptr& channel) NOEXCEPT
: protocol_performer(session, channel),
block_type_(session->config().network.witness_node() ?
type_id::witness_block : type_id::block),
map_(chaser_check::empty_map())
Expand Down Expand Up @@ -73,7 +73,6 @@ class BCN_API protocol_block_in_31800

private:
using type_id = network::messages::inventory::type_id;
static constexpr size_t minimum_for_stall_divide = 2;

code check(const system::chain::block& block,
const system::chain::context& ctx) const NOEXCEPT;
Expand All @@ -83,7 +82,7 @@ class BCN_API protocol_block_in_31800
const map_ptr& map) const NOEXCEPT;

void restore(const map_ptr& map) NOEXCEPT;
void handle_put_hashes(const code& ec) NOEXCEPT;
void handle_put_hashes(const code& ec, size_t count) NOEXCEPT;
void handle_get_hashes(const code& ec, const map_ptr& map) NOEXCEPT;
void do_complete_event(const code& ec, object_key key) NOEXCEPT;

Expand Down
13 changes: 6 additions & 7 deletions include/bitcoin/node/protocols/protocol_performer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@ class BCN_API protocol_performer

protected:
template <typename SessionPtr>
protocol_performer(const SessionPtr& session, const channel_ptr& channel,
bool enable) NOEXCEPT
protocol_performer(const SessionPtr& session,
const channel_ptr& channel) NOEXCEPT
: node::protocol(session, channel),
network::tracker<protocol_performer>(session->log),
use_deviation_(session->config().node.allowed_deviation > 0.0),
drop_stall_(enable &&
to_bool(session->config().node.sample_period_seconds)),
deviation_(session->config().node.allowed_deviation > 0.0),
enabled_(to_bool(session->config().node.sample_period_seconds)),
performance_timer_(std::make_shared<network::deadline>(session->log,
channel->strand(), session->config().node.sample_period()))
{
Expand All @@ -61,8 +60,8 @@ class BCN_API protocol_performer
void send_performance(uint64_t rate) NOEXCEPT;

// These are thread safe.
const bool use_deviation_;
const bool drop_stall_;
const bool deviation_;
const bool enabled_;

// These are protected by strand.
uint64_t bytes_{ zero };
Expand Down
37 changes: 20 additions & 17 deletions include/bitcoin/node/sessions/attach.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,41 +57,44 @@ class attach
void attach_protocols(
const network::channel::ptr& channel) NOEXCEPT override
{
constexpr auto bip130 = network::messages::level::bip130;
constexpr auto headers = network::messages::level::headers_protocol;
constexpr auto in = is_same_type<Session, network::session_inbound>;

const auto headers_first = config().node.headers_first;
const auto version = channel->negotiated_version();
const auto self = session::shared_from_sibling<attach<Session>,
network::session>();

// Only session_outbound channels compete on performance.
auto performance = false;
if constexpr (is_same_type<Session, network::session_outbound>)
{
performance = true;
}

// Attach appropriate alert, reject, ping, and/or address protocols.
Session::attach_protocols(channel);

// Very hard to find < 31800 peer to connect with.
if (headers_first && version >= network::messages::level::bip130)
if (headers_first && version >= bip130)
{
// Headers-first synchronization (parallel block download).
channel->attach<protocol_header_in_70012>(self)->start();
channel->attach<protocol_header_out_70012>(self)->start();
channel->attach<protocol_block_in_31800>(self, performance)->start();
if constexpr (!in)
{
channel->attach<protocol_block_in_31800>(self)->start();
}
}
else if (headers_first &&
version >= network::messages::level::headers_protocol)
else if (headers_first && version >= headers)
{
// Headers-first synchronization (parallel block download).
channel->attach<protocol_header_in_31800>(self)->start();
channel->attach<protocol_header_out_31800>(self)->start();
channel->attach<protocol_block_in_31800>(self, performance)->start();
if constexpr (!in)
{
channel->attach<protocol_block_in_31800>(self)->start();
}
}
else
{
// Blocks-first synchronization (no header protocol).
channel->attach<protocol_block_in>(self)->start();
// Very hard to find < 31800 peer to connect with.
// Blocks-first synchronization (no headers protocol).
if constexpr (!in)
{
channel->attach<protocol_block_in>(self)->start();
}
}

channel->attach<protocol_block_out>(self)->start();
Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/node/sessions/session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class BCN_API session
/// -----------------------------------------------------------------------

/// Suspend all connections.
virtual void suspend(const code& ec) NOEXCEPT;
virtual code suspend(const code& ec) NOEXCEPT;

/// Properties.
/// -----------------------------------------------------------------------
Expand Down
14 changes: 6 additions & 8 deletions include/bitcoin/node/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ class BCN_API settings
std::filesystem::path symbols;
#endif

virtual std::filesystem::path log_file1() NOEXCEPT;
virtual std::filesystem::path log_file2() NOEXCEPT;
virtual std::filesystem::path events_file() NOEXCEPT;
virtual std::filesystem::path log_file1() const NOEXCEPT;
virtual std::filesystem::path log_file2() const NOEXCEPT;
virtual std::filesystem::path events_file() const NOEXCEPT;
};

} // namespace log
Expand All @@ -66,14 +66,12 @@ class BCN_API settings
uint16_t maximum_inventory;
uint16_t sample_period_seconds;
uint32_t currency_window_minutes;
uint32_t maximum_advance;
uint32_t maximum_height;

////// TODO: these aren't actually node settings.
////uint16_t target{ 0 };
////uint16_t interval{ 0 };

/// Helpers.
virtual size_t maximum_block() const NOEXCEPT;
virtual size_t maximum_advance_() const NOEXCEPT;
virtual size_t maximum_height_() const NOEXCEPT;
virtual network::steady_clock::duration sample_period() const NOEXCEPT;
virtual network::wall_clock::duration currency_window() const NOEXCEPT;
};
Expand Down
4 changes: 2 additions & 2 deletions src/chasers/chaser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ bool chaser::closed() const NOEXCEPT
return node_.closed();
}

void chaser::suspend(const code& ec) const NOEXCEPT
code chaser::suspend(const code& ec) const NOEXCEPT
{
node_.suspend(ec);
return node_.suspend(ec);
}

// Events.
Expand Down
Loading

0 comments on commit 6936e06

Please sign in to comment.