比特幣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

轉載請說明出處

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