Bitcoin Forum
November 03, 2024, 04:26:17 PM *
News: Latest Bitcoin Core release: 28.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: How to find the block header ?  (Read 264 times)
startlord (OP)
Newbie
*
Offline Offline

Activity: 4
Merit: 0


View Profile
March 13, 2018, 05:26:40 AM
 #1

I am using ubuntu and trying to get into blockchain development. I found a post on stackoverflow where a person performed the hashing of a block header and compared it with traget value. I know how to find target value from nBits but I cannot figure out how to find block header of any block so that I can also compare both to figure out whether the PoW is correct or not.

I am using Ubuntu.
achow101
Moderator
Legendary
*
expert
Offline Offline

Activity: 3542
Merit: 6885


Just writing some code


View Profile WWW
March 13, 2018, 02:44:57 PM
 #2

Assuming that you are running Bitcoin Core, you can use the getblockheader command to get the block header for a given block hash. To get the raw block header so you can hash it, you would do
Code:
getblockheader <hash> false

startlord (OP)
Newbie
*
Offline Offline

Activity: 4
Merit: 0


View Profile
March 13, 2018, 03:00:30 PM
 #3

I made some changes in chainparams.h of bitcoin core and I am trying to create genesis block. I have genesis block hash and merkel root but debug.log says hash too high

What I am actually trying to do is compare the target value with hash of genesis block header.

On running 'getblockheader' it says command not found.
achow101
Moderator
Legendary
*
expert
Offline Offline

Activity: 3542
Merit: 6885


Just writing some code


View Profile WWW
March 13, 2018, 04:28:26 PM
Merited by ABCbits (1)
 #4

I made some changes in chainparams.h of bitcoin core and I am trying to create genesis block. I have genesis block hash and merkel root but debug.log says hash too high

What I am actually trying to do is compare the target value with hash of genesis block header.
You have to actually mine the genesis block. You can do that with a while loop in chainparams.cpp that calls CheckProofofWork (with some modification). Or just use a script like https://github.com/lhartikk/GenesisH0 to do it for you.

startlord (OP)
Newbie
*
Offline Offline

Activity: 4
Merit: 0


View Profile
March 13, 2018, 06:25:35 PM
Last edit: March 13, 2018, 08:14:29 PM by startlord
 #5

I already generated genesis hash and merkle root using this repository but after compiling and running the core it gave "assertion failed".

Then I used          printf("genesis.GetHash = %s\n", genesis.GetHash().ToString().c_str());

and hash generated from it was correct but it genrated a debug.log file which had error :-


    2018-03-13 16:48:44 ERROR: ReadBlockFromDisk: Errors in block header at CBlockDiskPos(nFile=0, nPos=8)
    2018-03-13 16:48:44 *** Failed to read block
    2018-03-13 16:48:44 Error: Error: A fatal internal error occurred, see debug.log for details

Tracing back to ReadBlockFromDisk I reached to file validation.h and found this function :-


bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos, const Consensus::Params& consensusParams)
{
    block.SetNull();

    // Open history file to read
    CAutoFile filein(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION);
    if (filein.IsNull())
        return error("ReadBlockFromDisk: OpenBlockFile failed for %s", pos.ToString());

    // Read block
    try {
        filein >> block;
    }
    catch (const std::exception& e) {
        return error("%s: Deserialize or I/O error - %s at %s", __func__, e.what(), pos.ToString());
    }

    // Check the header
    if (!CheckProofOfWork(block.GetPoWHash(), block.nBits, consensusParams))
        return error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString());

    return true;
}

while tracing back to CBlockDiskPos I reached this funtion in chain.h file :-


struct CDiskBlockPos
{
    int nFile;
    unsigned int nPos;

    ADD_SERIALIZE_METHODS;

    template <typename Stream, typename Operation>
    inline void SerializationOp(Stream& s, Operation ser_action) {
        READWRITE(VARINT(nFile));
        READWRITE(VARINT(nPos));
    }

    CDiskBlockPos() {
        SetNull();
    }

    CDiskBlockPos(int nFileIn, unsigned int nPosIn) {
        nFile = nFileIn;
        nPos = nPosIn;
    }

    friend bool operator==(const CDiskBlockPos &a, const CDiskBlockPos &b) {
        return ([Suspicious link removed]ile == b.nFile && a.nPos == b.nPos);
    }

    friend bool operator!=(const CDiskBlockPos &a, const CDiskBlockPos &b) {
        return !(a == b);
    }

    void SetNull() { nFile = -1; nPos = 0; }
    bool IsNull() const { return (nFile == -1); }

    std::string ToString() const
    {
        return strprintf("CBlockDiskPos(nFile=%i, nPos=%i)", nFile, nPos);
    }

};

Now I am trying to figure out what to do next?
starmyc
Full Member
***
Offline Offline

Activity: 198
Merit: 130

Some random software engineer


View Profile
March 19, 2018, 11:54:56 AM
 #6

and hash generated from it was correct but it genrated a debug.log file which had error :-


    2018-03-13 16:48:44 ERROR: ReadBlockFromDisk: Errors in block header at CBlockDiskPos(nFile=0, nPos=8)
    2018-03-13 16:48:44 *** Failed to read block
    2018-03-13 16:48:44 Error: Error: A fatal internal error occurred, see debug.log for details

Now I am trying to figure out what to do next?

Did you delete your blocks directory after modify code ?
If you are trying to read old blocks using old rules with new rules, you'll hit a lot of those errors. The best is to clean up everything at each try.

Hi, I'm just some random software engineer.
You can check my projects: Bitcoin & altcoin balances/addresses listing dumps: https://balances.crypto-nerdz.org/
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!