From 1dc49d51c95d26c2b5b803d2641f50a6516f3ce4 Mon Sep 17 00:00:00 2001 From: Eric Voskuil Date: Sun, 4 Oct 2026 19:54:31 -0400 Subject: [PATCH] Style. --- .../node/impl/sessions/session_peer.ipp | 8 --- .../protocols/protocol_header_in_70014.hpp | 14 ++-- .../bitcoin/node/protocols/protocol_peer.hpp | 14 ++-- src/protocols/protocol_block_out_70014.cpp | 3 +- src/protocols/protocol_header_in_70014.cpp | 57 ++++++++++------ src/protocols/protocol_header_out_70014.cpp | 7 +- src/protocols/protocol_peer.cpp | 66 +++++++++---------- 7 files changed, 91 insertions(+), 78 deletions(-) diff --git a/include/bitcoin/node/impl/sessions/session_peer.ipp b/include/bitcoin/node/impl/sessions/session_peer.ipp index 6f77f77c..6fdf01ee 100644 --- a/include/bitcoin/node/impl/sessions/session_peer.ipp +++ b/include/bitcoin/node/impl/sessions/session_peer.ipp @@ -86,14 +86,6 @@ inline void CLASS::attach_protocols(const channel_ptr& channel) NOEXCEPT // Ready to relay blocks or block filters. const auto blocks_out = !delay || this->is_recent(); - - /////////////////////////////////////////////////////////////////////// - // bip152: "Upon receipt of a `sendcmpct` message with the first and - // second integers set to 1, the node SHOULD announce new blocks by - // sending a cmpctblock message." IOW at 70014 bip152 is optional. - // This allows the node to support bip157 without supporting bip152. - /////////////////////////////////////////////////////////////////////// - const auto peer = std::dynamic_pointer_cast(channel); // Node must advertise node_client_filters or no out filters. diff --git a/include/bitcoin/node/protocols/protocol_header_in_70014.hpp b/include/bitcoin/node/protocols/protocol_header_in_70014.hpp index 4706964c..d917784d 100644 --- a/include/bitcoin/node/protocols/protocol_header_in_70014.hpp +++ b/include/bitcoin/node/protocols/protocol_header_in_70014.hpp @@ -45,8 +45,10 @@ class BCN_API protocol_header_in_70014 void start() NOEXCEPT override; protected: - using compact_block = network::messages::peer::compact_block; using compact_transactions = network::messages::peer::compact_transactions; + using compact_block = network::messages::peer::compact_block; + using short_id_t = system::chain::short_id::integer; + using short_ids_t = std::vector; /// A block being filled from its compact block. struct fill @@ -56,7 +58,7 @@ class BCN_API protocol_header_in_70014 system::hash_digest hash{}; system::chain::header::cptr header{}; system::siphash_key key{}; - std::vector short_ids{}; + short_ids_t short_ids{}; system::chain::transaction_cptrs txs{}; database::tx_links links{}; std::vector missing{}; @@ -80,18 +82,18 @@ class BCN_API protocol_header_in_70014 const system::hash_digest& hash, size_t height) NOEXCEPT; private: - bool decode(fill& block, const compact_block& message) NOEXCEPT; bool scan(fill& block) NOEXCEPT; + bool is_malleated64(const fill& block) NOEXCEPT; + bool decode(fill& block, const compact_block& message) NOEXCEPT; bool to_hashes(system::hashes& txids, system::hashes& wtxids, const fill& block) NOEXCEPT; - bool is_malleated64(const fill& block) NOEXCEPT; - void collect(const compact_block& message, - const database::header_link& link) NOEXCEPT; void request() NOEXCEPT; void identify() NOEXCEPT; bool resolve(const database::header_link& link) NOEXCEPT; bool matched(const database::header_link& link) const NOEXCEPT; + void collect(const compact_block& message, + const database::header_link& link) NOEXCEPT; // These are protected by strand. bool compact_{}; diff --git a/include/bitcoin/node/protocols/protocol_peer.hpp b/include/bitcoin/node/protocols/protocol_peer.hpp index 49449ee2..9fb38d8c 100644 --- a/include/bitcoin/node/protocols/protocol_peer.hpp +++ b/include/bitcoin/node/protocols/protocol_peer.hpp @@ -83,13 +83,6 @@ class BCN_API protocol_peer /// Determine if outgoing block or tx was previously announced by peer. virtual bool was_announced(const system::hash_digest&) const NOEXCEPT; - /// Compact blocks (bip152 version 2). - /// ----------------------------------------------------------------------- - - /// Compact block of an associated block (coinbase prefilled), or null. - virtual network::messages::peer::compact_block::cptr make_compact_block( - const database::header_link& link) const NOEXCEPT; - /// Currency. /// ----------------------------------------------------------------------- @@ -108,6 +101,13 @@ class BCN_API protocol_peer virtual void notify_one(object_key key, const code& ec, event_value value) const NOEXCEPT; + /// Compact blocks (bip152 version 2). + /// ----------------------------------------------------------------------- + + /// Compact block of an associated block (coinbase prefilled), or null. + virtual network::messages::peer::compact_block::cptr make_compact_block( + const database::header_link& link) const NOEXCEPT; + private: // This derived channel requires stranded calls, base is thread safe. const node::channel_peer::ptr channel_; diff --git a/src/protocols/protocol_block_out_70014.cpp b/src/protocols/protocol_block_out_70014.cpp index 1f3235fb..4a9c8e95 100644 --- a/src/protocols/protocol_block_out_70014.cpp +++ b/src/protocols/protocol_block_out_70014.cpp @@ -75,8 +75,7 @@ bool protocol_block_out_70014::handle_receive_get_data(const code& ec, const auto block = make_compact_block(link); if (!block) { - LOGF("Compact block " << encode_hash(item.hash) - << " not obtained."); + LOGF("Compact block " << encode_hash(item.hash) << " not made."); continue; } diff --git a/src/protocols/protocol_header_in_70014.cpp b/src/protocols/protocol_header_in_70014.cpp index 2d2401cc..f45bd338 100644 --- a/src/protocols/protocol_header_in_70014.cpp +++ b/src/protocols/protocol_header_in_70014.cpp @@ -57,13 +57,14 @@ void protocol_header_in_70014::complete() NOEXCEPT { BC_ASSERT(stranded()); + // TODO: this is hardwired in handshake. + constexpr auto version = send_compact::compact_version_2; + if (!compact_ && is_current_chain(true)) { compact_ = true; - SEND((send_compact{ true, send_compact::compact_version_2 }), - handle_send, _1); - LOGP("Subscribed to compact block announcements at [" << opposite() - << "]."); + SEND((send_compact{ true, version }), handle_send, _1); + LOGP("Subscribed to compact blocks at [" << opposite() << "]."); } protocol_header_in_70012::complete(); @@ -100,8 +101,8 @@ bool protocol_header_in_70014::handle_receive_compact_block(const code& ec, if (link.is_terminal()) { - organize_compact(header, BIND(handle_organize_compact, _1, _2, - message)); + organize_compact(header, + BIND(handle_organize_compact, _1, _2, message)); return true; } @@ -169,12 +170,14 @@ void protocol_header_in_70014::collect(const compact_block& message, return; } - fill block{}; - block.link = link; - block.height = ctx.height; - block.header = message.header_ptr; - block.hash = message.header_ptr->get_hash(); - block.key = chain::short_id::to_key(*message.header_ptr, message.nonce); + fill block + { + .link = link, + .height = ctx.height, + .hash = message.header_ptr->get_hash(), + .header = message.header_ptr, + .key = chain::short_id::to_key(*message.header_ptr, message.nonce) + }; if (!decode(block, message)) { @@ -206,14 +209,14 @@ bool protocol_header_in_70014::decode(fill& block, const compact_block& message) NOEXCEPT { BC_ASSERT(stranded()); - const auto& items = message.transactions; const auto& ids = message.short_ids; + const auto& items = message.transactions; const auto count = ids.size() + items.size(); if (is_zero(count) || (count > chain::max_block_size)) return false; - block.short_ids.resize(count); block.txs.resize(count); + block.short_ids.resize(count); block.links.assign(count, database::tx_link::terminal); size_t position{}; @@ -243,7 +246,7 @@ bool protocol_header_in_70014::decode(fill& block, bool protocol_header_in_70014::scan(fill& block) NOEXCEPT { BC_ASSERT(stranded()); - std::vector short_ids{}; + short_ids_t short_ids{}; std::vector positions{}; for (size_t position{}; position < block.txs.size(); ++position) { @@ -328,15 +331,25 @@ bool protocol_header_in_70014::handle_receive_compact_transactions( { const auto& tx = txs.at(index); const auto position = block.missing.at(index); - if (!tx || (chain::short_id::to_id(block.key, tx->is_coinbase() ? - bitcoin_hash(tx->to_data(true)) : tx->get_hash(true)) != - block.short_ids.at(position))) + if (!tx) { LOGR("Invalid compact transaction from [" << opposite() << "]."); stop(network::error::protocol_violation); return false; } + // TODO: use fast streaming hash to avoid allocation. + const auto tx_hash = tx->is_coinbase() ? + bitcoin_hash(tx->to_data(true)) : tx->get_hash(true); + + if (chain::short_id::to_id(block.key, tx_hash) != + block.short_ids.at(position)) + { + LOGR("Invalid compact short id from [" << opposite() << "]."); + stop(network::error::protocol_violation); + return false; + } + block.txs.at(position) = tx; } @@ -351,10 +364,11 @@ bool protocol_header_in_70014::handle_receive_compact_transactions( void protocol_header_in_70014::identify() NOEXCEPT { BC_ASSERT(stranded()); - auto& block = *fill_; - hashes txids{}, wtxids{}; + auto& block = *fill_; chain::context ctx{}; + hashes txids{}, wtxids{}; + if (!to_hashes(txids, wtxids, block) || !archive().get_context(ctx, block.link)) { @@ -369,6 +383,7 @@ void protocol_header_in_70014::identify() NOEXCEPT const auto& root = block.header->merkle_root(); const auto segregated = !std::equal(txids.cbegin(), txids.cend(), wtxids.cbegin()); + const auto witness_root = sha256::merkle_root(std::move(wtxids)); if ((ec = chain::block::identify(root, txids, is_malleated64(block))) || (ec = chain::block::identify(ctx, first, witness_root, segregated))) @@ -398,6 +413,7 @@ bool protocol_header_in_70014::to_hashes(hashes& txids, hashes& wtxids, const fill& block) NOEXCEPT { BC_ASSERT(stranded()); + const auto& query = archive(); const auto count = block.txs.size(); if (!block.txs.front()) @@ -429,6 +445,7 @@ bool protocol_header_in_70014::to_hashes(hashes& txids, hashes& wtxids, bool protocol_header_in_70014::is_malleated64(const fill& block) NOEXCEPT { BC_ASSERT(stranded()); + const auto& query = archive(); auto malleated = !block.txs.front()->is_coinbase(); for (size_t at{}; malleated && at < block.txs.size(); ++at) diff --git a/src/protocols/protocol_header_out_70014.cpp b/src/protocols/protocol_header_out_70014.cpp index 21f31e4f..a8c2a177 100644 --- a/src/protocols/protocol_header_out_70014.cpp +++ b/src/protocols/protocol_header_out_70014.cpp @@ -66,8 +66,9 @@ bool protocol_header_out_70014::do_announce(header_t link) NOEXCEPT if (const auto message = make_compact_block(link)) { - LOGN("Announce compact ..." << encode_hash(hash).substr( - hash_size - 8, 8) << " to [" << opposite() << "]."); + LOGN("Announce compact ..." + << encode_hash(hash).substr(hash_size - 8, 8) << " to [" + << opposite() << "]."); NOTIFY(*message, handle_send, _1); return true; } @@ -95,6 +96,8 @@ bool protocol_header_out_70014::handle_receive_send_compact(const code& ec, announce(); } + // TODO: drop peer on non-negotiated version. + return true; } diff --git a/src/protocols/protocol_peer.cpp b/src/protocols/protocol_peer.cpp index ecc839f2..9159440c 100644 --- a/src/protocols/protocol_peer.cpp +++ b/src/protocols/protocol_peer.cpp @@ -60,6 +60,38 @@ code protocol_peer::fault(const code& ec) NOEXCEPT return ec; } +// Announcements. +// ---------------------------------------------------------------------------- + +void protocol_peer::set_announced(const system::hash_digest& hash) NOEXCEPT +{ + channel_->set_announced(hash); +} + +void protocol_peer::set_current(bool value) NOEXCEPT +{ + channel_->set_current(value); +} + +bool protocol_peer::was_announced(const system::hash_digest& hash) const NOEXCEPT +{ + return channel_->was_announced(hash); +} + +// Events notification. +// ---------------------------------------------------------------------------- + +void protocol_peer::notify(const code& ec, event_value value) const NOEXCEPT +{ + session_->notify(ec, value); +} + +void protocol_peer::notify_one(object_key key, const code& ec, + event_value value) const NOEXCEPT +{ + session_->notify_one(key, ec, value); +} + // Compact blocks (bip152 version 2). // ---------------------------------------------------------------------------- @@ -83,7 +115,7 @@ network::messages::peer::compact_block::cptr protocol_peer::make_compact_block( compact_block::short_id_list ids{}; ids.reserve(sub1(wtxids.size())); - for (auto it = std::next(wtxids.begin()); it != wtxids.end(); ++it) + for (auto it = std::next(wtxids.cbegin()); it != wtxids.cend(); ++it) { if (*it == null_hash) return {}; @@ -101,37 +133,5 @@ network::messages::peer::compact_block::cptr protocol_peer::make_compact_block( }); } -// Announcements. -// ---------------------------------------------------------------------------- - -void protocol_peer::set_announced(const system::hash_digest& hash) NOEXCEPT -{ - channel_->set_announced(hash); -} - -void protocol_peer::set_current(bool value) NOEXCEPT -{ - channel_->set_current(value); -} - -bool protocol_peer::was_announced(const system::hash_digest& hash) const NOEXCEPT -{ - return channel_->was_announced(hash); -} - -// Events notification. -// ---------------------------------------------------------------------------- - -void protocol_peer::notify(const code& ec, event_value value) const NOEXCEPT -{ - session_->notify(ec, value); -} - -void protocol_peer::notify_one(object_key key, const code& ec, - event_value value) const NOEXCEPT -{ - session_->notify_one(key, ec, value); -} - } // namespace node } // namespace libbitcoin