<p>Yes, bitcoin should not trust anyone, but it trusts itself, i.e. its own blockchain. So, when the time comes to update the difficulty (as you said, every 2016 blocks), code grabs the timestamp of this last block, and the first block in this difficulty period (block with the index of: last index - 2016).</p>

<p>Comparing timestamps of those 2 blocks gives us the timespan between 2016 blocks, and allows for difficulty calculation.</p>

<p>You can check the code for it here:
<a href="https://github.com/bitcoin/bitcoin/blob/master/src/pow.cpp#L55" rel="nofollow noreferrer">https://github.com/bitcoin/bitcoin/blob/master/src/pow.cpp#L55</a></p>

<pre><code>actual_timespan = last_block-&gt;get_timestamp() - first_block-&gt;get_timestamp()
</code></pre>

<p>More information on what block timestamps are considered valid can be found here  <a href="http://en.bitcoin.it/wiki/Block_timestamp" rel="nofollow noreferrer">http://en.bitcoin.it/wiki/Block_timestamp</a></p>

CC BY-SA 3.0
2017-05-25T21:25:50.110Z
Ah so it's trusting that the miners of the first and last block in the set of 2016 wrote a correct timestamp. Couldn't malicious miners that want the difficulty to be lower or higher spoof different timestamps if they end up mining one of those critical blocks?
B T
CC BY-SA 3.0
2017-05-25T21:29:31.057Z
Is there something in the protocol that puts verifiable or statistical bounds on the correctness of the block timestamps?
B T
CC BY-SA 3.0
2017-05-27T16:12:10.337Z
@BT I'd have to look through the code for any possible checks on the timestamp, but there should not be any. Miners can put whatever timestamp they want, and if they have more than 50% hash power, they can force blockchain to act however they want to, since most of the blocks are theirs, and they control it. But with less hashing power, this is not possible because other people mine and find blocks, and see there is something fishy going on - same for the timestamp spoofing.
epson121
CC BY-SA 3.0
2017-05-27T17:11:41.120Z
edit: looks like I was way off on this last comment. See this for more info https://en.bitcoin.it/wiki/Block_timestamp
epson121
CC BY-SA 3.0
2017-05-28T22:34:17.803Z
Ah very interesting. Add this to your answer and I'll accept it! It would seem to me that this would allow time as measured by the bitcoin network to drift with respect to real time if there is some pressure to manipulate timestamps, but places some statistical constraints on how fast it can drift according to what percentage of the network has incentives to manipulate those timestamps. This could be pretty interesting  as a way to validate timestamps in the future!
B T
CC BY-SA 3.0
2017-05-25T21:00:39.613Z
2017-05-30T16:18:07.427Z
2017-05-30T16:18:07.427Z
epson121
<p>According to <a href="http://en.bitcoin.it/wiki/Block_timestamp" rel="nofollow noreferrer">http://en.bitcoin.it/wiki/Block_timestamp</a>, bitcoin nodes only accept blocks with timestamps greater than the median of the previous 11 blocks and less than network-time + 2 hours. Network-time is the median time reported by all the peers a node is connected to. This gives statistical bounds on how much the time can drift given a mostly honest network.</p>

<p>For lots more information on how timestamps work, the incentives involved, and how they could be manipulated in an attack situation see:</p>

<ul>
<li><a href="http://culubas.blogspot.com/2011/05/timejacking-bitcoin_802.html" rel="nofollow noreferrer">http://culubas.blogspot.com/2011/05/timejacking-bitcoin_802.html</a></li>
<li><a href="https://litecoin.info/Time_warp_attack" rel="nofollow noreferrer">https://litecoin.info/Time_warp_attack</a></li>
<li><a href="https://bitcoin.stackexchange.com/questions/5076/what-stops-miners-nodes-lying-about-what-time-a-block-was-mined">What stops miners/nodes lying about what time a block was mined?</a></li>
</ul>

CC BY-SA 3.0
2017-05-30T22:55:29.090Z
2017-05-30T22:55:29.090Z
B T
<p>So I understand that difficulty increases by some algorithm calculated every 2016 blocks. But my understanding is that this algorithm attempts to set a difficulty such that the average time it takes to mine a block is about 10 minutes. My understanding of verifiable time is that the only way to measure a time in minutes is by using <a href="https://en.wikipedia.org/wiki/Trusted_timestamping" rel="nofollow noreferrer">trusted timestamping</a>, which I assume bitcoin doesn't do (since it's not supposed to trust anyone). </p>

<p>So my question is, how does the bitcoin protocol measure time in seconds or minutes in order to determine what the difficulty should be?</p>

<p>I'm reading <a href="https://en.bitcoin.it/wiki/Target" rel="nofollow noreferrer">here</a> that "every Bitcoin client compares the actual time it took to generate these blocks with the two week goal and modifies the target by the percentage difference". How does every bitcoin client affect the difficulty set by the network? Is this a scenario where there's minimum and maximum bounds on difficulty and any difficulty within those bounds is accepted by nodes in the network? Or is the calculation of difficulty something discrete and precise? </p>

<mining-theory><difficulty>
How is time measured in the context of updates to mining difficulty?
B T
# How is time measured in the context of updates to mining difficulty?

(Asked by: username_0 on 2017-05-25)

So I understand that difficulty increases by some algorithm calculated every 2016 blocks. But my understanding is that this algorithm attempts to set a difficulty such that the average time it takes to mine a block is about 10 minutes. My understanding of verifiable time is that the only way to measure a time in minutes is by using [trusted timestamping](https://en.wikipedia.org/wiki/Trusted_timestamping), which I assume bitcoin doesn't do (since it's not supposed to trust anyone).

So my question is, how does the bitcoin protocol measure time in seconds or minutes in order to determine what the difficulty should be?

I'm reading [here](https://en.bitcoin.it/wiki/Target) that "every Bitcoin client compares the actual time it took to generate these blocks with the two week goal and modifies the target by the percentage difference". How does every bitcoin client affect the difficulty set by the network? Is this a scenario where there's minimum and maximum bounds on difficulty and any difficulty within those bounds is accepted by nodes in the network? Or is the calculation of difficulty something discrete and precise?

**Tags:** mining-theory, difficulty


---

## 2 answers

### Answer by username_1 on 2017-05-25. Score: 3

Yes, bitcoin should not trust anyone, but it trusts itself, i.e. its own blockchain. So, when the time comes to update the difficulty (as you said, every 2016 blocks), code grabs the timestamp of this last block, and the first block in this difficulty period (block with the index of: last index - 2016).

Comparing timestamps of those 2 blocks gives us the timespan between 2016 blocks, and allows for difficulty calculation.

You can check the code for it here:
<https://github.com/bitcoin/bitcoin/blob/master/src/pow.cpp#L55>

```
actual_timespan = last_block->get_timestamp() - first_block->get_timestamp()

```

More information on what block timestamps are considered valid can be found here <http://en.bitcoin.it/wiki/Block_timestamp>

> **Comments:**
> **username_0**: Ah so it's trusting that the miners of the first and last block in the set of 2016 wrote a correct timestamp. Couldn't malicious miners that want the difficulty to be lower or higher spoof different timestamps if they end up mining one of those critical blocks?  
> **username_0**: Is there something in the protocol that puts verifiable or statistical bounds on the correctness of the block timestamps?  
> **username_1**: @BT I'd have to look through the code for any possible checks on the timestamp, but there should not be any. Miners can put whatever timestamp they want, and if they have more than 50% hash power, they can force blockchain to act however they want to, since most of the blocks are theirs, and they control it. But with less hashing power, this is not possible because other people mine and find blocks, and see there is something fishy going on - same for the timestamp spoofing.  
> **username_1**: edit: looks like I was way off on this last comment. See this for more info https://en.bitcoin.it/wiki/Block_timestamp  
> **username_0**: Ah very interesting. Add this to your answer and I'll accept it! It would seem to me that this would allow time as measured by the bitcoin network to drift with respect to real time if there is some pressure to manipulate timestamps, but places some statistical constraints on how fast it can drift according to what percentage of the network has incentives to manipulate those timestamps. This could be pretty interesting  as a way to validate timestamps in the future!  

---

### Answer by username_0 on 2017-05-30. Score: 0

According to <http://en.bitcoin.it/wiki/Block_timestamp>, bitcoin nodes only accept blocks with timestamps greater than the median of the previous 11 blocks and less than network-time + 2 hours. Network-time is the median time reported by all the peers a node is connected to. This gives statistical bounds on how much the time can drift given a mostly honest network.

For lots more information on how timestamps work, the incentives involved, and how they could be manipulated in an attack situation see:

* <http://culubas.blogspot.com/2011/05/timejacking-bitcoin_802.html>
* <https://litecoin.info/Time_warp_attack>
* [What stops miners/nodes lying about what time a block was mined?](https://bitcoin.stackexchange.com/questions/5076/what-stops-miners-nodes-lying-about-what-time-a-block-was-mined)


---

