<p>Mining a valid block involves multiple steps.</p>

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

<p>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.</p>

<p>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.</p>

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

<p>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.</p>

<p>In order, your approach would be like:</p>

<ul>
<li>Get a list of transactions you'd like to include (this can be an empty list)</li>
<li>Select your wallet address to receive the block reward</li>
<li>Build a coinbase transaction that pays this reward to your selected address (plus any transaction fees)</li>
<li>Make the coinbase tx the first one in your list</li>
<li>Build a merkle tree from this list</li>
<li>Select the merkle root from this tree</li>
<li>Proceed as you're doing already</li>
</ul>

CC BY-SA 4.0
2020-02-17T17:20:30.773Z
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
CC BY-SA 4.0
2020-02-17T17:35:35.833Z
@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
CC BY-SA 4.0
2020-02-17T17:37:44.083Z
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
CC BY-SA 4.0
2020-02-17T17:15:54.210Z
2020-02-17T17:34:58.557Z
2020-02-17T17:34:58.557Z
Raghav Sood
<p>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.</p>

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

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

Block Hash  000000000000000015A8D88216918C8DE090268A5E7F53FEEF72CD111F7F27FF
</code></pre>

<p>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? </p>

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

<p><strong>my pseudo code upto now</strong></p>

<pre><code>**Step 1**
version -&gt; convert to hex

Prev. Block -&gt; convert to hex

Merkle Root -&gt; convert to hex

Time  -&gt; get unix time and convert to hex

Bits -&gt; convert to hex

Nonce -&gt; 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? 
</code></pre>

<bitcoin-core><hash>
Where do I include my wallet address after finding the hash less than target - bitcoin
Pretty Girl
# Where do I include my wallet address after finding the hash less than target - bitcoin

(Asked by: username_0 on 2020-02-17)

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? 

```

**Tags:** bitcoin-core, hash


---

## 1 answers

### Answer by username_1 on 2020-02-17. Score: 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

> **Comments:**
> **username_0**: 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?  
> **username_1**: @username_0 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  
> **username_0**: 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? :)  

---

