Page MenuHomePhabricator

[refactor] make the finalized BlockIndex* a private member of CChainState

Authored by majcosta on Sun, Sep 27, 19:39.



Upcoming Core PR16443 adds g_chainstate to validation.cpp, this caused a bit of a havoc with initializaion order of pindexFinalized (also declared in validation.cpp) so I took this opportunity to encapsulate the finalizedBlockIndex pointer into CChainState


  • moved an assertion that cs_main lock must be held down to where the actual pointer is accessed, to guarantee that all code paths trigger that.
  • added an assert to prevent users from changing the finalizationBlockIndex pointer to nullptr
Test Plan
cmake .. -DENABLE_SANITIZERS=address
ninja all check check-functional

Diff Detail

rABC Bitcoin ABC
Automatic diff as part of commit; lint not applicable.
Automatic diff as part of commit; unit tests not applicable.

Event Timeline

[Bot Message]
One or more PR numbers were detected in the summary.
Links to those PRs have been inserted into the summary for reference.

deadalnix requested changes to this revision.Sun, Sep 27, 23:03
deadalnix added a subscriber: deadalnix.
deadalnix added inline comments.
954 ↗(On Diff #23827)

Don't define state in random places. Also, this is to be the only blockindex that is in CChainState. What about others?

956 ↗(On Diff #23827)

I don't see what the function brings, especially since nullptr is a valid state.

This revision now requires changes to proceed.Sun, Sep 27, 23:03

moved m_finalizedBlockIndex initalization closer to the rest of CChainState state declarations and eliminated the setter function for it

majcosta marked an inline comment as done.
deadalnix requested changes to this revision.Mon, Sep 28, 22:26

Most calls to finalizedBlockIndex seems completely redundant. This is looking better.

3333 ↗(On Diff #23907)

Why is this removed?

821 ↗(On Diff #23907)

Isn't this supposed to be guarded by cs_main?

895 ↗(On Diff #23907)

Do not change comments for no reason.

956 ↗(On Diff #23907)


This revision now requires changes to proceed.Mon, Sep 28, 22:26
majcosta added inline comments.
3333 ↗(On Diff #23907)

Actually I moved it to const CBlockIndex* finalizedBlock const because that is now the "lowest level" function call that accesses m_finalizedBlockIndex, so that ensures users always hit the AssertLockHeld(cs_main).

That's also why I was using finalizedBlockIndex() consistently/redundantly, make sure all codepaths led to that assertion, although that is a weaker argument now that the thread safety annotation is in place.

821 ↗(On Diff #23907)

Yep, will do.

majcosta marked 2 inline comments as done.

addressed comments

finalizedBlockIndex() -> FinalizedBlockIndex()

deadalnix requested changes to this revision.Tue, Sep 29, 01:48

Many calls to FinalizedBlockIndex are still redundant.

This revision now requires changes to proceed.Tue, Sep 29, 01:48

used member variable name rather than accessor within member functions

majcosta edited the summary of this revision. (Show Details)
deadalnix requested changes to this revision.Tue, Sep 29, 09:26
deadalnix added inline comments.
3218 ↗(On Diff #23937)

Considering this has just one call site, some more refactoring seems to be in order.

3332 ↗(On Diff #23937)

This require locking annotations.

5019 ↗(On Diff #23937)

It is not freed in this function, so how can you possibly know it has been freed? (hint: it hasn't yet).

This revision now requires changes to proceed.Tue, Sep 29, 09:26
  • made GetFinalizedBlock the only read-accessor to m_finalizedBlockIndex and added a cs_main lock required annotation
  • included IsBlockFinalized into CChainState, const qualified it.
  • corrected comment in CChainState::UnloadBlockIndex() about setting m_finalizedBlockIndex to null before it's deleted
deadalnix requested changes to this revision.Wed, Sep 30, 07:05

Now this is much better. A small nit and this is good to go.

3222 ↗(On Diff #24041)

Keep this next to IsBlockFinalized

This revision now requires changes to proceed.Wed, Sep 30, 07:05

moved GetFinalizedBlock implementation closer to same-context IsBlockFinalized

This revision is now accepted and ready to land.Wed, Sep 30, 22:57