mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-03 09:56:38 -05:00
[init] Read/decode asmap before constructing addrman
Commit 181a1207
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`
This commit is contained in:
parent
eb09c26724
commit
50fd77045e
1 changed files with 34 additions and 28 deletions
62
src/init.cpp
62
src/init.cpp
|
@ -1171,10 +1171,39 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
|||
fDiscover = args.GetBoolArg("-discover", true);
|
||||
const bool ignores_incoming_txs{args.GetBoolArg("-blocksonly", DEFAULT_BLOCKSONLY)};
|
||||
|
||||
assert(!node.addrman);
|
||||
auto check_addrman = std::clamp<int32_t>(args.GetArg("-checkaddrman", DEFAULT_ADDRMAN_CONSISTENCY_CHECKS), 0, 1000000);
|
||||
node.addrman = std::make_unique<CAddrMan>(/* deterministic */ false, /* consistency_check_ratio */ check_addrman);
|
||||
{
|
||||
// Initialize addrman
|
||||
assert(!node.addrman);
|
||||
|
||||
// Read asmap file if configured
|
||||
std::vector<bool> asmap;
|
||||
if (args.IsArgSet("-asmap")) {
|
||||
fs::path asmap_path = fs::path(args.GetArg("-asmap", ""));
|
||||
if (asmap_path.empty()) {
|
||||
asmap_path = DEFAULT_ASMAP_FILENAME;
|
||||
}
|
||||
if (!asmap_path.is_absolute()) {
|
||||
asmap_path = gArgs.GetDataDirNet() / asmap_path;
|
||||
}
|
||||
if (!fs::exists(asmap_path)) {
|
||||
InitError(strprintf(_("Could not find asmap file %s"), asmap_path));
|
||||
return false;
|
||||
}
|
||||
asmap = CAddrMan::DecodeAsmap(asmap_path);
|
||||
if (asmap.size() == 0) {
|
||||
InitError(strprintf(_("Could not parse asmap file %s"), asmap_path));
|
||||
return false;
|
||||
}
|
||||
const uint256 asmap_version = SerializeHash(asmap);
|
||||
LogPrintf("Using asmap version %s for IP bucketing\n", asmap_version.ToString());
|
||||
} else {
|
||||
LogPrintf("Using /16 prefix for IP bucketing\n");
|
||||
}
|
||||
|
||||
auto check_addrman = std::clamp<int32_t>(args.GetArg("-checkaddrman", DEFAULT_ADDRMAN_CONSISTENCY_CHECKS), 0, 1000000);
|
||||
node.addrman = std::make_unique<CAddrMan>(/* deterministic */ false, /* consistency_check_ratio */ check_addrman);
|
||||
node.addrman->m_asmap = asmap;
|
||||
|
||||
// Load addresses from peers.dat
|
||||
uiInterface.InitMessage(_("Loading P2P addresses…").translated);
|
||||
int64_t nStart = GetTimeMillis();
|
||||
|
@ -1184,10 +1213,12 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
|||
} else {
|
||||
// Addrman can be in an inconsistent state after failure, reset it
|
||||
node.addrman = std::make_unique<CAddrMan>(/* deterministic */ false, /* consistency_check_ratio */ check_addrman);
|
||||
node.addrman->m_asmap = asmap;
|
||||
LogPrintf("Recreating peers.dat\n");
|
||||
adb.Write(*node.addrman);
|
||||
}
|
||||
}
|
||||
|
||||
assert(!node.banman);
|
||||
node.banman = std::make_unique<BanMan>(gArgs.GetDataDirNet() / "banlist", &uiInterface, args.GetArg("-bantime", DEFAULT_MISBEHAVING_BANTIME));
|
||||
assert(!node.connman);
|
||||
|
@ -1292,31 +1323,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
|||
return InitError(ResolveErrMsg("externalip", strAddr));
|
||||
}
|
||||
|
||||
// Read asmap file if configured
|
||||
if (args.IsArgSet("-asmap")) {
|
||||
fs::path asmap_path = fs::path(args.GetArg("-asmap", ""));
|
||||
if (asmap_path.empty()) {
|
||||
asmap_path = DEFAULT_ASMAP_FILENAME;
|
||||
}
|
||||
if (!asmap_path.is_absolute()) {
|
||||
asmap_path = gArgs.GetDataDirNet() / asmap_path;
|
||||
}
|
||||
if (!fs::exists(asmap_path)) {
|
||||
InitError(strprintf(_("Could not find asmap file %s"), asmap_path));
|
||||
return false;
|
||||
}
|
||||
std::vector<bool> asmap = CAddrMan::DecodeAsmap(asmap_path);
|
||||
if (asmap.size() == 0) {
|
||||
InitError(strprintf(_("Could not parse asmap file %s"), asmap_path));
|
||||
return false;
|
||||
}
|
||||
const uint256 asmap_version = SerializeHash(asmap);
|
||||
node.connman->SetAsmap(std::move(asmap));
|
||||
LogPrintf("Using asmap version %s for IP bucketing\n", asmap_version.ToString());
|
||||
} else {
|
||||
LogPrintf("Using /16 prefix for IP bucketing\n");
|
||||
}
|
||||
|
||||
#if ENABLE_ZMQ
|
||||
g_zmq_notification_interface = CZMQNotificationInterface::Create();
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue