<p>Assuming the miner as an up-to-date version of the blockchain, he can compute the difficulty from the data about the previous blocks. An explanation of how the difficulty is computed can be found in <a href="https://bitcoin.stackexchange.com/questions/5838/how-is-difficulty-calculated/5840#5840">this other answer</a>. </p>

<p>Given a specific chain, indeed, the computation of the difficulty always gives the same result. Every miner may compute this new difficulty by himself and the result will be the same since the computation is based on the 2016 blocks slot. There is no need to know the hashrate of the network, it is enough to know how much time it was needed to generate the 2016 blocks by looking at their timestamps. </p>

<p>Additionally, block headers also encode the target threshold through the <a href="https://bitcoin.org/en/developer-reference#block-headers" rel="nofollow noreferrer">nBits field</a>. The reason can be found in <a href="https://bitcoin.stackexchange.com/questions/34155/is-it-necessary-for-blocks-to-record-the-nbits-difficulty-parameter">this answer</a>.</p>

<p>PS: In order for a block to be considered valid, its timestamp must also be valid, that is, it must be:</p>

<blockquote>
  <p>greater than the median timestamp of previous 11 blocks, and less than
  the network-adjusted time + 2 hours.</p>
</blockquote>

<p>So the effects of the "attack" are very constrained.</p>

CC BY-SA 3.0
2016-10-17T20:12:10.307Z
2016-10-18T11:39:16.193Z
2017-04-13T12:47:33.743Z
cpsola
<p>Because the difficulty calculation is deterministic, everyone can calculate the difficulty independently.  Given that they have come to consensus on the blockchain (which blocks in which order), they should all calculate the same value.  In this way, the protocol never relies on trusting anyone about what the correct difficulty is.</p>

<p>Also, keep in mind that it's not just miners that would have to be fooled into accepting a block with insufficient work.  Full nodes also verify blocks as they receive them, and will reject blocks that do not have nonces that meet their own calculated difficulty requirement.  Since full nodes play an important role in relaying blocks, this should not be overlooked.</p>

<p>If you'd like to look at how the difficulty calculation is performed, it's in the code at: <a href="https://github.com/bitcoin/bitcoin/blob/master/src/pow.cpp" rel="nofollow">https://github.com/bitcoin/bitcoin/blob/master/src/pow.cpp</a></p>

CC BY-SA 3.0
2016-10-17T20:58:59.773Z
2016-10-17T20:58:59.773Z
Jestin
<p>To address the remark in the PS, i.e. </p>

<p><code>P.S. Assume also an adversary solves the proof-of-work and then generates a new block but forges the block's times-stamp to trick the network at the time of updating target. Since the target will be updated using the blocks' time-stamp.</code></p>

<p>That is indeed possible, it's called "<em>timejacking</em>" or the time traveller attack, you can find out more about it here: </p>

<p><a href="http://culubas.blogspot.de/2011/05/timejacking-bitcoin_802.html" rel="nofollow noreferrer">http://culubas.blogspot.de/2011/05/timejacking-bitcoin_802.html</a></p>

<p>or here: </p>

<p><a href="https://bitcoin.stackexchange.com/questions/4271/bitcoins-from-the-future">bitcoins from the future</a></p>

CC BY-SA 3.0
2016-10-18T11:25:07.860Z
2016-10-18T14:33:28.613Z
2017-04-13T12:47:33.610Z
smatthewenglish
<p>It's done through consensus. You can read about how it's calculated and also look at the source code as well. You can find the answer in the developer's guide: <a href="https://bitcoin.org/en/developer-guide#proof-of-work" rel="nofollow">https://bitcoin.org/en/developer-guide#proof-of-work</a></p>

<p>I believe the actual code is in bitcoin/src/chain.h</p>

<p>Excerpt:</p>

<blockquote>
  <p>New blocks will only be added to the block chain if their hash is at least as challenging as a difficulty value expected by the consensus protocol. Every 2,016 blocks, the network uses timestamps stored in each block header to calculate the number of seconds elapsed between generation of the first and last of those last 2,016 blocks. The ideal value is 1,209,600 seconds (two weeks).</p>
</blockquote>

CC BY-SA 3.0
2016-10-18T20:09:54.573Z
I don't think you have the correct source file, at least not for the difficulty target calculation.  You can find the median block time calculation in chain.h, if that's what you mean.  If so, you should clarify.
Jestin
CC BY-SA 3.0
2016-10-18T00:00:26.073Z
2016-10-18T00:00:26.073Z
Chris LoVerme
<p>All miners at any time must have a consensus on the "<em>difficulty target</em>" to be able to achieve the correct nonce (as answer of proof-of-work).</p>

<p>Assume that the target has been updated (it means that 2016 blocks has been discovered) now, the question is "<strong>who</strong>" calculates this new target such that every miner achieves the same target that is needed to solve proof-of-work of the next block? </p>

<p>Is it true that it's not possible for an attacker to manipulate the target value to trick the other miners such that they achieve a wrong nonce for proof-of-work? </p>

<p>In other words, how can every miner know that the <strong>target value has been changed</strong>? </p>

<p>--> Is it by the block header of the last block in the Blockchain? </p>

<p>If so, is this approach accountable to achieve a unique target?</p>

<p>Assume 2016 blocks have been discovered and the target must be updated using the following equation:</p>

<p>time = (difficulty x 2^32) / hashing power of the network</p>

<p>Is it right? It's unclear to me how to determine the value for <strong>hashing power of the network</strong>.</p>

<p>P.S. Assume also an adversary solves the proof-of-work and then generates a new block but forges the block's times-stamp to trick the network at the time of updating target. Since the target will be updated using the blocks' time-stamp.</p>

CC BY-SA 3.0
2016-10-17T21:29:57.997Z
I think you're a bit confused.  Nobody needs to know the current hashing power of the network in order to calculate the target.  It can be determined by from the average block time during the last 2016 blocks.  The equation you list doesn't calculate the difficulty, but the estimated time for a block to be discovered.  All that is needed to calculate a new target is the past 2016 blocks.
Jestin
CC BY-SA 3.0
2016-10-17T21:50:31.927Z
Short answer: The hashing power of the network can be estimated by the timestamps in the block headers. Since everyone uses the same algorithm on the same block headers, everyone gets the same result. It is, of course, only an estimate of the network's hashing power.
David Schwartz
CC BY-SA 3.0
2016-10-17T23:33:33.370Z
related: [How is difficulty calculated?](http://bitcoin.stackexchange.com/q/5838/5406)
Murch
<blockchain><block><difficulty><consensus>
where is difficulty target inserted?
Questioner
# where is difficulty target inserted?

(Asked by: username_0 on 2016-10-17)

All miners at any time must have a consensus on the "*difficulty target*" to be able to achieve the correct nonce (as answer of proof-of-work).

Assume that the target has been updated (it means that 2016 blocks has been discovered) now, the question is "**who**" calculates this new target such that every miner achieves the same target that is needed to solve proof-of-work of the next block?

Is it true that it's not possible for an attacker to manipulate the target value to trick the other miners such that they achieve a wrong nonce for proof-of-work?

In other words, how can every miner know that the **target value has been changed**?

--> Is it by the block header of the last block in the Blockchain?

If so, is this approach accountable to achieve a unique target?

Assume 2016 blocks have been discovered and the target must be updated using the following equation:

time = (difficulty x 2^32) / hashing power of the network

Is it right? It's unclear to me how to determine the value for **hashing power of the network**.

P.S. Assume also an adversary solves the proof-of-work and then generates a new block but forges the block's times-stamp to trick the network at the time of updating target. Since the target will be updated using the blocks' time-stamp.

**Tags:** blockchain, block, difficulty, consensus

> **Comments:**
> **username_1**: I think you're a bit confused.  Nobody needs to know the current hashing power of the network in order to calculate the target.  It can be determined by from the average block time during the last 2016 blocks.  The equation you list doesn't calculate the difficulty, but the estimated time for a block to be discovered.  All that is needed to calculate a new target is the past 2016 blocks.  
> **username_2**: Short answer: The hashing power of the network can be estimated by the timestamps in the block headers. Since everyone uses the same algorithm on the same block headers, everyone gets the same result. It is, of course, only an estimate of the network's hashing power.  
> **username_3**: related: [How is difficulty calculated?](http://bitcoin.stackexchange.com/q/5838/5406)  

---

## 4 answers

### Answer by username_4 on 2016-10-17. Score: 5

Assuming the miner as an up-to-date version of the blockchain, he can compute the difficulty from the data about the previous blocks. An explanation of how the difficulty is computed can be found in [this other answer](https://bitcoin.stackexchange.com/questions/5838/how-is-difficulty-calculated/5840#5840).

Given a specific chain, indeed, the computation of the difficulty always gives the same result. Every miner may compute this new difficulty by himself and the result will be the same since the computation is based on the 2016 blocks slot. There is no need to know the hashrate of the network, it is enough to know how much time it was needed to generate the 2016 blocks by looking at their timestamps.

Additionally, block headers also encode the target threshold through the [nBits field](https://bitcoin.org/en/developer-reference#block-headers). The reason can be found in [this answer](https://bitcoin.stackexchange.com/questions/34155/is-it-necessary-for-blocks-to-record-the-nbits-difficulty-parameter).

PS: In order for a block to be considered valid, its timestamp must also be valid, that is, it must be:

> greater than the median timestamp of previous 11 blocks, and less than
> the network-adjusted time + 2 hours.

So the effects of the "attack" are very constrained.


---

### Answer by username_1 on 2016-10-17. Score: 3

Because the difficulty calculation is deterministic, everyone can calculate the difficulty independently. Given that they have come to consensus on the blockchain (which blocks in which order), they should all calculate the same value. In this way, the protocol never relies on trusting anyone about what the correct difficulty is.

Also, keep in mind that it's not just miners that would have to be fooled into accepting a block with insufficient work. Full nodes also verify blocks as they receive them, and will reject blocks that do not have nonces that meet their own calculated difficulty requirement. Since full nodes play an important role in relaying blocks, this should not be overlooked.

If you'd like to look at how the difficulty calculation is performed, it's in the code at: <https://github.com/bitcoin/bitcoin/blob/master/src/pow.cpp>


---

### Answer by username_5 on 2016-10-18. Score: 2

To address the remark in the PS, i.e.

`P.S. Assume also an adversary solves the proof-of-work and then generates a new block but forges the block's times-stamp to trick the network at the time of updating target. Since the target will be updated using the blocks' time-stamp.`

That is indeed possible, it's called "*timejacking*" or the time traveller attack, you can find out more about it here:

<http://culubas.blogspot.de/2011/05/timejacking-bitcoin_802.html>

or here:

[bitcoins from the future](https://bitcoin.stackexchange.com/questions/4271/bitcoins-from-the-future)


---

### Answer by username_6 on 2016-10-18. Score: 0

It's done through consensus. You can read about how it's calculated and also look at the source code as well. You can find the answer in the developer's guide: <https://bitcoin.org/en/developer-guide#proof-of-work>

I believe the actual code is in bitcoin/src/chain.h

Excerpt:

> New blocks will only be added to the block chain if their hash is at least as challenging as a difficulty value expected by the consensus protocol. Every 2,016 blocks, the network uses timestamps stored in each block header to calculate the number of seconds elapsed between generation of the first and last of those last 2,016 blocks. The ideal value is 1,209,600 seconds (two weeks).

> **Comments:**
> **username_1**: I don't think you have the correct source file, at least not for the difficulty target calculation.  You can find the median block time calculation in chain.h, if that's what you mean.  If so, you should clarify.  

---

