From 618b8d1e4c1cfbc8e41ee46aa570f49e5017dce0 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sun, 29 Dec 2019 18:45:59 +0100 Subject: [PATCH] config: enable passing -asmap an absolute file path - allow passing an absolute file path to the -asmap config arg - update the -asmap config help - add a functional test in feature_asmap.py --- src/init.cpp | 12 +++++++----- test/functional/feature_asmap.py | 18 +++++++++++++++--- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index d5a456aa5caa7..b23260314cf21 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -475,7 +475,7 @@ std::string HelpMessage(HelpMessageMode mode) strUsage += HelpMessageGroup(_("Connection options:")); strUsage += HelpMessageOpt("-addnode=", _("Add a node to connect to and attempt to keep the connection open")); - strUsage += HelpMessageOpt("-asmap=", strprintf("Specify asn mapping used for bucketing of the peers (default: %s). Path should be relative to the -datadir path.", DEFAULT_ASMAP_FILENAME); + strUsage += HelpMessageOpt("-asmap=", strprintf("Specify asn mapping used for bucketing of the peers (default: %s). Relative paths will be prefixed by the net-specific datadir location.", DEFAULT_ASMAP_FILENAME)); strUsage += HelpMessageOpt("-banscore=", strprintf(_("Threshold for disconnecting misbehaving peers (default: %u)"), DEFAULT_BANSCORE_THRESHOLD)); strUsage += HelpMessageOpt("-bantime=", strprintf(_("Number of seconds to keep misbehaving peers from reconnecting (default: %u)"), DEFAULT_MISBEHAVING_BANTIME)); strUsage += HelpMessageOpt("-bind=", _("Bind to given address and always listen on it. Use [host]:port notation for IPv6")); @@ -1969,11 +1969,13 @@ bool AppInitMain() // Read asmap file if configured if (gArgs.IsArgSet("-asmap")) { - std::string asmap_file = gArgs.GetArg("-asmap", ""); - if (asmap_file.empty()) { - asmap_file = DEFAULT_ASMAP_FILENAME; + fs::path asmap_path = fs::path(gArgs.GetArg("-asmap", "")); + if (asmap_path.empty()) { + asmap_path = DEFAULT_ASMAP_FILENAME; + } + if (!asmap_path.is_absolute()) { + asmap_path = GetDataDir() / asmap_path; } - const fs::path asmap_path = GetDataDir() / asmap_file; std::vector asmap = CAddrMan::DecodeAsmap(asmap_path); if (asmap.size() == 0) { UIError(strprintf(_("Could not find or parse specified asmap: '%s'"), asmap_path)); diff --git a/test/functional/feature_asmap.py b/test/functional/feature_asmap.py index a134ae9739838..fc57c49696413 100755 --- a/test/functional/feature_asmap.py +++ b/test/functional/feature_asmap.py @@ -8,11 +8,13 @@ 1. `bitcoind` with no -asmap arg, using /16 prefix for IP bucketing -2. `bitcoind -asmap=`, using the unit test skeleton asmap +2. `bitcoind -asmap=`, using the unit test skeleton asmap -3. `bitcoind -asmap/-asmap=` with no file specified, using the default asmap +3. `bitcoind -asmap=`, using the unit test skeleton asmap -4. `bitcoind -asmap` with no file specified, and a missing default asmap file +4. `bitcoind -asmap/-asmap=` with no file specified, using the default asmap + +5. `bitcoind -asmap` with no file specified and a missing default asmap file The tests are order-independent. The slowest test (missing default asmap file) is placed last. @@ -42,6 +44,15 @@ def test_without_asmap_arg(self): with self.node.assert_debug_log(['Using /16 prefix for IP bucketing']): self.start_node(0) + def test_asmap_with_absolute_path(self): + self.log.info('Test bitcoind -asmap=') + self.stop_node(0) + filename = os.path.join(self.datadir, 'my-map-file.map') + shutil.copyfile(self.asmap_raw, filename) + with self.node.assert_debug_log(expected_messages(filename)): + self.start_node(0, ['-asmap={}'.format(filename)]) + os.remove(filename) + def test_asmap_with_relative_path(self): self.log.info('Test bitcoind -asmap=') self.stop_node(0) @@ -74,6 +85,7 @@ def run_test(self): self.asmap_raw = os.path.join(os.path.dirname(os.path.realpath(__file__)), ASMAP) self.test_without_asmap_arg() + self.test_asmap_with_absolute_path() self.test_asmap_with_relative_path() self.test_default_asmap() #self.test_default_asmap_with_missing_file() // fixme