Skip to content

Conversation

dongcarl
Copy link
Contributor

This PR builds on the work of #19927 towards #20049

Please see #20049 for the broader context, especially this section which applies specifically to this PR:

...for some functions (LookupBlockIndex being the main culprit), a resolution of their dependency on the g_chainman/::Chain(state|)Active() globals results in an increase in apparent references to g_chainman/::Chain(state|)Active(). However, it is important to note that these are not new dependencies by any means, those dependency were always there -- they was just hidden by Qux's use of the globals. Furthermore, all of these "new" apparent references to g_chainman/::Chain(state|)Active() will be dealt with as we prune the call graph upward.

Please also read the note in the commit message of 407faf2 (validation: Move FindForkInGlobalIndex to BlockManager), and let me know your thoughts.

@dongcarl dongcarl changed the title validation bundle 1: Prune (in)direct g_chainman usage related to ::LookupBlockIndex validation: Prune (in)direct g_chainman usage related to ::LookupBlockIndex (bundle 1) Sep 30, 2020
@dongcarl dongcarl changed the title validation: Prune (in)direct g_chainman usage related to ::LookupBlockIndex (bundle 1) validation: Prune (in)direct g_chainman usage related to ::LookupBlockIndex (bundle 1) Sep 30, 2020
@@ -1588,7 +1588,7 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
// If the loaded chain has a wrong genesis, bail out immediately
// (we're likely using a testnet datadir, or the other way around).
if (!chainman.BlockIndex().empty() &&
!LookupBlockIndex(chainparams.GetConsensus().hashGenesisBlock)) {
!g_chainman.m_blockman.LookupBlockIndex(chainparams.GetConsensus().hashGenesisBlock)) {
Copy link
Member

Choose a reason for hiding this comment

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

What is the point of moving a helper that accesses the global to be a member method of the class of the global, which is then called from the global again?

This should use the local reference (like the line immediately above it)

Suggested change
!g_chainman.m_blockman.LookupBlockIndex(chainparams.GetConsensus().hashGenesisBlock)) {
!chainman.m_blockman.LookupBlockIndex(chainparams.GetConsensus().hashGenesisBlock)) {

Feedback also applies to all other lines in this patch ;)

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
!g_chainman.m_blockman.LookupBlockIndex(chainparams.GetConsensus().hashGenesisBlock)) {
!g_chainman.LookupBlockIndex(chainparams.GetConsensus().hashGenesisBlock)) {

Also, to avoid verbosity, chainman could forward the call to its blockman member.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Continuing discussion here: #20049 (comment)

Previously, the validation_chainstatemanager_tests test suite
instantiated its own duplicate ChainstateManager on which tests were
performed.

This wasn't a problem for the specific actions performed in
that suite. However, the existence of this duplicate ChainstateManager
and the fact that many of our validation static functions reach for
g_chainman, ::Chain(state|)Active means we may end up acting on two
different CChainStates should we write more extensive tests in the
future.

Adding a new ChainTestingSetup which performs all initialization
previously done by TestingSetup except:

1. Mempool sanity check frequency setting
2. ChainState initialization
3. Genesis Activation
4. {Ban,Conn,Peer}Man initialization

Means that we will no longer need to initialize a duplicate
ChainstateManger in order to test the initialization codepaths of
CChainState and ChainstateManager. TestingSetup can also cleanly
inherits from this new ChainTestingSetup.

Lastly, this change has the additional benefit of allowing for
review-only assertions meant to show correctness to work in future work
de-globalizing g_chainman.
[META] This commit should be followed up by a scripted-diff commit which
       fixes calls to LookupBlockIndex tree-wide.
[META] This commit should be followed up by removing the comments and
       assertions meant only to show that the change is correct.

LookupBlockIndex only acts on BlockManager.
[META] In a previous commit, we moved ::LookupBlockIndex to become a
       member function of BlockManager. This commit is split out from
       that one since it can be expressed nicely as a scripted-diff.

-BEGIN VERIFY SCRIPT-
find_regex='LookupBlockIndex' \
    && git grep -l -E "$find_regex" -- src \
        | grep -v '^src/validation\.\(cpp\|h\)$' \
        | xargs sed -i -E "s@${find_regex}@g_chainman.m_blockman.LookupBlockIndex@g"
-END VERIFY SCRIPT-
[META] This commit should be followed up by removing the comments and
       assertions meant only to show that the change is correct.

FindForkInGlobalIndex only acts on BlockManager.

Note to reviewers: Since FindForkInGlobalIndex is always called with
::ChainActive() as its first parameter, it is possible to move
FindForkInGlobalIndex to CChainState and remove this const CChain&
parameter to instead use m_chain. However, it seems like the original
intention was for FindForkInGlobalIndex to work with _any_ chain, not
just the current active chain. Let me know if this should be changed.
[META] This commit should be followed up by removing the comments and
       assertions meant only to show that the change is correct.

GetSpendHeight only acts on BlockManager.
[META] This commit should be followed up by removing the comments and
       assertions meant only to show that the change is correct.

GetLastCheckPoint mainly acts on BlockManager.
[META] This commit should be followed up by removing the comments and
       assertions meant only to show that the change is correct.
Also guard it with ::cs_main like ChainstateManager does.
[META] This commit should be followed up by removing the comments and
       assertions meant only to show that the change is correct.
[META] This commit should be followed up by removing the comments and
       assertions meant only to show that the change is correct.
Instead use CChainState::ActivateBestChain, which is what the global one
calls anyway.
[META] This commit should be followed up by removing the comments and
       assertions meant only to show that the change is correct.

LoadExternalBlockFile mainly acts on CChainState.
…lockHeaders

[META] This commit should be followed up by removing the comments and
       assertions meant only to show that the change is correct.
[META] This commit should be followed up by removing the comments and
       assertions meant only to show that the change is correct.
-BEGIN VERIFY SCRIPT-
git grep -lwF 'assert(std::addressof' | xargs sed -i -e '/assert(std::addressof/d'
-END VERIFY SCRIPT-
@dongcarl dongcarl force-pushed the 2020-09-libbitcoinruntime-v4 branch from 41265b2 to 1ce3af3 Compare October 15, 2020 19:11
@dongcarl
Copy link
Contributor Author

dongcarl commented Oct 15, 2020

Closing temporarily for more definite decisions on how to split up #20158

@dongcarl dongcarl closed this Oct 15, 2020
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Feb 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants