Bitcoin Forum
May 12, 2024, 09:53:16 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: How to actually mine genesis block?  (Read 401 times)
wsxdrfv (OP)
Jr. Member
*
Offline Offline

Activity: 405
Merit: 5


View Profile WWW
February 24, 2018, 01:05:01 PM
 #1

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?



1715550796
Hero Member
*
Offline Offline

Posts: 1715550796

View Profile Personal Message (Offline)

Ignore
1715550796
Reply with quote  #2

1715550796
Report to moderator
"Your bitcoin is secured in a way that is physically impossible for others to access, no matter for what reason, no matter how good the excuse, no matter a majority of miners, no matter what." -- Greg Maxwell
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715550796
Hero Member
*
Offline Offline

Posts: 1715550796

View Profile Personal Message (Offline)

Ignore
1715550796
Reply with quote  #2

1715550796
Report to moderator
1715550796
Hero Member
*
Offline Offline

Posts: 1715550796

View Profile Personal Message (Offline)

Ignore
1715550796
Reply with quote  #2

1715550796
Report to moderator
1715550796
Hero Member
*
Offline Offline

Posts: 1715550796

View Profile Personal Message (Offline)

Ignore
1715550796
Reply with quote  #2

1715550796
Report to moderator
bob123
Legendary
*
Offline Offline

Activity: 1624
Merit: 2481



View Profile WWW
February 24, 2018, 04:01:36 PM
 #2

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):

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 and use the search function at the top.
You should find everything you are looking for.

wsxdrfv (OP)
Jr. Member
*
Offline Offline

Activity: 405
Merit: 5


View Profile WWW
February 25, 2018, 04:27:55 AM
 #3

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):

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?
achow101
Moderator
Legendary
*
expert
Offline Offline

Activity: 3388
Merit: 6635


Just writing some code


View Profile WWW
February 25, 2018, 04:13:16 PM
 #4

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.

starmyc
Full Member
***
Offline Offline

Activity: 198
Merit: 130

Some random software engineer


View Profile
February 26, 2018, 06:06:12 AM
 #5

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

Hi, I'm just some random software engineer.
You can check my projects: Bitcoin & altcoin balances/addresses listing dumps: https://balances.crypto-nerdz.org/
wsxdrfv (OP)
Jr. Member
*
Offline Offline

Activity: 405
Merit: 5


View Profile WWW
February 26, 2018, 06:26:56 AM
 #6

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.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!