bitcoin版本:0.16.2
区块结构分析
区块是组成区块链的基本单位,可以通过bitcoin-cli命令查看一个区块的基本信息
区块block的数据结构如下图(存储方式为小端):

整体结构
数据项 | 大小(Byte) | 描述 |
---|---|---|
Block Size | 4 | 区块大小 |
Block Header | 80 | 区块头信息大小 |
Transactions | m*n(n>=250) | 所有交易的列表 |
Transactions Counter | 1-9 | 交易数额 |
区块头
数据项 | 大小(Byte) | 存储方式 | 描述 |
---|---|---|---|
Version | 4 | 小端 | 区块版本,规定了区块遵守的验证规则 |
PreBlockhash | 32 | 内部字节顺序 | 上一个区块哈希值(SHA256 (SHA256(Block Header))) |
MerkleRoothash | 32 | 内部字节顺序 | Merkle树根,包含了所有交易的哈希 |
Timestamp | 4 | 小端 | 区块产生时间戳,大于前11个区块时间戳的平均值,全节点会拒绝时间戳超出自己2小时的区块 |
nBitS | 4 | 小端 | 工作量证明(POW)的目标难度值,当前区块难度值需要经过Target nBits编码才能转化为目标哈希值 |
Nonce | 4 | 小端 | 用于POW的一个随机数,随着算力增大可能会导致Nonce位数不够 协议规定时间戳和CoinbaseTransaction信息可修改用于扩展Nonce位数 |
创世块
通过bitcoin-cli命令查询一个区块的信息
jc@jc-desktop:~/Documents/project/btc$ bitcoin-cli -datadir="/home/jc/Documents/project/btc/bitdata" getblockhash 0
000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
jc@jc-desktop:~/Documents/project/btc$ bitcoin-cli -datadir="/home/jc/Documents/project/btc/bitdata" getblock 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
{
"hash": "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",
"confirmations": 190381,
"strippedsize": 285,
"size": 285,
"weight": 1140,
"height": 0,
"version": 1,
"versionHex": "00000001",
"merkleroot": "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b",
"tx": [
"4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"
],
"time": 1231006505,
"mediantime": 1231006505,
"nonce": 2083236893,
"bits": "1d00ffff",
"difficulty": 1,
"chainwork": "0000000000000000000000000000000000000000000000000000000100010001",
"nTx": 1,
"nextblockhash": "00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048"
}
源码分析
我们来看看源代码,block的结构是存放在src/primitives/block.h
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2017 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_PRIMITIVES_BLOCK_H
#define BITCOIN_PRIMITIVES_BLOCK_H
#include <primitives/transaction.h>
#include <serialize.h>
#include <uint256.h>
/** Nodes collect new transactions into a block, hash them into a hash tree,
* and scan through nonce values to make the block's hash satisfy proof-of-work
* requirements. When they solve the proof-of-work, they broadcast the block
* to everyone and the block is added to the block chain. The first transaction
* in the block is a special one that creates a new coin owned by the creator
* of the block.
* 网络中的节点不断收集新的交易打包到区块中,所有的交易会通过两两哈希的方式形成一个Merkle树
* 打包的过程就是要完成工作量证明的要求,当节点解出了当前的随机数时,
* 它就把当前的区块广播到其他所有节点,并且加到区块链上。
* 区块中的第一笔交易称之为CoinBase交易,是产生的新币,奖励给区块的产生者
*/
class CBlockHeader
{
public:
// header
int32_t nVersion; //版本
uint256 hashPrevBlock; //上一个版本的hash
uint256 hashMerkleRoot; //包含交易信息的Merkle树根
uint32_t nTime; //时间,打包时间的?
uint32_t nBits; //工作量证明(POW)的难道
uint32_t nNonce; //要找的符合POW的随机数
CBlockHeader() //构造函数,初始化成员变量
{
SetNull();
}
ADD_SERIALIZE_METHODS; //通过封装的模板实现类的序列化,还不知道怎么用
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(this->nVersion);
READWRITE(hashPrevBlock);
READWRITE(hashMerkleRoot);
READWRITE(nTime);
READWRITE(nBits);
READWRITE(nNonce);
}
void SetNull() //初始化成员变量
{
nVersion = 0;
hashPrevBlock.SetNull();
hashMerkleRoot.SetNull();
nTime = 0;
nBits = 0;
nNonce = 0;
}
bool IsNull() const //难度为0说明区块还未创建,区块头为空
{
return (nBits == 0);
}
uint256 GetHash() const; //获取哈希
int64_t GetBlockTime() const //获取区块时间
{
return (int64_t)nTime;
}
};
class CBlock : public CBlockHeader //继承自CBlockHeader,拥有其所有成员变量
{
public:
// network and disk
std::vector<CTransactionRef> vtx; //所有交易的容器,指针?
// memory only
mutable bool fChecked; //交易是否验证
CBlock() //构造函数
{
SetNull();
}
CBlock(const CBlockHeader &header) //构造函数?
{
SetNull();
*((CBlockHeader*)this) = header;
}
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(*(CBlockHeader*)this);
READWRITE(vtx);
}
void SetNull() //设置成员变量
{
CBlockHeader::SetNull();
vtx.clear();
fChecked = false;
}
CBlockHeader GetBlockHeader() const //获取头部信息
{
CBlockHeader block;
block.nVersion = nVersion;
block.hashPrevBlock = hashPrevBlock;
block.hashMerkleRoot = hashMerkleRoot;
block.nTime = nTime;
block.nBits = nBits;
block.nNonce = nNonce;
return block;
}
std::string ToString() const;
};
/** Describes a place in the block chain to another node such that if the
* other node doesn't have the same branch, it can find a recent common trunk.
* The further back it is, the further before the fork it may be.
* 描述区块链中在其他节点的一个位置,
* 如果其他节点没有相同的分支,它可以找到一个最近的中继(最近的相同块)。
* 更进一步地讲,它可能是分叉前的一个位置
*/
struct CBlockLocator //还不是很清楚这个结构体是用来干嘛的
{
std::vector<uint256> vHave;
CBlockLocator() {}
explicit CBlockLocator(const std::vector<uint256>& vHaveIn) : vHave(vHaveIn) {}
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
int nVersion = s.GetVersion();
if (!(s.GetType() & SER_GETHASH))
READWRITE(nVersion);
READWRITE(vHave);
}
void SetNull()
{
vHave.clear();
}
bool IsNull() const
{
return vHave.empty();
}
};
#endif // BITCOIN_PRIMITIVES_BLOCK_H
- BlockHash 区块哈希值,是通过SHA256算法对区块头信息进行哈希得到的,这个值必须满足POW的DifficultyTarget,该区块才被认为有效。
- BlockHeight 区块高度,是用来标示区块在区块链中的位置。创世区块高度为0,每一个加在后面的区块,区块高度递增1。
至于transaction、merkleRoothash是怎么生成的,在后续的文章会讲到。
网友评论