Author

Topic: What Controls the Difficulty? (Read 821 times)

kjj
legendary
Activity: 1302
Merit: 1026
June 17, 2013, 03:36:36 PM
#7
Oops, we are both wrong.  It is 2 hours.  No idea why I've been thinking 3.

main.cpp, function CBlock::CheckBlock:

Code:
    // Check timestamp
    if (GetBlockTime() > GetAdjustedTime() + 2 * 60 * 60)
        return error("CheckBlock() : block timestamp too far in the future");

Note that this changes the maximum amount of borrowed time from ~1% to ~0.6%.  Actually, I'm rounding (and I was rounding before).  2/336 is 0.5952% and 3/336 is 0.8929%.  (336 being the number of hours in 2 weeks.)
legendary
Activity: 2053
Merit: 1356
aka tonikt
June 17, 2013, 02:52:07 PM
#6
I though a block must be like 2 minutes max in a future, to get accepted, but not 3 hours
kjj
legendary
Activity: 1302
Merit: 1026
June 17, 2013, 02:35:22 PM
#5
Hmm, I went back and looked, and I was wrong.  The bug is not symmetric.  It is still unimportant because it only represents a fraction of 1% of the interval.

Basically, every 2016 blocks, we look at the previous 2015 blocks for the difficulty adjustment.  This is off by one, because we had intended to look at the previous 2016 blocks.

I was confused about the symmetry part because this was mostly discussed in threads about time-warp attacks.  Some alt-coins allow difficulty to be skewed more easily in one direction than the other, so Art Forz showed that he could game them by cranking difficulty way up, or way down.  Bitcoin's difficulty algorithm is perfectly symmetric*, so the best we can do is tweak the final block's timestamp (within the very liberal timestamp rules) to shift a few hours of actual time into or out of the apparent time of the current period.

Say you are a miner that wanted a lower difficulty in the next period.  You can make that happen if you find the appropriate block by setting the timestamp in that block as far forward as the rest of the network will accept, about 3 hours.  This makes the next difficulty about 1% lower than it would have been if you'd put the actual time into that block.  If not for the bug, the next retarget period would start at that timestamp, so the apparent interval of that period would be 3 hours shorter, which would bump the difficulty up by about 1%.  You could again manipulate the current timestamp, but the best you can do is 3 more hours, which compensates for the 3 hours borrowed before.  So, the worst that can happen is that if a lot of miners get together, they can keep the difficulty about 1% too low for a long time.  They can't ever get a second 1%.

But, because of the bug, the timestamp of that block is never actually checked.  The period is calculated based on the timestamp of the following block.  That block can be as early as 1 second later than the median timestamp of the 11 blocks before it.  This slight asymmetry means that a large group of miners could repeatedly keep the difficulty a fraction of a percent lower than they could with the liberal timestamp rules alone.  It also means that borrowed time doesn't necessarily need to be paid back, if they can get a 51% cabal working.  Not like there aren't worse things they could do at that point...

See this thread for some discussion on both issues.

Well, not perfectly symmetric, because of the bug.  But we use 2016 blocks at the retarget interval, come hell or high water.  Art had ASICs several years ago, so he would "test" alt-coins by throwing a lot of hashing power at them, then leaving.  If he represented 90% of the hashing power on some alt coin, it could take months for them to retarget back down to their normal speeds.  Some of them responded by adding code to allow difficulty to drop faster if hashing power left.  This is the sort of asymmetry that really matters, and Art showed them that it opened them up to serious manipulation.
legendary
Activity: 2053
Merit: 1356
aka tonikt
June 17, 2013, 01:14:21 PM
#4
(Note that there is a subtle and unimportant bug in the code that throws the calculation off by a small fraction of a percent.  The bug is symmetric, so no one can use it to tweak difficulty in the long run, so it isn't a security issue.)
What bug?
kjj
legendary
Activity: 1302
Merit: 1026
June 17, 2013, 01:10:19 PM
#3
Every 2016 blocks, each node looks at the timestamps of the recent blocks and calculates what the difficulty should have been to make the span 2 weeks.  This is used to set the new difficulty, and blocks with a different difficulty are rejected.  If there is a fork that covers the last block in the calculation window, then each branch will have a slightly different difficulty.

(Note that there is a subtle and unimportant bug in the code that throws the calculation off by a small fraction of a percent.  The bug is symmetric, so no one can use it to tweak difficulty in the long run, so it isn't a security issue.)
legendary
Activity: 2053
Merit: 1356
aka tonikt
June 17, 2013, 12:56:25 PM
#2
I was just wondering what component of the Bitcoin network controls the Difficulty? Is it included in the code of each of the Bitcoin clients like Bitcoin-QT, Bitcoind, etc?
Its the blockchain protocol that controls difficulty.
All the clients must comply to it.
legendary
Activity: 2026
Merit: 1034
Fill Your Barrel with Bitcoins!
June 17, 2013, 12:51:48 PM
#1
I was just wondering what component of the Bitcoin network controls the Difficulty? Is it included in the code of each of the Bitcoin clients like Bitcoin-QT, Bitcoind, etc?
Jump to: