Does it make sense to change the order and avoid locking cs_main or mempool.cs?
Something like:
bool GetTransaction(const uint256& hash, CTransactionRef& txOut, const Consensus::Params& consensusParams, uint256& hashBlock, const CBlockIndex* const block_index)
{
if (g_txindex) {
return g_txindex->FindTx(hash, hashBlock, txOut);
}
LOCK(cs_main);
...
I believe it would require to add some read-write mutex to TxIndex.