0

Please help me understand the logic behind mining a bitcoin from scratch. I want to create a bitcoin miner, it will not receive any blocks from any pools. All should be done inside the miner.

So, I have made some progress, I've coded with vb.net and successfully got a result of a hash (old example)

Version 2               
Prev. Block 00000000000000000A2940884E0C3BC96510CAD11912A527E9D15DF42F0E1D67                
Merkle Root 2E99F445C007A9158207CC30CEBAD2B3D26C45FDAB2EBDF50D261335FC00D92C                
Time    12/16/14 18:05:40               
Bits    404454260               
Nonce   3225483075              

Block Hash  000000000000000015A8D88216918C8DE090268A5E7F53FEEF72CD111F7F27FF

So, suppose old example is a real one, so, I want to know where do I include my bitcoin wallet address if I want to get the block reward to my wallet address? Should I have hashed the wallet address with the above and hash with the wallet and come up with a hash which is less than the target? Or after getting a good hash, just send data to blockchain with the wallet address to receive the block reward? or should I create some other hash with my wallet address? what should I do to include my wallet address in this?

pseudo code would also be nice If you can provide for me to understand how to include my wallet address.

my pseudo code upto now

**Step 1**
version -> convert to hex

Prev. Block -> convert to hex

Merkle Root -> convert to hex

Time  -> get unix time and convert to hex

Bits -> convert to hex

Nonce -> convert to hex

**step 2**
Add hex values of all the above and then hash it with Double SHA256 to receive the hash value.

**step3** 
Check if the final hash value is less than the target 

**step 4?** what should I do with the above hash? where should I include my wallet address? 
2

Mining a valid block involves multiple steps.

To begin with, you must know which block you are mining on top of, which is specified by the previous block hash.

Then, you must obtain a merkle root that locks in a valid set of transactions. Usually, this is a set of transactions from the mempool + the coinbase transaction, such that there are no double spends or invalid transactions. These transactions are assembled into a merkle tree, from which the merkle root is placed into the block header.

The coinbase transaction in particular is important, as it must follow the coinbase transaction rules (inclusion of block height, correct output for the block reward, extraNonce). It must be placed as the first transaction in the block, and contains the output that sends the BTC to your wallet.

Finally, you must add in the other fields such as block version, time, nonce, etc.

This complete block header of 80 bytes is the payload that is hashed and compared against the target value. If your resulting hash does not meet the target requirements, you must change the nonce in the block header, the extraNonce in the coinbase transaction, or any of the transactions in your merkle tree to obtain a new merkle root, and hash the new block header. This process is repeated until you find a valid hash for the target.

In order, your approach would be like:

  • Get a list of transactions you'd like to include (this can be an empty list)
  • Select your wallet address to receive the block reward
  • Build a coinbase transaction that pays this reward to your selected address (plus any transaction fees)
  • Make the coinbase tx the first one in your list
  • Build a merkle tree from this list
  • Select the merkle root from this tree
  • Proceed as you're doing already
| improve this answer | |
  • Did you see the pseudo code I wrote? can you simplify the steps as I did? because internet has a lot of essay like writings but no simplified steps. when we're coding we follow steps. can you please provide the simplified steps if you don't mind? – Pretty Girl Feb 17 at 17:20
  • 2
    @PrettyGirl I've added a more concise list, but beyond this, I would recommend picking up a book like Mastering Bitcoin to understand what's going on - you seem to be missing a few basic concepts here that are resulting in a bunch of confusion – Raghav Sood Feb 17 at 17:35
  • now I understood where my wallet address should include, it should include in the merkle root :) phew. Now I should focus on how to generate the merkle root which includes my wallet address. right? :) – Pretty Girl Feb 17 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.