Download single and specific block for study purposes I just started studying bitcoin.

I would like to be able to download a single block from the Bitcoin network.

This should be an old block, already mined, and which I can know the nonce.

Then I would like, via bash linux commands, to verify that such nonce solves the block.

Thanks to a question asked here, I know I could look at

https://blockchain.info/block-height/474044?format=json

However this is JSON, I imagine I can't use it to verify the nonce.

Hence my question is:


How can I download a block?
What is the file format of such block?
At what bytes start the nonce field I can increment, for example with a Python script?
Once I get to put the right (and known :) ) nonce how can verify that the block is solved? I mean, its hash should starts with lots of zeros, could I treat the block as a string and pass directly to the python hashlib function?


Thanks in advance.
However this is JSON, I imagine I can't use it to verify the nonce.


You can. You can build the block header using the data at the beginning of the JSON object and then hash that. Of course it would be easier to get the block hex, which you can get by going to the block hash on blockchain.info and appending ?format=hex to the url. For example, for the block height you linked, the hex of that block can be found here: https://blockchain.info/block/000000000000000000ff265a641721e79bbc559d8b0e4894332732109f1a3383?format=hex


  How can I download a block?


Get the block hex. That is the raw data for the block itself. You could make it a file by putting the hex in a hex editor or just save the hex as a string in a text file.


  What is the file format of such block?


Blocks don't have a file format. They are just blobs of binary data which you will need to understand how to parse. https://bitcoin.org/en/developer-reference#serialized-blocks explains the block serialization.


  At what bytes start the nonce field I can increment, for example with a Python script?


The part that is hashed is the block header, the first 80 bytes of the block. The nonce is the last 4 bytes of that header. You can read about the format of the block header here: https://bitcoin.org/en/developer-reference#block-headers


  Once I get to put the right (and known :) ) nonce how can verify that the block is solved? I mean, its hash should starts with lots of zeros, could I treat the block as a string and pass directly to the python hashlib function?


No. The block header is not a string. It is a blob of binary data. In python, the easiest thing to do is to make it a bytes object and then pass it in to hashlib.

If a block's proof of work is valid (note that does not mean the block itself is valid), then after you hash it with SHA256 Double, you treat the output as an integer and compare it to the nBits field of the header. The nBits field a 4 byte field beginning 8 bytes from the end of the header and is a compact representation of the target. You can read about how to interpret it here: https://bitcoin.org/en/developer-reference#target-nbits. The block hash (treated as a large integer) must be less than the target specified by the nBits field.