mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-04-10 07:37:59 +02:00
Merge bitcoin/bitcoin#33878: refactor, docs: Embedded ASMap [2/3]: Refactor asmap internals and add documentation
4fec726c4drefactor: Simplify Interpret asmap function (Fabian Jahr)79e97d45c1doc: Add more extensive docs to asmap implementation (Fabian Jahr)cf4943fdcdrefactor: Use span instead of vector for data in util/asmap (Fabian Jahr)385c34a052refactor: Unify asmap version calculation and naming (Fabian Jahr)fa41fc6a1arefactor: Operate on bytes instead of bits in Asmap code (Fabian Jahr) Pull request description: This is a second slice carved out of #28792. It contains the following changes that are crucial for the embedding of asmap data which is added the following PR in the series (probably this will remain in #28792). The changes are: - Modernizes and simplifies the asmap code by operating on `std::byte` instead of bits - Unifies asmap version calculation and naming (previously it was called version and checksum interchangeably) - Operate on a `span` rather than a vector in the asmap internal to prevent holding the asmap data in memory twice - Add more extensive documentation to the asmap implementation - Unify asmap casing in implemetation function names The first three commits were already part of #28792, the others are new. The documentation commit came out of feedback gathered at the latest CoreDev. The primary input for the documentation was the documentation that already existed in the Python implementation (`contrib/asmap/asmap.py`) but there are several other comments as well. Please note: I have also asked several LLMs to provide suggestions on how to explain pieces of the implementation and better demonstrate how the parts work together. I have copied bits and pieces that I liked but everything has been edited further by me and obviously all mistakes here are my own. ACKs for top commit: hodlinator: re-ACK4fec726c4dsipa: ACK4fec726c4dsedited: Re-ACK4fec726c4dTree-SHA512: 950a591c3fcc9ddb28fcfdc3164ad3fbd325fa5004533c4a8b670fbf8b956060a0daeedd1fc2fced1f761ac49cd992b79cabe12ef46bc60b2559a7a613d0e166
This commit is contained in:
@@ -25,7 +25,7 @@ using namespace std::literals;
|
||||
using node::NodeContext;
|
||||
using util::ToString;
|
||||
|
||||
static NetGroupManager EMPTY_NETGROUPMAN{std::vector<bool>()};
|
||||
static auto EMPTY_NETGROUPMAN{NetGroupManager::NoAsmap()};
|
||||
static const bool DETERMINISTIC{true};
|
||||
|
||||
static int32_t GetCheckRatio(const NodeContext& node_ctx)
|
||||
@@ -47,20 +47,6 @@ static CService ResolveService(const std::string& ip, uint16_t port = 0)
|
||||
return serv.value_or(CService{});
|
||||
}
|
||||
|
||||
|
||||
static std::vector<bool> FromBytes(std::span<const std::byte> source)
|
||||
{
|
||||
int vector_size(source.size() * 8);
|
||||
std::vector<bool> result(vector_size);
|
||||
for (int byte_i = 0; byte_i < vector_size / 8; ++byte_i) {
|
||||
uint8_t cur_byte{std::to_integer<uint8_t>(source[byte_i])};
|
||||
for (int bit_i = 0; bit_i < 8; ++bit_i) {
|
||||
result[byte_i * 8 + bit_i] = (cur_byte >> bit_i) & 1;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_SUITE(addrman_tests, BasicTestingSetup)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(addrman_simple)
|
||||
@@ -638,8 +624,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket_legacy)
|
||||
// 101.8.0.0/16 AS8
|
||||
BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket)
|
||||
{
|
||||
std::vector<bool> asmap = FromBytes(test::data::asmap);
|
||||
NetGroupManager ngm_asmap{asmap};
|
||||
auto ngm_asmap{NetGroupManager::WithEmbeddedAsmap(test::data::asmap)};
|
||||
|
||||
CAddress addr1 = CAddress(ResolveService("250.1.1.1", 8333), NODE_NONE);
|
||||
CAddress addr2 = CAddress(ResolveService("250.1.1.1", 9999), NODE_NONE);
|
||||
@@ -692,8 +677,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket)
|
||||
{
|
||||
std::vector<bool> asmap = FromBytes(test::data::asmap);
|
||||
NetGroupManager ngm_asmap{asmap};
|
||||
auto ngm_asmap{NetGroupManager::WithEmbeddedAsmap(test::data::asmap)};
|
||||
|
||||
CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE);
|
||||
CAddress addr2 = CAddress(ResolveService("250.1.2.1", 9999), NODE_NONE);
|
||||
@@ -770,8 +754,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(addrman_serialization)
|
||||
{
|
||||
std::vector<bool> asmap1 = FromBytes(test::data::asmap);
|
||||
NetGroupManager netgroupman{asmap1};
|
||||
auto netgroupman{NetGroupManager::WithEmbeddedAsmap(test::data::asmap)};
|
||||
|
||||
const auto ratio = GetCheckRatio(m_node);
|
||||
auto addrman_asmap1 = std::make_unique<AddrMan>(netgroupman, DETERMINISTIC, ratio);
|
||||
|
||||
Reference in New Issue
Block a user