<p>The difficulty is really a human readable expression. Under the hood, the Bitcoin protocol uses the <em>target</em>. <a href="https://bitcoin.stackexchange.com/a/57186/5406">The target is a custom 4-byte float value encoding an exponent and a mantissa</a> that is to represent a number used as the upper bound for the block hash of valid blocks.</p>
<p>The target is updated every 2016 blocks after the last block of a difficulty period has been found. It depends on the timestamps of the first and the last block of the difficulty period and the previous target:</p>
<pre><code>new_target = old_target × (t_last - t_first)/14 days
</code></pre>
<p>Every full node that has seen these two blocks can calculate the new target.</p>
<p>Miners assemble unconfirmed transactions into block templates, create coinbase transactions and fill up the remaining header fields to produce block candidates, test each block candidate by hashing its header and comparing the candidate's hash to the target to determine whether a block has been found. The current target since height 693,504 is <code>0x17136aa2</code> which is a compact representation for <code>0x000000000000000000136aa20000000000000000000000000000000000000000</code>. If I counted right, this hexadecimal number would translate to a binary number with 75 leading zeroes. At the current hashrate of about 109.7 EH/s, the network will try about 6.6×10<sup>22</sup> block candidates until a block is found.</p>

CC BY-SA 4.0
2021-08-02T19:47:54.810Z
2021-08-02T23:19:30.953Z
2021-08-02T23:19:30.953Z
Murch
<p>I have come up recently with <code>&quot;how to mine bitcoin using python&quot;</code> or <code>&quot;how to mine bitcoin using golang&quot;</code> and as I have been observing all these source code, not only I'm confused but also have some questions.</p>
<p>Some developers using python keep saying that <code>difficulty calculates the leading 0s on the hash</code> which after coming to blockchain explorer I think it is not true as the current difficulty is wayy too high to be <code>leading 0s</code> on the hash which determines that &quot;we mined a block&quot;. Also bitcoin wiki of difficulty just explains it plain short.</p>
<p>So how does it treats it? Does the mining software puts difficulty in the hashing algorithm?</p>

CC BY-SA 4.0
2021-07-29T22:26:05.507Z
Does this answer your question? [How does solving a block work in relation to the first letter/number after the 0's?](https://bitcoin.stackexchange.com/questions/102414/how-does-solving-a-block-work-in-relation-to-the-first-letter-number-after-the-0).  See also [What exactly is mining](https://bitcoin.stackexchange.com/q/148/13866)
RedGrittyBrick
CC BY-SA 4.0
2021-07-30T16:59:55.667Z
@RedGrittyBrick so true man, you are right but I have been said that I shouldn't edit questions once I made one. Tbh that's why I included that edit even after I knew exactly what I wanted to know. Thanks for the link i think it will come in handy.
RickyLo
CC BY-SA 4.0
2021-08-02T15:35:57.553Z
Hi thecow milk, our site works best when a topic is focused on one question only. You can find a description of how our site works in our [tour]. I think that all of your questions have answers on this site separately, see e.g. https://bitcoin.stackexchange.com/q/8806/5406, https://bitcoin.stackexchange.com/q/23912/5406, https://bitcoin.stackexchange.com/q/5838/5406, and https://bitcoin.stackexchange.com/q/8031/5406.
Murch
CC BY-SA 4.0
2021-08-02T15:37:42.650Z
Please feel free to edit this question to focus on a single topic, if any remain unanswered after you read the linked topics. Please ask additional remaining questions as separate topics. Thank you.
Murch
CC BY-SA 4.0
2021-08-02T18:08:25.830Z
@Murch yea you are right buddy. sorry for asking multiple questions at once but I thought it would clear my thoughts and I will be more careful the next time. I'm pretty confused if I should edit my question because it is stated in the rules that once asked you can't go back because the answers/comments will be irrelevant. I'm requesting to delete this question if possible because it may be misleading in the future. Thanks.
RickyLo
CC BY-SA 4.0
2021-08-02T18:15:19.687Z
Hi thecow milk, the "do not edit your question substantially" applies when people have answered a question. Especially if your question was closed without answers, editing is encouraged so that the question can be reopened.
Murch
CC BY-SA 4.0
2021-08-02T18:16:59.167Z
@Murch oh say less, thanks im editing right away!
RickyLo
CC BY-SA 4.0
2021-08-02T18:23:12.200Z
BTW, it's possible that other sites in the Stackexchange network have slightly different rules, each site has its own community with slightly different culture.
Murch
CC BY-SA 4.0
2021-08-02T18:26:47.820Z
@Murch last question just to be sure, may I edit the title as well?
RickyLo
CC BY-SA 4.0
2021-08-02T19:27:37.127Z
Yes, please. It should fit the question you're now asking.—Thanks for updating your post!
Murch
<bitcoin-core><mining-theory><hash><difficulty>
How a real mining code treats difficulty?
RickyLo
# How a real mining code treats difficulty?

(Asked by: username_0 on 2021-07-29)

I have come up recently with `"how to mine bitcoin using python"` or `"how to mine bitcoin using golang"` and as I have been observing all these source code, not only I'm confused but also have some questions.

Some developers using python keep saying that `difficulty calculates the leading 0s on the hash` which after coming to blockchain explorer I think it is not true as the current difficulty is wayy too high to be `leading 0s` on the hash which determines that "we mined a block". Also bitcoin wiki of difficulty just explains it plain short.

So how does it treats it? Does the mining software puts difficulty in the hashing algorithm?

**Tags:** bitcoin-core, mining-theory, hash, difficulty

> **Comments:**
> **username_1**: Does this answer your question? [How does solving a block work in relation to the first letter/number after the 0's?](https://bitcoin.stackexchange.com/questions/102414/how-does-solving-a-block-work-in-relation-to-the-first-letter-number-after-the-0).  See also [What exactly is mining](https://bitcoin.stackexchange.com/q/148/13866)  
> **username_0**: @username_1 so true man, you are right but I have been said that I shouldn't edit questions once I made one. Tbh that's why I included that edit even after I knew exactly what I wanted to know. Thanks for the link i think it will come in handy.  
> **username_2**: Hi thecow milk, our site works best when a topic is focused on one question only. You can find a description of how our site works in our [tour]. I think that all of your questions have answers on this site separately, see e.g. https://bitcoin.stackexchange.com/q/8806/5406, https://bitcoin.stackexchange.com/q/23912/5406, https://bitcoin.stackexchange.com/q/5838/5406, and https://bitcoin.stackexchange.com/q/8031/5406.  
> **username_2**: Please feel free to edit this question to focus on a single topic, if any remain unanswered after you read the linked topics. Please ask additional remaining questions as separate topics. Thank you.  
> **username_0**: @username_2 yea you are right buddy. sorry for asking multiple questions at once but I thought it would clear my thoughts and I will be more careful the next time. I'm pretty confused if I should edit my question because it is stated in the rules that once asked you can't go back because the answers/comments will be irrelevant. I'm requesting to delete this question if possible because it may be misleading in the future. Thanks.  
> **username_2**: Hi thecow milk, the "do not edit your question substantially" applies when people have answered a question. Especially if your question was closed without answers, editing is encouraged so that the question can be reopened.  
> **username_0**: @username_2 oh say less, thanks im editing right away!  
> **username_2**: BTW, it's possible that other sites in the Stackexchange network have slightly different rules, each site has its own community with slightly different culture.  
> **username_0**: @username_2 last question just to be sure, may I edit the title as well?  
> **username_2**: Yes, please. It should fit the question you're now asking.—Thanks for updating your post!  

---

## 1 answers

### Answer by username_2 on 2021-08-02. Score: 2 | Accepted answer

The difficulty is really a human readable expression. Under the hood, the Bitcoin protocol uses the *target*. [The target is a custom 4-byte float value encoding an exponent and a mantissa](https://bitcoin.stackexchange.com/a/57186/5406) that is to represent a number used as the upper bound for the block hash of valid blocks.

The target is updated every 2016 blocks after the last block of a difficulty period has been found. It depends on the timestamps of the first and the last block of the difficulty period and the previous target:

```
new_target = old_target × (t_last - t_first)/14 days

```

Every full node that has seen these two blocks can calculate the new target.

Miners assemble unconfirmed transactions into block templates, create coinbase transactions and fill up the remaining header fields to produce block candidates, test each block candidate by hashing its header and comparing the candidate's hash to the target to determine whether a block has been found. The current target since height 693,504 is `0x17136aa2` which is a compact representation for `0x000000000000000000136aa20000000000000000000000000000000000000000`. If I counted right, this hexadecimal number would translate to a binary number with 75 leading zeroes. At the current hashrate of about 109.7 EH/s, the network will try about 6.6×1022 block candidates until a block is found.


---

