message: Do some refactoring in pow.cpp to make it easier to introduce new difficulty adjustement algorithm.
  Summary: As per title.
  Test Plan:
  make check
  Reviewers: freetrader, schancel, sickpig, dagurval, #bitcoin_abc
diff: @@ -88,6 +88,82 @@ uint32_t GetNextWorkRequired(const CBlockIndex *pindexLast, const CBlockHeader *
  }
  #else
  +/**
  + * Compute the next required proof of work using the legacy Bitcoin difficulty
  + * adjustement + Emergency Difficulty Adjustement (EDA).
  + */
  +static uint32_t GetNextEDAWorkRequired(const CBlockIndex *pindexPrev,
  + const CBlockHeader *pblock,
  + const Consensus::Params &params)
  +{
  + // Only change once per difficulty adjustment interval
  + uint32_t nHeight = pindexPrev->nHeight + 1;
  + if (nHeight % params.DifficultyAdjustmentInterval() == 0)
  + {
  + // Go back by what we want to be 14 days worth of blocks
  + assert(nHeight >= params.DifficultyAdjustmentInterval());
  + uint32_t nHeightFirst = nHeight - params.DifficultyAdjustmentInterval();
  + const CBlockIndex *pindexFirst = pindexPrev->GetAncestor(nHeightFirst);
  + assert(pindexFirst);
  +
  + return CalculateNextWorkRequired(pindexPrev, pindexFirst->GetBlockTime(), params);
  + }
  +
  + const uint32_t nProofOfWorkLimit = UintToArith256(params.powLimit).GetCompact();
  +
  + if (params.fPowAllowMinDifficultyBlocks)
  + {
  + // Special difficulty rule for testnet:
  + // If the new block's timestamp is more than 2* 10 minutes then allow
  + // mining of a min-difficulty block.
  + if (pblock->GetBlockTime() > pindexPrev->GetBlockTime() + 2 * params.nPowTargetSpacing)
  + {
  + return nProofOfWorkLimit;
  + }
  +
  + // Return the last non-special-min-difficulty-rules-block
  + const CBlockIndex *pindex = pindexPrev;
  + while (pindex->pprev && pindex->nHeight % params.DifficultyAdjustmentInterval() != 0 &&
  + pindex->nBits == nProofOfWorkLimit)
  + {
  + pindex = pindex->pprev;
  + }
  +
  + return pindex->nBits;
  + }
  +
  + // We can't go bellow the minimum, so early bail.
  + uint32_t nBits = pindexPrev->nBits;
  + if (nBits == nProofOfWorkLimit)
  + {
  + return nProofOfWorkLimit;
  + }
  +
  + // If producing the last 6 block took less than 12h, we keep the same
  + // difficulty.
  + const CBlockIndex *pindex6 = pindexPrev->GetAncestor(nHeight - 7);
  + assert(pindex6);
  + int64_t mtp6blocks = pindexPrev->GetMedianTimePast() - pindex6->GetMedianTimePast();
  + if (mtp6blocks < 12 * 3600)
  + {
  + return nBits;
  + }
  +
  + // If producing the last 6 block took more than 12h, increase the difficulty
  + // target by 1/4 (which reduces the difficulty by 20%). This ensure the
  + // chain do not get stuck in case we lose hashrate abruptly.
  + arith_uint256 nPow;
  + nPow.SetCompact(nBits);
  + nPow += (nPow >> 2);
  +
  + // Make sure we do not go bellow allowed values.
  + const arith_uint256 bnPowLimit = UintToArith256(params.powLimit);
  + if (nPow > bnPowLimit)
  + nPow = bnPowLimit;
  +
  + return nPow.GetCompact();
  +}
  +
  uint32_t GetNextWorkRequired(const CBlockIndex *pindexPrev, const CBlockHeader *pblock, const Consensus::Params &params)
  {
  // Genesis block
  