text: // Copyright (c) 2010 Satoshi Nakamoto
  // Copyright (c) 2009-2014 The Bitcoin developers
  // Copyright (c) 2014-2015 The Dash developers
  // Copyright (c) 2015-2017 The PIVX developers
  // Copyright (c) 2017 The RecurPay 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 "random.h"
  #include "util.h"
  #include "utilstrencodings.h"
  
  #include <assert.h>
  
  #include <boost/assign/list_of.hpp>
  
  using namespace std;
  using namespace boost::assign;
  
  struct SeedSpec6 {
      uint8_t addr[16];
      uint16_t port;
  };
  
  #include "chainparamsseeds.h"
  
  /*
   * Main network
   */
  
  //! Convert the pnSeeds6 array into usable address objects.
  static void convertSeed6(std::vector<CAddress>& vSeedsOut, const SeedSpec6* data, unsigned int count)
  {
      // It'll only connect to one or two seed nodes because once it connects,
      // it'll get a pile of addresses with newer timestamps.
      // Seed nodes are given a random 'last seen time' of between one and two
      // weeks ago.
      const int64_t nOneWeek = 7 * 24 * 60 * 60;
      for (unsigned int i = 0; i < count; i++) {
          struct in6_addr ip;
          memcpy(&ip, data[i].addr, sizeof(ip));
          CAddress addr(CService(ip, data[i].port));
          addr.nTime = GetTime() - GetRand(nOneWeek) - nOneWeek;
          vSeedsOut.push_back(addr);
      }
  }
  
  //   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
  static Checkpoints::MapCheckpoints mapCheckpoints =
      boost::assign::map_list_of(0, uint256("000000cbed116ea9f8d877e8525ecdc13306bfd6f86a0bfcaa92cfbf7f6ba5f0"));
  static const Checkpoints::CCheckpointData data = {
      &mapCheckpoints,
      1533974418, // * UNIX timestamp of last checkpoint block
      0,    // * total number of transactions between genesis and last checkpoint
                  //   (the tx=... number in the SetBestChain debug.log lines)
      2000        // * estimated number of transactions per day after checkpoint
  };
  
  static Checkpoints::MapCheckpoints mapCheckpointsTestnet =
      boost::assign::map_list_of(0, uint256("0x001"));
  static const Checkpoints::CCheckpointData dataTestnet = {
      &mapCheckpointsTestnet,
      1454124731,
      0,
      250};
  
  static Checkpoints::MapCheckpoints mapCheckpointsRegtest =
      boost::assign::map_list_of(0, uint256("0x001"));
  static const Checkpoints::CCheckpointData dataRegtest = {
      &mapCheckpointsRegtest,
      1454124731,
      0,
      100};
  
  class CMainParams : public CChainParams
  {
  public:
      CMainParams()
      {
          networkID = CBaseChainParams::MAIN;
          strNetworkID = "main";
          /*
           * 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 4-byte int at any alignment.
           */
          pchMessageStart[0] = 0x93;
          pchMessageStart[1] = 0xc5;
          pchMessageStart[2] = 0xfd;
          pchMessageStart[3] = 0xea;
          vAlertPubKey = ParseHex("04bcc35db8f3389998ab919d6da8015c6f2a80311147bfb4ec7067638877cf70408d6fc94e907628ce434254af6085f743e44a3ca2a659ceea4550920eb237e15e");
          nDefaultPort = 51579;
          bnProofOfWorkLimit = ~uint256(0) >> 20; // RecurPay starting difficulty is 1 / 2^12
          nSubsidyHalvingInterval = 210000;
          nMaxReorganizationDepth = 30;
          nEnforceBlockUpgradeMajority = 750;
          nRejectBlockOutdatedMajority = 950;
          nToCheckBlockUpgradeMajority = 1000;
          nMinerThreads = 0;
          nTargetTimespan = 1 * 120; // RecurPay: 2 minute
          nTargetSpacing = 1 * 120;  // RecurPay: 2 minute
          nPastBlocksMin = 24;
          nLastPOWBlock = 10080;
          nMaturity = 90;
          nMasternodeCountDrift = 20;
          nModifierUpdateBlock = 0;
          nMaxMoneyOut = int64_t(20000000000) * COIN;
          nModifierInterval = 60;
          nModifierIntervalRatio = 3;
          nBudgetPercent = 5;
          nBudgetPaymentCycle = 60*60*24*30; // 1 month
          nMasternodePaymentSigTotal = 10;
          nMasternodePaymentSigRequired = 6;
          nMasternodeRewardPercent = 60; // % of block reward that goes to masternodes
          nRequiredMasternodeCollateral = 8000000 * COIN; //8,000,000
  
          /*
           * Build the genesis block. Note that the output of the genesis coinbase cannot
           * be spent as it did not originally exist in the database.
           *
           * CBlock(hash=00000ffd590b14, ver=1, hashPrevBlock=00000000000000, hashMerkleRoot=e0028e, nTime=1390095618, nBits=1e0ffff0, nNonce=28917698, vtx=1)
           *   CTransaction(hash=e0028e, ver=1, vin.size=1, vout.size=1, nLockTime=0)
           *     CTxIn(COutPoint(000000, -1), coinbase 04ffff001d01044c5957697265642030392f4a616e2f3230313420546865204772616e64204578706572696d656e7420476f6573204c6976653a204f76657273746f636b2e636f6d204973204e6f7720416363657074696e6720426974636f696e73)
           *     CTxOut(nValue=50.00000000, scriptPubKey=0xA9037BAC7050C479B121CF)
           *   vMerkleTree: e0028e
           */
          const std::string pszTimestamp = "Recur Pay has been created on August 11th 2018";
          CMutableTransaction txNew;
          txNew.vin.resize(1);
          txNew.vout.resize(1);
          txNew.vin[0].scriptSig = CScript() << 486604799 << CScriptNum(4) << vector<unsigned char>(pszTimestamp.begin(), pszTimestamp.end());
          txNew.vout[0].nValue = 250 * COIN;
          txNew.vout[0].scriptPubKey = CScript() << ParseHex("048796e97830abdd02c53775e37c034a8fd43e8370df5aebb593e2236a518f61ad01b9890188a9f13c1528961a2c104471b42eba30f96c3760926bb0e3ba24bb7f") << OP_CHECKSIG;
          genesis.vtx.push_back(txNew);
          genesis.hashPrevBlock = 0;
          genesis.hashMerkleRoot = genesis.BuildMerkleTree();
          genesis.nVersion = 1;
          genesis.nTime = 1533974418;
          genesis.nBits = 0x1e0ffff0;
          genesis.nNonce = 2122383;
  
  
  // printf("Searching for genesis block...\n");
  //             // This will figure out a valid hash and Nonce if youÆre
  //             // creating a different genesis block:
  //             uint256 hashTarget = uint256().SetCompact(genesis.nBits);
  //             uint256 thash;
  
  //             while(true)
  //             {
  //                 //thash = scrypt_blockhash(BEGIN(genesis.nVersion));
  //                 thash = HashQuark(BEGIN(genesis.nVersion), END(genesis.nNonce)); 
  //                 if (thash <= hashTarget)
  //                     break;
  //                 if ((genesis.nNonce & 0xFFF) == 0)
  //                 {
  //                     printf("nonce %08X: hash = %s (target = %s)\n", genesis.nNonce, thash.ToString().c_str(), hashTarget.ToString().c_str());
  //                 }
  //                 ++genesis.nNonce;
  //                 if (genesis.nNonce == 0)
  //                 {
  //                     printf("NONCE WRAPPED, incrementing time\n");
  //                     ++genesis.nTime;
  //                 }
  //             }
  
  //             printf("genesis.nTimemain = %u \n", genesis.nTime);
  //             printf("genesis.nNonce = %u \n", genesis.nNonce);
  //             printf("genesis.GetHash = %s\n", genesis.GetHash().ToString().c_str());
  //         printf("Gensis Hash Merkle: %s\n", genesis.hashMerkleRoot.ToString().c_str());
  
          hashGenesisBlock = genesis.GetHash();
          assert(hashGenesisBlock == uint256("0x000000cbed116ea9f8d877e8525ecdc13306bfd6f86a0bfcaa92cfbf7f6ba5f0"));
          assert(genesis.hashMerkleRoot == uint256("0xba86b1f1de988e8a69f4079f012c0386b00763f28cfd1e57813534b296665ed4"));
  
     //     vSeeds.push_back(CDNSSeedData("recur", "seed.recurpay.org"));
  
          base58Prefixes[PUBKEY_ADDRESS] = std::vector<unsigned char>(1, 30);
          base58Prefixes[SCRIPT_ADDRESS] = std::vector<unsigned char>(1, 13);
          base58Prefixes[SECRET_KEY] = std::vector<unsigned char>(1, 212);
          base58Prefixes[EXT_PUBLIC_KEY] = boost::assign::list_of(0x02)(0x2D)(0x25)(0x33).convert_to_container<std::vector<unsigned char> >();
          base58Prefixes[EXT_SECRET_KEY] = boost::assign::list_of(0x02)(0x21)(0x31)(0x2B).convert_to_container<std::vector<unsigned char> >();
          // 	BIP44 coin type is from https://github.com/satoshilabs/slips/blob/master/slip-0044.md
          base58Prefixes[EXT_COIN_TYPE] = boost::assign::list_of(0x80)(0x00)(0x00)(0x77).convert_to_container<std::vector<unsigned char> >();
  
          convertSeed6(vFixedSeeds, pnSeed6_main, ARRAYLEN(pnSeed6_main));
  
          fRequireRPCPassword = true;
          fMiningRequiresPeers = true;
          fAllowMinDifficultyBlocks = false;
          fDefaultConsistencyChecks = false;
          fRequireStandard = true;
          fMineBlocksOnDemand = false;
          fSkipProofOfWorkCheck = false;
          fTestnetToBeDeprecatedFieldRPC = false;
          fHeadersFirstSyncingActive = false;
  
          nPoolMaxTransactions = 3;
  
          strSporkKey = "0423f2b48d99f15a0bceedbe9b05a06d028aca587c3a0f0ee4a7dff6b0859181c1225b5842a17e8bb74758b8f1757a82025631f3276bec0734c6f61de71c1e4d28";
          strObfuscationPoolDummyAddress = "D87q2gC9j6nNrnzCsg4aY6bHMLsT9nUhEw";
          nStartMasternodePayments = 1403728576; //Wed, 25 Jun 2014 20:36:16 GMT
      }
  
      int64_t GetMinStakeAge(int nTargetHeight) const
      {
          if (nTargetHeight >= 300000)
              return 60*60*8; //8 hours
          else
              return 60*60*24*7; //7 days
      }
  
      const Checkpoints::CCheckpointData& Checkpoints() const
      {
          return data;
      }
  };
  static CMainParams mainParams;
  
  /*
   * Testnet (v3)
   */
  class CTestNetParams : public CMainParams
  {
  public:
      CTestNetParams()
      {
          networkID = CBaseChainParams::TESTNET;
          strNetworkID = "test";
          pchMessageStart[0] = 0x46;
          pchMessageStart[1] = 0x77;
          pchMessageStart[2] = 0x66;
          pchMessageStart[3] = 0xbb;
          vAlertPubKey = ParseHex("048796e97830abdd02c53775e37c034a8fd43e8370df5aebb593e2236a518f61ad01b9890188a9f13c1528961a2c104471b42eba30f96c3760926bb0e3ba24bb7f");
          nDefaultPort = 51374;
          bnProofOfWorkLimit = ~uint256(0) >> 1;
          nEnforceBlockUpgradeMajority = 51;
          nRejectBlockOutdatedMajority = 75;
          nToCheckBlockUpgradeMajority = 100;
          nMinerThreads = 0;
          nTargetTimespan = 1 * 60; // RecurPay: 1 day
          nTargetSpacing = 1 * 60;  // RecurPay: 1 minute
          nPastBlocksMin = 200;
          nLastPOWBlock = 10080;
          nMaturity = 15;
          nMasternodeCountDrift = 4;
          nModifierUpdateBlock = 0; //approx Mon, 17 Apr 2017 04:00:00 GMT
          nMaxMoneyOut = int64_t(20000000000) * COIN;
          nModifierInterval = 60;
          nModifierIntervalRatio = 3;
          nBudgetPercent = 5;
          nBudgetPaymentCycle = 60*60*2; // 2 hours
          nMasternodeRewardPercent = 60; // % of block reward that goes to masternodes
          nRequiredMasternodeCollateral = 8000000 * COIN; //8,000,000
          nMasternodePaymentSigTotal = 10;
          nMasternodePaymentSigRequired = 1;
  
          //! Modify the testnet genesis block so the timestamp is valid for a later start.
          genesis.nTime = 1533974418;
          genesis.nNonce = 2122383;
  
  // printf("Searching for genesis block...\n");
  //             // This will figure out a valid hash and Nonce if youÆre
  //             // creating a different genesis block:
  //             uint256 hashTarget = uint256().SetCompact(genesis.nBits);
  //             uint256 thash;
  
  //             while(true)
  //             {
  //                 //thash = scrypt_blockhash(BEGIN(genesis.nVersion));
  //                 thash = HashQuark(BEGIN(genesis.nVersion), END(genesis.nNonce)); 
  //                 if (thash <= hashTarget)
  //                     break;
  //                 if ((genesis.nNonce & 0xFFF) == 0)
  //                 {
  //                     printf("nonce %08X: hash = %s (target = %s)\n", genesis.nNonce, thash.ToString().c_str(), hashTarget.ToString().c_str());
  //                 }
  //                 ++genesis.nNonce;
  //                 if (genesis.nNonce == 0)
  //                 {
  //                     printf("NONCE WRAPPED, incrementing time\n");
  //                     ++genesis.nTime;
  //                 }
  //             }
  
  //             printf("genesis.nTimetest1 = %u \n", genesis.nTime);
  //             printf("genesis.nNonce = %u \n", genesis.nNonce);
  //             printf("genesis.GetHash = %s\n", genesis.GetHash().ToString().c_str());
  //         printf("Gensis Hash Merkle: %s\n", genesis.hashMerkleRoot.ToString().c_str());
  
  
          hashGenesisBlock = genesis.GetHash();
          assert(hashGenesisBlock == uint256("0x000000cbed116ea9f8d877e8525ecdc13306bfd6f86a0bfcaa92cfbf7f6ba5f0"));
  
          vFixedSeeds.clear();
          vSeeds.clear();
  
          base58Prefixes[PUBKEY_ADDRESS] = std::vector<unsigned char>(1, 139); // Testnet recur addresses start with 'x' or 'y'
          base58Prefixes[SCRIPT_ADDRESS] = std::vector<unsigned char>(1, 19);  // Testnet recur script addresses start with '8' or '9'
          base58Prefixes[SECRET_KEY] = std::vector<unsigned char>(1, 239);     // Testnet private keys start with '9' or 'c' (Bitcoin defaults)
          // Testnet recur BIP32 pubkeys start with 'DRKV'
          base58Prefixes[EXT_PUBLIC_KEY] = boost::assign::list_of(0x3a)(0x80)(0x61)(0xa0).convert_to_container<std::vector<unsigned char> >();
          // Testnet recur BIP32 prvkeys start with 'DRKP'
          base58Prefixes[EXT_SECRET_KEY] = boost::assign::list_of(0x3a)(0x80)(0x58)(0x37).convert_to_container<std::vector<unsigned char> >();
          // Testnet recur BIP44 coin type is '1' (All coin's testnet default)
          base58Prefixes[EXT_COIN_TYPE] = boost::assign::list_of(0x80)(0x00)(0x00)(0x01).convert_to_container<std::vector<unsigned char> >();
  
          convertSeed6(vFixedSeeds, pnSeed6_test, ARRAYLEN(pnSeed6_test));
  
          fRequireRPCPassword = true;
          fMiningRequiresPeers = true;
          fAllowMinDifficultyBlocks = false;
          fDefaultConsistencyChecks = false;
          fRequireStandard = true;
          fMineBlocksOnDemand = false;
          fSkipProofOfWorkCheck = false;
          fTestnetToBeDeprecatedFieldRPC = true;
          fHeadersFirstSyncingActive = false;
  
          nPoolMaxTransactions = 3;
          strSporkKey = "026ee678f254a97675a90ebea1e7593fdb53047321f3cb0560966d4202b32c48e2";
          strObfuscationPoolDummyAddress = "y57cqfGRkekRyDRNeJiLtYVEbvhXrNbmox";
          nStartMasternodePayments = 1420837558; //Fri, 09 Jan 2015 21:05:58 GMT
      }
  
      int64_t GetMinStakeAge(int nTargetHeight) const
      {
          return 60*60*8; //8 hours
      }
  
      const Checkpoints::CCheckpointData& Checkpoints() const
      {
          return dataTestnet;
      }
  };
  static CTestNetParams testNetParams;
  
  /*
   * Regression test
   */
  class CRegTestParams : public CTestNetParams
  {
  public:
      CRegTestParams()
      {
          networkID = CBaseChainParams::REGTEST;
          strNetworkID = "regtest";
          strNetworkID = "regtest";
          pchMessageStart[0] = 0xa1;
          pchMessageStart[1] = 0xcf;
          pchMessageStart[2] = 0x7e;
          pchMessageStart[3] = 0xac;
          nSubsidyHalvingInterval = 150;
          nEnforceBlockUpgradeMajority = 750;
          nRejectBlockOutdatedMajority = 950;
          nToCheckBlockUpgradeMajority = 1000;
          nMinerThreads = 1;
          nTargetTimespan = 24 * 60 * 60; // Pivx: 1 day
          nTargetSpacing = 1 * 60;        // Pivx: 1 minutes
          nPastBlocksMin = 200;
          bnProofOfWorkLimit = ~uint256(0) >> 1;
          genesis.nTime = 1533974418;
          genesis.nBits = 0x207fffff;
          genesis.nNonce = 2;
  
          hashGenesisBlock = genesis.GetHash();
          nDefaultPort = 51476;
  
  
  // printf("Searching for genesis block...\n");
  //             // This will figure out a valid hash and Nonce if youÆre
  //             // creating a different genesis block:
  //             uint256 hashTarget = uint256().SetCompact(genesis.nBits);
  //             uint256 thash;
  
  //             while(true)
  //             {
  //                 //thash = scrypt_blockhash(BEGIN(genesis.nVersion));
  //                 thash = HashQuark(BEGIN(genesis.nVersion), END(genesis.nNonce)); 
  //                 if (thash <= hashTarget)
  //                     break;
  //                 if ((genesis.nNonce & 0xFFF) == 0)
  //                 {
  //                     printf("nonce %08X: hash = %s (target = %s)\n", genesis.nNonce, thash.ToString().c_str(), hashTarget.ToString().c_str());
  //                 }
  //                 ++genesis.nNonce;
  //                 if (genesis.nNonce == 0)
  //                 {
  //                     printf("NONCE WRAPPED, incrementing time\n");
  //                     ++genesis.nTime;
  //                 }
  //             }
  
  //             printf("genesis.nTimetest2 = %u \n", genesis.nTime);
  //             printf("genesis.nNonce = %u \n", genesis.nNonce);
  //             printf("genesis.GetHash = %s\n", genesis.GetHash().ToString().c_str());
  //         printf("Gensis Hash Merkle: %s\n", genesis.hashMerkleRoot.ToString().c_str());
          assert(hashGenesisBlock == uint256("784b1c309f3c83c9a560ddc73a7adbd9794575304eb1ddb211cdd6e88b66eec7"));
  
          vFixedSeeds.clear(); //! Testnet mode doesn't have any fixed seeds.
          vSeeds.clear();      //! Testnet mode doesn't have any DNS seeds.
  
          fRequireRPCPassword = false;
          fMiningRequiresPeers = false;
          fAllowMinDifficultyBlocks = true;
          fDefaultConsistencyChecks = true;
          fRequireStandard = false;
          fMineBlocksOnDemand = true;
          fTestnetToBeDeprecatedFieldRPC = false;
      }
  
      const Checkpoints::CCheckpointData& Checkpoints() const
      {
          return dataRegtest;
      }
  };
  static CRegTestParams regTestParams;
  
  /*
   * Unit test
   */
  class CUnitTestParams : public CMainParams, public CModifiableParams
  {
  public:
      CUnitTestParams()
      {
          networkID = CBaseChainParams::UNITTEST;
          strNetworkID = "unittest";
          nDefaultPort = 51478;
          vFixedSeeds.clear(); //! Unit test mode doesn't have any fixed seeds.
          vSeeds.clear();      //! Unit test mode doesn't have any DNS seeds.
  
          fRequireRPCPassword = false;
          fMiningRequiresPeers = false;
          fDefaultConsistencyChecks = true;
          fAllowMinDifficultyBlocks = false;
          fMineBlocksOnDemand = true;
      }
  
      const Checkpoints::CCheckpointData& Checkpoints() const
      {
          // UnitTest share the same checkpoints as MAIN
          return data;
      }
  
      //! Published setters to allow changing values in unit test cases
      virtual void setSubsidyHalvingInterval(int anSubsidyHalvingInterval) { nSubsidyHalvingInterval = anSubsidyHalvingInterval; }
      virtual void setEnforceBlockUpgradeMajority(int anEnforceBlockUpgradeMajority) { nEnforceBlockUpgradeMajority = anEnforceBlockUpgradeMajority; }
      virtual void setRejectBlockOutdatedMajority(int anRejectBlockOutdatedMajority) { nRejectBlockOutdatedMajority = anRejectBlockOutdatedMajority; }
      virtual void setToCheckBlockUpgradeMajority(int anToCheckBlockUpgradeMajority) { nToCheckBlockUpgradeMajority = anToCheckBlockUpgradeMajority; }
      virtual void setDefaultConsistencyChecks(bool afDefaultConsistencyChecks) { fDefaultConsistencyChecks = afDefaultConsistencyChecks; }
      virtual void setAllowMinDifficultyBlocks(bool afAllowMinDifficultyBlocks) { fAllowMinDifficultyBlocks = afAllowMinDifficultyBlocks; }
      virtual void setSkipProofOfWorkCheck(bool afSkipProofOfWorkCheck) { fSkipProofOfWorkCheck = afSkipProofOfWorkCheck; }
  };
  static CUnitTestParams unitTestParams;
  
  
  static CChainParams* pCurrentParams = 0;
  
  CModifiableParams* ModifiableParams()
  {
      assert(pCurrentParams);
      assert(pCurrentParams == &unitTestParams);
      return (CModifiableParams*)&unitTestParams;
  }
  
  const CChainParams& Params()
  {
      assert(pCurrentParams);
      return *pCurrentParams;
  }
  
  CChainParams& Params(CBaseChainParams::Network network)
  {
      switch (network) {
      case CBaseChainParams::MAIN:
          return mainParams;
      case CBaseChainParams::TESTNET:
          return testNetParams;
      case CBaseChainParams::REGTEST:
          return regTestParams;
      case CBaseChainParams::UNITTEST:
          return unitTestParams;
      default:
          assert(false && "Unimplemented network");
          return mainParams;
      }
  }
  
  void SelectParams(CBaseChainParams::Network network)
  {
      SelectBaseParams(network);
      pCurrentParams = &Params(network);
  }
  
  bool SelectParamsFromCommandLine()
  {
      CBaseChainParams::Network network = NetworkIdFromCommandLine();
      if (network == CBaseChainParams::MAX_NETWORK_TYPES)
          return false;
  
      SelectParams(network);
      return true;
  }
  