How to create a new genesis block using CreateGenesisBlock() function



I'm trying to create a new genesis block, you know for science. I saw a lot of write ups online but most of them seem kinda outdated. I found the function CreateGenesisBlock() in the source code.

    genesis = CreateGenesisBlock(1317972665, 1337, 0x1e0ffff0, 1, 100 * COIN);

    std::cout<< genesis.GetHash().ToString().c_str() << std::endl;
    std::cout<< genesis.hashMerkleRoot.ToString().c_str() << std::endl;

    consensus.hashGenesisBlock = genesis.GetHash();

This is my current code. When running my coind it fails with ERROR: ReadBlockFromDisk: Errors in block header at CBlockDiskPos(nFile=0, nPos=8)

From my current research it seem the genesis block failed the PoW criteria somehow.

I thought about putting the CreateGenesisBlock in a loop to generate blocks until I found a valid one but I'm not sure how to check if the block is valid. Can someone gimme a tip?


Posted 2018-02-06T13:47:19.930

Reputation: 155

I met same problem. Have you solved? How? – creator – 2018-02-22T05:30:49.670



The genesis block still must meet the proof of work requirements that other blocks must also meet. So you must actually mine the genesis block. To do this, you need to put CreateGenesisBlock in a while loop and just keep incrementing the nonce, and if necessary, increment an extranonce in the coinbase transaction.

To check that it has a valid proof of work, use the CheckProofOfWork() function (grep for it and you can figure out what arguments it takes).

Andrew Chow

Posted 2018-02-06T13:47:19.930

Reputation: 50 267

To check that it has a valid proof of work, use the CheckProofOfWork() function I think this is what I was missing. Cheers! – soupdiver – 2018-02-06T16:20:19.217

@soupdiver Please write all the code you used. I am also stuck at this stage for days... – creator – 2018-02-17T14:08:30.530

@leegod I assume we're working on the same (demo) project as (most?) of the others who came up with that question in the recent months... In the end I found a python, which I had to modify, which "mined" a valid genesis block. – soupdiver – 2018-02-18T15:51:57.040

@soupdiver I am working on newest litecoin source. To make alt. So what is that python? You mean GenesisH0? How you modified and what values should I input exactly to use it correctly? – creator – 2018-02-19T21:20:07.043

@leegod yep it's GenesisH0. I don't know about requirements/specification of the coin you trying to create. So I can't give you exact values. – soupdiver – 2018-02-21T06:54:46.623

I just want to clone litecoin with some parameters change. And GenesisH0, I can't match right value, what values should I input there I don't know exactly, I tried various things, it just errors. so I give up using GenesisH0, just used printf method, now error says see debug.log file, debug.log says ReadBlockFromDisk error. – creator – 2018-02-21T07:54:05.337

and also, if use GenesisH0, it just mine in its script only, isn't it? Then no related with mycoind client? Then how make pre-mine reward to own wallet address? Because using GenesisH0 means no client-side's actual genesis block mining? – creator – 2018-02-22T05:39:30.853

@Andrew Chow Hi. Thanks for answering. But I am also stuck at this stage. Can you link or put he exact working code? I can't find anywhere. – creator – 2018-02-22T07:30:41.497

@Andrew Chow Where that CreateGenesisBlock is? chainparams.cpp's original place? (line 123~5)

and wrap this up with while loop?

And is this the code you answered?

– creator – 2018-02-22T08:02:27.407

@leegod I don't have any specific code that I can provide you. I have not written it nor do I plan to. I am not in the business of creating altcoins or helping people make altcoins. – Andrew Chow – 2018-02-22T16:09:12.083

@AndrewChow So I did at chainparams.cpp file like,
const CChainParams& chainparams = Params(); while(!CheckProofOfWork(genesis.GetHash(), genesis.nBits, chainparams.GetConsensus()) { ++ genesis.nNonce; }

But this does not even compiled. I added #include "pow.h" at top of chainparams.cpp, but when I try 'make' for compile, terminal error says,

chainparams.cpp:127 : undefined reference to "CheckProofOfWork(uint256,,,,, )'

How to pass this?... Plz. – creator – 2018-02-25T12:53:14.673

You will need to modify the file so that the generated Makefile can find pow.h and link it when it compiles validation.cpp. – Andrew Chow – 2018-02-25T16:11:25.047

@AndrewChow So I revised, so now that [undefined] error solved. But now I get new error, says,

lee@newlite:~/Desktop/BitcoinEssence/src$ ./bitcoinessenced bitcoinessenced: chainparams.cpp:524: const CChainParams& Params(): Assertion `globalChainParams' failed. Aborted (core dumped)

Here is detail.

– creator – 2018-02-27T13:22:22.057