index: Improve BaseIndex::BlockUntilSyncedToCurrentChain reliability
index: Improve BaseIndex::BlockUntilSyncedToCurrentChain reliability

Since commit f08c9fb0c6a799e3cb75ca5f763a746471625beb from PR
https://github.com/bitcoin/bitcoin/pull/21726, index
`BlockUntilSyncedToCurrentChain` behavior has been less reliable, and there has
also been a race condition in the `coinstatsindex_initial_sync` unit test.

It seems better for `BlockUntilSyncedToCurrentChain` to actually wait for the
last connected block to be fully processed, than to be able to return before
prune locks are set, so this switches the order of `m_best_block_index =
block;` and `UpdatePruneLock` statements in `SetBestBlockIndex` to make it more
reliable.

Also since commit f08c9fb0c6a799e3cb75ca5f763a746471625beb, there has been a
race condition in the `coinstatsindex_initial_sync` test. Before that commit,
the atomic index best block pointer `m_best_block_index` was updated as the
last step of `BaseIndex::BlockConnected`, so `BlockUntilSyncedToCurrentChain`
could safely be used in tests to wait for the last `BlockConnected`
notification to be finished before stopping and destroying the index. But
after that commit, calling `BlockUntilSyncedToCurrentChain` is no longer
sufficient, and there is a race between the test shutdown code which destroys
the index object and the new code introduced in that commit calling
`AllowPrune()` and `GetName()` on the index object. Reproducibility
instructions for this are in
https://github.com/bitcoin/bitcoin/issues/25365#issuecomment-1259744133

This commit fixes the `coinstatsindex_initial_sync` race condition, even though
it will require an additional change to silence TSAN false positives,
https://github.com/bitcoin/bitcoin/pull/26188, after it is fixed. So this
partially addresses but does not resolve the bug reporting TSAN errors
https://github.com/bitcoin/bitcoin/issues/25365.

There is no known race condition outside of test code currently, because the
bitcoind `Shutdown` function calls `FlushBackgroundCallbacks` not
`BlockUntilSyncedToCurrentChain` to safely shut down.

Co-authored-by: Vasil Dimov <ad45205a471752ff1e17b788c3e52521e319899e@FreeBSD.org>
Co-authored-by: MacroFake <94662c316543fb349a23cc41ada52e5549f80958@gmail.com>

AkioNak/bitcoin,tecnovert/particl-core,jambolo/bitcoin,sipsorcery/bitcoin,lateminer/bitcoin,particl/particl-core,namecoin/namecoin-core,sstone/bitcoin,mruddy/bitcoin,jambolo/bitcoin,lateminer/bitcoin,sstone/bitcoin,bitcoinsSG/bitcoin,fanquake/bitcoin,bitcoin/bitcoin,namecoin/namecoin-core,bitcoin/bitcoin,ajtowns/bitcoin,mruddy/bitcoin,sipsorcery/bitcoin,ajtowns/bitcoin,jambolo/bitcoin,fanquake/bitcoin,bitcoin/bitcoin,sstone/bitcoin,namecoin/namecoin-core,lateminer/bitcoin,AkioNak/bitcoin,tecnovert/particl-core,kallewoof/bitcoin,fanquake/bitcoin,jambolo/bitcoin,tecnovert/particl-core,kallewoof/bitcoin,ajtowns/bitcoin,mruddy/bitcoin,sstone/bitcoin,tecnovert/particl-core,kallewoof/bitcoin,lateminer/bitcoin,ajtowns/bitcoin,ajtowns/bitcoin,kallewoof/bitcoin,fanquake/bitcoin,ajtowns/bitcoin,particl/particl-core,sstone/bitcoin,lateminer/bitcoin,bitcoinsSG/bitcoin,AkioNak/bitcoin,jambolo/bitcoin,sipsorcery/bitcoin,bitcoinsSG/bitcoin,namecoin/namecoin-core,bitcoinsSG/bitcoin,bitcoin/bitcoin,lateminer/bitcoin,tecnovert/particl-core,bitcoin/bitcoin,particl/particl-core,bitcoin/bitcoin,fanquake/bitcoin,jambolo/bitcoin,AkioNak/bitcoin,mruddy/bitcoin,sipsorcery/bitcoin,particl/particl-core,particl/particl-core,kallewoof/bitcoin,bitcoinsSG/bitcoin,bitcoinsSG/bitcoin,particl/particl-core,fanquake/bitcoin,sstone/bitcoin,sipsorcery/bitcoin,tecnovert/particl-core,namecoin/namecoin-core,mruddy/bitcoin,AkioNak/bitcoin,sipsorcery/bitcoin,kallewoof/bitcoin,AkioNak/bitcoin,mruddy/bitcoin,namecoin/namecoin-core