<|start_filename|>src/pow.cpp<|end_filename|>
// Copyright (c) 2009-2010 <NAME>
// Copyright (c) 2009-2018 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <pow.h>

#include <arith_uint256.h>
#include <chain.h>
#include <powdata.h>
#include <uint256.h>

unsigned int
GetNextWorkRequired (const PowAlgo algo, const CBlockIndex* pindexLast,
                     const Consensus::Params& params)
{
  const arith_uint256 bnPowLimit
      = UintToArith256 (powLimitForAlgo (algo, params));

  if (pindexLast == nullptr || params.fPowNoRetargeting)
      return bnPowLimit.GetCompact ();

  /* The actual difficulty update below is DGW taken from Dash, except that
     we have to look at blocks of only one algo explicitly.  */

  constexpr int64_t nPastBlocks = 24;

  pindexLast = pindexLast->GetLastAncestorWithAlgo (algo);
  if (pindexLast == nullptr)
    return bnPowLimit.GetCompact ();

  const CBlockIndex* pindex = pindexLast;
  const CBlockIndex* pindexFirst = nullptr;
  arith_uint256 bnResult;
  for (size_t nCountBlocks = 1; nCountBlocks <= nPastBlocks; ++nCountBlocks)
    {
      assert (pindex != nullptr && pindex->algo == algo);

      arith_uint256 bnTarget;
      bnTarget.SetCompact (pindex->nBits);

      if (nCountBlocks == 1)
        bnResult = bnTarget;
      else
        bnResult = (bnResult * nCountBlocks + bnTarget) / (nCountBlocks + 1);

      if (nCountBlocks == nPastBlocks)
        pindexFirst = pindex;

      /* We need to step back to the last block with the given algo, but at
         least one block.  Note that GetLastAncestorWithAlgo returns the
         block itself if it matches.  */
      pindex = pindex->pprev;
      if (pindex != nullptr)
        pindex = pindex->GetLastAncestorWithAlgo (algo);

      if (pindex == nullptr)
        return bnPowLimit.GetCompact ();
    }

  int64_t nActualTimespan = pindexLast->GetBlockTime ()
                              - pindexFirst->GetBlockTime ();
  const int nextHeight = pindexLast->nHeight + 1;
  int64_t nTargetTimespan
      = nPastBlocks * params.rules->GetTargetSpacing(algo, nextHeight);

  if (nActualTimespan < nTargetTimespan / 3)
    nActualTimespan = nTargetTimespan / 3;
  if (nActualTimespan > nTargetTimespan * 3)
    nActualTimespan = nTargetTimespan * 3;

  bnResult *= nActualTimespan;
  bnResult /= nTargetTimespan;

  if (bnResult > bnPowLimit)
    bnResult = bnPowLimit;

  return bnResult.GetCompact ();
}


<|start_filename|>contrib/xaya/huc-snapshot/keyConversion.js<|end_filename|>
/*
Copyright (c) 2018 The Xaya developers
Distributed under the MIT software license, see the accompanying
file COPYING or http://www.opensource.org/licenses/mit-license.php.
*/

/* Very basic JavaScript code that converts WIF private keys from Huntercoin
   to the key format used by Xaya (with the same underlying PKH).  It is based
   on the bs58check Node module for the base58check logic.

   The code is used to build a stand-alone HTML file that can be used
   online or offline to convert the keys.  This will be useful for claiming
   CHI balances from the Huntercoin snapshot.  */

let bs58check = require ('bs58check');

function convertWifKey (huc)
{
  const chiPrivkeyVersion = 130;
  data = bs58check.decode (huc);
  data[0] = chiPrivkeyVersion;
  return bs58check.encode (data);
}

function update ()
{
  const domIdHuc = "huc-privkey";
  const domIdChi = "chi-privkey";

  let huc = document.getElementById (domIdHuc).value;
  let chi;
  try
    {
      chi = convertWifKey (huc);
    }
  catch (err)
    {
      chi = "<invalid Huntercoin private key>";
    }
  let chiField = document.getElementById (domIdChi);
  chiField.value = chi;
}

function init ()
{
  const domIdForm = "conversion-form";
  document.getElementById (domIdForm).onsubmit = function ()
    {
      update ();

      /* Don't actually submit the form.  */
      return false;
    };
}

window.addEventListener ("load", init);
