Skip to content

Commit

Permalink
init: move asmap code earlier in init process
Browse files Browse the repository at this point in the history
and update feature_asmap.py and test_runner.py

This commit moves the asmap init.cpp code from the end of "Step 12: start node"
to "Step 6: network initialization" to provide feedback on passing an -asmap
config arg much more quickly. This change speeds up the feature_asmap.py
functional test file from 60 to 5 seconds by accelerating the 2 tests that use
`assert_start_raises_init_error`.

Credit to Wladimir J. van der Laan for the suggestion.
  • Loading branch information
jonatack authored and furszy committed Jul 28, 2021
1 parent 65cd143 commit 672d9a2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
50 changes: 25 additions & 25 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1475,6 +1475,31 @@ bool AppInitMain()
return UIError(ResolveErrMsg("externalip", strAddr));
}

// Read asmap file if configured
if (gArgs.IsArgSet("-asmap")) {
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;
}
if (!fs::exists(asmap_path)) {
UIError(strprintf(_("Could not find asmap file %s"), asmap_path));
return false;
}
std::vector<bool> asmap = CAddrMan::DecodeAsmap(asmap_path);
if (asmap.size() == 0) {
UIError(strprintf(_("Could not parse asmap file %s"), asmap_path));
return false;
}
const uint256 asmap_version = SerializeHash(asmap);
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");
}

// Warn if network-specific options (-addnode, -connect, etc) are
// specified in default section of config file, but not overridden
// on the command line or in this network's section of the config file.
Expand Down Expand Up @@ -1967,31 +1992,6 @@ bool AppInitMain()
}
#endif

// Read asmap file if configured
if (gArgs.IsArgSet("-asmap")) {
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;
}
if (!fs::exists(asmap_path)) {
UIError(strprintf(_("Could not find asmap file %s"), asmap_path));
return false;
}
std::vector<bool> asmap = CAddrMan::DecodeAsmap(asmap_path);
if (asmap.size() == 0) {
UIError(strprintf(_("Could not parse asmap file %s"), asmap_path));
return false;
}
const uint256 asmap_version = SerializeHash(asmap);
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");
}

// ********************************************************* Step 12: finished

SetRPCWarmupFinished();
Expand Down
3 changes: 1 addition & 2 deletions test/functional/feature_asmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
6. `pivxd -asmap` with an empty (unparsable) default asmap file
The tests are order-independent. The slowest tests (missing default asmap and
empty asmap) are placed last.
The tests are order-independent.
"""
import os
Expand Down
2 changes: 1 addition & 1 deletion test/functional/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
'rpc_signrawtransaction.py', # ~ 50 sec
'rpc_decodescript.py', # ~ 50 sec
'rpc_blockchain.py', # ~ 50 sec
'feature_asmap.py',
'wallet_disable.py', # ~ 50 sec
'wallet_autocombine.py', # ~ 49 sec
'mining_v5_upgrade.py', # ~ 48 sec
Expand Down Expand Up @@ -183,7 +184,6 @@
# vv Tests less than 60s vv
#'p2p_feefilter.py',
'feature_abortnode.py',
'feature_asmap.py',
'rpc_bind.py',
# vv Tests less than 30s vv
#'example_test.py',
Expand Down

0 comments on commit 672d9a2

Please sign in to comment.