From a51e27d9d56da3d0d1bc1bbfdd925c6e01b73d57 Mon Sep 17 00:00:00 2001 From: evoskuil Date: Thu, 15 Jun 2023 16:18:26 -0700 Subject: [PATCH 1/8] Comments. --- include/bitcoin/network/sessions/session.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/bitcoin/network/sessions/session.hpp b/include/bitcoin/network/sessions/session.hpp index 57c5db02b..026932496 100644 --- a/include/bitcoin/network/sessions/session.hpp +++ b/include/bitcoin/network/sessions/session.hpp @@ -118,7 +118,7 @@ class BCT_API session /// Start the session (call from network strand). virtual void start(result_handler&& handler) NOEXCEPT; - /// Stop the subscriber (call from network strand). + /// Stop the session (call from network strand). virtual void stop() NOEXCEPT; /// Utilities. From 46abc9f98ffbcd73dae20a64a12d78ffef9be747 Mon Sep 17 00:00:00 2001 From: evoskuil Date: Thu, 15 Jun 2023 16:18:38 -0700 Subject: [PATCH 2/8] Delint. --- src/net/deadline.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/net/deadline.cpp b/src/net/deadline.cpp index c6077c7cb..dc2206991 100644 --- a/src/net/deadline.cpp +++ b/src/net/deadline.cpp @@ -28,6 +28,8 @@ namespace libbitcoin { namespace network { +BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT) + using namespace std::placeholders; BC_DEBUG_ONLY(static const steady_clock::time_point epoch{};) @@ -79,5 +81,7 @@ void deadline::handle_timer(const error::boost_code& ec, handle(error::asio_to_error_code(ec)); } +BC_POP_WARNING() + } // namespace network } // namespace libbitcoin From 95fb37a2bc8791e7fcd9595670424e715ebfb54a Mon Sep 17 00:00:00 2001 From: evoskuil Date: Thu, 15 Jun 2023 16:19:33 -0700 Subject: [PATCH 3/8] Remove dead code. --- src/sessions/session.cpp | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/sessions/session.cpp b/src/sessions/session.cpp index 24c33e3aa..32ae1c36a 100644 --- a/src/sessions/session.cpp +++ b/src/sessions/session.cpp @@ -62,7 +62,7 @@ session::~session() NOEXCEPT void session::start(result_handler&& handler) NOEXCEPT { - BC_ASSERT_MSG(network_.stranded(), "strand"); + BC_ASSERT_MSG(stranded(), "strand"); if (!stopped()) { @@ -76,7 +76,7 @@ void session::start(result_handler&& handler) NOEXCEPT void session::stop() NOEXCEPT { - BC_ASSERT_MSG(network_.stranded(), "strand"); + BC_ASSERT_MSG(stranded(), "strand"); // Break out of sesson loop as handlers execute. stopped_.store(true); @@ -95,7 +95,7 @@ void session::stop() NOEXCEPT void session::start_channel(const channel::ptr& channel, result_handler&& starter, result_handler&& stopper) NOEXCEPT { - BC_ASSERT_MSG(network_.stranded(), "strand"); + BC_ASSERT_MSG(stranded(), "strand"); if (stopped()) { @@ -185,7 +185,7 @@ void session::handle_handshake(const code& ec, const channel::ptr& channel, void session::do_handle_handshake(const code& ec, const channel::ptr& channel, const result_handler& start) NOEXCEPT { - BC_ASSERT_MSG(network_.stranded(), "strand"); + BC_ASSERT_MSG(stranded(), "strand"); // Handles channel and protocol start failures. if (ec) @@ -213,7 +213,7 @@ void session::do_handle_handshake(const code& ec, const channel::ptr& channel, void session::handle_channel_start(const code& ec, const channel::ptr& channel, const result_handler& started, const result_handler& stopped) NOEXCEPT { - BC_ASSERT_MSG(network_.stranded(), "strand"); + BC_ASSERT_MSG(stranded(), "strand"); if (ec) { @@ -230,7 +230,7 @@ void session::handle_channel_start(const code& ec, const channel::ptr& channel, void session::handle_channel_started(const code& ec, const channel::ptr& channel, const result_handler& started) NOEXCEPT { - BC_ASSERT_MSG(channel->stranded() || network_.stranded(), "strand"); + BC_ASSERT_MSG(channel->stranded() || stranded(), "strand"); // Return to network context. boost::asio::post(network_.strand(), @@ -240,7 +240,7 @@ void session::handle_channel_started(const code& ec, void session::do_handle_channel_started(const code& ec, const channel::ptr& channel, const result_handler& started) NOEXCEPT { - BC_ASSERT_MSG(network_.stranded(), "strand"); + BC_ASSERT_MSG(stranded(), "strand"); // Handles channel subscribe_stop code. if (ec) @@ -313,7 +313,7 @@ void session::attach_protocols(const channel::ptr& channel) NOEXCEPT void session::handle_channel_stopped(const code& ec, const channel::ptr& channel, const result_handler& stopped) NOEXCEPT { - BC_ASSERT_MSG(channel->stranded() || network_.stranded(), "strand"); + BC_ASSERT_MSG(channel->stranded() || stranded(), "strand"); // Return to network context. boost::asio::post(network_.strand(), @@ -324,7 +324,7 @@ void session::handle_channel_stopped(const code& ec, void session::do_handle_channel_stopped(const code& ec, const channel::ptr& channel, const result_handler& stopped) NOEXCEPT { - BC_ASSERT_MSG(network_.stranded(), "strand"); + BC_ASSERT_MSG(stranded(), "strand"); unpend(channel); network_.unstore_nonce(*channel); @@ -341,14 +341,14 @@ void session::do_handle_channel_stopped(const code& ec, void session::defer(result_handler&& handler) NOEXCEPT { - BC_ASSERT_MSG(network_.stranded(), "strand"); + BC_ASSERT_MSG(stranded(), "strand"); defer(settings().retry_timeout(), std::move(handler)); } void session::defer(const steady_clock::duration& timeout, result_handler&& handler) NOEXCEPT { - BC_ASSERT_MSG(network_.stranded(), "strand"); + BC_ASSERT_MSG(stranded(), "strand"); if (stopped()) { @@ -369,7 +369,7 @@ void session::defer(const steady_clock::duration& timeout, void session::handle_timer(const code& ec, object_key key, const result_handler& complete) NOEXCEPT { - BC_ASSERT_MSG(network_.stranded(), "strand"); + BC_ASSERT_MSG(stranded(), "strand"); stop_subscriber_.notify_one(key, ec); complete(ec); } @@ -377,14 +377,14 @@ void session::handle_timer(const code& ec, object_key key, bool session::handle_defer(const code&, object_key, const deadline::ptr& timer) NOEXCEPT { - BC_ASSERT_MSG(network_.stranded(), "strand"); + BC_ASSERT_MSG(stranded(), "strand"); timer->stop(); return false; } void session::pend(const channel::ptr& channel) NOEXCEPT { - BC_ASSERT_MSG(network_.stranded(), "strand"); + BC_ASSERT_MSG(stranded(), "strand"); stop_subscriber_.subscribe(BIND2(handle_pend, _1, channel), channel->identifier()); } @@ -392,13 +392,13 @@ void session::pend(const channel::ptr& channel) NOEXCEPT // Ok to not find after stop, clears before channel stop handlers fire. void session::unpend(const channel::ptr& channel) NOEXCEPT { - BC_ASSERT_MSG(network_.stranded(), "strand"); + BC_ASSERT_MSG(stranded(), "strand"); notify(channel->identifier()); } bool session::handle_pend(const code& ec, const channel::ptr& channel) NOEXCEPT { - BC_ASSERT_MSG(network_.stranded(), "strand"); + BC_ASSERT_MSG(stranded(), "strand"); if (ec) channel->stop(ec); return false; } @@ -406,7 +406,7 @@ bool session::handle_pend(const code& ec, const channel::ptr& channel) NOEXCEPT typename session::object_key session::subscribe_stop(notify_handler&& handler) NOEXCEPT { - BC_ASSERT_MSG(network_.stranded(), "strand"); + BC_ASSERT_MSG(stranded(), "strand"); const auto key = create_key(); stop_subscriber_.subscribe(std::move(handler), key); return key; @@ -443,7 +443,7 @@ connectors_ptr session::create_connectors(size_t count) NOEXCEPT channel::ptr session::create_channel(const socket::ptr& socket, bool quiet) NOEXCEPT { - BC_ASSERT_MSG(network_.stranded(), "strand"); + BC_ASSERT_MSG(stranded(), "strand"); // Channel id must be created using create_key(). const auto id = create_key(); @@ -453,7 +453,7 @@ channel::ptr session::create_channel(const socket::ptr& socket, // At one object/session/ns, this overflows in ~585 years (and handled). session::object_key session::create_key() NOEXCEPT { - BC_ASSERT_MSG(network_.stranded(), "strand"); + BC_ASSERT_MSG(stranded(), "strand"); if (is_zero(++keys_)) { From aab0cef54062fc82185d4eca261a8b35f1c69c73 Mon Sep 17 00:00:00 2001 From: evoskuil Date: Thu, 15 Jun 2023 16:34:03 -0700 Subject: [PATCH 4/8] Qualify levels. --- include/bitcoin/network/log/levels.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/bitcoin/network/log/levels.hpp b/include/bitcoin/network/log/levels.hpp index 403d00ded..b112fa7fb 100644 --- a/include/bitcoin/network/log/levels.hpp +++ b/include/bitcoin/network/log/levels.hpp @@ -46,7 +46,7 @@ enum : uint8_t #define LOG_ONLY(name) name #define LOG(level_, message) \ BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT) \ - log.write(levels::level_) << message << std::endl; \ + log.write(network::levels::level_) << message << std::endl; \ BC_POP_WARNING() #else #define LOG_ONLY(name) @@ -57,7 +57,7 @@ enum : uint8_t constexpr auto objects_defined = true; #define LOGO(message) \ BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT) \ - log_.write(levels::objects) << message << std::endl; \ + log_.write(network::levels::objects) << message << std::endl; \ BC_POP_WARNING() #else constexpr auto objects_defined = false; From c4dbf8273a4a2b09a92cecfb1ecbd2b6569d0f64 Mon Sep 17 00:00:00 2001 From: evoskuil Date: Thu, 15 Jun 2023 16:35:46 -0700 Subject: [PATCH 5/8] Cache messages::block size. --- include/bitcoin/network/messages/block.hpp | 1 + src/messages/block.cpp | 11 ++++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/include/bitcoin/network/messages/block.hpp b/include/bitcoin/network/messages/block.hpp index 16c5ad165..1c5e4626c 100644 --- a/include/bitcoin/network/messages/block.hpp +++ b/include/bitcoin/network/messages/block.hpp @@ -51,6 +51,7 @@ struct BCT_API block size_t size(uint32_t version, bool witness) const NOEXCEPT; system::chain::block::cptr block_ptr; + mutable size_t cached_size{}; }; } // namespace messages diff --git a/src/messages/block.cpp b/src/messages/block.cpp index 59483cddc..eb42f67f8 100644 --- a/src/messages/block.cpp +++ b/src/messages/block.cpp @@ -91,7 +91,10 @@ block block::deserialize(uint32_t version, reader& source, if (version < version_minimum || version > version_maximum) source.invalidate(); - return { to_shared(source, witness) }; + const auto start = source.get_read_position(); + const auto block_ptr = to_shared(source, witness); + const auto size = source.get_read_position() - start; + return { block_ptr, size }; } bool block::serialize(uint32_t version, @@ -107,12 +110,14 @@ void block::serialize(uint32_t BC_DEBUG_ONLY(version), writer& sink, bool witness) const NOEXCEPT { BC_DEBUG_ONLY(const auto bytes = size(version, witness);) - BC_DEBUG_ONLY(const auto start = sink.get_write_position();) + ////BC_DEBUG_ONLY(const auto start = sink.get_write_position();) + const auto start = sink.get_write_position(); if (block_ptr) block_ptr->to_data(sink, witness); - BC_ASSERT(sink && sink.get_write_position() - start == bytes); + cached_size = sink.get_write_position() - start; + BC_ASSERT(sink && cached_size == bytes); } size_t block::size(uint32_t, bool witness) const NOEXCEPT From d94070a5f035d03813e06c3ba9486f9d4c80c080 Mon Sep 17 00:00:00 2001 From: evoskuil Date: Thu, 15 Jun 2023 16:37:14 -0700 Subject: [PATCH 6/8] Cache messages::block size. --- include/bitcoin/network/p2p.hpp | 10 +++++----- src/p2p.cpp | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/bitcoin/network/p2p.hpp b/include/bitcoin/network/p2p.hpp index 660d173b6..08a556f9b 100644 --- a/include/bitcoin/network/p2p.hpp +++ b/include/bitcoin/network/p2p.hpp @@ -178,6 +178,11 @@ class BCT_API p2p return session; } + virtual void do_start(const result_handler& handler) NOEXCEPT; + virtual void do_run(const result_handler& handler) NOEXCEPT; + virtual void do_close() NOEXCEPT; + virtual bool closed() const NOEXCEPT; + /// Override to attach specialized sessions, require strand. virtual session_seed::ptr attach_seed_session() NOEXCEPT; virtual session_manual::ptr attach_manual_session() NOEXCEPT; @@ -216,14 +221,9 @@ class BCT_API p2p connectors_ptr create_connectors(size_t count) NOEXCEPT; object_key create_key() NOEXCEPT; - virtual bool closed() const NOEXCEPT; virtual code start_hosts() NOEXCEPT; virtual code stop_hosts() NOEXCEPT; - void do_start(const result_handler& handler) NOEXCEPT; - void do_run(const result_handler& handler) NOEXCEPT; - void do_close() NOEXCEPT; - void handle_start(const code& ec, const result_handler& handler) NOEXCEPT; void handle_run(const code& ec, const result_handler& handler) NOEXCEPT; diff --git a/src/p2p.cpp b/src/p2p.cpp index e16626874..0bb78f2eb 100644 --- a/src/p2p.cpp +++ b/src/p2p.cpp @@ -375,7 +375,7 @@ void p2p::do_connect_handled(const config::endpoint& endpoint, // Properties. // ---------------------------------------------------------------------------- -// private +// protected bool p2p::closed() const NOEXCEPT { return closed_.load(); From e4375b26555b883a9dc5e094b5a927bde7a7ddfb Mon Sep 17 00:00:00 2001 From: evoskuil Date: Sat, 27 Jan 2024 18:04:37 -0500 Subject: [PATCH 7/8] Use fast stream for desegregated tx hash pre-computation. --- src/messages/transaction.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/messages/transaction.cpp b/src/messages/transaction.cpp index f81266b7a..9e63f5a94 100644 --- a/src/messages/transaction.cpp +++ b/src/messages/transaction.cpp @@ -47,7 +47,8 @@ hash_digest transaction::desegregated_hash(size_t witnessed, const auto locktime = floored_subtract(witnessed, sizeof(uint32_t)); hash_digest digest{}; - hash::sha256x2::copy sink(digest); + stream::out::fast stream{ digest }; + hash::sha256x2::fast sink{ stream }; sink.write_bytes(data, sizeof(uint32_t)); sink.write_bytes(std::next(data, preamble), puts); sink.write_bytes(std::next(data, locktime), sizeof(uint32_t)); From 48675689153f6fa92d8112396532bde754c2bb1e Mon Sep 17 00:00:00 2001 From: evoskuil Date: Fri, 9 Feb 2024 15:01:07 -0500 Subject: [PATCH 8/8] Move strand() to protected. --- include/bitcoin/network/sessions/session.hpp | 4 +++- src/sessions/session.cpp | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/include/bitcoin/network/sessions/session.hpp b/include/bitcoin/network/sessions/session.hpp index 026932496..987c50bb6 100644 --- a/include/bitcoin/network/sessions/session.hpp +++ b/include/bitcoin/network/sessions/session.hpp @@ -231,8 +231,10 @@ class BCT_API session /// Number of outbound connected channels (including manual). virtual size_t outbound_channel_count() const NOEXCEPT; -private: + /// The network strand. asio::strand& strand() NOEXCEPT; + +private: object_key create_key() NOEXCEPT; void handle_channel_start(const code& ec, const channel::ptr& channel, diff --git a/src/sessions/session.cpp b/src/sessions/session.cpp index 32ae1c36a..4c35f3393 100644 --- a/src/sessions/session.cpp +++ b/src/sessions/session.cpp @@ -534,6 +534,7 @@ void session::save(const address_cptr& message, network_.save(message, std::move(handler)); } +// protected asio::strand& session::strand() NOEXCEPT { return network_.strand();