Learning Technology in a Bear Market—《Mining = POW = Proof of Work》

Learning Technology in a Bear Market—《Mining = POW = Proof of Work》

In "Bitcoin: The 480,000-Page Ledger", it is mentioned that the essence of currency is a way of accounting, and Bitcoin is a distributed accounting method. Then the question is, who keeps this account and how?

Answer: Miners keep accounts by mining.

When the word "mining" is mentioned, the first impression people have is a group of dark-skinned miners holding hoes and searchlights, digging in the dark underground. After countless clanging sounds, suddenly a miner picks up a transparent gem or golden gold and shouts, "I found it, I found it." This is a plot in movies or novels. The reality is that in the blockchain network world, the word "mining" is not accurately used.

However, the word "mining" has now become deeply rooted in people's minds, so I have no choice but to continue talking about it.

"Mining" is actually a figurative name for the accounting and issuance of digital currencies such as Bitcoin. The real action behind this behavior is that countless computers on the Internet compete for the right to record accounts and solve a specific problem . The computer that solves the problem first will complete the accounting (packaging a block) under the witness of all computers on the entire network and receive Bitcoin as a network reward.

So what is the problem?

It is to find a special hash value for the block header of a certain block. This special hash value must be less than the preset target value.

How do you understand this sentence?

For convenience, let's take a simple example. In the blockchain world, an algorithm called SHA256 is used. Its characteristic is that, given any statement, a hash value can be obtained after passing through the algorithm.

For example, if you input "power overwhelming" (those who have played StarCraft know who this person is, hehe), after the SHA256 algorithm, you get a 32-byte hash value:

'8fe12dec3dc8433bfe04ca1e4381feaa58d30c5ece06d37756d88face23aa3e1′

Due to the particularity of the sha256 algorithm, if you modify or add or subtract any character in this statement, the output hash value will change:

For example, add a "1" after the original sentence and enter "power overwhelming1" to get a new 32-byte hash value:

'd9e940764a410ecf7eae1f27a63248070ab45292e41de1cdfc89433ba2c20ac7′

Now let's set a goal. By modifying the number at the end of the statement, the first digit of the output hexadecimal hash value is 0 (equivalent to the first four digits of the 256-bit binary hash are all 0). The following is a simple code to implement the iteration of different numbers at the end of the statement and count the hash values ​​that meet the requirements.

The code is as follows

Figure 1

Execute the program and enter the statement "power overwhelming" and the number "4" respectively. The number "4" means that the first hexadecimal digit of the target hash value is 0.

Figure 2

The above operation results show that

- The target hash value is:

0×1000000000000000000000000000000000000000000000000000000000000

- A total of 32 calculations were performed, and the hash value that met the conditions appeared twice, which is an average of one hash value every 16 calculations. This is because a hexadecimal digit represents any digit from 0 to 16 (0 to f), so the probability of the occurrence of a tail digit that meets the requirements is 1/16.

If you execute the program again and input "show me the money", the number "4" will be changed to "8", which means that the first two bits of the target hash are all 0 (or the first 8 bits of the 256-bit binary are all 0)

- The target hash value is:

0×01000000000000000000000000000000000000000000000000000000000000

- A total of 1024 calculations were performed, and the hash value that met the conditions appeared 5 times. The amount of data is not large enough. If it is large enough, the probability of occurrence should be 1/256.

Therefore, the rule is very obvious. The more 0 bits of the hash value are required, the more calculations are required to meet the target. For example, from the data of block 477016 of the Bitcoin blockchain, its hash value is:

0000000000000000097cf400e2634e1b42cb0b55bbd25476a3e97a5e0d481d5

After conversion into binary, it requires 72 0 bits, which means that the correct hash value will appear approximately once every 4.7×10^21 calculations. This practice of repeatedly iterating the calculation by changing the tail value to obtain the correct hash value is called the proof of work mechanism - POW-proof of work, and the continuously iterating number at the tail is called the nonce value.

So how is the real block header hash calculated?

The following six data are used in the actual calculation of the block header hash value:

Figure 3

The first five are known values, namely the version number, the hash value of the previous block, the timestamp from the genesis block to the present, the root merkle value, and the difficulty mark.

The merkle value is a value recursively generated by all transactions waiting to be packaged in the block - please see the Day2 article for details. Due to various reasons, different mining machines on the network may have different transaction records and timestamps in the blocks waiting to be packaged.

The bits difficulty mark is a parameter that the entire Bitcoin network adjusts itself based on the total amount of Bitcoin and its annual output. According to Satoshi Nakamoto's plan, the total amount of Bitcoin is 21 million, and the mining volume is halved every four years. This parameter is created to maintain this rhythm. It has two functions:

First, ensure that no matter how fast the hash rate of the entire network is, the interval between the generation of two blocks is always kept at about 10 minutes;

Second, this parameter is also the target hash value of a block waiting to be packaged.

Back to block No. 477016, its difficulty target is 18015dcc. The first two digits are the power and the last six digits are the coefficient. By expanding the following formula, we can get the target HASH value.

Formula: Target hash=coefficient*2^(8*(exponent-3)

coefficient=0x015dcc

exponent=0×18

Target hash=

0x0000000000000000015dcc000000000000000000000000000000000000000000

Then iterate the nonce value of the block header and find a hash that is less than the target value

The formula is as follows:

hash=SHA256(SHA256(block header))

Block header = hexadecimal little endian (version information + previous block header + root merkle + timestamp + difficulty target + nonce value)

However, because the hexadecimal target hash has 17 zeros, after conversion to binary, there are 72 zeros. Every 4.7×10^21 calculations, there will be approximately one correct hash value, which is crazy. However, the current hash power of the entire Bitcoin network is -6200Ph/s, which means that 6.2×10^18 hash calculations can be performed per second, so the generation time of each block is about 750 seconds, equivalent to 12.5 minutes, slightly higher than the generation time of each Bitcoin block -10 minutes. This rhythm is the heartbeat of the Bitcoin network.

The computing power of an ordinary laptop is about 1*10^8 times/s. This data may not be correct, but it doesn’t matter, because according to such abnormal hash computing power requirements, one more zero or one less zero will make a difference of 150,000 years or 1.5 million years, which is the same for the short-lived existence of human beings.

However, once the correct nonce is found and the hash value is calculated to be less than the target, everyone can use the same set of parameters to verify whether the calculation is correct. For example, for block 477016, its block header information is as follows

Figure 4

According to the formula hash=SHA256(SHA256(block header)), write the python code as follows:

Figure 5

After running, enter the following 6 pieces of information respectively:

Version Information

Previous block header

Root merkle

Timestamp

Difficulty Target

nonce value

Get a hash value, which is equal to the hash value of block 477016

0000000000000000097cf400e2634e1b42cb0b55bbd25476a3e97a5e0d481d5

Figure 6

Once a mining machine calculates the correct hash value, it means that the block header of the block is successfully packaged, and the successful miner receives the Bitcoin reward from the network. The unsuccessful mining machine starts calculating the next block and repeats the above process.

Precisely because the amount of computing is so huge, so-called professional mining machines have emerged. They are all equipped with professional GPU arrays, which is very similar to big data computing. In fact, such a large amount of homogeneous data can be moved faster and more efficiently using GPUs. A Bitcoin mine usually looks like this. When so many professional mining machines work together, the energy consumption is astonishing.
Figure 7

As mentioned earlier, the hash power of the entire Bitcoin network is -6200Ph/s. Under this power, global mining farms consume more than 2.3 million kWh of electricity per hour, or 19 billion kWh per year. The current domestic civilian electricity consumption is about 0.55 RMB/kWh. Based on this price, the annual electricity consumption is 10.5 billion RMB. The current annual output of Bitcoin is about 660,000, and at the current price of 28,000 RMB per coin, it is worth about 18 billion RMB.

Due to the huge power consumption, in order to reduce costs, most mines are built in areas with abundant hydropower generation, such as southwest China, where the electricity price is about 0.35rmb/kWh. The picture below is the exterior view of a mine in the remote west. Combined with the surrounding scenery, it really looks like a "mine".
Figure 8


Below is the code mentioned above. If you have a Python interface, you can take it home and try it out.

#hash-nonce
import hashlib

text =raw_input(“please enter a sentence:”)
# iterate nonce from 0 to xxx
difficult=raw_input(“please enter a 0-32 number:”)
bit=int(difficult)
count=0
list=[]
target=2**(256-bit)
hextar=hex(target)
for nonce in xrange(4*(2**bit)):
# add the nonce to the end of the text
input = text + str(nonce)

# calculate the SHA-256 hash of the input (text+nonce)
hash = hashlib.sha256(input).hexdigest()
# show the input and hash result
print input, '=>', hash
if long(hash, 16)<target:
count=count+1
list.append(hash)

print “There are %d HASH meet target”%(count)
print “The target HASH is:”, hextar
print “Following HASH are meet target:”
for a in list:
print a

#hash-cal
import hashlib

#ask for input
ver=raw_input(“please enter 6 digi of version number:”)
phash=raw_input(“please enter hash of previous block:”)
rmerkle=raw_input(“please enter root merkle value:”)
t=raw_input(“please enter time in digital:”)
bits=raw_input(“please enter hex bits number in hex:”)
nonce=raw_input(“please enter digital nonce value:”)
#convert time and nonce to int
t=int(t)
n=int(nonce)
#convert all input to hex format and small end
sver=”0x”+str(ver)
hexver=sver[2::].decode('hex')[::-1].encode('hex')
hexphash=phash.decode('hex')[::-1].encode('hex')
hexrmerkle=rmerkle.decode('hex')[::-1].encode('hex')
hextime=hex(t)[2::].decode('hex')[::-1].encode('hex')
hexbits=bits.decode('hex')[::-1].encode('hex')
hexnonce=hex(n)[2::].decode('hex')[::-1].encode('hex')
print "hexver is:", hexver
print "hexphash is:", hexphash
print "hexrmerkle is:", hexrmerkle
print “hextime is:”, hextime
print “hexbits is:”, hexbits
print "hexnonce is:", hexnonce
#calculate hash value
header_hex=(hexver+hexphash+hexrmerkle+hextime+hexbits+hexnonce)
header_bin=header_hex.decode('hex')
hash=hashlib.sha256(hashlib.sha256(header_bin).digest()).digest()
new_hash=hash[::-1].encode('hex_codec')
print “hash is:”, new_hash

Winter is coming, learn more about technology and prepare for the next spring


<<:  Huobi.com and OKCoin stop all virtual currency business; platform managers are temporarily not allowed to leave Beijing

>>:  JPMorgan Chase denies secretly buying Bitcoin at a low price in Europe: customers bought it themselves!

Recommend

These men will abandon their lovers when they are successful in their careers

The most frightening thing between husband and wi...

What are the blockchain projects that are acceptable in China?

Since Satoshi Nakamoto created "Bitcoin"...

A woman's chin is very important

The recent brainwashing drama "The Rise of P...

Women with double wisdom lines

How to interpret a woman’s double wisdom lines? A...

What is the fate of a woman with a high forehead?

If a woman has a high forehead, what does it mean...

How to read forehead

The forehead is a very important part in physiogn...

Is it good for a woman to have a high forehead?

Is it good for a woman to have a high forehead? A...

A picture of a woman with a mole on her ear

Is it good for a woman to have a mole on her ear?...

What kind of personality does a woman with thick and dense eyebrows have?

Eyebrows are one of our five facial features. Goo...