How The Difficulty works – In-depth thread with a LOT of PICs.

Free Bitcoins: FreeBitcoin | BonusBitcoin

Coins Kaufen: Bitcoin.deAnycoinDirektCoinbaseCoinMama (mit Kreditkarte)Paxfull

Handelsplätze / Börsen: Bitcoin.de | KuCoinBinanceBitMexBitpandaeToro

Lending / Zinsen erhalten: Celsius NetworkCoinlend (Bot)

Cloud Mining: HashflareGenesis MiningIQ Mining


*This is a* [*twitter*](https://twitter.com/raw_avocado/status/1569679607158435840) *thread I made, but decided to publish it here, so hence why the sentences are kept short and the word format.*
*You can just scroll here in good ol’ reddit :)*

**The Difficulty Adjustment** is the **great missing innovation** that made Bitcoin work.

While it ensures that Bitcoin’s **supply** is **predictable** it also is a:

* fundamental **security** **mechanism**
* **self**-**regulating** mechanism
* crucial **consensus** **component**

Since its inception Bitcoin adjusted its difficulty 373 times.

For the 1st year of its existence, the difficulty did not increase, the 1st “big” change taking place on 2010-01-01 ~ block 32,256.

The biggest changes are: – upwards being 302% – downwards 27%

https://preview.redd.it/y3b65b0ojnn91.png?width=1600&format=png&auto=webp&s=46d22f3e7239ddb379ede231518d02b6ed519a1d

The Bitcoin Difficulty tells us how hard it is to find a hash that will make for a valid block.

At the current difficulty of 30,977,051,760,460 1 in every

13,315,579,227,696,403,823,071,581 SHA256 hash will be a valid block.
Valid means below a certain target.

https://preview.redd.it/yy283chpjnn91.png?width=1600&format=png&auto=webp&s=3345aa778de87a32655cf28991428676307c9911

Mining is a random and unpredictable process.

Even more so miners can leave and join the network at any time.
Both factors can influence how fast blocks come.

The difficulty regulates the speed at which new blocks are added on to the blockchain.

​

The adjustment:

1. Maintains a consistent issuance of new bitcoins.
2. Ensures further that the incentives of the miners are aligned with the users and keep them honest.

Point #2 will explained further.

Miners hash all the block data with a random nonce until they find a block hash that is smaller than a certain Target.

There is a direct relation between Target and Difficulty.

The smaller the target, the harder it is to mine.

https://preview.redd.it/scqztkxrjnn91.png?width=1600&format=png&auto=webp&s=a18798359f102a4b618f9ba6d03d5fabe69944d1

The Target number is stored in a block in the field Bits in a compressed format.

The fact that the Target itself is hashed with the rest of the block data makes the difficulty forgery-proof.*

The Difficulty is usually presented in decimal format.

https://preview.redd.it/8frjxv0tjnn91.png?width=1600&format=png&auto=webp&s=036db932ca0e5ffa29c06136c43ecbda5db7113d

Target = Max Target / Difficulty

Max Target: the target, if difficulty is 1 (lowest possible difficulty)

This value was picked by Satoshi.
Both Max Target and Difficulty 1 were hardcoded in the genesis block by him.

https://preview.redd.it/wij6le2ujnn91.png?width=1600&format=png&auto=webp&s=6860757c8176b9338c46f66a0b9e6bf8286deca9

A difficulty of 420, means that it will take 420 more tries(on average) to find a valid hash than with the easiest difficulty of 1.

As mentioned The Genesis Block started with difficulty 1.

Ever since every 2016 blocks, the Difficulty is recalculated.

​

When a block is mined, the miner also needs to add a timestamp to the block.

This Timestamp is used to recalculate the difficulty each time.

We want a new block to be found ~10 minutes, so after 2016 blocks we check what was the time between blocks.

https://preview.redd.it/v3l4231xjnn91.png?width=1600&format=png&auto=webp&s=a4bd7e1d87cb43d40c3db380ab8c906c6018da41

Expected time: 2016 blocks x 10 minutes = 20,160 minutes.

If the blocks were generated faster, let’s say 8 minutes, then we get 2016 x 9 = 18,144 20160 / 16128 = 1.25

New Difficulty = 1.25 * Old Difficulty

​

The hashrate is NOT known!
We just know how fast blocks are generated.

We use the timestamps in blocks to estimate how much hash rate is working on the network.

This is not really an exact science.

But the timestamps themselves are not exact science.

https://preview.redd.it/5hmx7qbyjnn91.png?width=1600&format=png&auto=webp&s=7d2f2189bf0f059a5f390237a9db63f7b76ea361

As mentioned we need some reference of time in order to make sure we adjust for a block to come every 10 minutes.

When a block is mined the miner adds the time inside.

But what if he has the wrong time or he is lying?

Why would miners lie?
To get more coins!

It is in the miner’s interest to say that blocks took longer to produce.

This will have the effect of lowering the next difficulty.
Lower difficulty -> easier to mine coins.

To mitigate this attack Bitcoin has 3 rules:

**MedianPast Time Rule** – a node will NOT accept a block unless it has a timestamp greater than the median of the previous 11 blocks.

This is a consensus rule.
If not satisfied, blocks are rejected.
[https://github.com/bitcoin/bitcoin/blob/4daadce36cfe9baa63c4d7d70de027add03a00df/src/chain.h#L259](https://github.com/bitcoin/bitcoin/blob/4daadce36cfe9baa63c4d7d70de027add03a00df/src/chain.h#L259)

[ ](https://preview.redd.it/bgrtpw60knn91.png?width=1600&format=png&auto=webp&s=60f76dddcbe09d7eefb503212e22db5d5e4ab7d9)

Future Block Time Rule – The time stamp can NOT be more than 2 hours relative to the median time from the node’s peers

`MAX_FUTURE_BLOCK_TIME` – is used.

This is a policy rule.
Blocks from the future can become valid in the future.
[https://github.com/bitcoin/bitcoin/blob/4daadce36cfe9baa63c4d7d70de027add03a00df/src/chain.h#L22](https://github.com/bitcoin/bitcoin/blob/4daadce36cfe9baa63c4d7d70de027add03a00df/src/chain.h#L22)

​

The maximum allowed gap between the time provided by the nodes and the local system clock is 90 minutes.

This is a local client rule.

[https://github.com/bitcoin/bitcoin/blob/4daadce36cfe9baa63c4d7d70de027add03a00df/src/chain.h#L38n](https://github.com/bitcoin/bitcoin/blob/4daadce36cfe9baa63c4d7d70de027add03a00df/src/chain.h#L38n)

https://preview.redd.it/t1yki7c6knn91.png?width=1600&format=png&auto=webp&s=ebd94f65d52c32050925976358bd0fc858a8563d

**Rule #1** – ensures that the blockchain **advances forward**.

Rule #2 – ensures that the chain does **not move too forward**.

Rule #3 is an **extra** **safeguard**.

Most importantly these rules tell us that **managing time in** **Bitcoin** is **very hard** and **imprecise**.

Even though the difficulty adjusts every 2016 blocks, it only takes into account 2015 blocks.

This is because Satoshi introduced a off-by-1 bug.

[https://sourceforge.net/p/bitcoin/code/1/tree//trunk/main.cpp#l689](https://sourceforge.net/p/bitcoin/code/1/tree//trunk/main.cpp#l689)

This is still present in the code today, as fixing it could result in a hard-fork.

https://preview.redd.it/zydqyyc8knn91.png?width=1600&format=png&auto=webp&s=18d56088e243d3fffd114626c9b77a159bbb2fc2

Instead of checking how much time has passed between the last blocks of each 2016 period, the code compares the time between the 1st and the 2016th block of each period.

Between 2016 things there are 2015 gaps.

https://preview.redd.it/dpq48cfaknn91.png?width=1600&format=png&auto=webp&s=ba3fdcd0e287ac8ba7d32b98c9a045bc474b76fe

As a consequence Blocks are NOT actually 10 minutes, but 10.005 minutes.

The 0.3 seconds are not a big deal but bug allows for 2 very weird things to occur.📷

You may expect retargeting interval to be (2 weeks)/2015, but it is even weirder.

If the Bitcoin hash rate is perfectly constant, and blocks have exact timestamps the difficulty would adjust once every 2 weeks, 20 minutes, 1.191658seconds.

As Pieter Wuille points out [https://twitter.com/pwuille/status/1098651788343955456](https://twitter.com/pwuille/status/1098651788343955456)

The 2nd effect is the Time Warp Attack.

Given the rules mentioned above:

* Block Timestamp > median of prev 11 blocks
* Block Timestamp < now + some buffer

Within these rules, a miner could lie that it took longer to generate blocks.

https://preview.redd.it/rv2x2jzbknn91.png?width=1600&format=png&auto=webp&s=b3e66a14fd51c1ea67b2286a852661078ee70053

The off-by-1 one makes the last block of the current period to not overlap with the 1st block of new period.

An attacker could do this indefinitely and bring the difficulty to 1.

However, this attack would be very visible and would require miner coordination from miners.

Besides the temporal restrictions, the difficulty can not change by more than a factor of 4.

This seems quite a peculiar restriction but it serves a good purpose.

[https://github.com/bitcoin/bitcoin/blob/master/src/pow.cpp#L54](https://t.co/hIK8JJW9bv)

https://preview.redd.it/g8a21kaeknn91.png?width=1600&format=png&auto=webp&s=6705c070424ca2ecece68d66b564b079bbb22638

This rule further protects against the attack mentioned.

Also, it is possible to isolate a node from the network and send it blocks with false time stamps.

The node would be tricked into lowering its difficulty and the attacker will be able to create false blocks easier.

https://preview.redd.it/jxo2pgmfknn91.png?width=1600&format=png&auto=webp&s=c5215174b6085e91b07c5f030240865e49e61665

Why 2016 blocks? 2 weeks / 10 minutes = 2016

Why not less than 2016?

It would allow for similar isolating attacks as explained above.

An attacker can isolate a node, and send it blocks with lower difficulty.
Any similar attacks, to succeed needs to keep up for 2 weeks.

&#x200B;

To give you an idea of how difficult current times are.

My Apple M1 Max computer does 5.8Mhashes/s.
It would take me 727 million years to find a block at the current difficulty.

Thymos in 2010 “generated 5 blocks” on a Pentium processor. (1000 times slower than mine)

https://preview.redd.it/ehgfvnbhknn91.png?width=1600&format=png&auto=webp&s=c830c5d0dfcb2e24085bd26498cb4e7af673b853

Having a Difficulty Adjustment and even more adding these restrictions in order to eliminate these attacks was visionary.

Never mind the fact we never had something like this before.

IF anything this shows us how untrustworthy/ineffective Bitcoin at time management.

Maybe why timechain was replaced with blockchain

As we can see from this comment in the Bitcoin Client released by Satoshi, was hoping add NTP.

NTP – is a networking protocol for clock synchronization between computer system.
This never happened.

[*https://sourceforge.net/p/bitcoin/code/1/tree//trunk/net.h#l491*](https://sourceforge.net/p/bitcoin/code/1/tree//trunk/net.h#l491)

[ ](https://preview.redd.it/wblvjdgiknn91.png?width=1600&format=png&auto=webp&s=141007e468418cbc6f7506642c3b4fd11e4047db)

Finally, I want to leave you with this Bitcointalk post from 2010 made by satoshi, when a 1.34x increase in difficulty from 1 to 1.82 was quite a braggadocious achievement.

[*https://bitcointalk.org/index.php?topic=43.0*](https://t.co/W21W24mOyW)

https://preview.redd.it/0nlq2m4jknn91.png?width=1600&format=png&auto=webp&s=79a7b4c7de9386ac2f9b918a244fd473c8d6bfb9

View Reddit by olivietti – View Source



Post Views:
2



[ad_2]

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close