Whatever I do, I am unable to mine the 1st block after the genesis block. All the while, I have assumed that I have mined the Genesis block, and I am unable to mine the 1st block using "setgenerate true" command. But, I may be wrong. I may have NOT actually mined the genesis block. Yes, I have got confused. I have generated new keys. So, here is what I have done from start.

1) Code before genesis block:

In "chainparams.php", I have updated the following.

boost::assign::map_list_of(0, uint256("0x01"));
static const Checkpoints::CCheckpointData data = {
    &mapCheckpoints,
    1538530667, // * 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)
    500        // * estimated number of transactions per day after checkpoint
};

pchMessageStart[0] = 0x91;
pchMessageStart[1] = 0xc5;
pchMessageStart[2] = 0xfa;
pchMessageStart[3] = 0xe3;
vAlertPubKey = ParseHex("04ae6724abca5c2e2e9524d3cd10a303e0ef3f02d324bac4d06aceb873fb8591f1a968d9a2e5ac7024d5a45643d189b99015314d7ef1ff5f8799b9680d6e668471");

const char* pszTimestamp = "my new message here. trying to learn how to create an altcoin.";
txNew.vout[0].nValue = 50 * COIN;
txNew.vout[0].scriptPubKey = CScript() << ParseHex("047394ed7e750f6202487e7c8ec1e61bbd2fb5cdc6f0f5488af4d2ea082bec606fea95eea381cd0466cf6ea53d8f42c299f9d0e2a3bc313515fc52f02a16fef0a0") << OP_CHECKSIG;
genesis.nTime = 1538530667;
genesis.nBits = 0x1e0ffff0;

assert(hashGenesisBlock == uint256("0x01"));
assert(genesis.hashMerkleRoot == uint256("0x01"));

fRequireRPCPassword = false;
fMiningRequiresPeers = false;
fAllowMinDifficultyBlocks = false;
fDefaultConsistencyChecks = false;
fRequireStandard = true;
fMineBlocksOnDemand = true;
fSkipProofOfWorkCheck = false;
fTestnetToBeDeprecatedFieldRPC = false;
fHeadersFirstSyncingActive = false;

vFixedSeeds.clear();
vSeeds.clear();

In the main.cpp file I changed the following

int64_t nSubsidy = 0;

if(nHeight == 0) {
    nSubsidy = 0;
} else if( .......

Please note, I have also tried changing nSubsidy value for nHight = 0 to "50 * COIN" and also to higher value like "5000 * COIN". Everything results in the same issue.

2) Used the following code to create the genesis block.

if(genesis.GetHash() != uint256("0x"))
{
      printf("MSearching for genesis block...\n");
      uint256 hashTarget;
      hashTarget.SetCompact(genesis.nBits);
      while(uint256(genesis.GetHash()) > uint256(hashTarget))
      {
          ++genesis.nNonce;
          if (genesis.nNonce == 0)
          {
              printf("Mainnet NONCE WRAPPED, incrementing time");
              std::cout << std::string("Mainnet NONCE WRAPPED, incrementing time:\n");
              ++genesis.nTime;
          }
          if (genesis.nNonce % 10000 == 0)
          {
              printf("Mainnet: nonce %08u: hash = %s \n", genesis.nNonce, genesis.GetHash().ToString().c_str());
          }
      }
      printf("Mainnet block.nTime = %u \n", genesis.nTime);
      printf("Mainnet block.nNonce = %u \n", genesis.nNonce);
      printf("Mainnet block.hashMerkleRoot: %s\n", genesis.hashMerkleRoot.ToString().c_str());
      printf("Mainnet block.GetHash = %s\n", genesis.GetHash().ToString().c_str());
}

3) I got the following results.

block.nTime = 1538530667
block.nNonce = 3916247
block.hashMerkleRoot: 668d1e9c1c4f354589b9357d0ac0135b5e236f13ea8cf3bdf79e3670d347e07b
block.GetHash = 00000320986bcdf6c2e62f36321232ce1dfc77dc0e0b4a79e2199d8b77fa4820

4) Updated the above info in the file "chainparams.cpp".

boost::assign::map_list_of(0, uint256("00000320986bcdf6c2e62f36321232ce1dfc77dc0e0b4a79e2199d8b77fa4820"));

genesis.nTime = 1538530667;
genesis.nBits = 0x1e0ffff0;
genesis.nNonce = 3916247;

assert(hashGenesisBlock == uint256("00000320986bcdf6c2e62f36321232ce1dfc77dc0e0b4a79e2199d8b77fa4820"));
assert(genesis.hashMerkleRoot == uint256("668d1e9c1c4f354589b9357d0ac0135b5e236f13ea8cf3bdf79e3670d347e07b"));

5) Removed the code that I used to create the genesis block which I used in step 2 (above).

6) Compiled again, no errors or issues.

7) I made this current VPS as server and created a "coin.conf" file and added the following:

server=1
rpcuser=user
rpcpassword=password
listen=1
daemon=1

8) I did NOT clone this server to create a new client. Instead, I created a new VPS, installed all the dependences and copied the full folder - complete project into that client VPS to setup the client.

9) I added the following in "coin.conf" file in the client VPS.

addnode=<ipaddress of the server> (Ex: 207.87.10.2)

10) Started the wallet - "coin-qt" on the server. No errors.

11) Started the wallet - "coin-qt" on the client. No errors.

12) Both connected without issues.

13) Ran the following command on the client to mine the 1st block.

setgenerate true

Got the following error.

"wallet keypool empty"

stopped setgenerate using

setgenerate false

14) Executed the following command.

keypoolrefill

15) Again tried setgenerate

setgenerate true

16) Checked the debug.log file. This is what I am getting.

CreateNewBlock(): total size 1000
ERROR: CheckProofOfWork() : hash doesn't match nBits
ERROR: CheckBlockHeader() : proof of work failed
ERROR: CheckBlock() : CheckBlockHeader failed
CreateNewBlock() : TestBlockValidity failed
keypool return 2
keypool reserve 2

I printed the hashes in the debug file. This is what I get.

Hash = 35301222bb9164c21222107e1c43045c50c190d63eb8be2ad193ed38228b26a1
bnTarget = 00000fffff000000000000000000000000000000000000000000000000000000

I am stuck here. Whatever I do, how many times I start from scratch, I end up here.

Are there any other setting that I have to change related to ProofOfWork? What am I doing wrong? Can someone help me?

  • I've been hacking away at a PIVX fork as well. I will try generating a genesis block with your code in my fork tomorrow, and see if I get a different result. We could compare the source code we're using via GitHub. Do you mind sharing a link? – WG91 Oct 4 at 17:05
  • Downvoted. We have many questions regarding programming issues when creating a new altcoin. I don't believe such questions benefit a general audience, and this is a Q&A site, not a place for support. Furthermore, as a learning experience, contributing to an existing project is far more valuable. – Pieter Wuille Nov 14 at 1:02

I can confirm your code works. I implented your code into a few projects that, in turn, forked from PIVX. I wasn't able to generate genesis blocks in every project - which would suggest there is a bit more to this then just chainparams.cpp. However I was succesful in having the daemon produce genesis blocks that were also usable in some of these projects that forked from PIVX.

I removed all old hashes/checkpoints/changed ports and then I included your code as follows:

        hashGenesisBlock = genesis.GetHash();
        if(genesis.GetHash() != uint256("0x"))
        {
        printf("MSearching for genesis block...\n");
        uint256 hashTarget;
        hashTarget.SetCompact(genesis.nBits);
        while(uint256(genesis.GetHash()) > uint256(hashTarget))
        {
            ++genesis.nNonce;
            if (genesis.nNonce == 0)
            {
                printf("Mainnet NONCE WRAPPED, incrementing time");
                std::cout << std::string("Mainnet NONCE WRAPPED, incrementing time:\n");
                ++genesis.nTime;
            }
            if (genesis.nNonce % 10000 == 0)
            {
               printf("Mainnet: nonce %08u: hash = %s \n", genesis.nNonce, genesis.GetHash().ToString().c_str());
            }
        }
        printf("Mainnet block.nTime = %u \n", genesis.nTime);
        printf("Mainnet block.nNonce = %u \n", genesis.nNonce);
        printf("Mainnet block.hashMerkleRoot: %s\n", genesis.hashMerkleRoot.ToString().c_str());
        printf("Mainnet block.GetHash = %s\n", genesis.GetHash().ToString().c_str());
        }

        vFixedSeeds.clear();
        vSeeds.clear();

        [...]

        fRequireRPCPassword = false;        // default true
        fMiningRequiresPeers = false;       // default true
        fAllowMinDifficultyBlocks = false;
        fDefaultConsistencyChecks = false;
        fRequireStandard = true;
        fMineBlocksOnDemand = true;     // default false
        fSkipProofOfWorkCheck = false;      // default false
        fTestnetToBeDeprecatedFieldRPC = false;
        fHeadersFirstSyncingActive = false;

Hope this can point you in the right direction.

  • Great! Can you give me the steps that you used to produce genesis blocks by daemon ? – Harish Kumar B P Oct 5 at 18:32
  • Please note that its NOT my code. I found in from a other source. I cant find the link to share it over here. I just changed a couple of lines to make it work. Thats all. – Harish Kumar B P Oct 5 at 19:09
  • Ah, I just assumed so because I have been looking for the right code myself for quite some time. Great work putting this together! After inserting the code in chainparams.cpp, I reconfigured and compiled the code (./configure && make) after which I ran daemon (cd coin/src && ./coind). It started searching and quit after a minute or two, giving me the block hash/merkle root/nonce. – WG91 Oct 6 at 6:54
  • Ok. Thank you. Have you encountered the error - "txout.nvalue negative" when trying to mine the first block using the command "setgenerate true". I am stuck over there. This is on a PIVX fork. – Harish Kumar B P Oct 6 at 10:04
  • Hi, sorry for being offline for a month. I am encountering the same issue: " 2018-11-08 14:45:15 CreateNewBlock() : TestBlockValidity failed 2018-11-08 14:45:15 CreateNewBlock: Failed to detect masternode to pay 2018-11-08 14:45:15 CreateNewBlock(): total size 1000 2018-11-08 14:45:15 CheckBlock() : skipping transaction locking checks 2018-11-08 14:45:15 ERROR: CheckTransaction() : txout.nValue negative 2018-11-08 14:45:15 ERROR: CheckBlock() : CheckTransaction failed " You had any luck? – WG91 Nov 8 at 14:53

protected by Raghav Sood Oct 10 at 21:46

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.