init, net: Implement usage of binary-embedded asmap data
This commit is contained in:
2 files changed
+58
-26
No files matched your search
+44
-19
@@ -119,6 +119,10 @@
|
|||||||
#include <zmq/zmqrpc.h>
|
#include <zmq/zmqrpc.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef ENABLE_EMBEDDED_ASMAP
|
||||||
|
#include <node/data/ip_asn.dat.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
using common::AmountErrMsg;
|
using common::AmountErrMsg;
|
||||||
using common::InvalidPortErrMsg;
|
using common::InvalidPortErrMsg;
|
||||||
using common::ResolveErrMsg;
|
using common::ResolveErrMsg;
|
||||||
@@ -1560,29 +1564,50 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
|||||||
ApplyArgsManOptions(args, peerman_opts);
|
ApplyArgsManOptions(args, peerman_opts);
|
||||||
|
|
||||||
{
|
{
|
||||||
// Read asmap file if configured and initialize
|
// Read asmap file if configured or embedded asmap data and initialize
|
||||||
// Netgroupman with or without it
|
// Netgroupman with or without it
|
||||||
assert(!node.netgroupman);
|
assert(!node.netgroupman);
|
||||||
if (args.IsArgSet("-asmap") && !args.IsArgNegated("-asmap")) {
|
if (args.IsArgSet("-asmap") && !args.IsArgNegated("-asmap")) {
|
||||||
fs::path asmap_path = args.GetPathArg("-asmap");
|
uint256 asmap_version{};
|
||||||
if (asmap_path.empty()) {
|
if (!args.GetBoolArg("-asmap", false)) {
|
||||||
InitError(_("-asmap requires a file path. Use -asmap=<file>."));
|
fs::path asmap_path = args.GetPathArg("-asmap");
|
||||||
return false;
|
if (!asmap_path.is_absolute()) {
|
||||||
|
asmap_path = args.GetDataDirNet() / 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)) {
|
||||||
|
InitError(strprintf(_("Could not find asmap file %s"), fs::quoted(fs::PathToString(asmap_path))));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If a file exists at the path, try to read the file
|
||||||
|
std::vector<std::byte> asmap{DecodeAsmap(asmap_path)};
|
||||||
|
if (asmap.empty()) {
|
||||||
|
InitError(strprintf(_("Could not parse asmap file %s"), fs::quoted(fs::PathToString(asmap_path))));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
asmap_version = AsmapVersion(asmap);
|
||||||
|
node.netgroupman = std::make_unique<NetGroupManager>(NetGroupManager::WithLoadedAsmap(std::move(asmap)));
|
||||||
|
} else {
|
||||||
|
#ifdef ENABLE_EMBEDDED_ASMAP
|
||||||
|
// Use the embedded asmap data
|
||||||
|
std::span<const std::byte> asmap{node::data::ip_asn};
|
||||||
|
if (asmap.empty() || !CheckStandardAsmap(asmap)) {
|
||||||
|
InitError(strprintf(_("Could not read embedded asmap data")));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
node.netgroupman = std::make_unique<NetGroupManager>(NetGroupManager::WithEmbeddedAsmap(asmap));
|
||||||
|
asmap_version = AsmapVersion(asmap);
|
||||||
|
LogInfo("Opened asmap data (%zu bytes) from embedded byte array\n", asmap.size());
|
||||||
|
#else
|
||||||
|
// If there is no embedded data, fail and report it since
|
||||||
|
// the user tried to use it
|
||||||
|
InitError(strprintf(_("Embedded asmap data not available")));
|
||||||
|
return false;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
if (!asmap_path.is_absolute()) {
|
|
||||||
asmap_path = args.GetDataDirNet() / asmap_path;
|
|
||||||
}
|
|
||||||
if (!fs::exists(asmap_path)) {
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
const uint256 asmap_version = AsmapVersion(asmap);
|
|
||||||
node.netgroupman = std::make_unique<NetGroupManager>(NetGroupManager::WithLoadedAsmap(std::move(asmap)));
|
|
||||||
LogInfo("Using asmap version %s for IP bucketing", asmap_version.ToString());
|
LogInfo("Using asmap version %s for IP bucketing", asmap_version.ToString());
|
||||||
} else {
|
} else {
|
||||||
node.netgroupman = std::make_unique<NetGroupManager>(NetGroupManager::NoAsmap());
|
node.netgroupman = std::make_unique<NetGroupManager>(NetGroupManager::NoAsmap());
|
||||||
|
|||||||
@@ -66,12 +66,19 @@ class AsmapTest(BitcoinTestFramework):
|
|||||||
self.start_node(0, [f'-asmap={name}'])
|
self.start_node(0, [f'-asmap={name}'])
|
||||||
os.remove(filename)
|
os.remove(filename)
|
||||||
|
|
||||||
def test_unspecified_asmap(self):
|
def test_embedded_asmap(self):
|
||||||
msg = "Error: -asmap requires a file path. Use -asmap=<file>."
|
if self.is_embedded_asmap_compiled():
|
||||||
for arg in ['-asmap', '-asmap=']:
|
self.log.info('Test bitcoind -asmap (using embedded map data)')
|
||||||
self.log.info(f'Test bitcoind {arg} (and no filename specified)')
|
for arg in ['-asmap', '-asmap=1']:
|
||||||
self.stop_node(0)
|
self.stop_node(0)
|
||||||
self.node.assert_start_raises_init_error(extra_args=[arg], expected_msg=msg)
|
with self.node.assert_debug_log(["Opened asmap data", "from embedded byte array"]):
|
||||||
|
self.start_node(0, [arg])
|
||||||
|
else:
|
||||||
|
self.log.info('Test bitcoind -asmap (compiled without embedded map data)')
|
||||||
|
for arg in ['-asmap', '-asmap=1']:
|
||||||
|
self.stop_node(0)
|
||||||
|
msg = "Error: Embedded asmap data not available"
|
||||||
|
self.node.assert_start_raises_init_error(extra_args=[arg], expected_msg=msg)
|
||||||
|
|
||||||
def test_asmap_interaction_with_addrman_containing_entries(self):
|
def test_asmap_interaction_with_addrman_containing_entries(self):
|
||||||
self.log.info("Test bitcoind -asmap restart with addrman containing new and tried entries")
|
self.log.info("Test bitcoind -asmap restart with addrman containing new and tried entries")
|
||||||
@@ -127,7 +134,7 @@ class AsmapTest(BitcoinTestFramework):
|
|||||||
self.test_noasmap_arg()
|
self.test_noasmap_arg()
|
||||||
self.test_asmap_with_absolute_path()
|
self.test_asmap_with_absolute_path()
|
||||||
self.test_asmap_with_relative_path()
|
self.test_asmap_with_relative_path()
|
||||||
self.test_unspecified_asmap()
|
self.test_embedded_asmap()
|
||||||
self.test_asmap_interaction_with_addrman_containing_entries()
|
self.test_asmap_interaction_with_addrman_containing_entries()
|
||||||
self.test_asmap_with_missing_file()
|
self.test_asmap_with_missing_file()
|
||||||
self.test_empty_asmap()
|
self.test_empty_asmap()
|
||||||
|
|||||||
Reference in new issue
Block a user