validation: Guard the active_chainstate with cs_main
Summary:
This avoids a potential race-condition where a thread is reading the
ChainstateManager::m_active_chainstate pointer while another one is
writing to it. There is no portable guarantee that reading/writing the
pointer is thread-safe.
This is also done in way that mimics ::ChainstateActive(), so the
transition from that function to this method is easy.
This is a backport of core#20749 [1/17]
https://github.com/bitcoin/bitcoin/pull/20749/commits/f92dc6557a153b390a1ae1d0808ff7ed5d02c66e
Depends on D11171
Test Plan:
With debug:
ninja all check-all
Reviewers: #bitcoin_abc, Fabien
Reviewed By: #bitcoin_abc, Fabien
Differential Revision: https://reviews.bitcoinabc.org/D11172