HomePhabricator

test: Add new ChainTestingSetup and use it

Description

test: Add new ChainTestingSetup and use it

Summary:
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.

This change adds a new ChainTestingSetup which performs all
initialization previously done by TestingSetup except:

  1. RPC command registration
  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.

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.

In the test chainstatemanager_rebalance_caches, an additional
LoadGenesisBlock call is added as MaybeReblanaceCaches eventually calls
FlushBlockFile, which tries to access vinfoBlockFile[nLastBlockFile],
which is out of bounds when LoadGenesisBlock hasn't been called yet.


Note for the future:

The class con/destructor inheritance structure we have for these
TestingSetup classes is probably not the most suitable abstraction. In
particular, for both TestingSetup and ChainTestingSetup, we need to stop
the scheduler first before anything else. Otherwise classes depending on
the scheduler may be referenced by the scheduler after said classes are
freed. This means that there's no clear parallel between our teardown
code and C++'s destructuring order for class hierarchies.

Future work should strive to coalesce (as much as possible) test and
non-test init codepaths and perhaps structure it in a more fail-proof
way.

This concludes backport of core#20323
https://github.com/bitcoin/bitcoin/pull/20323/commits/81137c60fe6234569e1c5e6760f3a6f016956944

Depends on D11170

Test Plan: ninja check

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Subscribers: Fabien

Differential Revision: https://reviews.bitcoinabc.org/D11171

Details

Provenance
Carl Dong <contact@carldong.me>Authored on Oct 13 2020, 20:55
PiRKCommitted on Mar 16 2022, 09:09
PiRKPushed on Mar 16 2022, 09:09
Reviewer
Restricted Project
Differential Revision
D11171: test: Add new ChainTestingSetup and use it
Parents
rABCfe2104e05494: qt/test: [FIX] Add forgotten Context setting in RPCNestedTests
Branches
Unknown
Tags
Unknown