Bitcoin Forum
May 11, 2024, 08:47:06 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: What Controls the Difficulty?  (Read 820 times)
BitcoinBarrel (OP)
Legendary
*
Offline Offline

Activity: 1961
Merit: 1020


Fill Your Barrel with Bitcoins!


View Profile WWW
June 17, 2013, 05: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?



        ▄▄▄▄▄▄▄▄▄▄
     ▄██████████████▄
   ▄█████████████████▌
  ▐███████████████████▌
 ▄█████████████████████▄
 ███████████████████████
▐███████████████████████
▐███████████████████████
▐███████████████████████
▐███████████████████████
 ██████████████████████▀
 ▀████████████████████▀
  ▀██████████████████
    ▀▀████████████▀▀
.
.....
.....
.....
.....
.....
.....





1715460426
Hero Member
*
Offline Offline

Posts: 1715460426

View Profile Personal Message (Offline)

Ignore
1715460426
Reply with quote  #2

1715460426
Report to moderator
1715460426
Hero Member
*
Offline Offline

Posts: 1715460426

View Profile Personal Message (Offline)

Ignore
1715460426
Reply with quote  #2

1715460426
Report to moderator
There are several different types of Bitcoin clients. The most secure are full nodes like Bitcoin Core, but full nodes are more resource-heavy, and they must do a lengthy initial syncing process. As a result, lightweight clients with somewhat less security are commonly used.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715460426
Hero Member
*
Offline Offline

Posts: 1715460426

View Profile Personal Message (Offline)

Ignore
1715460426
Reply with quote  #2

1715460426
Report to moderator
1715460426
Hero Member
*
Offline Offline

Posts: 1715460426

View Profile Personal Message (Offline)

Ignore
1715460426
Reply with quote  #2

1715460426
Report to moderator
1715460426
Hero Member
*
Offline Offline

Posts: 1715460426

View Profile Personal Message (Offline)

Ignore
1715460426
Reply with quote  #2

1715460426
Report to moderator
piotr_n
Legendary
*
Offline Offline

Activity: 2053
Merit: 1354


aka tonikt


View Profile WWW
June 17, 2013, 05: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.

Check out gocoin - my original project of full bitcoin node & cold wallet written in Go.
PGP fingerprint: AB9E A551 E262 A87A 13BB  9059 1BE7 B545 CDF3 FD0E
kjj
Legendary
*
Offline Offline

Activity: 1302
Merit: 1025



View Profile
June 17, 2013, 06: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.)

17Np17BSrpnHCZ2pgtiMNnhjnsWJ2TMqq8
I routinely ignore posters with paid advertising in their sigs.  You should too.
piotr_n
Legendary
*
Offline Offline

Activity: 2053
Merit: 1354


aka tonikt


View Profile WWW
June 17, 2013, 06: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?

Check out gocoin - my original project of full bitcoin node & cold wallet written in Go.
PGP fingerprint: AB9E A551 E262 A87A 13BB  9059 1BE7 B545 CDF3 FD0E
kjj
Legendary
*
Offline Offline

Activity: 1302
Merit: 1025



View Profile
June 17, 2013, 07: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.

17Np17BSrpnHCZ2pgtiMNnhjnsWJ2TMqq8
I routinely ignore posters with paid advertising in their sigs.  You should too.
piotr_n
Legendary
*
Offline Offline

Activity: 2053
Merit: 1354


aka tonikt


View Profile WWW
June 17, 2013, 07:52:07 PM
 #6

I though a block must be like 2 minutes max in a future, to get accepted, but not 3 hours

Check out gocoin - my original project of full bitcoin node & cold wallet written in Go.
PGP fingerprint: AB9E A551 E262 A87A 13BB  9059 1BE7 B545 CDF3 FD0E
kjj
Legendary
*
Offline Offline

Activity: 1302
Merit: 1025



View Profile
June 17, 2013, 08: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.)

17Np17BSrpnHCZ2pgtiMNnhjnsWJ2TMqq8
I routinely ignore posters with paid advertising in their sigs.  You should too.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!