blockmanager: avoid undefined behavior during FlushBlockFile
blockmanager: avoid undefined behavior during FlushBlockFile

If we call FlushBlockFile() without having intitialized the block index
with LoadBlockIndexDB(), we may be indexing into an empty vector.

Specifically this is an issue when we call MaybeRebalanceCaches() during
chainstate init before the block index has been loaded, which calls
FlushBlockFile().

Also add an assert to avoid undefined behavior.

Co-authored-by: Russell Yanofsky <d1818e90f775d1fa111cabb0c1a59ce802530221@yanofsky.org>

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