
The bitcoin mining algorithm from a programmer's viewpoint - mau
http://bitcoin.stackexchange.com/questions/12603/the-bitcoin-mining-algorithm-from-a-programmers-viewpoint
======
acchow
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?

~~~
jadeddrag
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)

------
Moral_
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?

~~~
blibble
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.

~~~
brainburn
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.

~~~
ars_technician
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.

~~~
letstryagain
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.

~~~
ars_technician
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.

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

------
aw3c2
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?

~~~
VLM
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](https://en.bitcoin.it/wiki/Transactions)

or more generally

[https://en.bitcoin.it/wiki/Category:Technical](https://en.bitcoin.it/wiki/Category:Technical)

------
omegant
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?

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

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

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

~~~
baddox
The biggest by far, I would argue.

