Skip to content

Commit 9fd5618

Browse files
committed
[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`
1 parent bfdf4ef commit 9fd5618

File tree

5 files changed

+43
-35
lines changed

5 files changed

+43
-35
lines changed

src/addrman.cpp

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <logging.h>
1010
#include <netaddress.h>
1111
#include <serialize.h>
12-
#include <util/asmap.h>
1312

1413
#include <cmath>
1514
#include <optional>
@@ -1008,30 +1007,3 @@ CAddrInfo CAddrMan::SelectTriedCollision_()
10081007

10091008
return mapInfo[id_old];
10101009
}
1011-
1012-
std::vector<bool> CAddrMan::DecodeAsmap(fs::path path)
1013-
{
1014-
std::vector<bool> bits;
1015-
FILE *filestr = fsbridge::fopen(path, "rb");
1016-
CAutoFile file(filestr, SER_DISK, CLIENT_VERSION);
1017-
if (file.IsNull()) {
1018-
LogPrintf("Failed to open asmap file from disk\n");
1019-
return bits;
1020-
}
1021-
fseek(filestr, 0, SEEK_END);
1022-
int length = ftell(filestr);
1023-
LogPrintf("Opened asmap file %s (%d bytes) from disk\n", path, length);
1024-
fseek(filestr, 0, SEEK_SET);
1025-
uint8_t cur_byte;
1026-
for (int i = 0; i < length; ++i) {
1027-
file >> cur_byte;
1028-
for (int bit = 0; bit < 8; ++bit) {
1029-
bits.push_back((cur_byte >> bit) & 1);
1030-
}
1031-
}
1032-
if (!SanityCheckASMap(bits, 128)) {
1033-
LogPrintf("Sanity check of asmap file %s failed\n", path);
1034-
return {};
1035-
}
1036-
return bits;
1037-
}

src/addrman.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,6 @@ static constexpr int ADDRMAN_BUCKET_SIZE{1 << ADDRMAN_BUCKET_SIZE_LOG2};
149149
class CAddrMan
150150
{
151151
public:
152-
// Read asmap from provided binary file
153-
static std::vector<bool> DecodeAsmap(fs::path path);
154-
155152
template <typename Stream>
156153
void Serialize(Stream& s_) const EXCLUSIVE_LOCKS_REQUIRED(!cs);
157154

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
11891189
InitError(strprintf(_("Could not find asmap file %s"), asmap_path));
11901190
return false;
11911191
}
1192-
asmap = CAddrMan::DecodeAsmap(asmap_path);
1192+
asmap = DecodeAsmap(asmap_path);
11931193
if (asmap.size() == 0) {
11941194
InitError(strprintf(_("Could not parse asmap file %s"), asmap_path));
11951195
return false;

src/util/asmap.cpp

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5+
#include <util/asmap.h>
6+
7+
#include <clientversion.h>
8+
#include <crypto/common.h>
9+
#include <logging.h>
10+
#include <streams.h>
11+
12+
#include <cassert>
513
#include <map>
614
#include <vector>
7-
#include <assert.h>
8-
#include <crypto/common.h>
915

1016
namespace {
1117

@@ -183,3 +189,31 @@ bool SanityCheckASMap(const std::vector<bool>& asmap, int bits)
183189
}
184190
return false; // Reached EOF without RETURN instruction
185191
}
192+
193+
std::vector<bool> DecodeAsmap(fs::path path)
194+
{
195+
std::vector<bool> bits;
196+
FILE *filestr = fsbridge::fopen(path, "rb");
197+
CAutoFile file(filestr, SER_DISK, CLIENT_VERSION);
198+
if (file.IsNull()) {
199+
LogPrintf("Failed to open asmap file from disk\n");
200+
return bits;
201+
}
202+
fseek(filestr, 0, SEEK_END);
203+
int length = ftell(filestr);
204+
LogPrintf("Opened asmap file %s (%d bytes) from disk\n", path, length);
205+
fseek(filestr, 0, SEEK_SET);
206+
uint8_t cur_byte;
207+
for (int i = 0; i < length; ++i) {
208+
file >> cur_byte;
209+
for (int bit = 0; bit < 8; ++bit) {
210+
bits.push_back((cur_byte >> bit) & 1);
211+
}
212+
}
213+
if (!SanityCheckASMap(bits, 128)) {
214+
LogPrintf("Sanity check of asmap file %s failed\n", path);
215+
return {};
216+
}
217+
return bits;
218+
}
219+

src/util/asmap.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@
55
#ifndef BITCOIN_UTIL_ASMAP_H
66
#define BITCOIN_UTIL_ASMAP_H
77

8-
#include <stdint.h>
8+
#include <fs.h>
9+
10+
#include <cstdint>
911
#include <vector>
1012

1113
uint32_t Interpret(const std::vector<bool> &asmap, const std::vector<bool> &ip);
1214

1315
bool SanityCheckASMap(const std::vector<bool>& asmap, int bits);
1416

17+
/** Read asmap from provided binary file */
18+
std::vector<bool> DecodeAsmap(fs::path path);
19+
1520
#endif // BITCOIN_UTIL_ASMAP_H

0 commit comments

Comments
 (0)