Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a genesis block of my altercoin?

Tags:

bitcoin

I create my new altercoin,but when I first run it,it already shows:"No block source available 5 week(s) behind". And I start anther computer in the LAN, they link succefully. I use the code of Datacoin.

static const uint256 hashGenesisBlockOfficial("f9f6d9a689f7a4093c71f397d8fe3fbef3a05cd6f919d51b4a9447aa22743dfb");
static const uint256 hashGenesisBlockTestNet("f9f6d9a689f7a4093c71f397d8fe3fbef3a05cd6f919d51b4a9447aa22743dfb");

  // Genesis block
        qDebug()<<"Genesis block";
        const char* pszStartTopic = "The Times 26/Dec/2013 Chancellor on brink of second bailout for banks";//https://bitcointalk.org/index.php?topic=325735.0";
        CTransaction txNew;
        txNew.vin.resize(1);
        txNew.vout.resize(1);
        txNew.vin[0].scriptSig = CScript() << 0 << CBigNum(999) << vector<unsigned char>((const unsigned char*)pszStartTopic, (const unsigned char*)pszStartTopic + strlen(pszStartTopic));
        txNew.vout[0].nValue = COIN;
        txNew.vout[0].scriptPubKey = CScript();
        CBlock block;
        block.vtx.push_back(txNew);
        block.hashPrevBlock = 0;
        block.hashMerkleRoot = block.BuildMerkleTree();
        block.nTime    = 1387977869 ;//http://www.unixtimestamp.com/index.php
        block.nBits    = TargetFromInt(6);
        block.nNonce   = 49030125;
        block.bnPrimeChainMultiplier = (uint64) 5651310;

        if (fTestNet)
        {
            block.nTime    = 1387977869 ;
            block.nBits    = TargetFromInt(4);
            block.nNonce   = 46032;
            block.bnPrimeChainMultiplier = (uint64) 211890;
        }

        //// debug print
        uint256 hash = block.GetHash();
        printf("%s\n", hash.ToString().c_str());

        qDebug()<<"hash:"<<hash.ToString().c_str();
        printf("%s\n", hashGenesisBlock.ToString().c_str());
        qDebug()<<"hashGenesisBlock:"<<hashGenesisBlock.ToString().c_str();
        printf("%s\n", block.hashMerkleRoot.ToString().c_str());
         qDebug()<<"block.hashMerkleRoot:"<<block.hashMerkleRoot.ToString().c_str();
        assert(block.hashMerkleRoot == uint256("a0c44c1b6dd50fcaa2bc1c4d7f8ca406506caee88578d751fb3824b41bc34d84"));
        block.print();

        assert(hash == hashGenesisBlock);
        {
            CValidationState state;
            assert(block.CheckBlock(state, true, true));
            assert(CheckProofOfWork(block.GetHeaderHash(), block.nBits, block.bnPrimeChainMultiplier, block.nPrimeChainType, block.nPrimeChainLength));
        }

Failed at assert(CheckProofOfWork(block.GetHeaderHash(), block.nBits, block.bnPrimeChainMultiplier, block.nPrimeChainType, block.nPrimeChainLength));. And the debug.log says:

    CBlock(hash=f9f6d9a689f7a4093c71f397d8fe3fbef3a05cd6f919d51b4a9447aa22743dfb, hashBlockHeader=7d6aeeb7ca2b87d2f48bbd7a675c8374691c4f44f0db1a10de66436bfbcb0188, ver=2, hashPrevBlock=0000000000000000000000000000000000000000000000000000000000000000, hashMerkleRoot=a0c44c1b6dd50fcaa2bc1c4d7f8ca406506caee88578d751fb3824b41bc34d84, nTime=1387977869, nBits=06000000, nNonce=49030125, vtx=1)
      CTransaction(hash=a0c44c1b6dd50fcaa2bc1c4d7f8ca406506caee88578d751fb3824b41bc34d84, ver=1, vin.size=1, vout.size=1, nLockTime=0, data.size=0)
        CTxIn(COutPoint(0000000000000000000000000000000000000000000000000000000000000000, 4294967295), coinbase 0002e703455468652054696d65732032362f4465632f32303133204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73)
        CTxOut(error)
ERROR: CheckPrimeProofOfWork() : block header hash under limit
ERROR: CheckProofOfWork() : check failed for prime proof-of-work

Anything else can I change to make the genesis block work?

like image 903
Gank Avatar asked Dec 25 '13 12:12

Gank


1 Answers

You must mine the genesis block. There is no code in the most recent heads of the altcoin sources that can do the mining for you, although earlier releases of the source code would automatically create a new genesis block at that point, instead of failing the assertion. That was taken out, since it's not needed and may in fact cause more headaches for the average miner if their local db becomes unavailable.

You can try to go back in time on your source code tree to find an earlier version of the source you forked from, and try to find the code that creates the genesis block. When it runs it will try to mine a new genesis block, and when it does it will fail one more time at that assertion. You then have to put that hash and nonce into your main.h file, and do it for both your main net and test net. After that, the assertion will not fail anymore and you have the hashes for your genesis block in the code now.

Here's the code that does it, in case you can't find it in your older source..

    // If genesis block hash does not match, then generate new genesis hash.
    if (block.GetHash() != hashGenesisBlock)
    {
        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 = CBigNum().SetCompact(block.nBits).getuint256();
        uint256 thash;

        while(true)
        {
            thash = scrypt_blockhash(BEGIN(block.nVersion));
            if (thash <= hashTarget)
                break;
            if ((block.nNonce & 0xFFF) == 0)
            {
                printf("nonce %08X: hash = %s (target = %s)\n", block.nNonce, thash.ToString().c_str(), hashTarget.ToString().c_str());
            }
            ++block.nNonce;
            if (block.nNonce == 0)
            {
                printf("NONCE WRAPPED, incrementing time\n");
                ++block.nTime;
            }
        }
        printf("block.nTime = %u \n", block.nTime);
        printf("block.nNonce = %u \n", block.nNonce);
        printf("block.GetHash = %s\n", block.GetHash().ToString().c_str());

Once it finds a nonce that makes a hash that satisfies the target, it will stop and print the nonce and hash values. You need to put these new genesis block nonce and hash values into your code so that it will pass the assertions next time.

If your code is based on older (non-optimized) source code, then you won't have a function scrypt_blockhash() and you likely need to use the original functions. So change the code:

            thash = scrypt_blockhash(BEGIN(block.nVersion));

to

            static char scratchpad[SCRYPT_SCRATCHPAD_SIZE];
            scrypt_1024_1_1_256_sp(BEGIN(block.nVersion), BEGIN(thash), scratchpad);

If you do need to use the older function scrypt_1024_1_1_256_sp then you should also find the definition of SCRYPT_SCRATCHPAD_SIZE in the header files too.

Finally, all this is printed in the debug.log files. You will need to look there to find it.

Cheers!

like image 67
Brian Onn Avatar answered Sep 27 '22 16:09

Brian Onn