The global ::BlockIndex() is problematic for several reasons:
- It returns a mutable reference to the block tree, without the appropriate lock annotation (m_block_index is guarded by cs_main). The current code is fine, but in the future this might lead to accidental races and data corruption.
- The rpc server shouldn't rely on node globals, but rather a context that is passed in to the RPC method.
- Tests might want to spin up their own block tree, and thus should also not rely on a single global.
Fix all issues by removing the global
This is a backport of core#19413