Skip to content

Conversation

@hodlinator
Copy link
Contributor

@hodlinator hodlinator commented Nov 4, 2024

  • Document -color as only applying to -getinfo, to be less confusing for bitcoin-cli users.
  • No longer print version information when getting passed -noversion.
  • Disallow -nodatadir as we cannot run without one. It was previously interpreted as a mix of unset and as a relative path of "0".
  • Support -norpccookiefile
  • Support -nopid
  • Properly support -noconf (instead of working by accident). Also detect when directories are specified instead of files.

Prompted by investigation in #16545 (review).

@DrahtBot
Copy link
Contributor

DrahtBot commented Nov 4, 2024

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage & Benchmarks

For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/31212.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK ryanofsky
Concept ACK rkrux
Ignored review l0rinc

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #31349 (ci: detect outbound internet traffic generated while running tests by vasild)
  • #31260 (scripted-diff: Type-safe settings retrieval by ryanofsky)
  • #30529 (Fix -norpcwhitelist, -norpcallowip, and similar corner case behavior by ryanofsky)
  • #29641 (scripted-diff: Use LogInfo over LogPrintf [WIP, NOMERGE, DRAFT] by maflcko)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

Copy link
Contributor

@rkrux rkrux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK 9e130811a3f68aea77ddeecf667aa8389a2940e8

Thanks for catching issues here, gave me an opportunity to read the app init code a bit more!

Successful make and functional tests. Left couple suggestions in the first 2 commits. Disallowing noconf and nodatadir conceptually makes sense to me. I tested with these 2 args and the output I received seems okay to me.

➜  bitcoin git:(2024/11/invalid_args) ✗ bitcoinclireg -noconf
Error parsing command line arguments: Negating of -conf is meaningless and therefore forbidden
➜  bitcoin git:(2024/11/invalid_args) ✗ bitcoinclireg -nodatadir
Error parsing command line arguments: Negating of -datadir is meaningless and therefore forbidden

Though I don't completely understand why this comment "ok to not have a config file" (and subsequent flow) is allowed because when I ran the CLI without any arg I received the version info, help options, & too few args message.
https://github.com/hodlinator/bitcoin/blob/2024/11/invalid_args/src/common/config.cpp#L138

@rkrux
Copy link
Contributor

rkrux commented Nov 7, 2024

Though I don't completely understand why this comment "ok to not have a config file" (and subsequent flow) is allowed

My guess it is applicable when the daemon is run without a conf because this portion lies in common/.

@hodlinator
Copy link
Contributor Author

hodlinator commented Nov 8, 2024

Though I don't completely understand why this comment "ok to not have a config file" (and subsequent flow) is allowed

My guess it is applicable when the daemon is run without a conf because this portion lies in common/.

My interpretation is that we shouldn't trigger an error on not finding a configuration file in the specified or default location. If it's there we read it, otherwise just skip. (The code does error just above if one is unable to open a configuration file at a non-default location).

@luke-jr
Copy link
Member

luke-jr commented Nov 9, 2024

Disallow -noconf since a config file is required

But it's not. Seems like -noconf should work to ignore the config file.

@hodlinator
Copy link
Contributor Author

Disallow -noconf since a config file is required

But it's not. Seems like -noconf should work to ignore the config file.

As mentioned in the commit message for 2d65e6ae0756b87e82dbafd032308e8687ec7941:

ArgsManager::GetConfigFilePath() asserts that a path is always set. Explicitly negating it is therefore invalid."

Previously, -noconf would result in an ifstream being opened to the ".bitcoin"-directory (not a file) in ArgsManager::ReadConfigFiles(), also circumventing the assert in aforementioned GetConfigFilePath().

(-conf="" is still allowed to reset to the default bitcoin.conf-file location)."

It's sheer luck when it comes to how ifstream treats opening of directories that has allowed this before the PR.

Here is GetConfigFilePath:

https://github.com/bitcoin/bitcoin/blob/2d65e6ae0756b87e82dbafd032308e8687ec7941/src/common/args.cpp#L761-L765

The part of ArgsManager::ReadConfigFiles which with -noconf would open a directory in the ifstream, and treat it as .good().

https://github.com/bitcoin/bitcoin/blob/2d65e6ae0756b87e82dbafd032308e8687ec7941/src/common/config.cpp#L121-L140

ReadConfigStream calls into GetConfigOptions which gets as far as trying to getline from the directory ifstream, only to return true (success).

https://github.com/bitcoin/bitcoin/blob/2d65e6ae0756b87e82dbafd032308e8687ec7941/src/common/config.cpp#L33-L38

It would be good to add detection of "directory as config path" and trigger an error, regardless of which path is given. But I'm still inclined to disallow -noconf because of the assert in GetConfigFilePath.

@l0rinc
Copy link
Contributor

l0rinc commented Nov 11, 2024

Concept ACK

@hodlinator
Copy link
Contributor Author

Added commit which now guards against passing directory paths as -conf values as per #31212 (comment).

@ryanofsky
Copy link
Contributor

Code review d9cfb2a314caa1cdcae8e47a03dad557ad96e069.

This generally looks good except commit 2d65e6ae0756b87e82dbafd032308e8687ec7941 disallowing -noconf. Specifying -noconf to disable reading from the default config file path is useful and something that should be supported, even if now it's only working in a very quirky way by treating the datadir as an empty configuration file. I think a better implementation would not look too different though, maybe like:

--- a/src/common/config.cpp
+++ b/src/common/config.cpp
@@ -131,7 +131,7 @@ bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys)
     std::ifstream stream{conf_path};
 
     // not ok to have a config file specified that cannot be opened
-    if (IsArgSet("-conf") && !stream.good()) {
+    if ((IsArgSet("-conf") && !IsArgNegated("-conf")) && !stream.good()) {
         error = strprintf("specified config file \"%s\" could not be opened.", fs::PathToString(conf_path));
         return false;
     }
@@ -213,7 +213,7 @@ bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys)
 
 fs::path AbsPathForConfigVal(const ArgsManager& args, const fs::path& path, bool net_specific)
 {
-    if (path.is_absolute()) {
+    if (path.is_absolute() || path.empty()) {
         return path;
     }
     return fsbridge::AbsPathJoin(net_specific ? args.GetDataDirNet() : args.GetDataDirBase(), path);

I don't think it's right to infer from Assert(m_config_path) that a configuration file is always required by because that assert is really meant to catch initialization order bugs. The type of m_config_path is std::optional<fs::path> not fs::path so an empty path is ok there.

@hodlinator hodlinator force-pushed the 2024/11/invalid_args branch 2 times, most recently from 20e950b to 77c8a53 Compare November 14, 2024 23:18
@hodlinator
Copy link
Contributor Author

@ryanofsky thanks for the review! Updated to cover negation for all users of AbsPathForConfigVal.

@hodlinator hodlinator changed the title util: Narrow scope of args (-color, -version, -conf, -datadir) util: Improve documentation and negation of args (-color, -version, -datadir, -rpccookiefile, -pid, -conf) Nov 14, 2024
@hodlinator hodlinator changed the title util: Improve documentation and negation of args (-color, -version, -datadir, -rpccookiefile, -pid, -conf) util: Improve documentation and negation of args Nov 14, 2024
Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review 77c8a538b7164f8b65576600eb4668ac160f6b14. Thanks for the updates! This all looks great, my only substantial comments are about the RPC commit which I think is more complicated than it needs to be and may have a bug where it may allow an empty auth string to be accepted if -norpccookiefile is specified, or if there is an initialization failure.

It may be nice to add python functional tests to exercise some of the new behaviors. It would be nontrivial work to add test coverage for all of these behaviors, but it doesn't have to be all-or-nothing. Just having some new end-to-end tests of argument parsing edge cases would be a nice improvement to our existing test coverage.

@hodlinator hodlinator force-pushed the 2024/11/invalid_args branch 3 times, most recently from 3c223b0 to 4680acc Compare November 15, 2024 15:44
@hodlinator hodlinator marked this pull request as draft November 15, 2024 15:51
Copy link
Contributor Author

@hodlinator hodlinator left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for continued review and reminding me to raise the bar for the PR @ryanofsky.

Added tests which helped uncover that bitcoin-cli might as well also support -norpccookiefile too, and part of src/common/init.cpp which was not handling -noconf correctly.

@hodlinator hodlinator force-pushed the 2024/11/invalid_args branch 2 times, most recently from 1465250 to c30e89f Compare November 18, 2024 22:35
@achow101 achow101 merged commit 11f68cc into bitcoin:master Dec 4, 2024
18 checks passed
@hodlinator hodlinator deleted the 2024/11/invalid_args branch December 4, 2024 21:58
@vasild
Copy link
Contributor

vasild commented Dec 5, 2024

Further idea:

-noconnect=0 is interpreted as -connect=1 which is interpreted as -connect=0.0.0.1

@hodlinator
Copy link
Contributor Author

Further idea:

-noconnect=0 is interpreted as -connect=1 which is interpreted as -connect=0.0.0.1

😅 Good one! I'll create a "Good first issue" for it.

@hodlinator
Copy link
Contributor Author

Done! #31426. @vasild let me know what you think of the issue description and proposed solution.

@ryanofsky ryanofsky mentioned this pull request Dec 5, 2024
fanquake added a commit that referenced this pull request Dec 8, 2024
41d934c chore: Typo Overriden -> Overridden (Hodlinator)
c9fb38a refactor test: Cleaner combine_logs.py logic (Hodlinator)

Pull request description:

  - Fixes typo caught by spelling linter (https://github.com/bitcoin/bitcoin/runs/33979284676).
  - Minor but nice refactoring of *combine_logs.py* change that was suggested late: #31212 (comment)

ACKs for top commit:
  l0rinc:
    ACK 41d934c
  maflcko:
    lgtm ACK 41d934c
  theStack:
    ACK 41d934c
  BrandonOdiwuor:
    Code Review ACK 41d934c
  tdb3:
    ACK 41d934c

Tree-SHA512: cf8ecc070d0b01df9c4e57a75820e17d4535591e85bf9d271c7b8f60875f7e04b9978c56e9b88c10e89e69ff755c35b23ed291949c32c875a91c3317105a3c79
sedited added a commit to sedited/rust-bitcoinkernel that referenced this pull request Jan 17, 2025