## Cannot mine Genesis / First Block (PIVX Fork / Clone)

0

4

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;


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"));

fMiningRequiresPeers = false;
fAllowMinDifficultyBlocks = false;
fDefaultConsistencyChecks = false;
fRequireStandard = true;
fMineBlocksOnDemand = true;
fSkipProofOfWorkCheck = false;
fTestnetToBeDeprecatedFieldRPC = 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
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
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 – 2018-10-04T17:05:05.137

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 – 2018-11-14T01:02:53.903

Were you able to solve this issue? No proper answer has been given but yet it seems that you have been successful in mining past the genesis block according to your newer question: https://bitcoin.stackexchange.com/questions/79977/how-can-i-simulate-proofofstake-pos-for-a-testnet-clone-of-pivx

– Joe Uhren – 2019-03-03T22:23:37.513

Hello Joe, well, I could not get this working with PIVX fork. I used an other fork which was a fork of PIVX and then, changed false to true for fSkipProofOfWorkCheck = true (this is not the proper way) and get it working. But, I never got this working with keeping fSkipProofOfWorkCheck = false; – Harish Kumar B P – 2019-03-05T01:32:30.913

1

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;


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 – 2018-10-05T18:32:33.980

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 – 2018-10-05T19:09:11.607

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. – None – 2018-10-06T06:54:08.710

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 – 2018-10-06T10:04:01.667

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? – None – 2018-11-08T14:53:38.657

I found the part the circumvent this. In main.cpp, look for the part that says: " // Check for negative or overflow output values CAmount nValueOut = 0;" I have changed this value to a positive block reward (say 100) and commented the 14 lines after it to circumvent the check. My issue now is that, when mining, it is looking for a masternode to pay out to - but since there are no coins, there are no masternodes.. Problem. Any idea on how to get around this? – None – 2018-11-09T20:21:32.730