Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: wsxdrfv on February 24, 2018, 01:05:01 PM



Title: How to actually mine genesis block?
Post by: wsxdrfv on February 24, 2018, 01:05:01 PM
How to actually mine genesis block?

At old source, there is  [ hashTarget ] and seems loop hash until become below than [ hashTarget ]

But at new source, there is no [ hashTarget ] variable.

What name it changed?

Where is the part actually mine block?

By reference it, maybe I can write code mine genesis block?


And at new source, validation.cpp  986 line,
there are code like,

Code:
bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos, const Consensus::Params& consensusParams)
.....
....
if(!CheckProofOfWork(block.GetPoWHash(), block.nBits, consensusParams))
...

How this [ CheckProofOfWork ] function can be used? There is no prior variable declaration.
There is just  [ #include "pow.h" ]
Is this enough?

I used this [ CheckProofOfWork ]  function same to  chainparams.cpp  , but error says

undefined reference to `CheckProofOfWork

Why? how to use this function in chainparams.cpp?





Title: Re: How to actually mine genesis block?
Post by: bob123 on February 24, 2018, 04:01:36 PM
And at new source, validation.cpp  986 line,
there are code like,

Code:
bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos, const Consensus::Params& consensusParams)
.....
....
if(!CheckProofOfWork(block.GetPoWHash(), block.nBits, consensusParams))
...

How this [ CheckProofOfWork ] function can be used? There is no prior variable declaration.
There is just  [ #include "pow.h" ]
Is this enough?

I used this [ CheckProofOfWork ]  function same to  chainparams.cpp  , but error says

undefined reference to `CheckProofOfWork


To use the function you simply call it. pow.h is necessary, but not the only file you need.
To see the CheckProofOfWork() you have to look at pow.cpp (https://github.com/bitcoin/bitcoin/blob/5961b23898ee7c0af2626c46d5d70e80136578d3/src/pow.cpp (https://github.com/bitcoin/bitcoin/blob/5961b23898ee7c0af2626c46d5d70e80136578d3/src/pow.cpp)):

Code:
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params& params)
{
    bool fNegative;
    bool fOverflow;
    arith_uint256 bnTarget;

    bnTarget.SetCompact(nBits, &fNegative, &fOverflow);

    // Check range
    if (fNegative || bnTarget == 0 || fOverflow || bnTarget > UintToArith256(params.powLimit))
        return false;

    // Check proof of work matches claimed amount
    if (UintToArith256(hash) > bnTarget)
        return false;

    return true;
}

I'd suggest you head over to https://github.com/bitcoin/bitcoin (https://github.com/bitcoin/bitcoin) and use the search function at the top.
You should find everything you are looking for.


Title: Re: How to actually mine genesis block?
Post by: wsxdrfv on February 25, 2018, 04:27:55 AM
To use the function you simply call it. pow.h is necessary, but not the only file you need.
To see the CheckProofOfWork() you have to look at pow.cpp (https://github.com/bitcoin/bitcoin/blob/5961b23898ee7c0af2626c46d5d70e80136578d3/src/pow.cpp (https://github.com/bitcoin/bitcoin/blob/5961b23898ee7c0af2626c46d5d70e80136578d3/src/pow.cpp)):

If simply call it at chainparams.cpp, error says there is no definition of 'CheckProofOfWork'.

And if I added #include "pow.cpp" at the top of chainparams.cpp, and wrote like
Code:
        pchMessageStart[3] = 0xaa;
        nDefaultPort = 1111;
        nPruneAfterHeight = 100000;
// nonce backup value : 591389
        genesis = CreateGenesisBlock(1519198776, 591389, 0x1e0ffff0, 1, 50 * COIN);  // 0x1effff0 is 504365040

const CChainParams& chainparams = Params();
while(!CheckProofOfWork(genesis.GetHash(), genesis.nBits, Params().GetConsensus())){ ++genesis.nNonce; }


then error says

----------
libbitcoin_common.a(libbitcoin_common_a-chainparams.o): In function `GetNextWorkRequired(CBlockIndex const*, CBlockHeader const*, Consensus::Params const&)':
/home/lee/Desktop/BitcoinEssence/src/pow.cpp:15: multiple definition of `GetNextWorkRequired(CBlockIndex const*, CBlockHeader const*, Consensus::Params const&)'
libbitcoin_server.a(libbitcoin_server_a-pow.o):/home/lee/Desktop/BitcoinEssence/src/pow.cpp:15: first defined here
libbitcoin_common.a(libbitcoin_common_a-chainparams.o): In function `CalculateNextWorkRequired(CBlockIndex const*, long, Consensus::Params const&)':
/home/lee/Desktop/BitcoinEssence/src/pow.cpp:59: multiple definition of `CalculateNextWorkRequired(CBlockIndex const*, long, Consensus::Params const&)'
libbitcoin_server.a(libbitcoin_server_a-pow.o):/home/lee/Desktop/BitcoinEssence/src/pow.cpp:59: first defined here
libbitcoin_common.a(libbitcoin_common_a-chainparams.o): In function `CheckProofOfWork(uint256, unsigned int, Consensus::Params const&)':
/home/lee/Desktop/BitcoinEssence/src/pow.cpp:92: multiple definition of `CheckProofOfWork(uint256, unsigned int, Consensus::Params const&)'
libbitcoin_server.a(libbitcoin_server_a-pow.o):/home/lee/Desktop/BitcoinEssence/src/pow.cpp:92: first defined here

-----------

So what now?

Strange is, as I mentioned already, validation.cpp just simply call 'CheckProofOfWork' without any prior declaration.

How?


Title: Re: How to actually mine genesis block?
Post by: achow101 on February 25, 2018, 04:13:16 PM
The error means that the linker could not find the function declaration. You will need to modify Makefile.am so that the linker can find pow.h when it is linking any compiled units that use chainparams.cpp.

What you actually have to change you will need to figure out yourself.


Title: Re: How to actually mine genesis block?
Post by: starmyc on February 26, 2018, 06:06:12 AM
If simply call it at chainparams.cpp, error says there is no definition of 'CheckProofOfWork'.
And if I added #include "pow.cpp" at the top of chainparams.cpp, and wrote like

You want to include "pow.h" and only, not "pow.cpp"

I used this [ CheckProofOfWork ]  function same to  chainparams.cpp  , but error says

undefined reference to `CheckProofOfWork

Why? how to use this function in chainparams.cpp?

Something like the following will work (Tested against last bitcoin version, commit bf3353de9059):

Code:
// In src/chainparams.cpp, in headers
#include <pow.h>

...
// still in src/chainparams.cpp, in CMainParams:CMainParams() (~ line #76)
    genesis = CreateGenesisBlock(1231006505, 2083236893, 0x1d00ffff, 1, 50 * COIN);
    consensus.hashGenesisBlock = genesis.GetHash();

    uint32_t nNonce;
    for(nNonce = 0; ; nNonce++){
        genesis.nNonce = nNonce;
        // You can also update genesis.nTime

        if (CheckProofOfWork(genesis.GetHash(), genesis.nBits, consensus)) {
            printf("hash: %s\n", genesis.GetHash().GetHex().c_str());
            printf("nonce: %i\n", nNonce);
            break;
        }

        if (nNonce == 0) {
            printf("Can't find a valid nNonce.\n");
            break;
        }
    }

    assert(consensus.hashGenesisBlock == uint256S("0x000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"));
    assert(genesis.hashMerkleRoot == uint256S("0x4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"));


Title: Re: How to actually mine genesis block?
Post by: wsxdrfv on February 26, 2018, 06:26:56 AM
If simply call it at chainparams.cpp, error says there is no definition of 'CheckProofOfWork'.
And if I added #include "pow.cpp" at the top of chainparams.cpp, and wrote like

You want to include "pow.h" and only, not "pow.cpp"

I used this [ CheckProofOfWork ]  function same to  chainparams.cpp  , but error says

undefined reference to `CheckProofOfWork

Why? how to use this function in chainparams.cpp?

Something like the following will work (Tested against last bitcoin version, commit bf3353de9059):

Code:
// In src/chainparams.cpp, in headers
#include <pow.h>

...
// still in src/chainparams.cpp, in CMainParams:CMainParams() (~ line #76)
    genesis = CreateGenesisBlock(1231006505, 2083236893, 0x1d00ffff, 1, 50 * COIN);
    consensus.hashGenesisBlock = genesis.GetHash();

    uint32_t nNonce;
    for(nNonce = 0; ; nNonce++){
        genesis.nNonce = nNonce;
        // You can also update genesis.nTime

        if (CheckProofOfWork(genesis.GetHash(), genesis.nBits, consensus)) {
            printf("hash: %s\n", genesis.GetHash().GetHex().c_str());
            printf("nonce: %i\n", nNonce);
            break;
        }

        if (nNonce == 0) {
            printf("Can't find a valid nNonce.\n");
            break;
        }
    }

    assert(consensus.hashGenesisBlock == uint256S("0x000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"));
    assert(genesis.hashMerkleRoot == uint256S("0x4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"));


Thanks, but still following errors.

---------------
lee@newlite:~/Desktop/BitcoinEssence/src$ make
make[1]: Entering directory '/home/lee/Desktop/BitcoinEssence/src'
make[2]: Entering directory '/home/lee/Desktop/BitcoinEssence'
make[2]: Leaving directory '/home/lee/Desktop/BitcoinEssence'
  CXX      libbitcoin_common_a-chainparams.o
  AR       libbitcoin_common.a
  CXXLD    bitcoinessenced
  CXXLD    bitcoinessence-tx
libbitcoin_common.a(libbitcoin_common_a-chainparams.o): In function `CMainParams::CMainParams()':
/home/lee/Desktop/BitcoinEssence/src/chainparams.cpp:262: undefined reference to `CheckProofOfWork(uint256, unsigned int, Consensus::Params const&)'
collect2: error: ld returned 1 exit status
Makefile:3663: recipe for target 'bitcoinessence-tx' failed
make[1]: *** [bitcoinessence-tx] Error 1
make[1]: Leaving directory '/home/lee/Desktop/BitcoinEssence/src'
Makefile:9337: recipe for target 'all-recursive' failed
make: *** [all-recursive] Error 1
lee@newlite:~/Desktop/BitcoinEssence/src$
---------------

Of course I added #include "pow.h" or <pow.h> ? -> but anyway both result error above.