diff --git a/builds/cmake/install-cmake.sh b/builds/cmake/install-cmake.sh index 053de1a55..b4f49ffd6 100755 --- a/builds/cmake/install-cmake.sh +++ b/builds/cmake/install-cmake.sh @@ -84,8 +84,6 @@ # Default: ON # -Dwith-valgrind= Use valgrind memcheck client requests. # Default: OFF -# -Dwith-verbose= Compile verbose logging. -# Default: OFF # # All unrecognized options provided shall be passed as configuration # options for all dependencies. @@ -1105,8 +1103,6 @@ help() msg " Default: ON" msg "-Dwith-valgrind= Use valgrind memcheck client requests." msg " Default: OFF" - msg "-Dwith-verbose= Compile verbose logging." - msg " Default: OFF" msg "" msg "All unrecognized options provided shall be passed as configuration" msg "options for all dependencies." diff --git a/builds/cmake/install-presets.sh b/builds/cmake/install-presets.sh index 4fb431640..5b21d6e73 100755 --- a/builds/cmake/install-presets.sh +++ b/builds/cmake/install-presets.sh @@ -74,8 +74,6 @@ # Default: ON # -Dwith-valgrind= Use valgrind memcheck client requests. # Default: OFF -# -Dwith-verbose= Compile verbose logging. -# Default: OFF # # All unrecognized options provided shall be passed as configuration # options for all dependencies. @@ -1160,8 +1158,6 @@ help() msg " Default: ON" msg "-Dwith-valgrind= Use valgrind memcheck client requests." msg " Default: OFF" - msg "-Dwith-verbose= Compile verbose logging." - msg " Default: OFF" msg "" msg "All unrecognized options provided shall be passed as configuration" msg "options for all dependencies." diff --git a/builds/gnu/Makefile.am b/builds/gnu/Makefile.am index 820faf625..9e5e4910a 100644 --- a/builds/gnu/Makefile.am +++ b/builds/gnu/Makefile.am @@ -58,6 +58,7 @@ src_libbitcoin_node_la_SOURCES = \ ${srcdir}/../../src/error.cpp \ ${srcdir}/../../src/estimator.cpp \ ${srcdir}/../../src/full_node.cpp \ + ${srcdir}/../../src/rolling_bloom_filter.cpp \ ${srcdir}/../../src/settings.cpp \ ${srcdir}/../../src/channels/channel_peer.cpp \ ${srcdir}/../../src/chasers/chaser.cpp \ @@ -141,6 +142,7 @@ include_bitcoin_node_HEADERS = \ ${srcdir}/../../include/bitcoin/node/events.hpp \ ${srcdir}/../../include/bitcoin/node/full_node.hpp \ ${srcdir}/../../include/bitcoin/node/preprocessor.hpp \ + ${srcdir}/../../include/bitcoin/node/rolling_bloom_filter.hpp \ ${srcdir}/../../include/bitcoin/node/settings.hpp \ include/bitcoin/node/version.hpp @@ -244,6 +246,7 @@ test_libbitcoin_node_test_SOURCES = \ ${srcdir}/../../test/estimator.cpp \ ${srcdir}/../../test/full_node.cpp \ ${srcdir}/../../test/main.cpp \ + ${srcdir}/../../test/rolling_bloom_filter.cpp \ ${srcdir}/../../test/settings.cpp \ ${srcdir}/../../test/test.cpp \ ${srcdir}/../../test/chasers/chaser.cpp \ diff --git a/builds/gnu/install-gnu.sh b/builds/gnu/install-gnu.sh index 6a4078813..0059a398e 100755 --- a/builds/gnu/install-gnu.sh +++ b/builds/gnu/install-gnu.sh @@ -85,8 +85,6 @@ # Default: --with-secp256k1 # ---valgrind Use valgrind memcheck client requests. # Default: --without-valgrind -# ---verbose Compile verbose logging. -# Default: --without-verbose # # All unrecognized options provided shall be passed as configuration # options for all dependencies. @@ -1179,8 +1177,6 @@ help() msg " Default: --with-secp256k1" msg "---valgrind Use valgrind memcheck client requests." msg " Default: --without-valgrind" - msg "---verbose Compile verbose logging." - msg " Default: --without-verbose" msg "" msg "All unrecognized options provided shall be passed as configuration" msg "options for all dependencies." diff --git a/builds/msvc/vs2026/libbitcoin-network.import.props b/builds/msvc/vs2026/libbitcoin-network.import.props index baeb82da3..f95585518 100644 --- a/builds/msvc/vs2026/libbitcoin-network.import.props +++ b/builds/msvc/vs2026/libbitcoin-network.import.props @@ -11,18 +11,6 @@ - - - - false - - - - - - - - @@ -30,7 +18,6 @@ $(ProjectDir)..\..\..\..\..\libbitcoin-network\include\;%(AdditionalIncludeDirectories) $(ProjectDir)..\..\..\..\..\libbitcoin-network\build\include\;%(AdditionalIncludeDirectories) BCT_STATIC;%(PreprocessorDefinitions) - WITH_LOGV;%(PreprocessorDefinitions) libbitcoin-network.lib;%(AdditionalDependencies) diff --git a/builds/msvc/vs2026/libbitcoin-network.import.xml b/builds/msvc/vs2026/libbitcoin-network.import.xml index 73e57e10b..7bcfea233 100644 --- a/builds/msvc/vs2026/libbitcoin-network.import.xml +++ b/builds/msvc/vs2026/libbitcoin-network.import.xml @@ -14,16 +14,4 @@ - - - - - - - - - - - - \ No newline at end of file diff --git a/builds/msvc/vs2026/libbitcoin-node-test/libbitcoin-node-test.vcxproj b/builds/msvc/vs2026/libbitcoin-node-test/libbitcoin-node-test.vcxproj index d1a9d2d15..f54709a7b 100644 --- a/builds/msvc/vs2026/libbitcoin-node-test/libbitcoin-node-test.vcxproj +++ b/builds/msvc/vs2026/libbitcoin-node-test/libbitcoin-node-test.vcxproj @@ -115,6 +115,7 @@ + diff --git a/builds/msvc/vs2026/libbitcoin-node-test/libbitcoin-node-test.vcxproj.filters b/builds/msvc/vs2026/libbitcoin-node-test/libbitcoin-node-test.vcxproj.filters index d66215d50..6dd6efb47 100644 --- a/builds/msvc/vs2026/libbitcoin-node-test/libbitcoin-node-test.vcxproj.filters +++ b/builds/msvc/vs2026/libbitcoin-node-test/libbitcoin-node-test.vcxproj.filters @@ -87,6 +87,9 @@ src\protocols + + src + src\sessions diff --git a/builds/msvc/vs2026/libbitcoin-node/libbitcoin-node.vcxproj b/builds/msvc/vs2026/libbitcoin-node/libbitcoin-node.vcxproj index cf70bde96..5f83ffca3 100644 --- a/builds/msvc/vs2026/libbitcoin-node/libbitcoin-node.vcxproj +++ b/builds/msvc/vs2026/libbitcoin-node/libbitcoin-node.vcxproj @@ -159,6 +159,7 @@ + @@ -217,6 +218,7 @@ + diff --git a/builds/msvc/vs2026/libbitcoin-node/libbitcoin-node.vcxproj.filters b/builds/msvc/vs2026/libbitcoin-node/libbitcoin-node.vcxproj.filters index 0c742e30a..cc479dd57 100644 --- a/builds/msvc/vs2026/libbitcoin-node/libbitcoin-node.vcxproj.filters +++ b/builds/msvc/vs2026/libbitcoin-node/libbitcoin-node.vcxproj.filters @@ -189,6 +189,9 @@ src\protocols + + src + src\sessions @@ -359,6 +362,9 @@ include\bitcoin\node\protocols + + include\bitcoin\node + include\bitcoin\node\sessions diff --git a/include/bitcoin/node.hpp b/include/bitcoin/node.hpp index a8b7632df..f174b9f8f 100644 --- a/include/bitcoin/node.hpp +++ b/include/bitcoin/node.hpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include diff --git a/include/bitcoin/node/channels/channel_peer.hpp b/include/bitcoin/node/channels/channel_peer.hpp index 97324d562..e4930c8e4 100644 --- a/include/bitcoin/node/channels/channel_peer.hpp +++ b/include/bitcoin/node/channels/channel_peer.hpp @@ -19,10 +19,10 @@ #ifndef LIBBITCOIN_NODE_CHANNELS_CHANNEL_PEER_HPP #define LIBBITCOIN_NODE_CHANNELS_CHANNEL_PEER_HPP -#include #include #include #include +#include namespace libbitcoin { namespace node { @@ -42,12 +42,14 @@ class BCN_API channel_peer : node::channel(log, socket, identifier, config), network::channel_peer(log, socket, identifier, config.network, options), network::tracker(log), - witness_(config.node.require_witness), - announcement_cache_(config.node.announcement_cache) + witness_(config.node.require_witness) { } + /// Record a block or tx hash known to the peer. void set_announced(const system::hash_digest& hash) NOEXCEPT; + + /// The hash is (probably) known to the peer. bool was_announced(const system::hash_digest& hash) const NOEXCEPT; protected: @@ -61,12 +63,11 @@ class BCN_API channel_peer } private: - // These are thread safe (const). + // This is thread safe (const). const bool witness_; - const size_t announcement_cache_; // This is protected by strand. - std::deque announced_{}; + rolling_bloom_filter announced_{}; }; } // namespace node diff --git a/include/bitcoin/node/protocols/protocol_transaction_out_106.hpp b/include/bitcoin/node/protocols/protocol_transaction_out_106.hpp index a887119e8..479a411b3 100644 --- a/include/bitcoin/node/protocols/protocol_transaction_out_106.hpp +++ b/include/bitcoin/node/protocols/protocol_transaction_out_106.hpp @@ -81,11 +81,22 @@ class BCN_API protocol_transaction_out_106 const network::messages::peer::get_data::cptr& message, const gate_t::ptr& gate) NOEXCEPT; + /// Queue the tx for announcement, unless known to the peer. virtual bool announce(const system::hash_digest& hash) NOEXCEPT; + /// Announce the queued txs unless an announcement is being written. + virtual void do_flush() NOEXCEPT; + virtual void handle_flush(const code& ec) NOEXCEPT; + private: + void send_queued() NOEXCEPT; + // These are thread safe. const bool node_witness_; + + // These are protected by strand. + inventory_items queued_{}; + bool writing_{}; }; } // namespace node diff --git a/include/bitcoin/node/rolling_bloom_filter.hpp b/include/bitcoin/node/rolling_bloom_filter.hpp new file mode 100644 index 000000000..0d1e4894a --- /dev/null +++ b/include/bitcoin/node/rolling_bloom_filter.hpp @@ -0,0 +1,67 @@ +/** + * Copyright (c) 2011-2026 libbitcoin developers + * + * This file is part of libbitcoin. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +#ifndef LIBBITCOIN_NODE_ROLLING_BLOOM_FILTER_HPP +#define LIBBITCOIN_NODE_ROLLING_BLOOM_FILTER_HPP + +#include + +namespace libbitcoin { +namespace node { + +/// Not thread safe rolling bloom filter of hashes, by generation. +class BCN_API rolling_bloom_filter +{ +public: + /// Insert the hash, replacing the oldest generation when current is full. + void insert(const system::hash_digest& hash) NOEXCEPT; + + /// The hash is (probably) inserted. + bool contains(const system::hash_digest& hash) const NOEXCEPT; + +private: + static uint64_t entropy(const system::hash_digest& hash) NOEXCEPT; + + // Retains at least the most recent (generations - 1) * generation. + static constexpr auto generations = 3_size; + static constexpr auto generation = network::messages::peer::max_inventory; + + // False positive rate of about 2^-probes per generation. + static constexpr auto probes = 20_size; + + // Optimal bits per generation, generation * probes / ln(2). + static constexpr auto micro = 1'000'000_u64; + static constexpr auto ln2_micro = 693'147_u64; + static constexpr auto load = system::multiply(generation, probes); + static constexpr auto scaled = system::multiply(load, micro); + static constexpr auto rounded = system::ceilinged_divide(scaled, ln2_micro); + static constexpr auto optimal = system::possible_narrow_cast(rounded); + + using filter = system::bloom_array; + using table = std::shared_ptr; + + // These are not thread safe. + std::array known_{}; + size_t current_{}; + size_t count_{}; +}; + +} // namespace node +} // namespace libbitcoin + +#endif diff --git a/include/bitcoin/node/settings.hpp b/include/bitcoin/node/settings.hpp index 01b914b85..6835b2efa 100644 --- a/include/bitcoin/node/settings.hpp +++ b/include/bitcoin/node/settings.hpp @@ -57,7 +57,6 @@ class BCN_API settings uint64_t batch_verify; uint64_t batch_silent; uint16_t current_connections; - uint16_t announcement_cache; uint16_t fee_estimate_horizon; uint32_t maximum_height; uint32_t maximum_concurrency; diff --git a/src/channels/channel_peer.cpp b/src/channels/channel_peer.cpp index af78887af..6ce6a275c 100644 --- a/src/channels/channel_peer.cpp +++ b/src/channels/channel_peer.cpp @@ -32,20 +32,13 @@ BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT) void channel_peer::set_announced(const hash_digest& hash) NOEXCEPT { BC_ASSERT(stranded()); - if (is_zero(announcement_cache_)) - return; - - if (announced_.size() == announcement_cache_) - announced_.pop_front(); - - announced_.push_back(hash); + announced_.insert(hash); } bool channel_peer::was_announced(const hash_digest& hash) const NOEXCEPT { BC_ASSERT(stranded()); - return std::find(announced_.begin(), announced_.end(), hash) != - announced_.end(); + return announced_.contains(hash); } BC_POP_WARNING() diff --git a/src/protocols/protocol_transaction_out_106.cpp b/src/protocols/protocol_transaction_out_106.cpp index fa8379800..8110b07ec 100644 --- a/src/protocols/protocol_transaction_out_106.cpp +++ b/src/protocols/protocol_transaction_out_106.cpp @@ -132,12 +132,53 @@ bool protocol_transaction_out_106::announce(const hash_digest& hash) NOEXCEPT return true; } + // Txs queued on the strand ahead of the flush coalesce into its inv. + if (queued_.empty() && !writing_) + { + POST(do_flush); + } + // bip144: get_data uses witness type_id but inv does not. - const inventory inv{ { { inventory_type(), hash } } }; - NOTIFY(inv, handle_send, _1); + queued_.emplace_back(inventory_type(), hash); + + if (queued_.size() == max_inventory) + send_queued(); + return true; } +void protocol_transaction_out_106::do_flush() NOEXCEPT +{ + BC_ASSERT(stranded()); + + if (stopped() || writing_ || queued_.empty()) + return; + + send_queued(); +} + +void protocol_transaction_out_106::handle_flush(const code& ec) NOEXCEPT +{ + BC_ASSERT(stranded()); + + if (stopped(ec)) + return; + + writing_ = false; + do_flush(); +} + +// private +void protocol_transaction_out_106::send_queued() NOEXCEPT +{ + BC_ASSERT(stranded()); + + auto items = std::move(queued_); + queued_.clear(); + writing_ = true; + NOTIFY(inventory{ std::move(items) }, handle_flush, _1); +} + // Inbound (get_data). // ---------------------------------------------------------------------------- diff --git a/src/rolling_bloom_filter.cpp b/src/rolling_bloom_filter.cpp new file mode 100644 index 000000000..a4df6b681 --- /dev/null +++ b/src/rolling_bloom_filter.cpp @@ -0,0 +1,67 @@ +/** + * Copyright (c) 2011-2026 libbitcoin developers + * + * This file is part of libbitcoin. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +#include + +#include + +namespace libbitcoin { +namespace node { + +using namespace system; + +BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT) + +void rolling_bloom_filter::insert(const hash_digest& hash) NOEXCEPT +{ + if (count_ == generation) + { + current_ = add1(current_) % generations; + known_[current_].reset(); + count_ = zero; + } + + auto& known = known_[current_]; + if (!known) + known = emplace_shared(); + + filter::screen(*known, entropy(hash)); + ++count_; +} + +bool rolling_bloom_filter::contains(const hash_digest& hash) const NOEXCEPT +{ + const auto value = entropy(hash); + const auto screened = [&](const table& known) NOEXCEPT + { + return known && filter::is_screened(*known, value); + }; + + return std::any_of(known_.cbegin(), known_.cend(), screened); +} + +// private +uint64_t rolling_bloom_filter::entropy(const hash_digest& hash) NOEXCEPT +{ + return unsafe_from_little_endian(hash.data()); +} + +BC_POP_WARNING() + +} // namespace node +} // namespace libbitcoin diff --git a/src/settings.cpp b/src/settings.cpp index 2a3e92371..ffa2540b5 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -50,7 +50,6 @@ settings::settings() NOEXCEPT batch_verify{ 1'000'000 }, batch_silent{ 1'000'000 }, current_connections{ 10 }, - announcement_cache{ 42 }, fee_estimate_horizon{ 0 }, ////snapshot_bytes{ 200'000'000'000 }, ////snapshot_valid{ 250'000 }, diff --git a/test/channel_peer.cpp b/test/channel_peer.cpp index c9c47b6f4..263cbb7e6 100644 --- a/test/channel_peer.cpp +++ b/test/channel_peer.cpp @@ -26,25 +26,8 @@ struct channel_peer_setup_fixture { DELETE_COPY_MOVE(channel_peer_setup_fixture); - channel_peer_setup_fixture(uint16_t announcement_cache) - : config_{ test::node_configuration() }, - channel_ - { - [&]() NOEXCEPT - { - config_.node.announcement_cache = announcement_cache; - network::socket::parameters params - { - .maximum_request = 42, - .maximum_buffer = 42 - }; - - const auto socket = std::make_shared(log_, - pool_.service(), std::move(params)); - return std::make_shared(log_, socket, 42, - config_, options); - }() - } + channel_peer_setup_fixture() + : config_{ test::node_configuration() }, channel_{ make_channel() } { } @@ -55,18 +38,49 @@ struct channel_peer_setup_fixture BC_REQUIRE(pool_.join()); } - /// Announce first then second, report whether each was announced. - std::pair announce(const system::hash_digest& first, - const system::hash_digest& second) + node::channel_peer::ptr make_channel() NOEXCEPT + { + using namespace system; + using socket = network::socket; + using channel = node::channel_peer; + socket::parameters params + { + .maximum_request = 42, + .maximum_buffer = 42 + }; + + auto& io = pool_.service(); + const auto wire = emplace_shared(log_, io, std::move(params)); + return emplace_shared(log_, wire, 42, config_, options); + } + + // A distinct uniform hash for each index. + static system::hash_digest hash(size_t index) + { + using namespace system; + return sha256_hash(to_little_endian(index)); + } + + // Announce the hash on the channel strand. + void announce(const system::hash_digest& hash) { - std::promise> promise{}; + std::promise promise{}; network::io::post(channel_->strand(), [&]() NOEXCEPT { - channel_->set_announced(first); - channel_->set_announced(second); - const auto announced1 = channel_->was_announced(first); - const auto announced2 = channel_->was_announced(second); - promise.set_value({ announced1, announced2 }); + channel_->set_announced(hash); + promise.set_value(true); + }); + + BC_REQUIRE(promise.get_future().get()); + } + + // The hash is reported announced on the channel strand. + bool announced(const system::hash_digest& hash) + { + std::promise promise{}; + network::io::post(channel_->strand(), [&]() NOEXCEPT + { + promise.set_value(channel_->was_announced(hash)); }); return promise.get_future().get(); @@ -81,57 +95,19 @@ struct channel_peer_setup_fixture node::channel_peer::ptr channel_; }; -struct channel_peer_disabled_setup_fixture - : channel_peer_setup_fixture -{ - channel_peer_disabled_setup_fixture() - : channel_peer_setup_fixture(0) - { - } -}; - -struct channel_peer_one_setup_fixture - : channel_peer_setup_fixture -{ - channel_peer_one_setup_fixture() - : channel_peer_setup_fixture(1) - { - } -}; - -struct channel_peer_two_setup_fixture - : channel_peer_setup_fixture -{ - channel_peer_two_setup_fixture() - : channel_peer_setup_fixture(2) - { - } -}; - -static const system::hash_digest first{ 0x01 }; -static const system::hash_digest second{ 0x02 }; - -BC_AUTO_TEST_SUITE(channel_peer_tests) - -BC_FIXTURE_TEST_CASE(channel_peer__set_announced__zero_cache__not_announced, channel_peer_disabled_setup_fixture) -{ - const auto announced = announce(first, second); - BC_REQUIRE(!announced.first); - BC_REQUIRE(!announced.second); -} +BC_FIXTURE_TEST_SUITE(channel_peer_tests, channel_peer_setup_fixture) -BC_FIXTURE_TEST_CASE(channel_peer__set_announced__full_cache__oldest_evicted, channel_peer_one_setup_fixture) +BC_AUTO_TEST_CASE(channel_peer__was_announced__empty__false) { - const auto announced = announce(first, second); - BC_REQUIRE(!announced.first); - BC_REQUIRE(announced.second); + const auto item = hash(0); + BC_REQUIRE(!announced(item)); } -BC_FIXTURE_TEST_CASE(channel_peer__set_announced__cache_not_full__both_announced, channel_peer_two_setup_fixture) +BC_AUTO_TEST_CASE(channel_peer__set_announced__one__announced) { - const auto announced = announce(first, second); - BC_REQUIRE(announced.first); - BC_REQUIRE(announced.second); + const auto item = hash(0); + announce(item); + BC_REQUIRE(announced(item)); } BC_AUTO_TEST_SUITE_END() diff --git a/test/chasers/chaser_confirm.cpp b/test/chasers/chaser_confirm.cpp index 9f56f2d91..adbbf0a82 100644 --- a/test/chasers/chaser_confirm.cpp +++ b/test/chasers/chaser_confirm.cpp @@ -835,7 +835,7 @@ BC_FIXTURE_TEST_CASE(chaser_confirm__start__immature_spend__rolled_back_to_fork_ BC_FIXTURE_TEST_CASE(chaser_confirm__start__immature_spend_fork__rolled_back, confirm_immature_fork_fixture) { BC_REQUIRE(stated(confirm_e2(), database::is_unconfirmable)); - BC_REQUIRE_MESSAGE(events_.await(chase::organized, 2), events_.dump()); + BC_REQUIRE_MESSAGE(events_.await(chase::unconfirmable), events_.dump()); BC_REQUIRE(confirmed(confirm_c1(), 1)); BC_REQUIRE_EQUAL(query_.get_top_confirmed(), 1u); BC_REQUIRE(!query_.is_confirmed_block(link(confirm_d1()))); @@ -894,7 +894,7 @@ BC_FIXTURE_TEST_CASE(chaser_confirm__start__bypass_candidate__organized_unvalida BC_REQUIRE(!query_.is_confirmable(link(confirm_d1()))); BC_REQUIRE(stated(confirm_d2(), database::is_confirmable)); BC_REQUIRE(query_.is_filtered_head(link(confirm_d1()))); - BC_REQUIRE_EQUAL(events_.count(chase::confirmable), 2u); + BC_REQUIRE_MESSAGE(events_.await(chase::confirmable, 2), events_.dump()); } BC_FIXTURE_TEST_CASE(chaser_confirm__start__unfiltered_candidate__not_organized, confirm_unfiltered_fixture) diff --git a/test/functional/p2p_setup_fixture.cpp b/test/functional/p2p_setup_fixture.cpp index f9e8f192b..e16ae5f53 100644 --- a/test/functional/p2p_setup_fixture.cpp +++ b/test/functional/p2p_setup_fixture.cpp @@ -99,7 +99,7 @@ p2p_setup_fixture::p2p_setup_fixture(const initializer& setup, // Block until the node is running. ec = running.get_future().get(); BC_REQUIRE_MESSAGE(!ec, ec.message()); - REQUIRE_SUCCESS(socket_, connect, config_.network.inbound.binds.back().to_endpoint()); + REQUIRE_SUCCESS(socket_, connect, node_.binding()); } p2p_setup_fixture::~p2p_setup_fixture() diff --git a/test/functional/p2p_setup_fixture.hpp b/test/functional/p2p_setup_fixture.hpp index 1d0999bdb..93ed6b507 100644 --- a/test/functional/p2p_setup_fixture.hpp +++ b/test/functional/p2p_setup_fixture.hpp @@ -22,9 +22,41 @@ #include "../test.hpp" #include #include +#include #include -#define P2P_FUNCTIONAL_ENDPOINT "127.0.0.1:65009" +#define P2P_FUNCTIONAL_ENDPOINT "127.0.0.1" + +// A full node that retains its peer acceptor, for its bound endpoint. +class bound_node + : public full_node +{ +public: + using full_node::full_node; + + /// The endpoint to which the peer acceptor is bound (requires run). + network::io::endpoint binding() NOEXCEPT + { + std::promise bound{}; + network::io::post(strand(), [&]() NOEXCEPT + { + bound.set_value(acceptor_->local().to_endpoint()); + }); + + return bound.get_future().get(); + } + +protected: + network::acceptor::ptr create_acceptor( + const network::socket::context& context) NOEXCEPT override + { + acceptor_ = full_node::create_acceptor(context); + return acceptor_; + } + +private: + network::acceptor::ptr acceptor_{}; +}; // Runs a real full node accepting on loopback, with the test acting as the // remote peer over a raw blocking socket (framing via peer messages). @@ -112,7 +144,7 @@ struct p2p_setup_fixture node::store store_; node::query query_; network::logger log_{}; - full_node node_; + bound_node node_; private: network::io::context io_{}; diff --git a/test/rolling_bloom_filter.cpp b/test/rolling_bloom_filter.cpp new file mode 100644 index 000000000..3640a1991 --- /dev/null +++ b/test/rolling_bloom_filter.cpp @@ -0,0 +1,114 @@ +/** + * Copyright (c) 2011-2026 libbitcoin developers + * + * This file is part of libbitcoin. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +#include "test.hpp" + +BC_AUTO_TEST_SUITE(rolling_bloom_filter_tests) + +constexpr auto generation = network::messages::peer::max_inventory; +constexpr auto generations2 = system::multiply(two, generation); +constexpr auto generations3 = system::multiply(3_size, generation); + +// A distinct uniform hash for each index. +static system::hash_digest to_hash(size_t index) +{ + using namespace system; + return sha256_hash(to_little_endian(index)); +} + +// The hashes of [first, first + count). +static system::hashes to_hashes(size_t first, size_t count) +{ + std_vector indexes(count); + std::iota(indexes.begin(), indexes.end(), first); + system::hashes out(count); + std::ranges::transform(indexes, out.begin(), to_hash); + return out; +} + +// Insert each of the hashes. +static void insert(rolling_bloom_filter& filter, const system::hashes& items) +{ + std::ranges::for_each(items, [&](const system::hash_digest& item) NOEXCEPT + { + filter.insert(item); + }); +} + +// The number of the hashes contained. +static size_t contained(const rolling_bloom_filter& filter, + const system::hashes& items) +{ + const auto count = [&](size_t total, const auto& item) NOEXCEPT + { + return total + to_int(filter.contains(item)); + }; + + return std::accumulate(items.cbegin(), items.cend(), zero, count); +} + +BC_AUTO_TEST_CASE(rolling_bloom_filter__contains__empty__false) +{ + const rolling_bloom_filter filter{}; + const auto item = to_hash(0); + BC_REQUIRE(!filter.contains(item)); +} + +BC_AUTO_TEST_CASE(rolling_bloom_filter__insert__one__contained) +{ + const auto item = to_hash(0); + rolling_bloom_filter filter{}; + filter.insert(item); + BC_REQUIRE(filter.contains(item)); +} + +BC_AUTO_TEST_CASE(rolling_bloom_filter__insert__three_generations__all_contained) +{ + const auto items = to_hashes(0, generations3); + rolling_bloom_filter filter{}; + insert(filter, items); + BC_REQUIRE_EQUAL(contained(filter, items), generations3); +} + +BC_AUTO_TEST_CASE(rolling_bloom_filter__insert__over_three_generations__oldest_generation_evicted) +{ + const auto items = to_hashes(0, add1(generations3)); + const auto first = to_hashes(0, one); + const auto oldest = to_hashes(0, generation); + const auto newer = to_hashes(generation, add1(generations2)); + rolling_bloom_filter filter{}; + insert(filter, items); + + const auto first_count = contained(filter, first); + const auto oldest_count = contained(filter, oldest); + const auto newer_count = contained(filter, newer); + BC_REQUIRE(is_zero(first_count)); + BC_REQUIRE_LE(oldest_count, two); + BC_REQUIRE_EQUAL(newer_count, add1(generations2)); +} + +BC_AUTO_TEST_CASE(rolling_bloom_filter__contains__three_generations__false_positives_bounded) +{ + const auto items = to_hashes(0, generations3); + const auto absent = to_hashes(generations3, 1'000'000); + rolling_bloom_filter filter{}; + insert(filter, items); + BC_REQUIRE_LE(contained(filter, absent), 10_size); +} + +BC_AUTO_TEST_SUITE_END() diff --git a/test/sessions/session.cpp b/test/sessions/session.cpp index a267746c4..5e265924a 100644 --- a/test/sessions/session.cpp +++ b/test/sessions/session.cpp @@ -164,7 +164,9 @@ struct session_outbound_setup_fixture BC_REQUIRE(query_.initialize(config_.bitcoin.genesis_block)); const auto services = service::node_network | service::node_witness; - const auto entry = "127.0.0.1:65115/0/" + std::to_string(services); + const auto port = std::to_string(acceptor_.local_endpoint().port()); + const auto flags = std::to_string(services); + const auto entry = "127.0.0.1:" + port + "/0/" + flags; std::ofstream{ config_.network.file() } << entry << std::endl; std::promise started{}; @@ -222,8 +224,7 @@ struct session_outbound_setup_fixture protected: network::io::context io_{}; - const network::io::endpoint endpoint_{ test::loopback(65115) }; - network::io::acceptor acceptor_{ io_, endpoint_ }; + network::io::acceptor acceptor_{ io_, test::loopback() }; network::io::socket socket_{ io_ }; configuration config_; node::store store_; @@ -445,9 +446,9 @@ BC_AUTO_TEST_CASE(session__connect__listening_endpoint__version_received) { using namespace network::messages::peer; network::io::context io{}; - network::io::acceptor acceptor{ io, test::loopback(65113) }; + network::io::acceptor acceptor{ io, test::loopback() }; network::io::socket socket{ io }; - session_.connect(network::config::endpoint{ "127.0.0.1:65113" }); + session_.connect(network::config::endpoint{ acceptor.local_endpoint() }); REQUIRE_SUCCESS(acceptor, accept, socket); system::data_array head{}; @@ -460,7 +461,7 @@ BC_AUTO_TEST_CASE(session__connect__listening_endpoint__version_received) BC_AUTO_TEST_CASE(session__connect__refused_endpoint__handler_error) { std::promise promise{}; - session_.connect(network::config::endpoint{ "127.0.0.1:65114" }, [&](const code& ec, const network::channel::ptr&) NOEXCEPT + session_.connect(network::config::endpoint{ test::refuser() }, [&](const code& ec, const network::channel::ptr&) NOEXCEPT { promise.set_value(ec); return false; @@ -473,10 +474,10 @@ BC_AUTO_TEST_CASE(protocol__connect__listening_endpoint__version_received) { using namespace network::messages::peer; network::io::context io{}; - network::io::acceptor acceptor{ io, test::loopback(65116) }; + network::io::acceptor acceptor{ io, test::loopback() }; network::io::socket socket{ io }; session_protocol_accessor protocol{ std::make_shared(node_) }; - protocol.connect(network::config::endpoint{ "127.0.0.1:65116" }); + protocol.connect(network::config::endpoint{ acceptor.local_endpoint() }); REQUIRE_SUCCESS(acceptor, accept, socket); system::data_array head{}; @@ -490,7 +491,7 @@ BC_AUTO_TEST_CASE(protocol__connect__refused_endpoint__handler_error) { std::promise promise{}; session_protocol_accessor protocol{ std::make_shared(node_) }; - protocol.connect(network::config::endpoint{ "127.0.0.1:65117" }, [&](const code& ec, const network::channel::ptr&) NOEXCEPT + protocol.connect(network::config::endpoint{ test::refuser() }, [&](const code& ec, const network::channel::ptr&) NOEXCEPT { promise.set_value(ec); return false; diff --git a/test/settings.cpp b/test/settings.cpp index 30b9796c5..075f1badd 100644 --- a/test/settings.cpp +++ b/test/settings.cpp @@ -52,7 +52,6 @@ BC_AUTO_TEST_CASE(settings__node__default_context__expected) BC_REQUIRE_EQUAL(node.batch_verify, 1'000'000_u64); BC_REQUIRE_EQUAL(node.batch_silent, 1'000'000_u64); BC_REQUIRE_EQUAL(node.current_connections, 10_u16); - BC_REQUIRE_EQUAL(node.announcement_cache, 42_u16); BC_REQUIRE_EQUAL(node.fee_estimate_horizon, 0u); BC_REQUIRE_EQUAL(node.maximum_height, 0_u32); BC_REQUIRE_EQUAL(node.maximum_height_(), max_size_t); diff --git a/test/test.hpp b/test/test.hpp index 6c4d9297f..95a983dd2 100644 --- a/test/test.hpp +++ b/test/test.hpp @@ -139,6 +139,18 @@ inline network::io::endpoint loopback(uint16_t port=0) NOEXCEPT return { system::ip::address{ system::ip::ipv4{ { 127, 0, 0, 1 } } }, port }; } +// A loopback endpoint on an ephemeral port with no socket bound to it. +inline network::io::endpoint refuser() NOEXCEPT +{ + network::io::context context{}; + network::io::socket socket{ context }; + REQUIRE_SUCCESS(socket, open, network::io::tcp::v4()); + REQUIRE_SUCCESS(socket, bind, loopback()); + const auto endpoint = socket.local_endpoint(); + REQUIRE_SUCCESS(socket, close); + return endpoint; +} + } // namespace test #endif \ No newline at end of file