Skip to content

Commit

Permalink
Merge pull request #366 from evoskuil/master
Browse files Browse the repository at this point in the history
Use fast stream for desegregated tx hash pre-computation.
  • Loading branch information
evoskuil authored Feb 9, 2024
2 parents 8d6b184 + 4867568 commit d4b3735
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 33 deletions.
4 changes: 2 additions & 2 deletions include/bitcoin/network/log/levels.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions include/bitcoin/network/messages/block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions include/bitcoin/network/p2p.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
6 changes: 4 additions & 2 deletions include/bitcoin/network/sessions/session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 8 additions & 3 deletions src/messages/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ block block::deserialize(uint32_t version, reader& source,
if (version < version_minimum || version > version_maximum)
source.invalidate();

return { to_shared<chain::block>(source, witness) };
const auto start = source.get_read_position();
const auto block_ptr = to_shared<chain::block>(source, witness);
const auto size = source.get_read_position() - start;
return { block_ptr, size };
}

bool block::serialize(uint32_t version,
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/messages/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
4 changes: 4 additions & 0 deletions src/net/deadline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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{};)
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/p2p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ void p2p::do_connect_handled(const config::endpoint& endpoint,
// Properties.
// ----------------------------------------------------------------------------

// private
// protected
bool p2p::closed() const NOEXCEPT
{
return closed_.load();
Expand Down
39 changes: 20 additions & 19 deletions src/sessions/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand All @@ -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);
Expand All @@ -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())
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
{
Expand All @@ -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(),
Expand All @@ -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)
Expand Down Expand Up @@ -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(),
Expand All @@ -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);
Expand All @@ -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())
{
Expand All @@ -369,44 +369,44 @@ 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);
}

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());
}

// 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;
}

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;
Expand Down Expand Up @@ -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();
Expand All @@ -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_))
{
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit d4b3735

Please sign in to comment.