Hello,
I am trying to write a pure JAVA miner, a nice webstart app or applet that can be embedded in webpages, and wanted to clarify my understanding of how things work..
1) If when I add a transaction to my current block, the Merkle Root (of all the transactions) in the Block Header changes, doesn't that mean that ALL the work i have done before is for nothing as I will have to start looking for the correct HASH again ? Wouldn't it be better to not add more transactions, and keep working on my current block, with the current Merkle Root ?
It doesn't work that way. If you test a hash and it fails, then that work is already lost.
It is like rolling 10 dice and hoping for all 6's. If you do it a million times, and haven't succeeded, all that work is already gone.
Each time you update the nonce and fail, you are trying a whole new attempt.
2) This may be answered by the answer to 1), but just in case - Since you get 25 btc for finding a block, much more than you make from the transaction fees, Why not just work soley on finding hashes of blocks with no transactions in it ? This way your Merkle root stays constant until you need to use the extraNonce in the coinbase txn ? Would this not give you the greatest chance of finding a valid hash of a certain difficulty ?
That is anti-social. However, it would work. There is talk of putting in some ways to deal with it, if it became a problem.
3) In the Bitcoin Protocol Wiki it says -
'The SHA256 hash that identifies each block (and which must have a run of 0 bits) is calculated from the first 6 fields of this structure (version, prev_block, merkle_root, timestamp, bits, nonce, and standard SHA256 padding, making two 64-byte chunks in all) and not from the complete block. To calculate the hash, only two chunks need to be processed by the SHA256 algorithm. Since the nonce field is in the second chunk, the first chunk stays constant during mining and therefore only the second chunk needs to be processed..'
This I don't get.. Where do the 2 * 64 Byte Chunks come from ? When I Hash my Block Header I get a 32 byte hash.. ?
Sha-2 works on 512 bit "chunks". When hashing a file, it is padded so that it is an integer multiple of 512.
So, since it is double sha-2
First call to the sha function
Sha2(header)
This is > 64 bytes, so extend to 2X64 bytes
Round 1: bytes 0 to 63 of the header
Round 2: 64 - 79 of the header (the nonce is here)
Sha2(output from round 1)
This is 32 bytes, so it is padded to 64 bytes
Round 1: bytes 0 to 63 of the padded output
The output of the 2nd call is the hash of the header.
Since the nonce is in the first round of the first sha2 call, you can compute it once and save the state. Each time you change the nonce, it only changes the results from round 2 onwards.