mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-03-12 10:30:08 +01:00
init, net: Implement usage of binary-embedded asmap data
This commit is contained in:
parent
780533e4f3
commit
8dd781c0f9
2 changed files with 68 additions and 15 deletions
51
src/init.cpp
51
src/init.cpp
|
@ -117,6 +117,10 @@
|
|||
#include <zmq/zmqrpc.h>
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_EMBEDDED_ASMAP
|
||||
#include <node/data/ip_asn.dat.h>
|
||||
#endif
|
||||
|
||||
using common::AmountErrMsg;
|
||||
using common::InvalidPortErrMsg;
|
||||
using common::ResolveErrMsg;
|
||||
|
@ -1436,21 +1440,54 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
|||
assert(!node.netgroupman);
|
||||
uint256 asmap_version;
|
||||
if (args.IsArgSet("-asmap") && !args.IsArgNegated("-asmap")) {
|
||||
const bool asmap_file_set{args.GetPathArg("-asmap") != ""};
|
||||
fs::path asmap_path = args.GetPathArg("-asmap", DEFAULT_ASMAP_FILENAME);
|
||||
if (!asmap_path.is_absolute()) {
|
||||
asmap_path = args.GetDataDirNet() / asmap_path;
|
||||
}
|
||||
if (!fs::exists(asmap_path)) {
|
||||
|
||||
// If a specific path was passed with the asmap argument check if
|
||||
// the file actually exists in that location
|
||||
if (!fs::exists(asmap_path) && asmap_file_set) {
|
||||
InitError(strprintf(_("Could not find asmap file %s"), fs::quoted(fs::PathToString(asmap_path))));
|
||||
return false;
|
||||
}
|
||||
std::vector<std::byte> asmap{DecodeAsmap(asmap_path)};
|
||||
if (asmap.size() == 0) {
|
||||
InitError(strprintf(_("Could not parse asmap file %s"), fs::quoted(fs::PathToString(asmap_path))));
|
||||
return false;
|
||||
|
||||
if (fs::exists(asmap_path)) {
|
||||
// If a file exists at the path (could be passed or the default
|
||||
// location), try to read the file
|
||||
std::vector<std::byte> asmap{DecodeAsmap(asmap_path)};
|
||||
if (asmap.empty()) {
|
||||
// If the file could not be read, print the error depending
|
||||
// on if it was passed or the default location
|
||||
if (asmap_file_set) {
|
||||
InitError(strprintf(_("Could not parse asmap file %s"), fs::quoted(fs::PathToString(asmap_path))));
|
||||
} else {
|
||||
InitError(strprintf(_("Could not parse asmap file in default location %s"), fs::quoted(fs::PathToString(asmap_path))));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
node.netgroupman = std::make_unique<NetGroupManager>(NetGroupManager::WithLoadedAsmap(asmap));
|
||||
asmap_version = AsmapChecksum(asmap);
|
||||
} else {
|
||||
#ifdef ENABLE_EMBEDDED_ASMAP
|
||||
// If the file doesn't exist, try to use the embedded data
|
||||
std::span<const std::byte> asmap{CheckAsmap(node::data::ip_asn)};
|
||||
if (asmap.empty()) {
|
||||
InitError(strprintf(_("Could not read embedded asmap data")));
|
||||
return false;
|
||||
}
|
||||
node.netgroupman = std::make_unique<NetGroupManager>(NetGroupManager::WithEmbeddedAsmap(asmap));
|
||||
asmap_version = AsmapChecksum(asmap);
|
||||
LogInfo("Opened asmap data (%zu bytes) from embedded byte array\n", asmap.size());
|
||||
#else
|
||||
// If there is no embedded data, fail and report the default
|
||||
// file as missing since we only end up here if the no
|
||||
// specific path was passed as an argument
|
||||
InitError(strprintf(_("Could not find asmap file in default location %s"), fs::quoted(fs::PathToString(asmap_path))));
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
node.netgroupman = std::make_unique<NetGroupManager>(NetGroupManager::WithLoadedAsmap(asmap));
|
||||
asmap_version = AsmapChecksum(asmap);
|
||||
LogPrintf("Using asmap version %s for IP bucketing\n", asmap_version.ToString());
|
||||
} else {
|
||||
node.netgroupman = std::make_unique<NetGroupManager>(NetGroupManager::NoAsmap());
|
||||
|
|
|
@ -79,7 +79,7 @@ class AsmapTest(BitcoinTestFramework):
|
|||
self.start_node(0, [f'-asmap={name}'])
|
||||
os.remove(filename)
|
||||
|
||||
def test_default_asmap(self):
|
||||
def test_default_asmap_file(self):
|
||||
shutil.copyfile(self.asmap_raw, self.default_asmap)
|
||||
for arg in ['-asmap', '-asmap=']:
|
||||
self.log.info(f'Test bitcoind {arg} (using default map file)')
|
||||
|
@ -88,6 +88,20 @@ class AsmapTest(BitcoinTestFramework):
|
|||
self.start_node(0, [arg])
|
||||
os.remove(self.default_asmap)
|
||||
|
||||
def test_embedded_asmap(self):
|
||||
if self.is_embedded_asmap_compiled():
|
||||
for arg in ['-asmap', '-asmap=1']:
|
||||
self.log.info(f'Test bitcoind {arg} (using embedded map data)')
|
||||
self.stop_node(0)
|
||||
with self.node.assert_debug_log(["Opened asmap data", "from embedded byte array"]):
|
||||
self.start_node(0, [arg])
|
||||
else:
|
||||
self.stop_node(0)
|
||||
for arg in ['-asmap', '-asmap=1']:
|
||||
self.log.info(f'Test bitcoind {arg} (compiled without embedded map data)')
|
||||
msg = f"Error: Could not find asmap file in default location \"{self.default_asmap}\""
|
||||
self.node.assert_start_raises_init_error(extra_args=[arg], expected_msg=msg)
|
||||
|
||||
def test_asmap_interaction_with_addrman_containing_entries(self):
|
||||
self.log.info("Test bitcoind -asmap restart with addrman containing new and tried entries")
|
||||
self.stop_node(0)
|
||||
|
@ -104,18 +118,19 @@ class AsmapTest(BitcoinTestFramework):
|
|||
self.node.getnodeaddresses() # getnodeaddresses re-runs the addrman checks
|
||||
os.remove(self.default_asmap)
|
||||
|
||||
def test_default_asmap_with_missing_file(self):
|
||||
self.log.info('Test bitcoind -asmap with missing default map file')
|
||||
def test_asmap_with_missing_file(self):
|
||||
self.log.info('Test bitcoind -asmap= with missing map file')
|
||||
self.stop_node(0)
|
||||
msg = f"Error: Could not find asmap file \"{self.default_asmap}\""
|
||||
self.node.assert_start_raises_init_error(extra_args=['-asmap'], expected_msg=msg)
|
||||
bogus_map = os.path.join(self.datadir, "bogus.map")
|
||||
msg = f"Error: Could not find asmap file \"{bogus_map}\""
|
||||
self.node.assert_start_raises_init_error(extra_args=[f"-asmap={bogus_map}"], expected_msg=msg)
|
||||
|
||||
def test_empty_asmap(self):
|
||||
self.log.info('Test bitcoind -asmap with empty map file')
|
||||
self.stop_node(0)
|
||||
with open(self.default_asmap, "w", encoding="utf-8") as f:
|
||||
f.write("")
|
||||
msg = f"Error: Could not parse asmap file \"{self.default_asmap}\""
|
||||
msg = f"Error: Could not parse asmap file in default location \"{self.default_asmap}\""
|
||||
self.node.assert_start_raises_init_error(extra_args=['-asmap'], expected_msg=msg)
|
||||
os.remove(self.default_asmap)
|
||||
|
||||
|
@ -146,9 +161,10 @@ class AsmapTest(BitcoinTestFramework):
|
|||
self.test_noasmap_arg()
|
||||
self.test_asmap_with_absolute_path()
|
||||
self.test_asmap_with_relative_path()
|
||||
self.test_default_asmap()
|
||||
self.test_default_asmap_file()
|
||||
self.test_embedded_asmap()
|
||||
self.test_asmap_interaction_with_addrman_containing_entries()
|
||||
self.test_default_asmap_with_missing_file()
|
||||
self.test_asmap_with_missing_file()
|
||||
self.test_empty_asmap()
|
||||
self.test_asmap_health_check()
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue