Skip to content

Commit 4a87098

Browse files
author
MacroFake
committed
Merge bitcoin/bitcoin#24830: init: Allow -proxy="" setting values
1d4122d init: Allow -proxy="" setting values (Ryan Ofsky) Pull request description: This drops the `No proxy server specified. Use -proxy=<ip> or -proxy=<ip:port>` error when a empty `-proxy=` command line argument, `bitcoin.conf` value, or `settings.json` value is specified, and just makes bitcoin connect and listen normally in these cases. The error was originally added in bitcoin/bitcoin#20003 to prevent a bare `-proxy` command line argument with no assignment from clearing proxy settings. But it was implemented in an overbroad way breaking empty `-proxy=` assignments as well. The motivation for this change is to prevent a GUI bug that happens with bitcoin/bitcoin#15936, reported in bitcoin/bitcoin#15936 (review) by vasild, that happens after a proxy setting is enabled and disabled in the GUI. But this change also makes sense on its own to remove a potentially confusing error message. ACKs for top commit: hebasto: re-ACK 1d4122d, only rebased since my recent [review](bitcoin/bitcoin#24830 (review)). Tree-SHA512: 753adfce199ed078a6cd9e0ea78e76c0b14070f8fcfe2a4632cd0c6dfe6b4e135ddffbe11a97e5e30520ea9e5bda00bad1493cbaef74cf425aa8613249167f53
2 parents d433f59 + 1d4122d commit 4a87098

File tree

4 files changed

+14
-13
lines changed

4 files changed

+14
-13
lines changed

src/init.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ void SetupServerArgs(ArgsManager& argsman)
471471
// TODO: remove the sentence "Nodes not using ... incoming connections." once the changes from
472472
// https://github.com/bitcoin/bitcoin/pull/23542 have become widespread.
473473
argsman.AddArg("-port=<port>", strprintf("Listen for connections on <port>. Nodes not using the default ports (default: %u, testnet: %u, signet: %u, regtest: %u) are unlikely to get incoming connections. Not relevant for I2P (see doc/i2p.md).", defaultChainParams->GetDefaultPort(), testnetChainParams->GetDefaultPort(), signetChainParams->GetDefaultPort(), regtestChainParams->GetDefaultPort()), ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::CONNECTION);
474-
argsman.AddArg("-proxy=<ip:port>", "Connect through SOCKS5 proxy, set -noproxy to disable (default: disabled)", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
474+
argsman.AddArg("-proxy=<ip:port>", "Connect through SOCKS5 proxy, set -noproxy to disable (default: disabled)", ArgsManager::ALLOW_ANY | ArgsManager::DISALLOW_ELISION, OptionsCategory::CONNECTION);
475475
argsman.AddArg("-proxyrandomize", strprintf("Randomize credentials for every proxy connection. This enables Tor stream isolation (default: %u)", DEFAULT_PROXYRANDOMIZE), ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
476476
argsman.AddArg("-seednode=<ip>", "Connect to a node to retrieve peer addresses, and disconnect. This option can be specified multiple times to connect to multiple nodes.", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
477477
argsman.AddArg("-networkactive", "Enable all P2P network activity (default: 1). Can be changed by the setnetworkactive RPC command", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
@@ -1029,10 +1029,6 @@ bool AppInitParameterInteraction(const ArgsManager& args, bool use_syscall_sandb
10291029

10301030
nMaxTipAge = args.GetIntArg("-maxtipage", DEFAULT_MAX_TIP_AGE);
10311031

1032-
if (args.IsArgSet("-proxy") && args.GetArg("-proxy", "").empty()) {
1033-
return InitError(_("No proxy server specified. Use -proxy=<ip> or -proxy=<ip:port>."));
1034-
}
1035-
10361032
if (args.GetBoolArg("-reindex-chainstate", false)) {
10371033
// indexes that must be deactivated to prevent index corruption, see #24630
10381034
if (args.GetBoolArg("-coinstatsindex", DEFAULT_COINSTATSINDEX)) {

src/util/system.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ KeyInfo InterpretKey(std::string key)
236236
* @return parsed settings value if it is valid, otherwise nullopt accompanied
237237
* by a descriptive error string
238238
*/
239-
static std::optional<util::SettingsValue> InterpretValue(const KeyInfo& key, const std::string& value,
239+
static std::optional<util::SettingsValue> InterpretValue(const KeyInfo& key, const std::string* value,
240240
unsigned int flags, std::string& error)
241241
{
242242
// Return negated settings as false values.
@@ -246,13 +246,17 @@ static std::optional<util::SettingsValue> InterpretValue(const KeyInfo& key, con
246246
return std::nullopt;
247247
}
248248
// Double negatives like -nofoo=0 are supported (but discouraged)
249-
if (!InterpretBool(value)) {
250-
LogPrintf("Warning: parsed potentially confusing double-negative -%s=%s\n", key.name, value);
249+
if (value && !InterpretBool(*value)) {
250+
LogPrintf("Warning: parsed potentially confusing double-negative -%s=%s\n", key.name, *value);
251251
return true;
252252
}
253253
return false;
254254
}
255-
return value;
255+
if (!value && (flags & ArgsManager::DISALLOW_ELISION)) {
256+
error = strprintf("Can not set -%s with no value. Please specify value with -%s=value.", key.name, key.name);
257+
return std::nullopt;
258+
}
259+
return value ? *value : "";
256260
}
257261

258262
// Define default constructor and destructor that are not inline, so code instantiating this class doesn't need to
@@ -320,7 +324,7 @@ bool ArgsManager::ParseParameters(int argc, const char* const argv[], std::strin
320324
#endif
321325

322326
if (key == "-") break; //bitcoin-tx using stdin
323-
std::string val;
327+
std::optional<std::string> val;
324328
size_t is_index = key.find('=');
325329
if (is_index != std::string::npos) {
326330
val = key.substr(is_index + 1);
@@ -366,7 +370,7 @@ bool ArgsManager::ParseParameters(int argc, const char* const argv[], std::strin
366370
return false;
367371
}
368372

369-
std::optional<util::SettingsValue> value = InterpretValue(keyinfo, val, *flags, error);
373+
std::optional<util::SettingsValue> value = InterpretValue(keyinfo, val ? &*val : nullptr, *flags, error);
370374
if (!value) return false;
371375

372376
m_settings.command_line_options[keyinfo.name].push_back(*value);
@@ -887,7 +891,7 @@ bool ArgsManager::ReadConfigStream(std::istream& stream, const std::string& file
887891
KeyInfo key = InterpretKey(option.first);
888892
std::optional<unsigned int> flags = GetArgFlags('-' + key.name);
889893
if (flags) {
890-
std::optional<util::SettingsValue> value = InterpretValue(key, option.second, *flags, error);
894+
std::optional<util::SettingsValue> value = InterpretValue(key, &option.second, *flags, error);
891895
if (!value) {
892896
return false;
893897
}

src/util/system.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ class ArgsManager
175175
// ALLOW_STRING = 0x08, //!< unimplemented, draft implementation in #16545
176176
// ALLOW_LIST = 0x10, //!< unimplemented, draft implementation in #16545
177177
DISALLOW_NEGATION = 0x20, //!< disallow -nofoo syntax
178+
DISALLOW_ELISION = 0x40, //!< disallow -foo syntax that doesn't assign any value
178179

179180
DEBUG_ONLY = 0x100,
180181
/* Some options would cause cross-contamination if values for

test/functional/feature_config_args.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def test_config_file_parser(self):
8585

8686
def test_invalid_command_line_options(self):
8787
self.nodes[0].assert_start_raises_init_error(
88-
expected_msg='Error: No proxy server specified. Use -proxy=<ip> or -proxy=<ip:port>.',
88+
expected_msg='Error: Error parsing command line arguments: Can not set -proxy with no value. Please specify value with -proxy=value.',
8989
extra_args=['-proxy'],
9090
)
9191

0 commit comments

Comments
 (0)