Skip to content

Commit 50fd770

Browse files
committed
[init] Read/decode asmap before constructing addrman
Commit 181a120 introduced an initialization order bug: CAddrMan's m_asmap must be set before deserializing peers.dat. Restore that ordering. review hint: use `git diff --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space`
1 parent eb09c26 commit 50fd770

File tree

1 file changed

+34
-28
lines changed

1 file changed

+34
-28
lines changed

src/init.cpp

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,10 +1171,39 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
11711171
fDiscover = args.GetBoolArg("-discover", true);
11721172
const bool ignores_incoming_txs{args.GetBoolArg("-blocksonly", DEFAULT_BLOCKSONLY)};
11731173

1174-
assert(!node.addrman);
1175-
auto check_addrman = std::clamp<int32_t>(args.GetArg("-checkaddrman", DEFAULT_ADDRMAN_CONSISTENCY_CHECKS), 0, 1000000);
1176-
node.addrman = std::make_unique<CAddrMan>(/* deterministic */ false, /* consistency_check_ratio */ check_addrman);
11771174
{
1175+
// Initialize addrman
1176+
assert(!node.addrman);
1177+
1178+
// Read asmap file if configured
1179+
std::vector<bool> asmap;
1180+
if (args.IsArgSet("-asmap")) {
1181+
fs::path asmap_path = fs::path(args.GetArg("-asmap", ""));
1182+
if (asmap_path.empty()) {
1183+
asmap_path = DEFAULT_ASMAP_FILENAME;
1184+
}
1185+
if (!asmap_path.is_absolute()) {
1186+
asmap_path = gArgs.GetDataDirNet() / asmap_path;
1187+
}
1188+
if (!fs::exists(asmap_path)) {
1189+
InitError(strprintf(_("Could not find asmap file %s"), asmap_path));
1190+
return false;
1191+
}
1192+
asmap = CAddrMan::DecodeAsmap(asmap_path);
1193+
if (asmap.size() == 0) {
1194+
InitError(strprintf(_("Could not parse asmap file %s"), asmap_path));
1195+
return false;
1196+
}
1197+
const uint256 asmap_version = SerializeHash(asmap);
1198+
LogPrintf("Using asmap version %s for IP bucketing\n", asmap_version.ToString());
1199+
} else {
1200+
LogPrintf("Using /16 prefix for IP bucketing\n");
1201+
}
1202+
1203+
auto check_addrman = std::clamp<int32_t>(args.GetArg("-checkaddrman", DEFAULT_ADDRMAN_CONSISTENCY_CHECKS), 0, 1000000);
1204+
node.addrman = std::make_unique<CAddrMan>(/* deterministic */ false, /* consistency_check_ratio */ check_addrman);
1205+
node.addrman->m_asmap = asmap;
1206+
11781207
// Load addresses from peers.dat
11791208
uiInterface.InitMessage(_("Loading P2P addresses…").translated);
11801209
int64_t nStart = GetTimeMillis();
@@ -1184,10 +1213,12 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
11841213
} else {
11851214
// Addrman can be in an inconsistent state after failure, reset it
11861215
node.addrman = std::make_unique<CAddrMan>(/* deterministic */ false, /* consistency_check_ratio */ check_addrman);
1216+
node.addrman->m_asmap = asmap;
11871217
LogPrintf("Recreating peers.dat\n");
11881218
adb.Write(*node.addrman);
11891219
}
11901220
}
1221+
11911222
assert(!node.banman);
11921223
node.banman = std::make_unique<BanMan>(gArgs.GetDataDirNet() / "banlist", &uiInterface, args.GetArg("-bantime", DEFAULT_MISBEHAVING_BANTIME));
11931224
assert(!node.connman);
@@ -1292,31 +1323,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
12921323
return InitError(ResolveErrMsg("externalip", strAddr));
12931324
}
12941325

1295-
// Read asmap file if configured
1296-
if (args.IsArgSet("-asmap")) {
1297-
fs::path asmap_path = fs::path(args.GetArg("-asmap", ""));
1298-
if (asmap_path.empty()) {
1299-
asmap_path = DEFAULT_ASMAP_FILENAME;
1300-
}
1301-
if (!asmap_path.is_absolute()) {
1302-
asmap_path = gArgs.GetDataDirNet() / asmap_path;
1303-
}
1304-
if (!fs::exists(asmap_path)) {
1305-
InitError(strprintf(_("Could not find asmap file %s"), asmap_path));
1306-
return false;
1307-
}
1308-
std::vector<bool> asmap = CAddrMan::DecodeAsmap(asmap_path);
1309-
if (asmap.size() == 0) {
1310-
InitError(strprintf(_("Could not parse asmap file %s"), asmap_path));
1311-
return false;
1312-
}
1313-
const uint256 asmap_version = SerializeHash(asmap);
1314-
node.connman->SetAsmap(std::move(asmap));
1315-
LogPrintf("Using asmap version %s for IP bucketing\n", asmap_version.ToString());
1316-
} else {
1317-
LogPrintf("Using /16 prefix for IP bucketing\n");
1318-
}
1319-
13201326
#if ENABLE_ZMQ
13211327
g_zmq_notification_interface = CZMQNotificationInterface::Create();
13221328

0 commit comments

Comments
 (0)