text: // Copyright (c) 2010 Satoshi Nakamoto
  // Copyright (c) 2009-2016 The Helveticum Core developers
  // Distributed under the MIT software license, see the accompanying
  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  
  #include "chainparams.h"
  #include "consensus/merkle.h"
  
  #include "tinyformat.h"
  #include "util.h"
  #include "utilstrencodings.h"
  
  #include <assert.h>
  
  #include "chainparamsseeds.h"
  
  static CBlock CreateGenesisBlock(const char* pszTimestamp, const CScript& genesisOutputScript, uint32_t nTime, uint32_t nNonce, uint32_t nBits, int32_t nVersion, const CAmount& genesisReward)
  {
      CMutableTransaction txNew;
      txNew.nVersion = 1;
      txNew.vin.resize(1);
      txNew.vout.resize(1);
      txNew.vin[0].scriptSig = CScript() << 486604799 << CScriptNum(4) << std::vector<unsigned char>((const unsigned char*)pszTimestamp, (const unsigned char*)pszTimestamp + strlen(pszTimestamp));
      txNew.vout[0].nValue = genesisReward;
      txNew.vout[0].scriptPubKey = genesisOutputScript;
  
      CBlock genesis;
      genesis.nTime    = nTime;
      genesis.nBits    = nBits;
      genesis.nNonce   = nNonce;
      genesis.nVersion = nVersion;
      genesis.vtx.push_back(MakeTransactionRef(std::move(txNew)));
      genesis.hashPrevBlock.SetNull();
      genesis.hashMerkleRoot = BlockMerkleRoot(genesis);
      return genesis;
  }
  
  /**
   * Build the genesis block. Note that the output of its generation
   * transaction cannot be spent since it did not originally exist in the
   * database.
   *
   * CBlock(hash=000000000019d6, ver=1, hashPrevBlock=00000000000000, hashMerkleRoot=4a5e1e, nTime=1231006505, nBits=1d00ffff, nNonce=2083236893, vtx=1)
   *   CTransaction(hash=4a5e1e, ver=1, vin.size=1, vout.size=1, nLockTime=0)
   *     CTxIn(COutPoint(000000, -1), coinbase 04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73)
   *     CTxOut(nValue=50.00000000, scriptPubKey=0x5F1DF16B2B704C8A578D0B)
   *   vMerkleTree: 4a5e1e
   */
  static CBlock CreateGenesisBlock(uint32_t nTime, uint32_t nNonce, uint32_t nBits, int32_t nVersion, const CAmount& genesisReward)
  {
      const char* pszTimestamp = "CONFOEDERATIO HELVETICA, 12910801, UNUS PRO OMNIBUS, OMNES PRO UNO";
      const CScript genesisOutputScript = CScript() << ParseHex("04aede0ef9e6aec2b6bb9db2932045043cbf0feea776aafd374a8091b05c2bb684dedf15b6e91d5cd8c4b93cf313c0bd973d95f07b1d21749593ac042d657880e9") << OP_CHECKSIG;
      return CreateGenesisBlock(pszTimestamp, genesisOutputScript, nTime, nNonce, nBits, nVersion, genesisReward);
  }
  
  void CChainParams::UpdateVersionBitsParameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_t nTimeout)
  {
      consensus.vDeployments[d].nStartTime = nStartTime;
      consensus.vDeployments[d].nTimeout = nTimeout;
  }
  
  /**
   * Main network
   */
  /**
   * What makes a good checkpoint block?
   * + Is surrounded by blocks with reasonable timestamps
   *   (no blocks before with a timestamp after, none after with
   *    timestamp before)
   * + Contains no strange transactions
   */
  
  class CMainParams : public CChainParams {
  public:
      CMainParams() {
          strNetworkID = "main";
          consensus.nSubsidyHalvingInterval = 210000;
          consensus.BIP34Height = 0;
          consensus.BIP34Hash = uint256S("0x00000000f5d7795a2faa48e50c639c3c207fec39447acd4b02183886c81d5bd1");
          consensus.BIP65Height = 0; // 
          consensus.BIP66Height = 0; // 
          consensus.powLimit = uint256S("00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
          consensus.nPowTargetTimespan = 14 * 24 * 60 * 60; // two weeks
          consensus.nPowTargetSpacing = 10 * 60;
          consensus.fPowAllowMinDifficultyBlocks = false;
          consensus.fPowNoRetargeting = false;
          consensus.nRuleChangeActivationThreshold = 1916; // 95% of 2016
          consensus.nMinerConfirmationWindow = 2016; // nPowTargetTimespan / nPowTargetSpacing
          consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].bit = 28;
          consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nStartTime = 1199145601; // January 1, 2008
          consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nTimeout = 1230767999; // December 31, 2008
  
          // Deployment of BIP68, BIP112, and BIP113.
          consensus.vDeployments[Consensus::DEPLOYMENT_CSV].bit = 0;
          consensus.vDeployments[Consensus::DEPLOYMENT_CSV].nStartTime = 1462060800; // May 1st, 2016
          consensus.vDeployments[Consensus::DEPLOYMENT_CSV].nTimeout = 1493596800; // May 1st, 2017
  
          // Deployment of SegWit (BIP141, BIP143, and BIP147)
          consensus.vDeployments[Consensus::DEPLOYMENT_SEGWIT].bit = 1;
          consensus.vDeployments[Consensus::DEPLOYMENT_SEGWIT].nStartTime = 1479168000; // November 15th, 2016.
          consensus.vDeployments[Consensus::DEPLOYMENT_SEGWIT].nTimeout = 1510704000; // November 15th, 2017.
  
          // The best chain should have at least this much work.
          consensus.nMinimumChainWork = uint256S("0x00");
  
          // By default assume that the signatures in ancestors of this block are valid.
          consensus.defaultAssumeValid = uint256S("0x00000000f5d7795a2faa48e50c639c3c207fec39447acd4b02183886c81d5bd1"); //453354
  
          /**
           * The message start string is designed to be unlikely to occur in normal data.
           * The characters are rarely used upper ASCII, not valid as UTF-8, and produce
           * a large 32-bit integer with any alignment.
           */
          pchMessageStart[0] = 0xf9;
          pchMessageStart[1] = 0xbe;
          pchMessageStart[2] = 0xb4;
          pchMessageStart[3] = 0xd9;
          nDefaultPort = 2113;
          nPruneAfterHeight = 100000;
  
          genesis = CreateGenesisBlock(1497987918, 819158136, 0x1d00ffff, 1, 50 * COIN);
          consensus.hashGenesisBlock = genesis.GetHash();
          assert(consensus.hashGenesisBlock == uint256S("0x00000000f5d7795a2faa48e50c639c3c207fec39447acd4b02183886c81d5bd1"));
          assert(genesis.hashMerkleRoot == uint256S("0xf4b397dd2c0389b3ca8a4f7a9501395647170fd31e49c18ffb379699ff7c96e4"));
  
          // Note that of those with the service bits flag, most only support a subset of possible options
          // vSeeds.push_back(CDNSSeedData("helveticum.sipa.be", "seed.helveticum.sipa.be", true)); // Pieter Wuille, only supports x1, x5, x9, and xd
          // vSeeds.push_back(CDNSSeedData("bluematt.me", "dnsseed.bluematt.me", true)); // Matt Corallo, only supports x9
          // vSeeds.push_back(CDNSSeedData("dashjr.org", "dnsseed.helveticum.dashjr.org")); // Luke Dashjr
          // vSeeds.push_back(CDNSSeedData("helveticumstats.com", "seed.helveticumstats.com", true)); // Christian Decker, supports x1 - xf
          // vSeeds.push_back(CDNSSeedData("helveticum.jonasschnelli.ch", "seed.helveticum.jonasschnelli.ch", true)); // Jonas Schnelli, only supports x1, x5, x9, and xd
          // vSeeds.push_back(CDNSSeedData("petertodd.org", "seed.btc.petertodd.org", true)); // Peter Todd, only supports x1, x5, x9, and xd
  
          base58Prefixes[PUBKEY_ADDRESS] = std::vector<unsigned char>(1,1);
          base58Prefixes[SCRIPT_ADDRESS] = std::vector<unsigned char>(1,6);
          base58Prefixes[SECRET_KEY] =     std::vector<unsigned char>(1,129);
          base58Prefixes[EXT_PUBLIC_KEY] = {0x04, 0x88, 0xB2, 0x1E};
          base58Prefixes[EXT_SECRET_KEY] = {0x04, 0x88, 0xAD, 0xE4};
  
          vFixedSeeds = std::vector<SeedSpec6>(pnSeed6_main, pnSeed6_main + ARRAYLEN(pnSeed6_main));
  
          fDefaultConsistencyChecks = false;
          fRequireStandard = true;
          fMineBlocksOnDemand = false;
  
          checkpointData = (CCheckpointData) {
              {
                  {0, uint256S("00000000f5d7795a2faa48e50c639c3c207fec39447acd4b02183886c81d5bd1")},
              }
          };
  
          chainTxData = ChainTxData{
              // Data as of block 00000000000000000166d612d5595e2b1cd88d71d695fc580af64d8da8658c23 (height 446482).
              1497987918, // * UNIX timestamp of last known number of transactions
              0,  // * total number of transactions between genesis and that timestamp
                          //   (the tx=... number in the SetBestChain debug.log lines)
              3.2         // * estimated number of transactions per second after that timestamp
          };
      }
  };
  
  /**
   * Testnet (v3)
   */
  class CTestNetParams : public CChainParams {
  public:
      CTestNetParams() {
          strNetworkID = "test";
          consensus.nSubsidyHalvingInterval = 210000;
          consensus.BIP34Height = 0;
          consensus.BIP34Hash = uint256S("0x00000000f5d7795a2faa48e50c639c3c207fec39447acd4b02183886c81d5bd1");
          consensus.BIP65Height = 0; // 00000000007f6655f22f98e72ed80d8b06dc761d5da09df0fa1dc4be4f861eb6
          consensus.BIP66Height = 0; // 000000002104c8c45e99a8853285a3b592602a3ccde2b832481da85e9e4ba182
          consensus.powLimit = uint256S("00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
          consensus.nPowTargetTimespan = 14 * 24 * 60 * 60; // two weeks
          consensus.nPowTargetSpacing = 10 * 60;
          consensus.fPowAllowMinDifficultyBlocks = true;
          consensus.fPowNoRetargeting = false;
          consensus.nRuleChangeActivationThreshold = 1512; // 75% for testchains
          consensus.nMinerConfirmationWindow = 2016; // nPowTargetTimespan / nPowTargetSpacing
          consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].bit = 28;
          consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nStartTime = 1199145601; // January 1, 2008
          consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nTimeout = 1230767999; // December 31, 2008
  
          // Deployment of BIP68, BIP112, and BIP113.
          consensus.vDeployments[Consensus::DEPLOYMENT_CSV].bit = 0;
          consensus.vDeployments[Consensus::DEPLOYMENT_CSV].nStartTime = 1456790400; // March 1st, 2016
          consensus.vDeployments[Consensus::DEPLOYMENT_CSV].nTimeout = 1493596800; // May 1st, 2017
  
          // Deployment of SegWit (BIP141, BIP143, and BIP147)
          consensus.vDeployments[Consensus::DEPLOYMENT_SEGWIT].bit = 1;
          consensus.vDeployments[Consensus::DEPLOYMENT_SEGWIT].nStartTime = 1462060800; // May 1st 2016
          consensus.vDeployments[Consensus::DEPLOYMENT_SEGWIT].nTimeout = 1493596800; // May 1st 2017
  
          // The best chain should have at least this much work.
          consensus.nMinimumChainWork = uint256S("0x00");
  
          // By default assume that the signatures in ancestors of this block are valid.
          consensus.defaultAssumeValid = uint256S("0x00000000f5d7795a2faa48e50c639c3c207fec39447acd4b02183886c81d5bd1"); //1079274
  
          pchMessageStart[0] = 0x0b;
          pchMessageStart[1] = 0x11;
          pchMessageStart[2] = 0x09;
          pchMessageStart[3] = 0x07;
          nDefaultPort = 12113;
          nPruneAfterHeight = 1000;
  
          genesis = CreateGenesisBlock(1497987918, 819158136, 0x1d00ffff, 1, 50 * COIN);
          // genesis = CreateGenesisBlock(1296688602, 414098458, 0x1d00ffff, 1, 50 * COIN);
          consensus.hashGenesisBlock = genesis.GetHash();
          assert(consensus.hashGenesisBlock == uint256S("0x00000000f5d7795a2faa48e50c639c3c207fec39447acd4b02183886c81d5bd1"));
          assert(genesis.hashMerkleRoot == uint256S("0xf4b397dd2c0389b3ca8a4f7a9501395647170fd31e49c18ffb379699ff7c96e4"));
  
          //  assert(consensus.hashGenesisBlock == uint256S("0x000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943"));
          // assert(genesis.hashMerkleRoot == uint256S("0xe4967cff999637fb8fc1491ed30f1747563901957a4f8acab389032cdd97b3f4"));
  
          vFixedSeeds.clear();
          vSeeds.clear();
          // nodes with support for servicebits filtering should be at the top
          // vSeeds.push_back(CDNSSeedData("testnethelveticum.jonasschnelli.ch", "testnet-seed.helveticum.jonasschnelli.ch", true));
          //vSeeds.push_back(CDNSSeedData("petertodd.org", "seed.tbtc.petertodd.org", true));
          //vSeeds.push_back(CDNSSeedData("bluematt.me", "testnet-seed.bluematt.me"));
          //vSeeds.push_back(CDNSSeedData("helveticum.schildbach.de", "testnet-seed.helveticum.schildbach.de"));
  
          base58Prefixes[PUBKEY_ADDRESS] = std::vector<unsigned char>(1,111);
          base58Prefixes[SCRIPT_ADDRESS] = std::vector<unsigned char>(1,196);
          base58Prefixes[SECRET_KEY] =     std::vector<unsigned char>(1,239);
          base58Prefixes[EXT_PUBLIC_KEY] = {0x04, 0x35, 0x87, 0xCF};
          base58Prefixes[EXT_SECRET_KEY] = {0x04, 0x35, 0x83, 0x94};
  
          vFixedSeeds = std::vector<SeedSpec6>(pnSeed6_test, pnSeed6_test + ARRAYLEN(pnSeed6_test));
  
          fDefaultConsistencyChecks = false;
          fRequireStandard = false;
          fMineBlocksOnDemand = false;
  
  
          checkpointData = (CCheckpointData) {
              {
                  {0, uint256S("00000000f5d7795a2faa48e50c639c3c207fec39447acd4b02183886c81d5bd1")},
              }
          };
  
          chainTxData = ChainTxData{
              0,
              0,
              0
          };
  
      }
  };
  
  /**
   * Regression test
   */
  class CRegTestParams : public CChainParams {
  public:
      CRegTestParams() {
          strNetworkID = "regtest";
          consensus.nSubsidyHalvingInterval = 150;
          consensus.BIP34Height = 100000000; // BIP34 has not activated on regtest (far in the future so block v1 are not rejected in tests)
          consensus.BIP34Hash = uint256();
          consensus.BIP65Height = 1351; // BIP65 activated on regtest (Used in rpc activation tests)
          consensus.BIP66Height = 1251; // BIP66 activated on regtest (Used in rpc activation tests)
          consensus.powLimit = uint256S("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
          consensus.nPowTargetTimespan = 14 * 24 * 60 * 60; // two weeks
          consensus.nPowTargetSpacing = 10 * 60;
          consensus.fPowAllowMinDifficultyBlocks = true;
          consensus.fPowNoRetargeting = true;
          consensus.nRuleChangeActivationThreshold = 108; // 75% for testchains
          consensus.nMinerConfirmationWindow = 144; // Faster than normal for regtest (144 instead of 2016)
          consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].bit = 28;
          consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nStartTime = 0;
          consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nTimeout = 999999999999ULL;
          consensus.vDeployments[Consensus::DEPLOYMENT_CSV].bit = 0;
          consensus.vDeployments[Consensus::DEPLOYMENT_CSV].nStartTime = 0;
          consensus.vDeployments[Consensus::DEPLOYMENT_CSV].nTimeout = 999999999999ULL;
          consensus.vDeployments[Consensus::DEPLOYMENT_SEGWIT].bit = 1;
          consensus.vDeployments[Consensus::DEPLOYMENT_SEGWIT].nStartTime = 0;
          consensus.vDeployments[Consensus::DEPLOYMENT_SEGWIT].nTimeout = 999999999999ULL;
  
          // The best chain should have at least this much work.
          consensus.nMinimumChainWork = uint256S("0x00");
  
          // By default assume that the signatures in ancestors of this block are valid.
          consensus.defaultAssumeValid = uint256S("0x00");
  
          pchMessageStart[0] = 0xfa;
          pchMessageStart[1] = 0xbf;
          pchMessageStart[2] = 0xb5;
          pchMessageStart[3] = 0xda;
          nDefaultPort = 18444;
          nPruneAfterHeight = 1000;
  
          genesis = CreateGenesisBlock(1296688602, 2, 0x207fffff, 1, 50 * COIN);
          consensus.hashGenesisBlock = genesis.GetHash();
          assert(consensus.hashGenesisBlock == uint256S("0x0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206"));
          assert(genesis.hashMerkleRoot == uint256S("0x4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"));
  
          vFixedSeeds.clear(); //!< Regtest mode doesn't have any fixed seeds.
          vSeeds.clear();      //!< Regtest mode doesn't have any DNS seeds.
  
          fDefaultConsistencyChecks = true;
          fRequireStandard = false;
          fMineBlocksOnDemand = true;
  
          checkpointData = (CCheckpointData) {
              {
                  {0, uint256S("0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206")},
              }
          };
  
          chainTxData = ChainTxData{
              0,
              0,
              0
          };
  
          base58Prefixes[PUBKEY_ADDRESS] = std::vector<unsigned char>(1,111);
          base58Prefixes[SCRIPT_ADDRESS] = std::vector<unsigned char>(1,196);
          base58Prefixes[SECRET_KEY] =     std::vector<unsigned char>(1,239);
          base58Prefixes[EXT_PUBLIC_KEY] = {0x04, 0x35, 0x87, 0xCF};
          base58Prefixes[EXT_SECRET_KEY] = {0x04, 0x35, 0x83, 0x94};
      }
  };
  
  static std::unique_ptr<CChainParams> globalChainParams;
  
  const CChainParams &Params() {
      assert(globalChainParams);
      return *globalChainParams;
  }
  
  std::unique_ptr<CChainParams> CreateChainParams(const std::string& chain)
  {
      if (chain == CBaseChainParams::MAIN)
          return std::unique_ptr<CChainParams>(new CMainParams());
      else if (chain == CBaseChainParams::TESTNET)
          return std::unique_ptr<CChainParams>(new CTestNetParams());
      else if (chain == CBaseChainParams::REGTEST)
          return std::unique_ptr<CChainParams>(new CRegTestParams());
      throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain));
  }
  
  void SelectParams(const std::string& network)
  {
      SelectBaseParams(network);
      globalChainParams = CreateChainParams(network);
  }
  
  void UpdateVersionBitsParameters(Consensus::DeploymentPos d, int64_t nStartTime, int64_t nTimeout)
  {
      globalChainParams->UpdateVersionBitsParameters(d, nStartTime, nTimeout);
  }
  