Author

Topic: What is the calculation bitcoin miners do when hashing away? (Read 580 times)

hero member
Activity: 926
Merit: 1001
weaving spiders come not here
Here is the calculation being done on paper - https://www.youtube.com/watch?v=y3dqhixzGVo

Here is an in-depth video explaining how Bitcoin works under the hood - https://www.youtube.com/watch?v=Lx9zgZCMqXE
staff
Activity: 3458
Merit: 6793
Just writing some code
The difficulty is adjusted every 2016 blocks.  The formula for adjusting difficulty requires looking at the total amount of time that it took for those 2016 blocks to be solved.  If it is more than 20160 minutes, then it took too long and difficulty is too hard.  If it is less than 20160 minutes, then it didn't take long enough and difficulty is too easy. The current difficulty is adjusted by the exact same proportion as the amount of time that the 20160 minutes was missed by.  So if the 2016 blocks were solved in 10080 minutes, they came twice as fast as they should, and the target is cut in half (difficulty is doubled).  If the 2016 blocks were solved in 22176 minutes, then they came 10% too slow, and the target is increased by 10% (difficulty is reduced by 10%).
To be specific, the calculation is defined here: https://github.com/bitcoin/bitcoin/blob/20f9ecd343bbd305f0aeb829f42e61edea8de62f/src/pow.cpp#L52

It is:
(bits of previous retarget period)*(time in seconds of the retarget period)/(time in seconds of the intended target period). The result is converted to hex and then encoded as described here: https://bitcoin.org/en/developer-reference#target-nbits
legendary
Activity: 3472
Merit: 4801
Thanks, im going to have to go over that a few times before i can work out exactly what your talking about but i will get their in the end . Seems quite complicated.

So what decides what the current target is? are all mining clients synchronised and contain the answer to solving the block? why cant someone just look at the source to find whatever the target is and mine it instantly?

Imagine I told you that you had to roll a set of 15 six-sided dice repeatedly until you rolled a 6 on at least 10 of them simultaneously.

By changing the target that you need to hit, I can change the "difficulty" of getting a solution.  I can set the target at "a 6 on at least 3 dice simultaneously" if I want to make it easier, or I can set the target as "a 6 on at least 14 dice simultaneously" if I want to make it harder.  It doesn't change the process that you are using, it just changes the target you need to hit.

This is a good analogy of how bitcoin hashing works.

The hash is a mathematical process that works in a way that it is impossible to know ahead of time what the result will be until you calculate it.  So, you have to pick a value, try hashing it, and see what the result is.  If the result meets the target, then you "win".  If the result doesn't meet the target, all you can do is pick another value and try again.  You keep trying over and over until either you find a result that meets the target, or you receive a block from someone else that has successfully found a result that meets the target.

For bitcoin, the "target" is just a number that the result has to be smaller than.   The results of a SHA256 hash will be somewhere between 0 and 1.158 X 1077, but lets work with smaller numbers to make it easier to understand.  Imagine that you have a hash function that results in a number between 1 and 100.  It is impossible to predict what the result will be until you've calculated it to see.  I tell you that the target is "50", meaning that any number smaller than 50 will count as a success.  You can continue to use the same hash function, and I can make succeeding more difficult by lowering the target to "5", or I can make it easier by raising the target to "95".

Every full node that is running on the network use the same formula for calculating what the difficulty should be right now.  Therefore, if any miner tries to broadcast a block that doesn't have a solution that meets the current difficulty, then every node on the network will just ignore that miner's block.  It will get tossed out, and he will have wasted all of his effort in solving that block.  Therefore, every miner has an incentive to make sure that he is using the same formula to determine the current difficulty as everyone else.  If he sets his own difficulty too easy, then everyone will ignore his blocks.  If he sets his own difficulty too hard, then he might find a valid solution that everyone would accept, but he won't notice it because he's checking against the wrong difficulty (and so he'll fail to broadcast that block and get paid for it).

The difficulty is adjusted every 2016 blocks.  The formula for adjusting difficulty requires looking at the total amount of time that it took for those 2016 blocks to be solved.  If it is more than 20160 minutes, then it took too long and difficulty is too hard.  If it is less than 20160 minutes, then it didn't take long enough and difficulty is too easy. The current difficulty is adjusted by the exact same proportion as the amount of time that the 20160 minutes was missed by.  So if the 2016 blocks were solved in 10080 minutes, they came twice as fast as they should, and the target is cut in half (difficulty is doubled).  If the 2016 blocks were solved in 22176 minutes, then they came 10% too slow, and the target is increased by 10% (difficulty is reduced by 10%).
legendary
Activity: 1008
Merit: 1000
★YoBit.Net★ 350+ Coins Exchange & Dice
Thanks, im going to have to go over that a few times before i can work out exactly what your talking about but i will get their in the end . Seems quite complicated.

So what decides what the current target is? are all mining clients synchronised and contain the answer to solving the block? why cant someone just look at the source to find whatever the target is and mine it instantly?
copper member
Activity: 1498
Merit: 1528
No I dont escrow anymore.
Can anyone explain this in layman's words? Ive looked up other threads but cant work out how the network determines who is rewarded with a block and what calculations your miner is actually doing.

Every time someone who doesnt know about bitcoin asks me about it this is one of the questions that comes up.

Miners search for a hash that seen as a number is lower than the "target". The particular hash function used in bitcoin is sha256d (=sha256(sha256(data)) ). I think its not important to understand the particular hash function, but what hash functions are. A (cryptographic) hash function has a few important properties:

- one way; You can not derive the data from the hash itself.
- easy to verify; if you have the data you can easily (in terms of CPU cycles) check the hash
- collision very unlikely; if you are given a hash its practially impossible to find different data to match the hash

If we assume for a second bitcoin would use a hash function that would result in 8 bit values (sha256 procudes 256 bit) and a target of "77" (in decimal) mining could be see like this:

Code:
target  = 0011 1111
hash(0) = 1010 1001
hash(1) = 1010 0101
hash(2) = 0011 1000

The hash values for 0 and 1 are larger than the target so they are discarded. The hash value for 2 is smaller, so we found a block after 3 hashes. Bitcoins current target is 178 659 257 772.5273 higher than the minimum and the entire network is trying ~1.15 billion billion hashes per second to find a block every 10 minutes (on average).

Code:
 "networkhashps": 1.155037292536815e+018,
legendary
Activity: 1008
Merit: 1000
★YoBit.Net★ 350+ Coins Exchange & Dice
Can anyone explain this in layman's words? Ive looked up other threads but cant work out how the network determines who is rewarded with a block and what calculations your miner is actually doing.

Every time someone who doesnt know about bitcoin asks me about it this is one of the questions that comes up.
Jump to: