text: // Copyright (c) 2010 Satoshi Nakamoto
  // Copyright (c) 2009-2012 The Bitcoin developers
  // Distributed under the MIT/X11 software license, see the accompanying
  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  
  #include "assert.h"
  
  #include "chainparams.h"
  #include "main.h"
  #include "util.h"
  
  #include <boost/assign/list_of.hpp>
  
  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);
      }
  }
  
  class CMainParams : public CChainParams {
  public:
      CMainParams() {
          // 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] = 0x4d;
          pchMessageStart[1] = 0x5f;
          pchMessageStart[2] = 0x92;
          pchMessageStart[3] = 0xf0;
          vAlertPubKey = ParseHex("09454852a2d08c6314aad5ca3cbe5616262db3d4a6efac478e96c9056be6b61e659c04b94acefeac5d5257fe028e80695c62f7c2f81f85d131a446df3be611393f");
          nDefaultPort = 34361;
          nRPCPort = 34362;
          bnProofOfWorkLimit = CBigNum(~uint256(0) >> 20);
          bnProofOfStakeLimit = CBigNum(~uint256(0) >> 20);
  
          const char* pszTimestamp = "http://uk.reuters.com/article/uk-cricket-icc-usa-idUKKBN17X1BZ";
          std::vector<CTxIn> vin;
          vin.resize(1);
          vin[0].scriptSig = CScript() << 4866479 << CBigNum(444) << vector<unsigned char>((const unsigned char*)pszTimestamp, (const unsigned char*)pszTimestamp + strlen(pszTimestamp));
          std::vector<CTxOut> vout;
          vout.resize(1);
          vout[0].SetEmpty();
          CTransaction txNew(1, 1493664780, vin, vout, 0);
          genesis.vtx.push_back(txNew);
          genesis.hashPrevBlock = 0;
          genesis.hashMerkleRoot = genesis.BuildMerkleTree();
          genesis.nVersion = 1;
          genesis.nTime    = 1493664780;
          genesis.nBits    = bnProofOfWorkLimit.GetCompact();
          genesis.nNonce   = 457472;
  
          hashGenesisBlock = genesis.GetHash();
          assert(hashGenesisBlock == uint256("0xfaac51d7e9e21bda8506943e301f795eaee422cb7531b3a1adb27acf6d5c415f"));
          assert(genesis.hashMerkleRoot == uint256("0x9d8b1ef5f9d7710809dffd9e562b1f17111eab9b881b0ea7b413c8868ddd12ad"));
  
          base58Prefixes[PUBKEY_ADDRESS] = std::vector<unsigned char>(1,47);
          base58Prefixes[SCRIPT_ADDRESS] = std::vector<unsigned char>(1,69);
          base58Prefixes[SECRET_KEY] =     std::vector<unsigned char>(1,92);
          base58Prefixes[EXT_PUBLIC_KEY] = boost::assign::list_of(0x04)(0x99)(0xB2)(0x8A).convert_to_container<std::vector<unsigned char> >();
          base58Prefixes[EXT_SECRET_KEY] = boost::assign::list_of(0x04)(0x99)(0xEC)(0x1F).convert_to_container<std::vector<unsigned char> >();
  
          convertSeed6(vFixedSeeds, pnSeed6_main, ARRAYLEN(pnSeed6_main));
  
          nTargetSpacing = 2 * 60; // 2 minutes
          if(nBestHeight > nLastPoWBlock) // Scaled down for PoS only phase
          {
            nTargetSpacing = 1 * 60;
          }
          if(nBestHeight > nStartPoSBlock) // Scaled up for PoW/PoS twin phase
          {
            if(nBestHeight <= nLastPoWBlock)
            {
              nTargetSpacing = 2 * 60;
            }
          }
          nTargetTimespan = 5 * nTargetSpacing;
          nLastPoWBlock = 100000;
          nStartPoSBlock = 1;
      }
  
      virtual const CBlock& GenesisBlock() const { return genesis; }
      virtual Network NetworkID() const { return CChainParams::MAIN; }
  
      virtual const vector<CAddress>& FixedSeeds() const {
          return vFixedSeeds;
      }
  protected:
      CBlock genesis;
      vector<CAddress> vFixedSeeds;
  };
  static CMainParams mainParams;
  
  
  //
  // Testnet
  //
  
  class CTestNetParams : public CMainParams {
  public:
      CTestNetParams() {
          // 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] = 0xa0;
          pchMessageStart[1] = 0xb4;
          pchMessageStart[2] = 0x26;
          pchMessageStart[3] = 0xd2;
          bnProofOfWorkLimit = CBigNum(~uint256(0) >> 16);
          bnProofOfStakeLimit = CBigNum(~uint256(0) >> 16);
  
          vAlertPubKey = ParseHex("09454852a2d08c1687ecf5ca3cbe5616262db3d4a6efac4acefeac5d5257fe028e80695c62f7c2f1e69478e96c9056be6b659c04b81f85d131a446df3be611393f");
          nDefaultPort = 44361;
          nRPCPort = 44362;
          strDataDir = "testnet";
  
          // Modify the testnet genesis block so the timestamp is valid for a later start.
          genesis.nBits  = bnProofOfWorkLimit.GetCompact();
          genesis.nNonce = 29642;
  
          hashGenesisBlock = genesis.GetHash();
          vFixedSeeds.clear();
          vSeeds.clear();
  
          base58Prefixes[PUBKEY_ADDRESS] = std::vector<unsigned char>(1,90);
          base58Prefixes[SCRIPT_ADDRESS] = std::vector<unsigned char>(1,97);
          base58Prefixes[SECRET_KEY] =     std::vector<unsigned char>(1,38);
          base58Prefixes[EXT_PUBLIC_KEY] = boost::assign::list_of(0x04)(0x66)(0x1A)(0x7E).convert_to_container<std::vector<unsigned char> >();
          base58Prefixes[EXT_SECRET_KEY] = boost::assign::list_of(0x04)(0x66)(0x4D)(0xC0).convert_to_container<std::vector<unsigned char> >();
  
          convertSeed6(vFixedSeeds, pnSeed6_test, ARRAYLEN(pnSeed6_test));
  
          nTargetTimespan = 12 * nTargetSpacing;
          nLastPoWBlock = 1000;
          nStartPoSBlock = 1;
      }
      virtual Network NetworkID() const { return CChainParams::TESTNET; }
  };
  static CTestNetParams testNetParams;
  
  
  //
  // Regression test
  //
  class CRegTestParams : public CTestNetParams {
  public:
      CRegTestParams() {
          pchMessageStart[0] = 0x0a;
          pchMessageStart[1] = 0xdc;
          pchMessageStart[2] = 0x71;
          pchMessageStart[3] = 0xb3;
          bnProofOfWorkLimit = CBigNum(~uint256(0) >> 1);
          genesis.nTime = 1490645291;
          genesis.nBits  = bnProofOfWorkLimit.GetCompact();
          genesis.nNonce = 386;
          hashGenesisBlock = genesis.GetHash();
          nDefaultPort = 35362;
          strDataDir = "regtest";
  
          vSeeds.clear();  // Regtest mode doesn't have any DNS seeds.
      }
  
      virtual bool RequireRPCPassword() const { return false; }
      virtual Network NetworkID() const { return CChainParams::REGTEST; }
  };
  static CRegTestParams regTestParams;
  
  static CChainParams *pCurrentParams = &mainParams;
  
  const CChainParams &Params() {
      return *pCurrentParams;
  }
  
  void SelectParams(CChainParams::Network network) {
      switch (network) {
          case CChainParams::MAIN:
              pCurrentParams = &mainParams;
              break;
          case CChainParams::TESTNET:
              pCurrentParams = &testNetParams;
              break;
          case CChainParams::REGTEST:
              pCurrentParams = &regTestParams;
              break;
          default:
              assert(false && "Unimplemented network");
              return;
      }
  }
  
  bool SelectParamsFromCommandLine() {
      bool fRegTest = GetBoolArg("-regtest", false);
      bool fTestNet = GetBoolArg("-testnet", false);
  
      if (fTestNet && fRegTest) {
          return false;
      }
  
      if (fRegTest) {
          SelectParams(CChainParams::REGTEST);
      } else if (fTestNet) {
          SelectParams(CChainParams::TESTNET);
      } else {
          SelectParams(CChainParams::MAIN);
      }
      return true;
  }
  