-
Notifications
You must be signed in to change notification settings - Fork 37.3k
tests: Create or use existing properly initialized NodeContexts #20323
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
Conversation
Concept ACK. Thanks for moving ChainTestingSetup into validation_chainstatemanager_tests.cpp! |
Concept ACK |
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. |
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.
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.
utACK 3938763
Some suggestions to remove unneeded code inline.
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.
Concept ACK.
3938763
to
12399f0
Compare
Addressed a few comments. fuzz CI failing because of #20188 (comment). |
12399f0
to
1319b17
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 1319b17. Only changes since last review were suggested simplifications to ChainTestingSetup constructor
Hopefully as a test-only cleanup PR with a lot of other changes building on top it (#20158), this can be merged soon |
Reason I didn't merge this is that this overlaps with the test changes here: #19425 (review). Though there have been outstanding questions and those questions also apply to this pull (in the extent that they overlap). |
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.
Reason I didn't merge this is that this overlaps with the test changes here: #19425 (review). Though there have been outstanding questions and those questions also apply to this pull (in the extent that they overlap).
Thanks. I guess I need to rebase #19425. I'll check out the test changes too there and make updates if needed to make sure they are compatible with this PR.
This PR just takes approach of having fewer duplicate disconnected state objects inside tests, so I don't think there are real questions about it or major drawbacks. But if anyone has concerns or questions (even vague ones), it'd be good to post something so Carl can reply and this work doesn't languish
@@ -122,6 +167,7 @@ BOOST_AUTO_TEST_CASE(chainstatemanager_rebalance_caches) | |||
{ | |||
LOCK(::cs_main); | |||
c1.InitCoinsCache(1 << 23); | |||
assert(c1.LoadGenesisBlock(Params())); |
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 "test: Add new ChainTestingSetup and use it" (43c880a)
Would suggest using BOOST_REQUIRE
instead of assert
or splitting as bool loaded = c1.LoadGenesisBlock(Params()); assert(loaded);
. Writing code with side effects in an assert
adds some ambiguity because behavior will depend on value of NDEBUG
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.
Oh! Didn't know about NDEBUG
... So when NDEBUG
is set, the code inside assert(...)
may be optimized out? Or is it the case that only the result isn't checked/asserted?
utACK 1319b17 |
1319b17
to
11fac8b
Compare
In (3), ordering-wise, the script-checking thread initialization was moved up to before the chainstate gets initialized and RPC command registration was moved down. Neither of which should have any tangible effect on the correctness of initialization. I can also remove the |
I've had a quick look through the new branch and find the Was there an actual problem with the previous branch, or are you just trying to reduce code duplication? |
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.
left some comments
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 11fac8b. Changes since last review are deduping test init functions and replacing asserts (thanks!).
Other review suggestions for splitting up functions instead of using a bool arguments seem reasonable.
Another idea (not sure whether it is good or applicable): an alternative to calling pairs of functions in constructors and destructors is to just add class members. Member constructors will run in containing class's default constructor, and same with destructors. Seems like this would allow getting rid of the 4 one-line constructor/destructors.
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.
11fac8b
to
81137c6
Compare
Thanks everyone for the reviews. The latest push 81137c6 is now rebased over #19485. This PR is intended to just move things around a bit for the rest of the Personally, I don't particularly care how the abstraction works here. The I do fully intend on coming back to the initialization codepaths later, most likely after having moved a couple of mutable statics into classes they logically belong in, and having a larger PR that coalesces and cleans all of this up. In this latest push I:
There wasn't an actual problem with the previous branch, MarcoFalke brought up the concern that it was harder to maintain duplicate codepaths, so this previous push was a (perhaps poorly conceived) attempt at doing so. I agree w/re the bool argument and have since reverted to something similar to how I originally attempted this which avoids using a bool argument. |
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 81137c6. This change is simpler after the rebase because wallet & bench commits are dropped.
Along with the rebase, new test init functions were dropped again in response to #20323 (comment), which is fine. I think in the longer run more init code can be moved into RAII classes, and we won't need these intricate initialization functions and class hierarchies (tests could just state their resource requirements as variable declarations).
Thanks for shuffling this around for over a month ACK 81137c6 looking excellent now 🐩 Show signature and timestampSignature:
Timestamp of file with hash |
Ok, let's wait for @jnewbery 's green light before merge |
ACK 81137c6 |
This is part 1/n of the effort to de-globalize
ChainstateManager
Reviewers: Looking for tested/Code-Review/plain-ACKs
Context
In many of our tests, we manually instantiate
NodeContext
s orChainstateManager
s in the test code, which is error prone. Instead, we should create or use existing references because:ChainstateManager
, much of our code still acts ong_chainman
(our globalChainstateManager
), sometimes even when you're calling a method on a specific instance ofChainstateManager
! This means that we may act on two instances ofChainstateManager
, which is most likely not what we want.{Basic,}TestingSetup
constructors) means that you're acting on objects which are properly initialized, instead of "just initialized enough for this dang test to pass". Also, they're already there! It's free!ChainstateManager
to work and demonstrate correctness.Some more detailed debugging notes can be found in the first commit, reproduced below: