Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: startlord on March 13, 2018, 05:26:40 AM



Title: How to find the block header ?
Post by: startlord on March 13, 2018, 05:26:40 AM
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.


Title: Re: How to find the block header ?
Post by: achow101 on March 13, 2018, 02:44:57 PM
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


Title: Re: How to find the block header ?
Post by: startlord on March 13, 2018, 03:00:30 PM
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.


Title: Re: How to find the block header ?
Post by: achow101 on March 13, 2018, 04:28:26 PM
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.


Title: Re: How to find the block header ?
Post by: startlord on March 13, 2018, 06:25:35 PM
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?


Title: Re: How to find the block header ?
Post by: starmyc on March 19, 2018, 11:54:56 AM
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.