-
Notifications
You must be signed in to change notification settings - Fork 36.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move parsing from init.cpp to where values are needed #17434
Conversation
src/validation.cpp
Outdated
@@ -4729,6 +4728,7 @@ bool LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, FlatFi | |||
|
|||
void CChainState::CheckBlockIndex(const Consensus::Params& consensusParams) | |||
{ | |||
static bool fCheckBlockIndex = gArgs.GetBoolArg("-checkblockindex", chainparams.DefaultConsistencyChecks()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
validation.cpp:4731:73: error: use of undeclared identifier 'chainparams'
static bool fCheckBlockIndex = gArgs.GetBoolArg("-checkblockindex", chainparams.DefaultConsistencyChecks());
chainparams
is not available in the scope, you should give a CChainParams
argument in the function (like in the other functions) or use a global variable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your review @emilengler. I've updated the CheckBlockIndex function header to take all of chainparams
instead of only the consensusParams
subset.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of parsing command line arguments every time this function is called, what about adding this as a member variable that is only parsed once (e.g. at the time of construction).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I was wrong this was parsed on every call (missed the static
), however I'd still argue that this should be parsed at the time of construction (during initialization), so that parse errors can be returned to the caller.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(See also the reply by @ryanofsky which says exactly the same)
1b1e421
to
340acd0
Compare
src/validation.cpp
Outdated
{ | ||
static bool fCheckBlockIndex = gArgs.GetBoolArg("-checkblockindex", chainparams.DefaultConsistencyChecks()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In commit "Move CheckBlockIndex to validation.cpp" (340acd0)
Using a static local variable here isn't great because it makes makes the method harder to test and reuse. For example, it would be impossible to write a unit test that invokes this fucntion with different fCheckBlockIndex
values. I haven't looked at all the other variables you want to extend this to, but using static locals could create real problems in other cases.
Would suggest making this a CChainState object member and initializing it in the constructor as an alternative to leaving it as a global, and in general avoiding static locals in application logic outside of specialized init code.
Also, it'd be good to clarify the goal and advantages of this PR. It's not obvious that it's better to parse config values when they are first used instead of in AppInitParameterInteraction. For example an advantage of parsing in AppInitParameterInteraction is that if the user specifies an invalid or unparseable value, they can get error feedback immediately, and code that runs later doesn't need to deal with errors.
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ConflictsReviewers, this pull request conflicts with the following ones:
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. |
I think that
This seems reasonable, although note |
I think we agree that |
Ok, would be good to look at specific cases, but the general tradeoff with this PR seems to be better error feedback for users and potentially simpler error handling in code vs "polluting the global namespace" and "code churn hotspots", and "merge conflicts and rebases".
Yeah, sorry I was suggesting two different things. My first preference would be to keep parsing in AppInitParameterInteraction so it can happen as early as possible. But if that's a losing proposition, CChainState member would be preferable to static local. If there are a lot of related options, it might make sense to introduce a struct similar to the coin control struct which could hold parsed options and be passed around where needed. |
We already do that for connman: https://dev.visucore.com/bitcoin/doxygen/struct_c_connman_1_1_options.html |
I don't agree |
I agree this would be good. We do something similar for wallet: Line 1116 in a6f6333
|
Alright. I've moved the |
4092aa1
to
43d7bab
Compare
The global fCheckBlockIndex is only used once and has no interaction.
43d7bab
to
eec8608
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code review ACK eec8608. Seems good. Param checking is now split between:
- InitParameterInteraction
- AppInitParameterInteraction
- WalletInit::ParameterInteraction
- ValidationParameterInteraction
EDIT: Removed ack. Seems to have bug pointed out by Marco below that test code isn't calling ValidationParameterInteraction so gArgs.ForceSetArg("-checkblockindex", "1");
has no effect and checks are being skipped during testing.
Great, thanks. I'm thinking that I'll add another commit to move |
Could you add a motivation to the OP explaining the change, please. This is still a global (while hidden from other compilation untis) and it seems that the only way for tests to set this is to force set the argument and then call It doesn't look like the tests are currently calling If the long-term goal is to limit parameter parsing to a single function per module |
I assume that is actually the goal of the PR: to limit access to the global so it's internal to validation.cpp and outside code (including test code) can't access it without setting a up a real configuration and initializing it with I don't know if this a good goal or not. I'm +0. Maybe isolating these global variables more will reduce merge conflicts and so on as suggested #17434 (comment).
That does seem like a bug. Could be fixed by test code calling ValidationParameterInteraction, or by changing the default value from false to true: Line 118 in eec8608
This would be reasonable. Could be partially implemented here. Implementing it fully might expand this PR too much to non-validation code. |
🐙 This pull request conflicts with the target branch and needs rebase. |
@xekyo Are you still working on this? Otherwise I suggest to close |
There appear to be a few more of these globals that are only used once. I could do more of them, if this sort of change is welcome.
Reference: #17383 (comment)