Improve chainstate/blockindex disk writing policy

There are 3 pieces of data that are maintained on disk. The actual block and undo data, the block index (which can refer to positions on disk), and the chainstate (which refers to the best block hash).

Earlier, there was no guarantee that blocks were written to disk before block index entries referring to them were written. This commit introduces dirty flags for block index data, and delays writing it until the actual block data is flushed.

With this stricter ordering in writes, it is now safe to not always flush after every block, so there is no need for the IsInitialBlockDownload() check there - instead we just write whenever enough time has passed or the cache size grows too large.

In addition, only do a write inside the block processing loop if necessary (because of cache size exceeded). Otherwise, move the writing to a point after processing is done, after relaying.

This should improve block relay speed, by not writing chainstate and index entries before announcing the new hash. 

Future work: move block and undo data writing into FlushStateToDisk, and move it to a background thread.
Parametrization on multiple bool parameters leads to hard-to-read code; I'd prefer to pass an enum, or bit field