Bitcoin block structure analysis

foreword

This article mainly analyzes the value of a block in detail. Through byte-by-byte analysis, we can find out the part corresponding to the Bitcoin block field, and we can deepen our understanding of the Bitcoin block.

Ready to work

We query the block with the block hash value on the https://webbtc.com website, 00000000d1145790a8694403d4063f323d499e655c83426834d4ce2f8dd4a2eeand find the json format information and hexadecimal format information of the block.

json format information:

{
  "hash": "00000000d1145790a8694403d4063f323d499e655c83426834d4ce2f8dd4a2ee",
  "ver": 1,
  "prev_block": "000000002a22cfee1f2c846adbd12b3e183d4f97683f85dad08a79780a84bd55",
  "mrkl_root": "7dac2c5666815c17a3b36427de37bb9d2e2c5ccec3f8633eb91a4205cb4c10ff",
  "time": 1231731025,
  "bits": 486604799,
  "nonce": 1889418792,
  "n_tx": 2,
  "size": 490,
  "tx": [
    {
      "hash": "b1fea52486ce0c62bb442b530a3f0132b826c74e473d1f2c220bfa78111c5082",
      "ver": 1,
      "vin_sz": 1,
      "vout_sz": 1,
      "lock_time": 0,
      "size": 134,
      "in": [
        {
          "prev_out": {
            "hash": "0000000000000000000000000000000000000000000000000000000000000000",
            "n": 4294967295
          },
          "coinbase": "04ffff001d0102"
        }
      ],
      "out": [
        {
          "value": "50.00000000",
          "scriptPubKey": "04d46c4968bde02899d2aa0963367c7a6ce34eec332b32e42e5f3407e052d64ac625da6f0718e7b302140434bd725706957c092db53805b821a85b23a7ac61725b OP_CHECKSIG"
        }
      ],
      "nid": "c56705435de47674259d6c92125907645d4fb512fa8e7f31457f5f29ba983d80"
    },
    {
      "hash": "f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16",
      "ver": 1,
      "vin_sz": 1,
      "vout_sz": 2,
      "lock_time": 0,
      "size": 275,
      "in": [
        {
          "prev_out": {
            "hash": "0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9",
            "n": 0
          },
          "scriptSig": "304402204e45e16932b8af514961a1d3a1a25fdf3f4f7732e9d624c6c61548ab5fb8cd410220181522ec8eca07de4860a4acdd12909d831cc56cbbac4622082221a8768d1d0901"
        }
      ],
      "out": [
        {
          "value": "10.00000000",
          "scriptPubKey": "04ae1a62fe09c5f51b13905f07f06b99a2f7159b2225f374cd378d71302fa28414e7aab37397f554a7df5f142c21c1b7303b8a0626f1baded5c72a704f7e6cd84c OP_CHECKSIG",
          "next_in": {
            "hash": "ea44e97271691990157559d0bdd9959e02790c34db6c006d779e82fa5aee708e",
            "n": 0
          }
        },
        {
          "value": "40.00000000",
          "scriptPubKey": "0411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3 OP_CHECKSIG",
          "next_in": {
            "hash": "a16f3ce4dd5deb92d98ef5cf8afeaf0775ebca408f708b2146c4fb42b41e14be",
            "n": 0
          }
        }
      ],
      "nid": "a1629e004eb3d703ecf3807f976e402a626d84c559f8eab1450adf207619f319"
    }
  ],
  "mrkl_tree": [
    "b1fea52486ce0c62bb442b530a3f0132b826c74e473d1f2c220bfa78111c5082",
    "f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16",
    "7dac2c5666815c17a3b36427de37bb9d2e2c5ccec3f8633eb91a4205cb4c10ff"
  ],
  "next_block": "00000000c9ec538cab7f38ef9c67a95742f56ab07b0a37c5be6b02808dbfb4e0"
}

Hexadecimal format information:

0100000055bd840a78798ad0da853f68974f3d183e2bd1db6a842c1feecf222a00000000ff104ccb05421ab93e63f8c3ce5c2c2e9dbb37de2764b3a3175c8166562cac7d51b96a49ffff001d283e9e700201000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d0102ffffffff0100f2052a01000000434104d46c4968bde02899d2aa0963367c7a6ce34eec332b32e42e5f3407e052d64ac625da6f0718e7b302140434bd725706957c092db53805b821a85b23a7ac61725bac000000000100000001c997a5e56e104102fa209c6a852dd90660a20b2d9c352423edce25857fcd3704000000004847304402204e45e16932b8af514961a1d3a1a25fdf3f4f7732e9d624c6c61548ab5fb8cd410220181522ec8eca07de4860a4acdd12909d831cc56cbbac4622082221a8768d1d0901ffffffff0200ca9a3b00000000434104ae1a62fe09c5f51b13905f07f06b99a2f7159b2225f374cd378d71302fa28414e7aab37397f554a7df5f142c21c1b7303b8a0626f1baded5c72a704f7e6cd84cac00286bee0000000043410411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3ac00000000

The overall structure of the block

A complete block structure is mainly composed of the following parts:

byte field illustrate
4 block size The block size after this field in bytes
80 block header Several fields that make up the block header
1-9 transaction counter The number of transactions included in this block, including coinbase transactions
indefinite trade The transaction information recorded in the block uses the native transaction information format, and the position of the transaction in the data stream must be consistent with the order of the leaf nodes of the Merkle tree

Bitcoin’s block size is currently strictly limited to 1MB. The 4-byte block size field is not included here.

Block header information analysis

We first analyze the first 80 bytes of block header information:

0100000055bd840a78798ad0da853f68974f3d183e2bd1db6a842c1feecf222a00000000ff104ccb05421ab93e63f8c3ce5c2c2e9dbb37de2764b3a3175c8166562cac7d51b96a49ffff001d283e9e70

Block header structure 1 :

byte field illustrate
4 Version The block version number, indicating the verification rules complied with by this block
32 parent block header hash The hash value of the previous block, calculated using SHA256 (SHA256(parent block header))
32 Merkle root The hash value of the Merkle tree root of the transaction in this block is also calculated by SHA256 (SHA256())
4 timestamp The approximate time generated by the block, the UNIX timestamp accurate to the second, must be strictly greater than the median time of the previous 11 blocks, and the full node will also reject those blocks that exceed their own 2-hour timestamp
4 Difficulty target The difficulty target of the proof-of-work algorithm for this block, which has been coded using a specific algorithm
4 Nonce In order to find the random number set to meet the difficulty target, in order to solve the problem that the 32-bit random number is not enough when the computing power is soaring, it is stipulated that the timestamp and coinbase transaction information can be changed, so as to expand the number of nonce digits

The block header information is compared with the actual information :

Description :

  • The version, parent block header hash and Merkle root are encoded in little-endian format, that is, the least significant bits are first.
  • The timestamp represents the number of seconds since January 1, 1970 0:0:0:0, 1231731025 seconds converted to hexadecimal value 0x496AB951, and then encoded in little endian format as 0x51b96a49.
  • The difficulty target 486604799 translates to a hexadecimal value of 0x1d00ffff, which is encoded in little-endian format. The difficulty value of the current block, 0x1d00ffff, requires a special encoding method to be correctly converted into the target hash value.
  • The random value represented by the random number nonce is converted to hexadecimal as 0x709E3E28, and then encoded in little-endian format as 0x283e9e70.

Overall structure of the transaction

Next we analyze the transaction information:

0201000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d0102ffffffff0100f2052a01000000434104d46c4968bde02899d2aa0963367c7a6ce34eec332b32e42e5f3407e052d64ac625da6f0718e7b302140434bd725706957c092db53805b821a85b23a7ac61725bac000000000100000001c997a5e56e104102fa209c6a852dd90660a20b2d9c352423edce25857fcd3704000000004847304402204e45e16932b8af514961a1d3a1a25fdf3f4f7732e9d624c6c61548ab5fb8cd410220181522ec8eca07de4860a4acdd12909d831cc56cbbac4622082221a8768d1d0901ffffffff0200ca9a3b00000000434104ae1a62fe09c5f51b13905f07f06b99a2f7159b2225f374cd378d71302fa28414e7aab37397f554a7df5f142c21c1b7303b8a0626f1baded5c72a704f7e6cd84cac00286bee0000000043410411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3ac00000000

The first few bytes of transaction information indicate the number of transactions contained in the block, including coinbase transactions. The transaction amount type is a variable-length integer with a compressed size . We can see that the first byte is 0x02, which means that there are only 2 transactions in this block: a Coinbase transaction and a normal transaction. Next we will analyze Coinbase transactions and general transaction information separately.

Coinbase transaction information analysis

The first transaction of a block is defined as a coinbase transaction.

The Coinbase transaction structure is :

byte field describe
4 Version Rules for this transaction
1-9 input counter Number of transaction inputs included
32 transaction hash Does not refer to any transaction, the values ​​are all 0
4 Transaction output index Fixed to 0xFFFFFFFF
1-9 Coinbase data length coinbase data length
indefinite Coinbase data In the V2 version of the block, except for the block height, other data can be filled in arbitrarily for extra nonce and mining tags
4 Sequence number Values ​​are all 1, 0xFFFFFFFF
1-9 output counter Number of transaction outputs included
8 total Bitcoin value in satoshis
1-9 Lock script size The length of the following lock script in bytes
indefinite lock script A script that defines the conditions required to pay the output
4 lock time A block number or UNIX timestamp

Comparison of Coinbase transaction information and actual information :

说明

  • 版本采用小端格式编码。
  • Coinbase交易没有交易输入,也就是说它不引用任何一个UTXO,它所引用的交易哈希只是一个256位的0,表示引用为空。
  • 交易输出索引也是固定值,为0xffffffff,转化为十进制值为4294967295。
  • Coinbase数据采用大端格式编码。
  • Coinbase交易只有一个特殊的“交易输入”和一个交易输出,均采用变长整型来表示。
  • 输出总量为50亿聪的比特币,转化为十六进制为0x12A05F200,将其扩充为8字节,变为0x000000012A05F200,之后采用小端格式表示。
  • 锁定脚本共有67个字节。开头的41含义,表示要将接下来的65个字节压入堆栈。最后一个字节的16进制数值,ac,表示的是OP_CHECKSIG。
  • 最后的锁定时间为0,表示立即执行。

普通交易信息分析

剩余的信息就是一个普通交易信息:

0100000001c997a5e56e104102fa209c6a852dd90660a20b2d9c352423edce25857fcd3704000000004847304402204e45e16932b8af514961a1d3a1a25fdf3f4f7732e9d624c6c61548ab5fb8cd410220181522ec8eca07de4860a4acdd12909d831cc56cbbac4622082221a8768d1d0901ffffffff0200ca9a3b00000000434104ae1a62fe09c5f51b13905f07f06b99a2f7159b2225f374cd378d71302fa28414e7aab37397f554a7df5f142c21c1b7303b8a0626f1baded5c72a704f7e6cd84cac00286bee0000000043410411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3ac00000000

普通交易结构为

字节 字段 描述
4 版本 明确这笔交易参照的规则
1-9 输入计数器 包含的交易输入数量
不定 输入 一个或多个交易输入
1-9 输出计数器 包含的交易输出数量
不定 输出 一个或多个交易输出
4 锁定时间 一个区块号或UNIX时间戳

普通交易输入为

字节 字段 描述
32 交易哈希值 指向被花费的UTXO所在的交易的哈希指针
4 输出索引 被花费的UTXO的索引号,第一个是0
1-9 解锁脚本大小 用字节表示的后面的解锁脚本长度
不定 解锁脚本 满足UTXO解锁脚本条件的脚本
4 序列号 目前未被使用的交易替换功能,设为0xFFFFFFFF

普通交易输出为

字节 字段 描述
8 总量 用聪表示的比特币值
1-9 锁定脚本大小 用字节表示的后面的锁定脚本长度
不定 锁定脚本 一个定义了支付输出所需条件的脚本

普通交易信息与实际信息对照

说明

  • 版本号、交易哈希值采用小端格式编码。
  • 输入计数器、输出计数器、解锁脚本大小和锁定交易大小均采用变长整型值。
  • The output index is 0, which indicates that the UTXO referenced by this input is 0437cd7f8525ceed2324359c2d0ba26006d92d856a9c20fa0241106ee5a597c9the first output of the transaction.
  • If the size of the unlock script is 0x48, it means that there is a 72-byte unlock script behind.
  • The 0x47 at the beginning of the unlock script means that the next 71 bytes are now pushed onto the stack.
  • There are two outputs in this transaction. The first output has a total amount of 1 billion satoshis, which is converted to a hexadecimal value of 0x000000003B9ACA00, and finally encoded in little-endian format. Similarly, the total output of the second output is 4 billion satoshis, which is converted into a hexadecimal value of 0x00000000EE6B2800, and finally encoded in little-endian format.
  • The lock script size for the first transaction has a hex value of 0x43, which means there is a 67-byte lock script that follows.
  • The 0x41 at the beginning of the first lock script means to push the next 65 bytes onto the stack. And the trailing 0xac means OP_CHECKSIG.
  • The second transaction is analyzed in the same way as the first transaction.
  • The last lock time is 0, which means immediate execution.

  1. Block Headers : The description of block headers in the Bitcoin Developer Reference.

Guess you like

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