比特币bitcoin源码解析之数据结构

一. 数据结构介绍

整体主要类图

1. CTxOut

An output of a transaction. It contains the public key that the next input must be able to sign with to claim it.
CTxOut类图

字段属性 说明
nValue 交易输出对应的金额
scriptPubKey 交易对应的公钥

2. COutPoint

对应交易的第几个输出
COutPoint类图

字段属性 说明
hash 交易对应的hash
n 交易对应的第几个输出

3. CTxIn

An input of a transaction. It contains the location of the previous transaction’s output that it claims and a signature that matches the output’s public key.
CTxIn类图

字段属性 说明
prevout 前一个交易对应的输出(叫一个交易对应的hash值和对应的第几个输出)
scriptSig 输入脚本对应的签名
nSequence 主要是用于判断相同输入的交易哪一个更新,值越大越新

4. CTransaction

The basic transaction that is broadcasted on the network and contained in blocks. A transaction can contain multiple inputs and outputs.
CTransaction类图

字段属性 说明
nVersion 交易的版本号,用于升级
vin 交易对应的输入列表
vout 交易对应的输出列表
nLockTime 交易对应的锁定时间,目前没有使用(在最初版本的比特币中)

5. CMerkleTx

A transaction with a merkle branch linking it to the block chain
CMerkleTx类图

字段属性 说明
hashBlock 交易所在block对应的hash值,因为block中有对应整个交易的默克尔树,这样才能根据分支来校验当前交易是否在block中
vMerkleBranch 当前交易对应的默克尔分支
nIndex 当前交易在对应的block对应的输入vtx列表中的索引,CMerkleTx就是根据索引来计算这个交易对应的默克尔树分支的
fMerkleVerified 标记默克尔交易是否已经校验,如果没有校验则进行校验,校验之后将这个值设为true

6. CWalletTx

A transaction with a bunch of additional info that only the owner cares about. It includes any unrecorded transactions needed to link it back to the block chain.
CWalletTx类图

字段属性 说明
vtxPrev 当前交易A对应的输入对应的交易B,如果B所在block到最长链末尾的长度小于3,则将次交易放入
fTimeReceivedIsTxTime 接收时间是否是交易时间标记
nTimeReceived 交易被这个节点接收的时间

7. CBlock

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. Blocks are appended to blk0001.dat files on disk. Their location on disk is indexed by CBlockIndex objects in memory.
CBlock类图

字段属性 说明
nVersion 块的版本,主要为了后续的升级使用
hashPrevBlock 前一个块对应的hash
hashMerkleRoot 默克尔对应的根
nTime 区块创建时间:这个值取max(前11个区块对应创建时间中位数,当前时间)
nBits 记录本区块难度
nNonce 工作量证明获得随机数,这个随机数正好满足当前挖矿对应的难度
vtx 块中交易列表
vMerkleTree 整个交易对应的默克尔树列表

8. CBlockIndex

The block chain is a tree shaped structure starting with the genesis block at the root, with each block potentially having multiple candidates to be the next block. pprev and pnext link a path through the main/longest chain. A blockindex may have multiple pprev pointing back to it, but pnext will only point forward to the longest branch, or will be null if the block is not part of the longest chain. 如果块索引对应的pNext不为空,则这个块索引一定对应的是主链
CBlockIndex类图

字段属性 说明
phashBlock 对应块hash值指针
pprev 指向前一个blockIndex
pnext 指向当前区块索引的下一个,只有当前区块索引在主链上的时候,这个值才是非空
nFile 块所在文件的信息,而且块文件的命名一般是blk${nFile}.dat
nBlockPos 块在文件中的偏移
nHeight 块索引在最长链的深度,即是中间隔了多少个block,即是从创世区块到当前区块中间隔了多少个区块
nVersion 块的版本,主要为了后续的升级使用
hashMerkleRoot 默克尔对应的根
nTime 区块创建时间:这个值取max(前11个区块对应创建时间中位数,当前时间)
nBits 记录本区块难度
nNonce 工作量证明获得随机数,这个随机数正好满足当前挖矿对应的难度

9. CDiskTxPos

交易在文件中对应的索引位置
CDiskTxPos类图

字段属性 说明
nFile 块所在文件的信息,而且块文件的命名一般是blk${nFile}.dat
nBlockPos 块在文件中的偏移
nTxPos 交易在对应块中的偏移

10. CTxIndex

A txdb record that contains the disk location of a transaction and the locations of transactions that spend its outputs. vSpent is really only used as a flag, but having the location is very helpful for debugging. 交易索引—每一个交易对应一个索引
CTxIndex类图

字段属性 说明
pos 交易对应的在硬盘中文件的位置
vSpent 标记交易的输出是否已经被消费了,根据下标来标记对应交易指定位置的输出是否已经被消费了

11. CDataStream

Double ended buffer combining vector and stream-like interfaces. >> and << read and write unformatted data using the above serialization templates. Fills with data in linear time; some stringstream implementations take N^2 time.
CDataStream类图

字段属性 说明
vch 流中存放的数据
nReadPos 流中读取的位置
nType 类型
nVersion 版本

12. CAddress

地址信息
CAddress类图

字段属性 说明
nServices 服务标识
pchReserved 保留内容:{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff }
ip Ip地址
port 端口
nTime 时间
nLastFailed 对应这个地址最近连接失败时间

13. CNode

节点定义
CNode类图
CInv类图

字段属性 说明
nServices 服务标识
hSocket 对应本地和这个节点连接的套接字
vSend 发送缓存区
vRecv 接收缓冲区
nPushPos 指定发送区已经发送的位置
addr 对应这个节点的地址信息
nVersion 节点对应的版本,如果节点版本为0,则消息发送不出去
fClient 标记是否是客户端,如果是客户端则需要区块的头部进行校验就可以了,不需要保存整个区块的内容
fNetworkNode 设置对应的节点为网络节点
fDisconnect 节点断开连接的标记
nRefCount 引用计数器
nReleaseTime 节点释放的时间
vAddrToSend flood 洪泛:消息需要发送对应的地址,对需要发送的地址进行已知地址的集合过滤之后再发送
setAddrKnown 已知地址的集合
setInventoryKnown 基于转播的库存:已知库存的集合
vInventoryToSend 库存准备发送的集合,对库存准备发送的集合根据已知库存的集合进行过滤之后在发送

二. 源码地址

我对比特币bitcoin-0.1.0源码加了详细的注释,对应的下载地址:https://github.com/lwjaiyjk/bitcoin-comment-0.1.0.git

转载请说明出处

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章