From 9fd5618610e91e3949536c5122cf31eb58c9aa6b Mon Sep 17 00:00:00 2001 From: John Newbery Date: Wed, 1 Sep 2021 12:41:47 +0100 Subject: [PATCH] [asmap] Make DecodeAsmap() a utility function DecopeAsmap is a pure utility function and doesn't have any dependencies on addrman, so move it to util/asmap. Reviewer hint: use: `git diff --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space` --- src/addrman.cpp | 28 ---------------------------- src/addrman.h | 3 --- src/init.cpp | 2 +- src/util/asmap.cpp | 38 ++++++++++++++++++++++++++++++++++++-- src/util/asmap.h | 7 ++++++- 5 files changed, 43 insertions(+), 35 deletions(-) diff --git a/src/addrman.cpp b/src/addrman.cpp index fd0d3dc6fa8..49226dde42c 100644 --- a/src/addrman.cpp +++ b/src/addrman.cpp @@ -9,7 +9,6 @@ #include #include #include -#include #include #include @@ -1008,30 +1007,3 @@ CAddrInfo CAddrMan::SelectTriedCollision_() return mapInfo[id_old]; } - -std::vector CAddrMan::DecodeAsmap(fs::path path) -{ - std::vector bits; - FILE *filestr = fsbridge::fopen(path, "rb"); - CAutoFile file(filestr, SER_DISK, CLIENT_VERSION); - if (file.IsNull()) { - LogPrintf("Failed to open asmap file from disk\n"); - return bits; - } - fseek(filestr, 0, SEEK_END); - int length = ftell(filestr); - LogPrintf("Opened asmap file %s (%d bytes) from disk\n", path, length); - fseek(filestr, 0, SEEK_SET); - uint8_t cur_byte; - for (int i = 0; i < length; ++i) { - file >> cur_byte; - for (int bit = 0; bit < 8; ++bit) { - bits.push_back((cur_byte >> bit) & 1); - } - } - if (!SanityCheckASMap(bits, 128)) { - LogPrintf("Sanity check of asmap file %s failed\n", path); - return {}; - } - return bits; -} diff --git a/src/addrman.h b/src/addrman.h index 74bfe9748b6..48e0f8b8713 100644 --- a/src/addrman.h +++ b/src/addrman.h @@ -149,9 +149,6 @@ static constexpr int ADDRMAN_BUCKET_SIZE{1 << ADDRMAN_BUCKET_SIZE_LOG2}; class CAddrMan { public: - // Read asmap from provided binary file - static std::vector DecodeAsmap(fs::path path); - template void Serialize(Stream& s_) const EXCLUSIVE_LOCKS_REQUIRED(!cs); diff --git a/src/init.cpp b/src/init.cpp index b7442986670..1d18d80ec4a 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1189,7 +1189,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) InitError(strprintf(_("Could not find asmap file %s"), asmap_path)); return false; } - asmap = CAddrMan::DecodeAsmap(asmap_path); + asmap = DecodeAsmap(asmap_path); if (asmap.size() == 0) { InitError(strprintf(_("Could not parse asmap file %s"), asmap_path)); return false; diff --git a/src/util/asmap.cpp b/src/util/asmap.cpp index bacc3690a20..5695c620125 100644 --- a/src/util/asmap.cpp +++ b/src/util/asmap.cpp @@ -2,10 +2,16 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include + +#include +#include +#include +#include + +#include #include #include -#include -#include namespace { @@ -183,3 +189,31 @@ bool SanityCheckASMap(const std::vector& asmap, int bits) } return false; // Reached EOF without RETURN instruction } + +std::vector DecodeAsmap(fs::path path) +{ + std::vector bits; + FILE *filestr = fsbridge::fopen(path, "rb"); + CAutoFile file(filestr, SER_DISK, CLIENT_VERSION); + if (file.IsNull()) { + LogPrintf("Failed to open asmap file from disk\n"); + return bits; + } + fseek(filestr, 0, SEEK_END); + int length = ftell(filestr); + LogPrintf("Opened asmap file %s (%d bytes) from disk\n", path, length); + fseek(filestr, 0, SEEK_SET); + uint8_t cur_byte; + for (int i = 0; i < length; ++i) { + file >> cur_byte; + for (int bit = 0; bit < 8; ++bit) { + bits.push_back((cur_byte >> bit) & 1); + } + } + if (!SanityCheckASMap(bits, 128)) { + LogPrintf("Sanity check of asmap file %s failed\n", path); + return {}; + } + return bits; +} + diff --git a/src/util/asmap.h b/src/util/asmap.h index d0588bc8c3d..810d70b9a1a 100644 --- a/src/util/asmap.h +++ b/src/util/asmap.h @@ -5,11 +5,16 @@ #ifndef BITCOIN_UTIL_ASMAP_H #define BITCOIN_UTIL_ASMAP_H -#include +#include + +#include #include uint32_t Interpret(const std::vector &asmap, const std::vector &ip); bool SanityCheckASMap(const std::vector& asmap, int bits); +/** Read asmap from provided binary file */ +std::vector DecodeAsmap(fs::path path); + #endif // BITCOIN_UTIL_ASMAP_H