Hacker News new | past | comments | ask | show | jobs | submit login
The bitcoin mining algorithm from a programmer's viewpoint (bitcoin.stackexchange.com)
53 points by mau on Nov 24, 2013 | hide | past | favorite | 34 comments



How does the network raise the difficulty? How do the nodes agree on a "next difficulty".

It's somewhat surprising to me that the next difficulty level isn't an input the hash function. H(B, random number, next difficulty level).

Edit: How does the network agree on clock? How does it handle rogues?


Since the difficulty is the proportion to the amount of time over or under two weeks the previous 2016 blocks took to find, it's a deterministic calculation that other nodes use to accept and validate each new block. And since it's in the best interest of nodes to keep the longest chain of blocks, it's not in their interest to break this rule. (or they will end up on an abandoned blockchain fork)


Each block has a timestamp. Each 2016 blocks difficulty is adjusted so that next 2016 blocks are produced closer to 10 minute interval between the blocks. Nodes do not tolerate timestamps 2 hours in the future relative to their own timestamps: https://github.com/bitcoin/bitcoin/blob/master/src/main.cpp#...

Since difficulty is adjusted based on a simple median, if the hashrate is always growing (like for the past 8 months), average time between blocks is always below the target 10 minutes (it was around 7-8 minutes on average).


By looking at how long it took to mine the last set of blocks. The rate is supposed to be 1 block per 10 minutes so you can estimate the hashing power of the network by looking at the time taken to solve the last X blocks. If it's too fast, difficulty goes up, too slow and it goes down.

The nodes can "agree" without needing to communicate , because they all use the same formula (and share the blockchain) they should all agree on the same answer for what the difficulty of the next set of blocks should be.


The difficulty is in the block header, B.


How is the hash compared to a difficulty? The current difficulty is 609482680... How do you compare a hash to a number to determine if it's greater than the number?


"What is the formula for difficulty? difficulty = maximum_target / current_target (target is a 256 bit number)"

source: https://en.bitcoin.it/wiki/Difficulty


The serialized hash is interpreted as a 256-bit integer with little-endian semantics. It is then compared to see if it is less then the target value (which difficulty is really just a short-hand for).


the proof of work requires miners to generate some value such that HASH(block_value, generated_value) begins with fn(difficulty) zeros.

miners choose a value for generated_value, hash it and hope it comes out with the required number of zeros, if not they try again, and again, and again until they (or someone else) finds a value.

for simplicity let's say the fn(x) = x.

difficulty of 5? hash must have 5 zeros at the beginning: 00000XXXXXXXXXXXXXXXXXXXXXXXXX

10? must have 10: 0000000000XXXXXXXXXXXXXXXXXXXX

each zero you add on requires exponentially more time.


I know the docs say it has to be a bunch of zeroes at the start, the actual test is whether or not the generated value is below a certain number.


So the procedure described by blibble isn't actually used in the client software?

I've always thought that's how it worked since that is what is written in the original Bitcoin paper (which has for a long time been my only source of information on the protocol).


Yes blibble's procedure has nothing to do with the actual calculation. The protocol says to treat the 256-bit hash as a little-endian integer, and then check if that is less than the target value. The target value is an integer value that is stored as a custom-format floating point value in the block header and adjusted every 2016 blocks. "Difficulty" per se doesn't actually exist at the protocol level, it's a purely human-interface concern.


When you are looking at a binary number, the number of zeros at the start is the exact same thing as making sure the number is below a certain threshold.


Not exactly the same thing. Consider this example:

Threshold is 001010

The numbers 001001 and 001011 have the same number of leading zeroes but one is below the threshold and the other is not.


What I said is correct, but I suppose ambiguous. What I mean to say is that counting MSB zeros does specify that the number will be below a threshold, just not an arbitrary threshold like the one you gave in your example.

For a more concrete example, if the requirement is that a 16-bit word is less than 4096, you only have to verify that it leads with 4 zeros.


Yes but Bitcoin uses 'below number X' which has more precision than just counting leading zeroes.


No it's not the exact same thing.

Example:

Treshold = 001010

If you check 001011 or 001001 then the number of leading zeros are the same but one number is below the threshold and the other is not.


You're mostly correct, except the difficulty number is not exactly the same as the number of leading zeros.


Hence the "for simplicity let's say the fn(x) = x."


Remotely related question: How do transactions get into the blockchain? Who manages the blockchain? Do others see the IP address(es) related to a transaction?


Some of your questions are ambiguous. Like who is this who's looking at IP addrs and transactions and where's his protocol analyzer(s) deployed and ...

This is probably the best place for you to start with these specific questions:

https://en.bitcoin.it/wiki/Transactions

or more generally

https://en.bitcoin.it/wiki/Category:Technical


Does somebody has an informed opinion about the skyrocketing of difficulty the next months?. What will happen when the top of the line ASICS will be mining at a loss in 4 to 5 months? Are new processors coming or the number of people mining will drop?


Don't forget that the USD/BTC price could also skyrocket to partially compensate for the difficulty increase.


Does there seriously need a Bitcoin article on HN every single day?


Yes. It's one of the biggest online revolutions going on at the moment.


The biggest by far, I would argue.


You don't like programmable money? Ok, I'll try to post more CSS tricks and startups drama to make you feel more comfortable.


Don't forget psychology experiments and motivational advice.

BTC is the most interesting thing since the Internet, IMO. Yeah, I'm tired of speculative 'is BTC going to fail' posts, but I love reading about all the new developments.


Bitcoin has the potential to be an innovation on the level of email. Fortunes will be created and destroyed. Companies will rise and fall. Whatever happens at the end, even if bitcoin itself fails, the world is unlikely to be the same.

At the same time, it's fascinating from a purely technical perspective as well. I think as a topic it's a gold mine for interesting conversation, because it touches programming, economics, mathematics, and human psychology all so deeply.


Maybe not as much, but I have to say that in regards to bitcoin, this is the most HN worthy link so far.


As oppose to what?

    "Blah in X lines of javascript"

    "Is college worth it?"

    http://bradconte.com/files/misc/HackerNewsParodyThread/


Many people may not perceive the importance of Bitcoin, hence the need to educate the public about it. Decentralized digital currency really has the potential to profoundly change society, on the same scale as the Internet changed it. As Jeremy Allaire [1] put it: "global digital currency represents one of the most important technical and economic innovations of our time".

[1] Multi-millionaire, creator of ColdFusion, CTO at Macromedia, now founded a $9 million-backed startup on Bitcoin (http://circle.com)


Imagine you were on a BBS or walled garden in the early nineties, and every day keep seeing news articles about this HTML-based Internet. Is the correct response to ignore it and make trite "get off my lawn" responses?


Absolutely yes. Bitcoin is huge. If you don't like it, don't click it.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: