Skip to content

Conversation

ryanofsky
Copy link
Contributor

@ryanofsky ryanofsky commented Feb 23, 2023

Add common InitConfig function to deduplicate bitcoind and bitcoin-qt code reading config files and creating the datadir.

Noticed the duplicate code while reviewing #27073 and want to remove it because difference in bitcoin-qt and bitcoind behavior make it hard to evaluate changes like #27073

There are a few minor changes in behavior:

  • In bitcoin-qt, when there is a problem reading the configuration file, the GUI error text has changed from "Error: Cannot parse configuration file:" to "Error reading configuration file:" to be consistent with bitcoind.
  • In bitcoind, when there is a problem reading the settings.json file, the error text has changed from "Failed loading settings file" to "Settings file could not be read" to be consistent with bitcoin-qt.
  • In bitcoind, when there is a problem writing the settings.json file, the error text has changed from "Failed saving settings file" to "Settings file could not be written" to be consistent with bitcoin-qt.
  • In bitcoin-qt, if there datadir is not accessible (e.g. no permission to read), there is an normal error dialog showing "Error: filesystem error: status: Permission denied [.../settings.json]", instead of an uncaught exception.

@DrahtBot
Copy link
Contributor

DrahtBot commented Feb 23, 2023

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

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK Sjors, TheCharlatan, achow101
Concept ACK fanquake, hebasto

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:

  • #26688 (Move CopyrightHolders() and LicenseInfo() into libbitcoin_common by hebasto)
  • #26504 (Add UNREACHABLE macro and drop -Wreturn-type/C4715 warnings suppressions by hebasto)

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.

@ryanofsky
Copy link
Contributor Author

ryanofsky commented Feb 23, 2023

Leaving as draft because #27073 could be merged first, and if it is, more duplicate code can be moved to the common function.

@jarolrod
Copy link
Member

concept ack

@fanquake
Copy link
Member

Concept ACK - #27073 has now been merged.

@hebasto
Copy link
Member

hebasto commented Feb 24, 2023

Concept ACK.

@ryanofsky ryanofsky force-pushed the pr/oneconfig branch 2 times, most recently from ebc1659 to e248926 Compare February 24, 2023 19:28
@ryanofsky ryanofsky marked this pull request as ready for review February 24, 2023 19:37
@ryanofsky
Copy link
Contributor Author

Marking ready for review since I was able to merge the duplicate InitSettings functions after #27073.


Rebased eeff271 -> e248926 (pr/oneconfig.1 -> pr/oneconfig.2, compare) after #27073 merge

@ryanofsky
Copy link
Contributor Author

Updated e248926 -> 25f9cbf (pr/oneconfig.2 -> pr/oneconfig.3, compare) to fix lint spelling error https://cirrus-ci.com/task/5488540285403136

Copy link
Contributor Author

@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.

Copy link
Contributor

@TheCharlatan TheCharlatan 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

} catch (const std::exception& e) {
return InitError(Untranslated(strprintf("%s", e.what())));
if (auto error = common::InitConfig(args)) {
return InitError(error->message, error->details);
}

// Error out when loose non-argument tokens are encountered on command line
Copy link
Contributor

Choose a reason for hiding this comment

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

More a comment in case anybody else stumbles over this than a change request, but I'm surprised this is done in bitcoind and not in bitcoin-qt. Indeed I can start bitcoin-qt with any number of positional arguments, e.g. ./qt/bitcoin-qt h j k, but not so bitcoind. The historical context is that this is a leftover from when bitcoind was also used as a command line client.

Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

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

Maybe it makes sense from the beginning to add src/common/init.cpp to the list of files which are processed by IWYU in ci/test/06_script_b.sh?

Some InitError calls had trailing \n characters, causing double newlines in
error output. After this change InitError calls consistently output one newline
instead of two. Appearance of messages in the GUI does not seem to be affected.
Can be tested with:

  src/bitcoind -regtest -datadir=noexist
  src/qt/bitcoin-qt -regtest -datadir=noexist

-BEGIN VERIFY SCRIPT-
git grep -l InitError src/ | xargs sed -i 's/\(InitError(.*\)\\n"/\1"/'
-END VERIFY SCRIPT-
Previous bilingual_str tinyformat::format accepted bilingual format strings,
but not bilingual arguments. Extend it to accept both. This is useful when
embedding one translated string inside another translated string, for example:
`strprintf(_("Error: %s"), message)` which would fail previously if `message`
was a bilingual_str.
This is only used in the current PR to avoid ugly
`strprintf(Untranslated("%s:\n%s"), str, MakeUnorderedList(details)`
boilerplate in init code. But in the future the function could be extended and
more widely used to include more details in GUI error messages or display them
in a more readable way, see code comment.
Add common InitConfig function to deduplicate bitcoind and bitcoin-qt code
reading config files and creating the datadir.

There are a few minor changes in behavior:

- In bitcoin-qt, when there is a problem reading the configuration file, the
  GUI error text has changed from "Error: Cannot parse configuration file:" to
  "Error reading configuration file:" to be consistent with bitcoind.
- In bitcoind, when there is a problem reading the settings.json file, the
  error text has changed from "Failed loading settings file" to "Settings
  file could not be read" to be consistent with bitcoin-qt.
- In bitcoind, when there is a problem writing the settings.json file, the
  error text has changed from "Failed saving settings file" to "Settings file
  could not be written" to be consistent with bitcoin-qt.
- In bitcoin-qt, if there datadir is not accessible (e.g. no permission to read),
  there is an normal error dialog showing "Error: filesystem error: status:
  Permission denied [.../settings.json]", instead of an uncaught exception
Copy link
Contributor Author

@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.

Thanks for the reviews!


Rebased 15cb0a2 -> 802cc1e (pr/oneconfig.4 -> pr/oneconfig.5, compare) due to conflict with #27170 and applied suggestions

@Sjors
Copy link
Member

Sjors commented Mar 1, 2023

Light review ACK 802cc1e

I'm not super familiar with the init code, but the changes look sane to me. I tested some of the error messages on macOS.

Regarding d172b5c, any hints on how to produce a fatal error?

@ryanofsky
Copy link
Contributor Author

@Sjors

Regarding d172b5c, any hints on how to produce a fatal error?

I don't know of a way to trigger the AbortError calls in that commit offhand but I think it should be straightforward to verify that replacing them with InitError has no effect, because the previous definition of AbortError was constexpr auto AbortError = InitError; (The commit dropped the AbortError alias because it seemed pointless, and dropping it made overloading InitError easier).

If it helps, I used the following commands for testing the other errors in the PR:

src/bitcoind -regtest -conf=bad
src/qt/bitcoin-qt -regtest -conf=bad
src/qt/bitcoin-qt -regtest
echo bad > ~/.bitcoin/regtest/settings.json
sudo chown root ~/.bitcoin/regtest
sudo chmod 755 ~/.bitcoin/regtest
sudo chmod 700 ~/.bitcoin/regtest
sudo chown 1000:1000 ~/.bitcoin/regtest

@TheCharlatan
Copy link
Contributor

ACK 802cc1e

@fanquake fanquake requested a review from pinheadmz March 4, 2023 08:01
@achow101
Copy link
Member

achow101 commented Mar 7, 2023

ACK 802cc1e

@achow101 achow101 merged commit fc037c8 into bitcoin:master Mar 7, 2023
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Mar 7, 2023
@bitcoin bitcoin locked and limited conversation to collaborators Mar 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants