Bitcoin mining algorithm

Original address: https://blog.csdn.net/u013137970/article/details/79041038

Basic data

Total issuance: 21 million. 
New block generation cycle: about 10 minutes. 
Mining difficulty adjustment period: every 2016 blocks, about 2 weeks. 
Mining rewards: Bitcoin mining rewards come from two parts:

  • The genesis block rewards 50 bitcoins, which will be halved every 210,000 blocks thereafter, which is adjusted every 4 years. There have been two halvings so far, and the current mining reward is 12.5 bitcoins.
  • Every Bitcoin transaction must pay a certain amount of fees to the miners. This setting is to prevent malicious nodes from sending a large number of spam transactions to conduct DOS attacks on the Bitcoin network.

mining algorithm

Mining reference algorithm: The mining algorithm is SHA256. During the mining process, miners perform two SHA256 operations on the 80-byte block header data of Bitcoin, and the result of the operation is a 256-bit (32-byte) string. Judge whether the current block is legal or not by comparing it with the current difficulty value. That is, the following conditions are met: 
SHA256(SHA256(block_header))< difficulty 
if the above conditions are not met, you need to change the random value in the block header, or use random data to fill in the coinbase transaction, so that the data in the block header can be changed to find a block that meets the conditions. This is the essence of the PoW mechanism. The one-way function is used to force miners to continuously try random numbers to find qualified blocks to complete a certain amount of calculation and ensure the security and stability of the system.

Case Analysis

In order to better understand the mining algorithm of Bitcoin, take an actual block data as an example.

First, obtain the original data of the block with block number 100000, which can be obtained at https://webbtc.com/block/000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506.hex :

0100000050120119172a610421a6c3011dd330d9df07b63616c2cc1f1cd00200000000006657a9252aacd5c0b2940996ecff952228c3067cc38d4885efb5a4ac4247e9f337221b4d4c86041b0f2b57100401000000010000000000000000000000000000000000000000000000000000000000000000ffffffff08044c86041b020602ffffffff0100f2052a010000004341041b0e8c2567c12536aa13357b79a073dc4444acb83c4ec7a0e2f99dd7457516c5817242da796924ca4e99947d087fedf9ce467cb9f7c6287078f801df276fdf84ac000000000100000001032e38e9c0a84c6046d687d10556dcacc41d275ec55fc00779ac88fdf357a187000000008c493046022100c352d3dd993a981beba4a63ad15c209275ca9470abfcd57da93b58e4eb5dce82022100840792bc1f456062819f15d33ee7055cf7b5ee1af1ebcc6028d9cdb1c3af7748014104f46db5e9d61a9dc27b8d64ad23e7383a4e6ca164593c2527c038c0857eb67ee8e825dca65046b82c9331586c82e0fd1f633f25f87c161bc6f8a630121df2b3d3ffffffff0200e32321000000001976a914c398efa9c392ba6013c5e04ee729755ef7f58b3288ac000fe208010000001976a914948c765a6914d43f2a7ac177da2c2f6b52de3d7c88ac000000000100000001c33ebff2a709f13d9f9a7569ab16a32786af7d7e2de09265e41c61d078294ecf010000008a4730440220032d30df5ee6f57fa46cddb5eb8d0d9fe8de6b342d27942ae90a3231e0ba333e02203deee8060fdc70230a7f5b4ad7d7bc3e628cbe219a886b84269eaeb81e26b4fe014104ae31c31bf91278d99b8377a35bbce5b27d9fff15456839e919453fc7b3f721f0ba403ff96c9deeb680e5fd341c0fc3a7b90da4631ee39560639db462e9cb850fffffffff0240420f00000000001976a914b0dcbf97eabf4404e31d952477ce822dadbe7e1088acc060d211000000001976a9146b1281eec25ab4e1e0793ff4e08ab1abb3409cd988ac0000000001000000010b6072b386d4a773235237f64c1126ac3b240c84b917a3909ba1c43ded5f51f4000000008c493046022100bb1ad26df930a51cce110cf44f7a48c3c561fd977500b1ae5d6b6fd13d0b3f4a022100c5b42951acedff14abba2736fd574bdb465f3e6f8da12e2c5303954aca7f78f3014104a7135bfe824c97ecc01ec7d7e336185c81e2aa2c41ab175407c09484ce9694b44953fcb751206564a9c24dd094d42fdbfdd5aad3e063ce6af4cfaaea4ea14fbbffffffff0140420f00000000001976a91439aa3d569e06a1d7926dc4be1193c99bf2eb9ee088ac00000000
  • 1

The acquired data is in hexadecimal, of which the first 80 bytes are block header data. Perform double SHA256 operation on the first 80 bytes of data to get the hash value of the current block. The Go language code is as follows:

package main

import (
    "crypto/sha256"
    "encoding/hex"
    "fmt"
)

const HashSize = 32
type Hash [32]byte

func (hash Hash) String() string {
    for i := 0; i < HashSize/2; i++ {
        hash[i], hash[HashSize-1-i] = hash[HashSize-1-i], hash[i]
    }
    return hex.EncodeToString(hash[:])
}

func main() {
    block, _ := hex.DecodeString("0100000050120119172a610421a6c3011dd330d9df07b63616c2cc1f1cd00200000000006657a9252aacd5c0b2940996ecff952228c3067cc38d4885efb5a4ac4247e9f337221b4d4c86041b0f2b57100401000000010000000000000000000000000000000000000000000000000000000000000000ffffffff08044c86041b020602ffffffff0100f2052a010000004341041b0e8c2567c12536aa13357b79a073dc4444acb83c4ec7a0e2f99dd7457516c5817242da796924ca4e99947d087fedf9ce467cb9f7c6287078f801df276fdf84ac000000000100000001032e38e9c0a84c6046d687d10556dcacc41d275ec55fc00779ac88fdf357a187000000008c493046022100c352d3dd993a981beba4a63ad15c209275ca9470abfcd57da93b58e4eb5dce82022100840792bc1f456062819f15d33ee7055cf7b5ee1af1ebcc6028d9cdb1c3af7748014104f46db5e9d61a9dc27b8d64ad23e7383a4e6ca164593c2527c038c0857eb67ee8e825dca65046b82c9331586c82e0fd1f633f25f87c161bc6f8a630121df2b3d3ffffffff0200e32321000000001976a914c398efa9c392ba6013c5e04ee729755ef7f58b3288ac000fe208010000001976a914948c765a6914d43f2a7ac177da2c2f6b52de3d7c88ac000000000100000001c33ebff2a709f13d9f9a7569ab16a32786af7d7e2de09265e41c61d078294ecf010000008a4730440220032d30df5ee6f57fa46cddb5eb8d0d9fe8de6b342d27942ae90a3231e0ba333e02203deee8060fdc70230a7f5b4ad7d7bc3e628cbe219a886b84269eaeb81e26b4fe014104ae31c31bf91278d99b8377a35bbce5b27d9fff15456839e919453fc7b3f721f0ba403ff96c9deeb680e5fd341c0fc3a7b90da4631ee39560639db462e9cb850fffffffff0240420f00000000001976a914b0dcbf97eabf4404e31d952477ce822dadbe7e1088acc060d211000000001976a9146b1281eec25ab4e1e0793ff4e08ab1abb3409cd988ac0000000001000000010b6072b386d4a773235237f64c1126ac3b240c84b917a3909ba1c43ded5f51f4000000008c493046022100bb1ad26df930a51cce110cf44f7a48c3c561fd977500b1ae5d6b6fd13d0b3f4a022100c5b42951acedff14abba2736fd574bdb465f3e6f8da12e2c5303954aca7f78f3014104a7135bfe824c97ecc01ec7d7e336185c81e2aa2c41ab175407c09484ce9694b44953fcb751206564a9c24dd094d42fdbfdd5aad3e063ce6af4cfaaea4ea14fbbffffffff0140420f00000000001976a91439aa3d569e06a1d7926dc4be1193c99bf2eb9ee088ac00000000")
    first := sha256.Sum256(block[:80])  // 选择区块的前80个字节,即区块头数据,进行第一次哈希运算
    second := sha256.Sum256(first[:])   // 将第一次结果继续哈希,得到第二个结果,该结果为区块哈希值
    fmt.Printf("blockheader is: \n%x\n", block[:80])
    fmt.Printf("doublehash is:\n%v\n", Hash(second))
}
  • The running result is:
blockheader is:
0100000050120119172a610421a6c3011dd330d9df07b63616c2cc1f1cd00200000000006657a9252aacd5c0b2940996ecff952228c3067cc38d4885efb5a4ac4247e9f337221b4d4c86041b0f2b5710
doublehash is:
000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506

可以发现经过两次SHA256运算之后得到的结果就是区块哈希。


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325472319&siteId=291194637