message: validation: Pass in chain to FindBlockPos+SaveBlockToDisk
diff: @@ -3066,7 +3066,7 @@ void CChainState::ReceivedBlockTransactions(const CBlock& block, CBlockIndex* pi
  }
  }
  -static bool FindBlockPos(FlatFilePos &pos, unsigned int nAddSize, unsigned int nHeight, uint64_t nTime, bool fKnown = false)
  +static bool FindBlockPos(FlatFilePos &pos, unsigned int nAddSize, unsigned int nHeight, CChain& active_chain, uint64_t nTime, bool fKnown = false)
  {
  LOCK(cs_LastBlockFile);
  @@ -3081,7 +3081,8 @@ static bool FindBlockPos(FlatFilePos &pos, unsigned int nAddSize, unsigned int n
  // when the undo file is keeping up with the block file, we want to flush it explicitly
  // when it is lagging behind (more blocks arrive than are being connected), we let the
  // undo block write case handle it
  - finalize_undo = (vinfoBlockFile[nFile].nHeightLast == (unsigned int)ChainActive().Tip()->nHeight);
  + assert(std::addressof(::ChainActive()) == std::addressof(active_chain));
  + finalize_undo = (vinfoBlockFile[nFile].nHeightLast == (unsigned int)active_chain.Tip()->nHeight);
  nFile++;
  if (vinfoBlockFile.size() <= nFile) {
  vinfoBlockFile.resize(nFile + 1);
  @@ -3584,12 +3585,12 @@ bool ChainstateManager::ProcessNewBlockHeaders(int32_t& nPoSTemperature, const u
  }
  /** Store block on disk. If dbp is non-nullptr, the file is known to already reside on disk */
  -static FlatFilePos SaveBlockToDisk(const CBlock& block, int nHeight, const CChainParams& chainparams, const FlatFilePos* dbp) {
  +static FlatFilePos SaveBlockToDisk(const CBlock& block, int nHeight, CChain& active_chain, const CChainParams& chainparams, const FlatFilePos* dbp) {
  unsigned int nBlockSize = ::GetSerializeSize(block, CLIENT_VERSION);
  FlatFilePos blockPos;
  if (dbp != nullptr)
  blockPos = *dbp;
  - if (!FindBlockPos(blockPos, nBlockSize+8, nHeight, block.GetBlockTime(), dbp != nullptr)) {
  + if (!FindBlockPos(blockPos, nBlockSize+8, nHeight, active_chain, block.GetBlockTime(), dbp != nullptr)) {
  error("%s: FindBlockPos failed", __func__);
  return FlatFilePos();
  }
  @@ -3682,7 +3683,7 @@ bool CChainState::AcceptBlock(const std::shared_ptr<const CBlock>& pblock, Block
  // Write block to history file
  if (fNewBlock) *fNewBlock = true;
  try {
  - FlatFilePos blockPos = SaveBlockToDisk(block, pindex->nHeight, chainparams, dbp);
  + FlatFilePos blockPos = SaveBlockToDisk(block, pindex->nHeight, ::ChainActive(), chainparams, dbp);
  if (blockPos.IsNull()) {
  state.Error(strprintf("%s: Failed to find position to write new block to disk", __func__));
  return false;
  @@ -4373,7 +4374,7 @@ bool CChainState::LoadGenesisBlock(const CChainParams& chainparams)
  try {
  const CBlock& block = chainparams.GenesisBlock();
  - FlatFilePos blockPos = SaveBlockToDisk(block, 0, chainparams, nullptr);
  + FlatFilePos blockPos = SaveBlockToDisk(block, 0, ::ChainActive(), chainparams, nullptr);
  if (blockPos.IsNull())
  return error("%s: writing genesis block to disk failed", __func__);
  CBlockIndex *pindex = m_blockman.AddToBlockIndex(block);
  